Binary File I/O: {Gumley 4.4} Unformatted (binary) files: READU reads data from an unformatted file WRITEU writes data to an unformatted file (note, not printu) For example: IDL> a=1. IDL> b=2. IDL> c=3. IDL> OPENW,3,'utestfile' IDL> PRINT,a,b,c IDL> WRITEU,3,a,b,c IDL> arr=FINDGEN(3,2) IDL> PRINT,TRANSPOSE(arr) IDL> WRITEU,3,arr IDL> CLOSE,3 IDL> OPENR,4,'utestfile' IDL> d=0. IDL> e=0. IDL> f=0. IDL> PRINT,d,e,f IDL> READU,4,d,e,f IDL> PRINT,d,e,f IDL> array=FLTARR(3,2) IDL> PRINT,TRANSPOSE(array) IDL> READU,4,array IDL> PRINT,TRANSPOSE(array) IDL> CLOSE,4 note, unformatted sequential Fortran places extra information at the beginning and end of each data record; so if reading an unformatted file produced by Fortran code or writing an unformatted file to later be read by a Fortran code add the following keyword to the OPENR, OPENW and OPENU statements: /F77_UNFORMATTED byte swapping - big_endian (Sun Solaris Ultra, SGI, IBM, HP, Mac) - little_endian (Sun Solaris X86, Linux, Windows, Compaq) -if you always want your data in "big_endian" format, then add /SWAP_IF_LITTLE_ENDIAN to your open statements, i.e., swapping occurs if you are running IDL on a little_endian computer. -if you always want your data in "little_endian" format, then add /SWAP_IF_BIG_ENDIAN to your open statements. Random access: When dealing with large data files, it is often not desirable or feasible to read the entire file, when only some small part of it is needed. This brings up the need for random access to files, meaning the ability to read and write from arbitrary positions within the file. POINT_LUN moves the file pointer to a specified position or reads the curent position IDL> OPENR,4,'utestfile' IDL> d=0. IDL> READU,4,d ; read the first number again IDL> POINT_LUN,-4,loc ; read the current file position IDL> READU,4,e,f ; read the next two numbers IDL> PRINT,d,e,f ; print the numbers IDL> e=0. IDL> f=0. IDL> PRINT,d,e,f IDL> POINT_LUN,4,loc ; return to the stored position IDL> READU,4,e,f ; read the second and third numers again IDL> PRINT,d,e,f IDL> CLOSE,4 ASSOC associates a data structure with a file to facilitate random acess Often binary files contain a series of large records that are all of the same size and format. The ASSOC command lets us tell IDL the format of each record, and then lets us automatically access them whenever needed. IDL> arr=FINDGEN(256,256) ; create a 2D index array IDL> TVSCL,arr ; display the array as an image IDL> TVSCL,-arr ; display minus the array IDL> TVSCL,TRANSPOSE(arr) ; display the transpose of the array IDL> OPENW,5,'imgtestfile' ; create file in which to write images IDL> WRITEU,5,arr ; write first image IDL> WRITEU,5,-arr ; write second image IDL> WRITEU,5,TRANSPOSE(arr) ; write third image IDL> CLOSE,5 ; close the image file IDL> OPENR,6,'imgtestfile' ; open the image file to read IDL> image=ASSOC(6, FLTARR(256,256)) ; associate a 256 x 256 ; array of floats to this file IDL> TVSCL,image[1] ; show middle image IDL> TVSCL,image[2] ; show last image IDL> TVSCL,image[0] ; show first image IDL> CLOSE,6