; docformat = 'rst' ;+ ; Batch file containing all the code intended to be typed at the command line ; from the "File I/O" chapter. ;- ; Handling files people = filepath('people.jpg', subdir=['examples', 'data']) print, people datafile = filepath('data.txt', root='..') filename = dialog_pickfile() print, people print, file_basename(people) print, people print, file_dirname(people) print, file_expand_path('.') f = filepath('ascii.txt', subdir=['examples', 'data']) print, file_lines(f) projects = file_search('$HOME/*projects/*{vorpal,vp}*') print, transpose(projects) print, file_test(people) f2 = filepath('non_existing_file', subdir=['examples', 'data']) print, file_test(f2) print, file_which('congrid.pro') ; Supported file formats mineralFilename = filepath('mineral.png', subdir=['examples', 'data']) exists = query_png(mineralFilename, info) help, exists help, info, /structures mineralImage = read_png(mineralFilename, r, g, b) help, mineralImage device, decomposed=0 tvlct, r, g, b window, /free, title='Mineral PNG image', xsize=288, ysize=216 tv, mineralImage ; High-level input and output asciiFilename = filepath('ascii.dat', subdir=['examples', 'data']) asciiData = read_ascii(asciiFilename, comment_symbol='%', $ header=header, data_start=3, $ delimiter=string(9B)) help, asciiData, /structures print, asciiData.field1 asciiTemplate = ascii_template(asciiFilename) asciiData2 = read_ascii(asciiFilename, template=asciiTemplate) help, asciiData2, /structures print, asciiData2.x print, asciiData2.y print, asciiData2.velocity ; Low-level routines windFilename = filepath('ascii.txt', subdir=['examples', 'data']) header = strarr(5) windData = fltarr(7, 15) openr, lun, windFilename, /get_lun readf, lun, header, windData free_lun, lun header = strarr(5) windRow = { lon: 0.0, lat: 0.0, elev: 0L, temp: 0L, dewpt: 0L, speed: 0L, dir: 0L } windData = replicate(windRow, 15) openr, lun, windFilename, /get_lun readf, lun, header, windData free_lun, lun print, windData.temp windRow = { lonlat: fltarr(2), others: lonarr(5) } windData = replicate(windRow, 15) openr, lun, windFilename, /get_lun readf, lun, header, windData free_lun, lun print, windData.lonlat sineFilename = filepath('sine_waves.txt', subdir=['examples', 'data']) nlines = file_lines(sineFilename) sineWaves = lonarr(2, nlines) openr, lun, sineFilename, /get_lun readf, lun, sineWaves free_lun, lun window, /free plot, sineWaves[0, *] oplot, sineWaves[1, *] print, 5, format='("Area = ", F0.2, " m^2")' print, 5, format='(%"Area = %0.2f m^2")' print, findgen(20), format='(5F7.2)' print, findgen(20), format='(5E10.2)' print, lonarr(4) + 255, format='(I10, B10, O10, Z10)' filenames = string(indgen(20), format='("image", I03, ".png")') print, filenames, format='(5(A, " "))' files = file_basename(file_search(filepath('*.*'))) print, files, format='(2A-30)' print, findgen(5), format='(5(F0.1, :, ", "))' .run mg_continuation_format_code dataFiles = file_search(filepath('*.*', subdir=['examples', 'data']), count=nDataFiles) info = file_info(dataFiles) result = replicate({ name: '', size: 0L }, nDataFiles) result.name = file_basename(info.name) result.size = info.size openw, lun, 'datafiles.txt', /get_lun printf, lun, result, format='(A-20, I, " bytes")' free_lun, lun print, string('BLD', 88.5, format='(A, " temperature = ", F0.1)') convecFilename = filepath('convec.dat', subdir=['examples', 'data']) convec = bytarr(248, 248) openr, lun, convecFilename, /get_lun readu, lun, convec free_lun, lun window, /free, xsize=248, ysize=248 tv, convec openw, lun, 'hanning.dat', /get_lun writeu, lun, hanning(20, 20) free_lun, lun littleEndian = (byte(1, 0, 1))[0] openw, lun, 'compressed.gz', /get_lun, /compress printf, lun, transpose(['Hamlet', 'King Lear', 'Macbeth']) free_lun, lun openr, lun, 'compressed.gz', /get_lun, /compress names = strarr(3) readf, lun, names free_lun, lun print, names headFilename = filepath('head.dat', subdir=['examples', 'data']) openr, lun, headFilename, /get_lun head = assoc(lun, bytarr(80, 100)) help, head[0] help, head window, xsize=80*10, ysize=100*6 for i = 0, 56 do tv, head[i], i free_lun, lun ; .sav files restore, filename=filepath('plot_data.sav', subdir=['examples', 'data']), /verbose help, plot_data a = 10 b = 20 c = 30 save, a, b, c, filename='vars.sav', description='Variables A, B, C' s = obj_new('IDL_Savefile', filename=filepath('cow10.sav', subdir=['examples', 'data'])) help, s->contents(), /structures print, s->names() s->restore, 'polylist' help, polylist obj_destroy, s ; Scientific data formats: HDF5 overview hdf5file = filepath('hdf5_test.h5', subdir=['examples', 'data']) ok = h5_browser(hdf5File) s = h5_parse(hdf5file) help, s, /structures fileID = h5f_open(hdf5File) print, fileID eskimoID = h5d_open(fileID, '/images/Eskimo') print, eskimoID eskimo = h5d_read(eskimoID) help, eskimo h5d_close, eskimoID h5f_close, fileID fileId = h5f_open(hdf5file) varId = h5d_open(fileId, '/arrays/3D int array') varSpace = h5d_get_space(varId) start = [3, 5, 0] count = [1, 23, 17] h5s_select_hyperslab, varSpace, start, count, $ block=[1, 1, 1], stride=[1, 2, 3], /reset resultSpace = h5s_create_simple(count) data = h5d_read(varId, $ file_space=varSpace, $ memory_space=resultSpace) help, data h5s_close, resultSpace h5s_close, varSpace h5d_close, varId h5f_close, fileId result = mg_h5_getdata(hdf5file, '/arrays/3D int array', bounds='3, 5:*:2, 0:49:3') help, result data = randomu(seed, 3, 100) outfileID = h5f_create('test.h5') datatypeID = h5t_idl_create(data) dataspaceID = h5s_create_simple([3, 100]) electronsID = h5d_create(outfileID, 'electrons', datatypeID, dataspaceID) h5d_write, electronsID, data h5t_close, datatypeID h5s_close, dataspaceID h5d_close, electronsID h5f_close, outfileID ; Video video = obj_new('IDLffVideoWrite', 'head.mp4') print, video->getFormats() print, video->getCodecs() video->setMetaData, 'title', 'Head sections' video->setMetaData, 'artist', 'Michael Galloy' video->setMetaData, 'album', 'Modern IDL examples' video_index = video->addVideoStream(80, 100, 10) openr, lun, filepath('head.dat', subdir=['examples', 'data']), /get_lun head = assoc(lun, bytarr(80, 100)) for i = 0, 56 do time = video->put(video_index, rebin(reform(head[i], 1, 80, 100), 3, 80, 100)) obj_destroy, video ; Networking ourl = obj_new('IDLnetURL') content = ourl->get(/string_array, url='http://google.com') obj_destroy, ourl print, content slices = [10, 20, 30] baseUrl = 'http://chart.apis.google.com/chart?chs=200x100&cht=p&chd=t:' url = baseUrl + strjoin(strtrim(slices, 2), ',') ourl = obj_new('IDLnetURL') png = ourl->get(filename='gchart.png', url=url) obj_destroy, ourl im = read_png('gchart.png') window, xsize=200, ysize=100 tv, im, true=1 ; XML saxPlanets = obj_new('MGffPlanets') saxPlanets->parseFile, filepath('planets.xml', subdir=['examples', 'data']) print, saxPlanets->getPlanets() obj_destroy, saxPlanets domPlanets = obj_new('IDLffXMLDOMDocument') domPlanets->load, filename=filepath('planets.xml', subdir=['examples', 'data']) mg_planets, domPlanets, planetNames obj_destroy, domPlanets print, planetNames .run mg_xmlsax_example__define