0

i am new to IDL. I have a data file consists of 4 column, 96 rows (saved as new.dat). I am trying to read values only if 2nd column is positive but unable to do it.compiling well but getting no output.Help, please???

data=fltarr(4,96)
openr,1,'new.dat'
openw,2,'file.dat'
readf,1,data
for i=0,95 do begin
if (data(1,*) ge 0) then printf,2, data 
endfor
close,/all
Reti43
  • 9,656
  • 3
  • 28
  • 44
user50695
  • 3
  • 2

2 Answers2

0

Well, there are several things to mention here.

First, when you index an array in IDL you should use [] instead of () to avoid confusion and compiling errors.

Second, in your statement
if (data(1,*) ge 0) then printf,2, data
you are trying to print the entire data file for every time your test in your IF statement is satisfied.

Third, you did not specify a FORMAT statement for your READF statement, which should be done.

Fourth, are you trying to index the second dimension of the array data? If so, you need to use i in the FOR loop.

Fifth, would it be easier to just read in the data and use it? A [4,96]-element array is incredibly small and would take a fraction of a second to read in each time you need it. Then you could use the WHERE routine to determine which elements are greater than zero. For instance, you could try (in the FOR loop):
good = WHERE(data[*,i] GE 0,gd)
where the variable good would contain all the indices satisfying the test statement.

Sixth, I should say that you can and should use WHERE without using a FOR loop here. You can just do the following:
good = WHERE(data GE 0,gd)
and the variable good will contain only elements of data ≥ 0. To index data, you need only do something similar to the following:
data_pos = data[good]
If you want to keep the new variable data_pos the same dimensions as data, then you could use the ARRAY_INDICES routine in IDL and leave the rest of the indices of data_pos as some dummy value you define and know not to be used later.

honeste_vivere
  • 308
  • 5
  • 11
  • I should also point out that you should use `FREE_LUN` instead of `CLOSE` for closing files in IDL. The former frees the logical unit number or LUN in IDL and does so cleanly. – honeste_vivere Jan 12 '15 at 14:16
0

Your code might become much easier if you use "readcol", from http://idlastro.gsfc.nasa.gov/ftp/pro/misc/readcol.pro

Here is a sample code:

openw, filenumber, 'file.dat', /get_lun
readcol, 'new.dat', data0, data1, data2, data3, count=num_read
for i=0,num_read-1 do begin
    if data1[i] gt 0 then printf,2, data2[i] 
endfor
free_lun, filenumber

Another code is:

openw, lun,'file.dat', /get_lun
readcol, 'new.dat', data0, data1, data2, data3, count=num_read
select = where(data1 gt 0, num_select)
for i=0,num_select-1 do printf,2, data2[i] 
free_lun, lun

Note that it is better to use /get_lun while opening files, so that you do not have to manually keep track of open logical unit numbers while coding.

VBB
  • 1,305
  • 7
  • 17