create a loop on IDL [message #93322] |
Wed, 15 June 2016 23:08  |
smnadoum
Messages: 24 Registered: June 2016
|
Junior Member |
|
|
Hi, I am new to IDL. I need help in creating a loop for a code that I am working on.
Here is my code:
pro soil_timeseries, dir, soil, outarr
files = file_search(dir, '*.bsq', count = nfiles)
file= 'F:\soil\MCD43A4.2000.049_soil.bsq\'
outarr = [ ]
get_lun, read_lun
openr, read_lun, file
a = assoc(read_lun, intarr(2400,2400))
for i = 0, nfiles - 1 do begin
print, 'files ', i
file = files(i)
soil= a[i]
roi= soil[28,1759]*0.0001
outarr = [outarr, roi]
endfor
free_lun
close, /all
return
end
It only reads one file, how can I get the code to read all of the files. I have 534 files, however, it is only ready one that is specified in the code:file= 'F:\soil\MCD43A4.2000.049_soil.bsq\'. I don't know if I am doing the right thing with:'for i = 0, nfiles - 1 do begin' because it doesn't seems to work (read all of the files)
Thank you.
|
|
|
Re: create a loop on IDL [message #93330 is a reply to message #93322] |
Thu, 16 June 2016 07:17   |
natha
Messages: 482 Registered: October 2007
|
Senior Member |
|
|
I am not exactly sure of what you are doing but check my code:
pro soil_timeseries, dir, soil, outarr
files = file_search(dir, '*.bsq', count = nfiles)
file= 'F:\soil\MCD43A4.2000.049_soil.bsq\'
outarr = [ ]
data=intarr(2400,2400,/nozero)
for i = 0, nfiles - 1 do begin
print, files(i)
openr, lun, files(i), /get_lun
get_lun, read_lun
soil = assoc(read_lun, data)
roi = soil[28,1759]*0.0001
outarr = [outarr, roi]
free_lun
endfor
return
end
|
|
|
|
Re: create a loop on IDL [message #93342 is a reply to message #93336] |
Fri, 17 June 2016 23:01  |
andrewcool777
Messages: 27 Registered: November 2012
|
Junior Member |
|
|
On Friday, 17 June 2016 05:07:08 UTC+9:30, Shereen Nadoum wrote:
> Thank you very much. Your code makes more sense than mine.
>
> I am trying to write a code to read all 534 bsq files and extract data for specific pixel values.
> I am creating a time series for soil data for 12 years. I am very new to IDL.. been unsuccessful in writing this code.
> 1) read the bsq data
> 2)write the data in an array
> 3) extract the data specific pixel values.
>
> I don't really know if the code I have is complete yet.
Shereen,
Try this.
Regards,
Andrew
pro soil_timeseries, dir, soil, outarr
files = file_search(dir, '*.bsq', count = nfiles)
help,files
If files[0] EQ '' then begin
message,'No files found'
Endif
; file= 'F:\soil\MCD43A4.2000.049_soil.bsq\'
; how many files were found?
nfiles = N_Elements(files)
; as you have 534 files, and are reading just 1 element from the data in each file, then create an
; array that is simply 534 elements long
outarr = FLTARR(nfiles)
data=intarr(2400,2400,/nozero)
for i = 0, nfiles - 1 do begin
print, i, ' ', files(i)
openr, read_lun, files(i), /get_lun
READF,read_lun, data
; this is reading just 1 element out of a 2D array)
; so copy single element into "ith" position of output array
outarr[i] = data[28,1759]*0.0001
free_lun,read_lun
endfor
end
|
|
|