trying to export pixel data from .dat files, based on coordinate loc [message #71623] |
Fri, 09 July 2010 09:53  |
Snow53
Messages: 32 Registered: July 2010
|
Member |
|
|
Hi, I'm new to IDL so this might sound very easy to some. Sorry!
I have 200+ .dat files in one folder, and one associated .hdr file
that will work for all of them.
I would like to loop through all the files and extract pixel value
information based on an input coordinate location (lat, long) for each
file, and then export all this information into a .txt file or
similar.
I've been trying to follow other posts that have done similar, but I
seem to be writing this out wrong as my code isn't compiling correctly
(I seem to have problems on lines 9 & 14, see below). I don't know
enough about IDL rules to know the correct way to do this.
If anyone could advise, I'd be so grateful! Cheers!
Name: extractdata.pro
;
; Goal: Extract pixel data based on input coordinate location for each
file (.dat)
; within a specified folder location. Export this data to a .txt file.
pro extractdata
;define path
filepath='X:\MERRA\HDF_Output_Lena\'
;open envi files within given folder
file_array=file_search[filepath, '*.dat', count['*.dat']= num_file]
for i=0, num_file-1 do begin
file=file_array[i]
print, num_file
;read ENVI binary file
read_ENVI_image (file, headerfile= filepath, '*.hdr')
;extract pixel information based on lat long coordinates
b=ENVI_CONVERT_FILE_COORDINATES [106.002, 83.0]
v=b
print, v
;open text file to write data to
OPENU, U, 'pixel_value.txt', /get_lun, /append
;write data
printf, U, v
;close LUN
close, U
endfor
end
|
|
|
Re: trying to export pixel data from .dat files, based on coordinate loc [message #71696 is a reply to message #71623] |
Mon, 12 July 2010 14:51  |
jeanh
Messages: 79 Registered: November 2009
|
Member |
|
|
On 12/07/2010 4:37 PM, Snow53 wrote:
> 3. The way the code currently reads, it will output twice when I run
> it but only giving me pixel data from the first file read under
> 'file'. I think that I need to write a loop to specify to read from
> the 'file' list one at a time, go through the code, close that file,
> and then start with the next. I'm not sure, though, how to write
> this, and would appreciate advice.
>for i=0, num_file-1 do begin
> file=file_array
> endfor
ok, first, change the line to
file=file_array[i]
so that you get the i-th element.
Then, simply move the "endfor" statement to the bottom of your code,
just before the "end" of you procedure
Jean
|
|
|
Re: trying to export pixel data from .dat files, based on coordinate loc [message #71713 is a reply to message #71623] |
Sat, 10 July 2010 07:16  |
jeanh
Messages: 79 Registered: November 2009
|
Member |
|
|
> file_array=file_search[filepath, '*.dat', count['*.dat']= num_file]
This can not work. You are subscripting a keyword (count), moreover in a
very strange way (IDL is not PHP, you can't really use strings to subset
an array)
try:
file_array=file_search[filepath, '*.dat', count= num_file]
> for i=0, num_file-1 do begin
>
> file=file_array[i]
> print, num_file
>
> ;read ENVI binary file
> read_ENVI_image (file, headerfile= filepath, '*.hdr')
> ;extract pixel information based on lat long coordinates
> b=ENVI_CONVERT_FILE_COORDINATES [106.002, 83.0]
look up the help file for ENVI_CONVERT_FILE_COORDINATES
You are calling it in a totally wrong way... it is a procedure, not a
function. It requires 5 arguments
> v=b
> print, v
this is useless... you can work on b directly (though, once you have
call the ENVI_CONVERT_FILE_COORDINATES procedure the proper way, you
will have 2 variables to handle)
>
> ;open text file to write data to
> OPENU, U, 'pixel_value.txt', /get_lun, /append
> ;write data
> printf, U, v
> ;close LUN
> close, U
For efficiency, you can open the file before the loop, close it after
the loop and simply write your variables within the loop.
>
> endfor
>
> end
>
>
Jean
|
|
|
Re: trying to export pixel data from .dat files, based on coordinate loc [message #71714 is a reply to message #71623] |
Fri, 09 July 2010 21:05  |
Jeremy Bailin
Messages: 618 Registered: April 2008
|
Senior Member |
|
|
On Jul 9, 12:53 pm, Snow53 <jennifer_wa...@hotmail.com> wrote:
> Hi, I'm new to IDL so this might sound very easy to some. Sorry!
> I have 200+ .dat files in one folder, and one associated .hdr file
> that will work for all of them.
> I would like to loop through all the files and extract pixel value
> information based on an input coordinate location (lat, long) for each
> file, and then export all this information into a .txt file or
> similar.
>
> I've been trying to follow other posts that have done similar, but I
> seem to be writing this out wrong as my code isn't compiling correctly
> (I seem to have problems on lines 9 & 14, see below). I don't know
> enough about IDL rules to know the correct way to do this.
>
> If anyone could advise, I'd be so grateful! Cheers!
>
> Name: extractdata.pro
> ;
> ; Goal: Extract pixel data based on input coordinate location for each
> file (.dat)
> ; within a specified folder location. Export this data to a .txt file.
>
> pro extractdata
> ;define path
> filepath='X:\MERRA\HDF_Output_Lena\'
> ;open envi files within given folder
> file_array=file_search[filepath, '*.dat', count['*.dat']= num_file]
> for i=0, num_file-1 do begin
>
> file=file_array[i]
> print, num_file
>
> ;read ENVI binary file
> read_ENVI_image (file, headerfile= filepath, '*.hdr')
> ;extract pixel information based on lat long coordinates
> b=ENVI_CONVERT_FILE_COORDINATES [106.002, 83.0]
> v=b
> print, v
>
> ;open text file to write data to
> OPENU, U, 'pixel_value.txt', /get_lun, /append
> ;write data
> printf, U, v
> ;close LUN
> close, U
>
> endfor
>
> end
I think you're mixing up parentheses () and square brackets []. Use
the former for function calls and mathematical precedence, and the
latter for subscripting arrays.
(well, you can actually use parentheses to subscript arrays too, but
it's generally a better idea to use square brackets. but you
definitely can't use square brackets to call functions, as you're
doing)
-Jeremy.
|
|
|