; docformat = 'rst' ;+ ; Batch file containing all the code intended to be typed at the command line ; from the "Variables" chapter. ;- ; Introduction a = 0 help, a a = 1.0 help, a a = [0, 1, 2] help, a ; Variable names print, idl_validname('5 particles', /convert_all) ; Variable types help, 0L help, 0 help, 0S help, 'AF'x help, '12'o help, 'AF'xL help, long(3.5) help, byte(256) help, byte(257) print, byte('Test string') print, n_elements(someUndefinedVariable) a = 0 print, n_elements(a) help, 1.0 / 0.0 help, !values.f_infinity help, !values, /structures print, finite(1.0 / 0.0) ; Arrays zeros = fltarr(100) ones = fltarr(100) + 1.0 print, indgen(10) arr = [1, 5, 7, 8] help, arr print, arr arr = [[1], [2], [3], [4]] help, arr print, arr t = [[1, 0, 0, 1], [0, 1, 0, 2], [0, 0, 1, 3]] help, t print, t arr = [findgen(3), reverse(findgen(3))] print, arr arr = [[findgen(3)], [reverse(findgen(3))]] help, arr print, arr a = [1, 2, 3] a = [a, 4] print, a n = 20 minValue = 10.0 maxValue = 20.0 print, (maxValue - minValue) * findgen(n) / n + minValue arr = findgen(20) print, arr print, arr[0] print, arr[2:5] print, arr[3:11:2] print, arr[*] print, arr[14:*] print, arr[14:*:2] print, arr[[0, 6, 12]] multi = findgen(5, 4) print, multi print, multi[3, 2] print, multi[*, 2] print, multi[3, *] print, multi[2:4, 1:2] print, multi[[0, 1, 2], [0, 1, 2]] print, multi[11] print, multi[[0, 6, 12]] print, array_indices(multi, [0, 6, 12]) y = sin(findgen(360) * !dtor) ind = where(y gt 0.5 and y lt 0.8, count) print, count if (count gt 0) then y[ind] = !values.f_nan plot, y d = dist(20) surface, d ind = where(d gt 13, count) print, count if (count gt 0) then d[ind] = !values.f_nan surface, d print, ind print, array_indices(d, ind) arr = [] ; IDL 8.0+ only print, n_elements(arr) help, arr arr = [arr, 5] print, arr print, n_elements(arr) ; Structures s1 = { x: 1.0, y: 2.0, color: bytarr(3) } help, s1.x help, s1.(0) help, s1 help, s1, /structures s1.x = 3.0D help, s1, /structures s2 = { point, x: 3.0, y: 4.0, color: bytarr(3) } help, s2, /structures s3 = { point } help, s3, /structures s4 = { point, 5.0, 6.0, [1B, 2B, 3B] } help, s4, /structures ;s5 = { point, x: 3.0, y: 4.0, z:5.0, color: bytarr(3) } ; error! s6 = { point3d, inherits point, z: 5.0 } help, s6, /structures for f = 0L, n_tags(s6) - 1L do help, s6.(f) fieldname = 'Z' ind = where(tag_names(s6) eq fieldname, count) if (count gt 0L) then help, s6.(ind[0]) s7 = create_struct('x', 1.0, 'y', 2.0, 'color', bytarr(3)) help, s7, /structures s8 = create_struct(name='point') help, s8, /structures s9 = create_struct(s7, 'z', 7.0) help, s9, /structures src = { a: 1.0, b: 2.0 } dst = { a: 2.0D, c: 3.0 } struct_assign, src, dst help, dst, /structures p = { mg_point } s = {} ; IDL 8.0+ only help, s s = create_struct(s, 'a', '0') help, s, /structures ; Operators help, 1 / 2 help, 1. / 2 help, 1 + '2' help, 'text' + 1 arr = bytarr(512) + 1 help, arr arr = bytarr(512) + 1B help, arr print, 1 < 2 print, 1 > 2 print, 1 > [-1, 0, 1, 2, 3] a = findgen(4) a[[0, 0, 1]] += 1 print, a a = findgen(4) a[[0, 0, 1]]++ print, a ; Variable information barr = bytarr(10, 20) print, size(barr) print, size(barr, /type) sz = size(barr, /structure) help, sz, /structures ; Strings basename = 'data-08-2007' ext = ".txt" tag = '<div class="article">' print, tag tag = "<div class=""article"">" print, tag help, "12 ; age = "22 years" ; error! age = '22 years' age = "8 years" print, basename print, ext print, basename + ext help, '1' + 2 msg = string(format='("Results for ", A, ": ", F5.2, " (", I0, " trials)")', 'Seq A', 1.2, 6) help, msg help, strtrim(' test ', 2) filenames = 'test' + string(indgen(6), format='(I02)') + '.txt' filenames = string(format='("test", I06, ".txt")', indgen(6)) print, filenames ind = where(strmatch(filenames, '*0[34]*'), count) print, count print, filenames[ind] print, strtrim(5.0, 2) print, filenames matches = strmatch(filenames, '*0[34]*') print, matches print, strcmp('test', 'test.txt') print, strcmp('test', 'test.txt', 4) print, strcmp('test', 'Test.txt', 4, /fold_case) vfilename = 'coax_electrons_5.h5' underpos = strpos(vfilename, '_') runname = strmid(vfilename, 0, underpos) print, runname filename = filepath('people.jpg', subdir=['examples', 'data']) dotpos = strpos(filename, '.', /reverse_search) extension = strmid(filename, dotpos + 1) color = [255, 255, 0] print, strjoin(strtrim(color, 2), ', ') print, transpose(strsplit('coax_electrons_5.h5', '_.', /extract)) print, byte('Test string') print, 'a' + string(9B) + 'b' byteValue = 65b print, string(byteValue) print, string(fix(byteValue)) ; Regular expressions filename = 'coax_electrons_5.h5' re = '^([[:alpha:]]+)_([[:alpha:]]+)_([[:digit:]]+)\.h5$' parts = stregex(filename, re, /extract, /subexpr) print, transpose(parts) print, stregex(['a+b', 'a-b', 'a*b', 'a + b'], 'a.b', /boolean) filename = 'coax_elecfield_1.h5' print, stregex(filename, '^coax', /extract) print, stregex(filename, '^elecfield', /boolean) print, stregex(filename, 'h5$', /boolean) print, stregex(filename, '^coax.*h5$', /extract) filename = 'data.txt' print, stregex(filename, '.+\.(txt|text|dat)', /extract) print, stregex(['abcd', 'abcde'], '^(.{2})+$', /boolean) parts = stregex('data.txt', '.+\.(txt|text|dat)', /extract, /subexpr) print, transpose(parts) filenames = ['data.txt', 'data_txt'] print, stregex(filenames, '\.txt', /boolean) print, stregex('A6FF08', '[ABCDEF0123456789]{6}', /boolean) print, stregex('A6FF08', '[A-F0-9]{6}', /boolean) print, stregex('A6FF08', '[[:xdigit:]]{6}', /boolean) print, stregex('A6FF08', '[^0-9]{6}', /boolean) help, stregex('a or b', '[[:space:]]b', /extract) help, stregex('a or b', '[[:space:]]a', /extract) help, stregex('a or b', '[[:<:]]b', /extract) help, stregex('a or b', '[[:<:]]a', /extract) location = '<location>Boulder, CO</location>' print, stregex(location, '<[^>]+>', /extract) parts = stregex(location, '<([^>]+)>', /subexpr, /extract) print, transpose(parts) parts = stregex(location, '<[^>]+>([^<]+)</[^>]+>', /subexpr, /extract) print, transpose(parts) t = systime() re = '(...) (...) (..) (..):(..):(..) (....)' tokens = stregex(t, re, /extract, /subexpr) print, tokens[2] ; month print, tokens[7] ; year s = 'My email address is someone@gmail.com.' print, stregex(s, '[^@ ]+@([^. ]+\.)+(com|edu|gov)', /extract) delimiters = ',; ' print, transpose(strsplit('1.2, 3.4; 5.6', delimiters, /extract)) re = '[[:space:]]+' print, transpose(strsplit('a and b or c', re, /extract, /regex)) re = '[[:space:]]+(or|and)[[:space:]]+' print, transpose(strsplit('a and b or c', re, /extract, /regex)) ; System variables print, !pi, !dpi ; !pi = 3 ; error! help, !d, /structures defsysv, '!lib', '~/lib' print, !lib !lib = '~/library' print, !lib ; this would actually work because !lib is already defined to be read-write, ; so the following statement would not change this; try this after exiting IDL ;defsysv, '!lib', '~/lib', 1 ;!lib = '~/IDL/lib' ; Pointers p = ptr_new(10) help, p help, *p *p = 20 help, *p a = 30 aptr = ptr_new(a) help, a, *aptr a = 31 *aptr = 32 help, a, *aptr b = 40 bptr = ptr_new(b, /no_copy) help, b, *bptr ptr_free, bptr pcheck = ptr_new(50) print, ptr_valid(pcheck) ptr_free, pcheck print, ptr_valid(pcheck) null = ptr_new() help, null ; help, *null ; error ptr = ptr_new(/allocate_heap) help, ptr print, ptr_valid(ptr) help, *ptr heap_gc, /verbose leaked = ptr_new(60) leaked = 0 ; this leaks memory! heap_gc, /verbose help, /heap anotherLeaked = ptr_new(findgen(10)) anotherLeaked = 0 help, /heap anotherLeaked = ptr_valid(7, /cast) help, anotherLeaked, *anotherLeaked state = { pvalue: ptr_new(findgen(20), /no_copy) } pstate = ptr_new(state, /no_copy) help, pstate help, *pstate help, (*pstate).pvalue help, *(*pstate).pvalue help, (*(*pstate).pvalue)[7] *pstate = bindgen(100) localP = temporary(*p) help, localP, *p *p = temporary(localP) help, localP, *p ; Objects container = obj_new('IDL_Container') print, obj_valid(container) print, obj_class(container) print, obj_isa(container, 'IDL_Container') print, obj_hasmethod(container, 'get') obj_destroy, container filename = filepath('people.jpg', subdir=['examples', 'data']) ali = read_image(filename) window, xsize=256, ysize=256 tv, ali, true=1 x = [98, 132, 187, 200, 203, 155, 123, 85, 91] y = [167, 204, 190, 174, 116, 49, 50, 109, 160] roi = obj_new('IDLanROI', x, y) print, obj_valid(roi) mask = roi->computeMask(dimensions=[256, 256], mask_rule=2) obj_destroy, roi tv, mask, channel=1 ; Lists and hashes data = list() print, n_elements(data) data->add, 0. help, data print, data data->add, [1., 2., 3.] help, data print, data data->add, [4., 5., 6.], /extract help, data print, data data->add, 'Boulder, CO' help, data print, data data->remove, 0 print, data data->remove, [1, 4] print, data data->remove, /all print, data lst = list(['Colorado', 'Utah', 'Idaho'], /extract) print, lst + list(['Washington', 'Nevada'], /extract) print, lst[1:2] lst[2] = 'South Dakota' print, lst print, lst eq 'Utah' if (lst) then print, 'lst not empty' print, ~lst foreach item, lst, i do print, i, item, format='(%"lst[%d] = %s")' elevations = hash() elevations['DEN'] = 1640 elevations['BOU'] = 1625 elevations['COS'] = 1856 elevations['FNL'] = 1529 ; another way to do the same thing ;elevations->set, ['DEN', 'BOU', 'COS', 'FNL'], [1640, 1625, 1856, 1529] print, elevations print, elevations[['BOU', 'DEN']] print, elevations[list(['BOU', 'DEN'], /extract)] print, elevations.keys() help, elevations.toStruct(), /structures foreach elev, elevations, station do print, station, elev, format='(%"%s is at %dm")' ; Common blocks mg_common_example1 mg_common_example2 ; Shared memory shmmap, 'myseg', /double, 20 z = shmvar('myseg') z[0] = dindgen(20) ; careful to not overwrite z ; from another session: ;shmmap, 'myseg', /double, 20 ;z = shmvar('myseg') ;print, z shmunmap, 'myseg' filename = filepath('head.dat', subdir=['examples', 'data']) shmmap, /byte, dimension=[80, 100, 57], get_name=segname, filename=filename, os_handle='head' h = shmvar(segname) window, xsize=80*10, ysize=100*6 for i = 0, 56 do tv, h[*, *, i], i shmunmap, segname