Back Home Next

Subchapters:

Reading and writing data

There is a lot of ways to read and write data in different formats, from gif, mpeg to fits and others that I even don't know <;-). Have a look at the help... I will here deal with ascii and fits files. To save a sequence of commands, use the  journal command.

For ASCII data like, for example, a catalog of stars like used in the above structure example. You have to know the format of your data and you have to initialize the array of structures. Give a name for each tag corresponding to the different columns of the data file and give an initial value that describe their type. For example, to read a 10000-lines file of the form:

[...]
 etoile_15 30.015 -0.752 10.722
 etoile_11 31.163 -9.109 10.761
 etoile_16 39.789 -7.716 11.071
 etoile_14 35.110  6.785 11.176
 etoile_31 33.530  9.306 11.823
 etoile_04 33.480  5.568 11.978
 etoile_00 31.795  7.631 12.055
 etoile_18 35.884  8.612 12.516
 etoile_17 38.860  2.498 12.628
 etoile_19 37.472  8.993 12.722
[...]
define:

        IDL>newcat = replicate({etoile, name: 'etoile', ra: 0.0, dec: 0.0, mag : 0.0},10000)

Now, open the file. You have to specify a logical unit number. This number will be associated with the file (you can open many files at the same time, each of them with a different logical unit number). You will have to specify this number each time you will access the file. For example:

        IDL>openr,122,'stars.cat'
        IDL>readf,122,variable

You can put this number in a variable, let's say lun1.

        IDL>lun1 = 122
        IDL>openr,lun1,'stars.cat'

If you don't want to risk a collision with an already open file with the same logical unit number, ask IDL to give you this number, by the /get_lun keyword. IDL knows what numbers are not used at the time you opened the file:

        IDL>openr,lun1,'stars.cat',/get_lun
        IDL>print,lun1
              101

then you can read the whole catalog in one time:

        IDL>readf,lun1,newcat,format='(a10,f7.3,f7.3,f7.3)'

If you have only numerical data, you can read them without format. The format is similar to the Fortran one. Now, don't forget to close the file and to free the logical unit number:

        IDL>close,lun1
        IDL>free_lun,lun1

You can now play with the newcat array of structures. If you don't know the number of data nor the format, the READ_ASCII procedure will help you. Or you can do a loop with a while not eof(lun).

Writing data follow the same principle, except the opening is:

        IDL>openw,lun,'test.tmp',/get_lun
        IDL>printf,lun,img
        IDL>close,lun
        IDL>free_lun,lun

You can write gif and jpeg images.

Quick access to ASCII files

Using my lect_ascii() function, it's easy to read an ascii file and have the data in one array of structure.

        IDL> lect_ascii() ; will let you choose the file to read
        IDL> lect_ascii('table.dat') 
        IDL> lect_ascii(template=templ) ;first use of template: will store the format
        IDL> lect_ascii(template=template) ; second use of template: will use the previously defined template for the format of the file (line number can change)

The lect_ascii() function is not very well written, it use twice the memory needed for the input file, so don't use it with very very big files. Better in this case to use readu. 

Save and Restore


If you have to go and exit from the IDL session, you can save the values of the variables (even arrays and structure). Just do:

        IDL>save,filename='minha_sessao.sav'

When coming back from the beach, just restore the session with:

        IDL>restore,'minha_sessao.sav'

You can also specify one or more variables that you just want to save:

        IDL>save,filename='minhas_images.sav',image1,image2

Fits files


There is no way to work with the Fits format in the standard version of IDL, but there is a lot of public external libraries to do this. For example, the astro package contains the fits_read procedure:

        IDL>add_path,/as,/es
        IDL>$ ls *.fits
              q1.fits
        IDL>fits_read,'test/q1.fits',img,head
        IDL>help,img,head
              IMG             INT       = Array[71, 71]
              HEAD            STRING    = Array[126]
        IDL>tvim,img,/scale

You have all the information about the image in the string array head. 

 

IDL courses C. Morisset 2004 IA/UNAM V 2.2

Back Home Next