comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: trying to export pixel data from .dat files, based on coordinate loc
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: trying to export pixel data from .dat files, based on coordinate loc [message #71616] Fri, 09 July 2010 15:40 Go to next message
Maxwell Peck is currently offline  Maxwell Peck
Messages: 61
Registered: February 2010
Member
On Jul 10, 2:53 am, 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

Use ENVI_OPEN_FILE. Use the FID from this command in the
ENVI_CONVERT_FILE_COORDINATES. ENVI_CONVERT_FILE_COORDINATES is a
procedure, you need to specify the output variables in the command.

This is relatively straightforward and can almost be copy and pasted
directly from the ENVI help..

Max
Re: trying to export pixel data from .dat files, based on coordinate loc [message #71695 is a reply to message #71616] Mon, 12 July 2010 14:51 Go to previous message
Maxwell Peck is currently offline  Maxwell Peck
Messages: 61
Registered: February 2010
Member
On Jul 13, 7:39 am, Maxwell Peck <maxjp...@gmail.com> wrote:
> On Jul 13, 7:29 am, Snow53 <jennifer_wa...@hotmail.com> wrote:
>
>
>
>> On Jul 12, 3:12 pm, Maxwell Peck <maxjp...@gmail.com> wrote:
>
>>> On Jul 13, 6:37 am, Snow53 <jennifer_wa...@hotmail.com> wrote:
>
>>>> Thanks to all who gave great advice. I almost have this up and
>>>> running, but have a few more questions.
>
>>>> 1. At the moment, I have tested this code based on two files that each
>>>> have their own .hdr. For the real thing, I want to create only
>>>> one .hdr that can be used for all (200+) files since they have the
>>>> same dims, data type, etc. How can I modify this code to look for that
>>>> one .hdr file and use information from that file when looping through
>>>> each file in the folder?
>
>>>> 2. The ENVI Available Bands GUI pops up when I run this.  Is this
>>>> supposed to happen? I read that envi_open_file was non-interactive in
>>>> idl (with batch mode).
>
>>>> 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.
>
>>>> 4. I did notice that I'm not getting back the correct sample/line
>>>> pixel file locations from my input map locations (x,y). They seem to
>>>> be one pixel off. Has anyone else had this happen?
>
>>>> The code is shown below. Thanks again!
>
>>>>  Goal: Extract pixel data based on input coordinate location for each
>>>> file (ENVI binary)
>>>> ; within a specified folder location. Export this data to a .csv file.
>
>>>> pro extractdata4
>
>>>> ;define path
>>>>  cd, 'X:\MERRA\HDF_Output_Lena\test\'
>
>>>> ;open envi files within given folder
>>>>   file_array=file_search('*.dat', count=num_file)
>
>>>> for i=0, num_file-1 do begin
>>>>   file=file_array
>>>>   endfor
>>>> print, num_file
>>>>   print, file
>
>>>> ;read ENVI binary files
>
>>>>   envi_open_file , file, r_fid=fid
>
>>>> ;convert x,y map coordinates to corresponding pixel coordinates. note
>>>> that xmap and ymap can be single values or arrays if
>>>> ;needed to extract info for multiple pixels.
>>>>   XMap=[109.55551335]
>>>>   YMap=[79.25]
>
>>>>   ENVI_CONVERT_FILE_COORDINATES, fid,XF, YF, XMap, YMap
>
>>>> XF_out=Round(XF)
>>>> YF_out=Round(YF)
>>>> print, 'x pix' ,XF_out
>>>> print, 'y pix', YF_out
>
>>>> ;specify the data dims for the pixels who's info you want to extract.
>>>> pos specifies which band(s) you want to extract from.
>>>> ;for example, if I have 4 bands and I only want to extract from bands
>>>> 1 and 4, pos would be [0,3]. Then extract for these pixels/bands.
>>>>   dims=[-1, XF_out, XF_out, YF_out, YF_out]
>>>>   pos=[0]
>>>>   pixdata = ENVI_GET_DATA(fid=fid, dims=dims, pos=pos)
>
>>>>   print, pixdata
>>>> ;open text file to write data to
>>>> OPENU, U, 'pixel_value.csv', /get_lun, /append
>>>> ;write data
>>>> printf, U, pixdata
>>>> ;close LUN
>>>> close, U
>
>>>> end
>
>>> 1. It can be done just using IDL to read in the images but it will
>>> probably be easier for you just to duplicate the header file and keep
>>> them in ENVI format. (Unless you wanted to completely rewrite the
>>> program)
>
>>> 2. You probably want ,/NO_REALISE in your envi_open_file.
>
>>> 3. Yes, use ENVI_FILE_MNG to close the file each time after the map
>>> conversion.
>
>>> 4. Is it one pixel off or half a pixel off ?
>
>>> Max- Hide quoted text -
>
>>> - Show quoted text -
>
>> Thanks Max.
>
>> I just checked and it is one pixel off, not 0.5.
>
> Ok, im assuming you're using ENVI to check. Envi starts at 1,1 in the
> top left whereas the conversion i think uses 0,0 . I don't have envi
> here to check at the moment so I'd take a look yourself.

(I should add this is defined by the xstart and ystart in the ENVI
header.. Whether yours will change from 1,1 i don't know.)
Re: trying to export pixel data from .dat files, based on coordinate loc [message #71697 is a reply to message #71616] Mon, 12 July 2010 14:39 Go to previous message
Maxwell Peck is currently offline  Maxwell Peck
Messages: 61
Registered: February 2010
Member
On Jul 13, 7:29 am, Snow53 <jennifer_wa...@hotmail.com> wrote:
> On Jul 12, 3:12 pm, Maxwell Peck <maxjp...@gmail.com> wrote:
>
>
>
>> On Jul 13, 6:37 am, Snow53 <jennifer_wa...@hotmail.com> wrote:
>
>>> Thanks to all who gave great advice. I almost have this up and
>>> running, but have a few more questions.
>
>>> 1. At the moment, I have tested this code based on two files that each
>>> have their own .hdr. For the real thing, I want to create only
>>> one .hdr that can be used for all (200+) files since they have the
>>> same dims, data type, etc. How can I modify this code to look for that
>>> one .hdr file and use information from that file when looping through
>>> each file in the folder?
>
>>> 2. The ENVI Available Bands GUI pops up when I run this.  Is this
>>> supposed to happen? I read that envi_open_file was non-interactive in
>>> idl (with batch mode).
>
>>> 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.
>
>>> 4. I did notice that I'm not getting back the correct sample/line
>>> pixel file locations from my input map locations (x,y). They seem to
>>> be one pixel off. Has anyone else had this happen?
>
>>> The code is shown below. Thanks again!
>
>>>  Goal: Extract pixel data based on input coordinate location for each
>>> file (ENVI binary)
>>> ; within a specified folder location. Export this data to a .csv file.
>
>>> pro extractdata4
>
>>> ;define path
>>>  cd, 'X:\MERRA\HDF_Output_Lena\test\'
>
>>> ;open envi files within given folder
>>>   file_array=file_search('*.dat', count=num_file)
>
>>> for i=0, num_file-1 do begin
>>>   file=file_array
>>>   endfor
>>> print, num_file
>>>   print, file
>
>>> ;read ENVI binary files
>
>>>   envi_open_file , file, r_fid=fid
>
>>> ;convert x,y map coordinates to corresponding pixel coordinates. note
>>> that xmap and ymap can be single values or arrays if
>>> ;needed to extract info for multiple pixels.
>>>   XMap=[109.55551335]
>>>   YMap=[79.25]
>
>>>   ENVI_CONVERT_FILE_COORDINATES, fid,XF, YF, XMap, YMap
>
>>> XF_out=Round(XF)
>>> YF_out=Round(YF)
>>> print, 'x pix' ,XF_out
>>> print, 'y pix', YF_out
>
>>> ;specify the data dims for the pixels who's info you want to extract.
>>> pos specifies which band(s) you want to extract from.
>>> ;for example, if I have 4 bands and I only want to extract from bands
>>> 1 and 4, pos would be [0,3]. Then extract for these pixels/bands.
>>>   dims=[-1, XF_out, XF_out, YF_out, YF_out]
>>>   pos=[0]
>>>   pixdata = ENVI_GET_DATA(fid=fid, dims=dims, pos=pos)
>
>>>   print, pixdata
>>> ;open text file to write data to
>>> OPENU, U, 'pixel_value.csv', /get_lun, /append
>>> ;write data
>>> printf, U, pixdata
>>> ;close LUN
>>> close, U
>
>>> end
>
>> 1. It can be done just using IDL to read in the images but it will
>> probably be easier for you just to duplicate the header file and keep
>> them in ENVI format. (Unless you wanted to completely rewrite the
>> program)
>
>> 2. You probably want ,/NO_REALISE in your envi_open_file.
>
>> 3. Yes, use ENVI_FILE_MNG to close the file each time after the map
>> conversion.
>
>> 4. Is it one pixel off or half a pixel off ?
>
>> Max- Hide quoted text -
>
>> - Show quoted text -
>
> Thanks Max.
>
> I just checked and it is one pixel off, not 0.5.

Ok, im assuming you're using ENVI to check. Envi starts at 1,1 in the
top left whereas the conversion i think uses 0,0 . I don't have envi
here to check at the moment so I'd take a look yourself.
Re: trying to export pixel data from .dat files, based on coordinate loc [message #71698 is a reply to message #71616] Mon, 12 July 2010 14:29 Go to previous message
Snow53 is currently offline  Snow53
Messages: 32
Registered: July 2010
Member
On Jul 12, 3:12 pm, Maxwell Peck <maxjp...@gmail.com> wrote:
> On Jul 13, 6:37 am, Snow53 <jennifer_wa...@hotmail.com> wrote:
>
>
>
>
>
>> Thanks to all who gave great advice. I almost have this up and
>> running, but have a few more questions.
>
>> 1. At the moment, I have tested this code based on two files that each
>> have their own .hdr. For the real thing, I want to create only
>> one .hdr that can be used for all (200+) files since they have the
>> same dims, data type, etc. How can I modify this code to look for that
>> one .hdr file and use information from that file when looping through
>> each file in the folder?
>
>> 2. The ENVI Available Bands GUI pops up when I run this.  Is this
>> supposed to happen? I read that envi_open_file was non-interactive in
>> idl (with batch mode).
>
>> 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.
>
>> 4. I did notice that I'm not getting back the correct sample/line
>> pixel file locations from my input map locations (x,y). They seem to
>> be one pixel off. Has anyone else had this happen?
>
>> The code is shown below. Thanks again!
>
>>  Goal: Extract pixel data based on input coordinate location for each
>> file (ENVI binary)
>> ; within a specified folder location. Export this data to a .csv file.
>
>> pro extractdata4
>
>> ;define path
>>  cd, 'X:\MERRA\HDF_Output_Lena\test\'
>
>> ;open envi files within given folder
>>   file_array=file_search('*.dat', count=num_file)
>
>> for i=0, num_file-1 do begin
>>   file=file_array
>>   endfor
>> print, num_file
>>   print, file
>
>> ;read ENVI binary files
>
>>   envi_open_file , file, r_fid=fid
>
>> ;convert x,y map coordinates to corresponding pixel coordinates. note
>> that xmap and ymap can be single values or arrays if
>> ;needed to extract info for multiple pixels.
>>   XMap=[109.55551335]
>>   YMap=[79.25]
>
>>   ENVI_CONVERT_FILE_COORDINATES, fid,XF, YF, XMap, YMap
>
>> XF_out=Round(XF)
>> YF_out=Round(YF)
>> print, 'x pix' ,XF_out
>> print, 'y pix', YF_out
>
>> ;specify the data dims for the pixels who's info you want to extract.
>> pos specifies which band(s) you want to extract from.
>> ;for example, if I have 4 bands and I only want to extract from bands
>> 1 and 4, pos would be [0,3]. Then extract for these pixels/bands.
>>   dims=[-1, XF_out, XF_out, YF_out, YF_out]
>>   pos=[0]
>>   pixdata = ENVI_GET_DATA(fid=fid, dims=dims, pos=pos)
>
>>   print, pixdata
>> ;open text file to write data to
>> OPENU, U, 'pixel_value.csv', /get_lun, /append
>> ;write data
>> printf, U, pixdata
>> ;close LUN
>> close, U
>
>> end
>
> 1. It can be done just using IDL to read in the images but it will
> probably be easier for you just to duplicate the header file and keep
> them in ENVI format. (Unless you wanted to completely rewrite the
> program)
>
> 2. You probably want ,/NO_REALISE in your envi_open_file.
>
> 3. Yes, use ENVI_FILE_MNG to close the file each time after the map
> conversion.
>
> 4. Is it one pixel off or half a pixel off ?
>
> Max- Hide quoted text -
>
> - Show quoted text -

Thanks Max.

I just checked and it is one pixel off, not 0.5.
Re: trying to export pixel data from .dat files, based on coordinate loc [message #71699 is a reply to message #71616] Mon, 12 July 2010 14:12 Go to previous message
Maxwell Peck is currently offline  Maxwell Peck
Messages: 61
Registered: February 2010
Member
On Jul 13, 6:37 am, Snow53 <jennifer_wa...@hotmail.com> wrote:
> Thanks to all who gave great advice. I almost have this up and
> running, but have a few more questions.
>
> 1. At the moment, I have tested this code based on two files that each
> have their own .hdr. For the real thing, I want to create only
> one .hdr that can be used for all (200+) files since they have the
> same dims, data type, etc. How can I modify this code to look for that
> one .hdr file and use information from that file when looping through
> each file in the folder?
>
> 2. The ENVI Available Bands GUI pops up when I run this.  Is this
> supposed to happen? I read that envi_open_file was non-interactive in
> idl (with batch mode).
>
> 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.
>
> 4. I did notice that I'm not getting back the correct sample/line
> pixel file locations from my input map locations (x,y). They seem to
> be one pixel off. Has anyone else had this happen?
>
> The code is shown below. Thanks again!
>
>  Goal: Extract pixel data based on input coordinate location for each
> file (ENVI binary)
> ; within a specified folder location. Export this data to a .csv file.
>
> pro extractdata4
>
> ;define path
>  cd, 'X:\MERRA\HDF_Output_Lena\test\'
>
> ;open envi files within given folder
>   file_array=file_search('*.dat', count=num_file)
>
> for i=0, num_file-1 do begin
>   file=file_array
>   endfor
> print, num_file
>   print, file
>
> ;read ENVI binary files
>
>   envi_open_file , file, r_fid=fid
>
> ;convert x,y map coordinates to corresponding pixel coordinates. note
> that xmap and ymap can be single values or arrays if
> ;needed to extract info for multiple pixels.
>   XMap=[109.55551335]
>   YMap=[79.25]
>
>   ENVI_CONVERT_FILE_COORDINATES, fid,XF, YF, XMap, YMap
>
> XF_out=Round(XF)
> YF_out=Round(YF)
> print, 'x pix' ,XF_out
> print, 'y pix', YF_out
>
> ;specify the data dims for the pixels who's info you want to extract.
> pos specifies which band(s) you want to extract from.
> ;for example, if I have 4 bands and I only want to extract from bands
> 1 and 4, pos would be [0,3]. Then extract for these pixels/bands.
>   dims=[-1, XF_out, XF_out, YF_out, YF_out]
>   pos=[0]
>   pixdata = ENVI_GET_DATA(fid=fid, dims=dims, pos=pos)
>
>   print, pixdata
> ;open text file to write data to
> OPENU, U, 'pixel_value.csv', /get_lun, /append
> ;write data
> printf, U, pixdata
> ;close LUN
> close, U
>
> end

1. It can be done just using IDL to read in the images but it will
probably be easier for you just to duplicate the header file and keep
them in ENVI format. (Unless you wanted to completely rewrite the
program)

2. You probably want ,/NO_REALISE in your envi_open_file.

3. Yes, use ENVI_FILE_MNG to close the file each time after the map
conversion.

4. Is it one pixel off or half a pixel off ?

Max
RE: trying to export pixel data from .dat files, based on coordinate loc [message #71700 is a reply to message #71616] Mon, 12 July 2010 13:37 Go to previous message
Snow53 is currently offline  Snow53
Messages: 32
Registered: July 2010
Member
Thanks to all who gave great advice. I almost have this up and
running, but have a few more questions.


1. At the moment, I have tested this code based on two files that each
have their own .hdr. For the real thing, I want to create only
one .hdr that can be used for all (200+) files since they have the
same dims, data type, etc. How can I modify this code to look for that
one .hdr file and use information from that file when looping through
each file in the folder?

2. The ENVI Available Bands GUI pops up when I run this. Is this
supposed to happen? I read that envi_open_file was non-interactive in
idl (with batch mode).

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.

4. I did notice that I'm not getting back the correct sample/line
pixel file locations from my input map locations (x,y). They seem to
be one pixel off. Has anyone else had this happen?

The code is shown below. Thanks again!


Goal: Extract pixel data based on input coordinate location for each
file (ENVI binary)
; within a specified folder location. Export this data to a .csv file.

pro extractdata4


;define path
cd, 'X:\MERRA\HDF_Output_Lena\test\'

;open envi files within given folder
file_array=file_search('*.dat', count=num_file)

for i=0, num_file-1 do begin
file=file_array
endfor
print, num_file
print, file


;read ENVI binary files

envi_open_file , file, r_fid=fid



;convert x,y map coordinates to corresponding pixel coordinates. note
that xmap and ymap can be single values or arrays if
;needed to extract info for multiple pixels.
XMap=[109.55551335]
YMap=[79.25]

ENVI_CONVERT_FILE_COORDINATES, fid,XF, YF, XMap, YMap

XF_out=Round(XF)
YF_out=Round(YF)
print, 'x pix' ,XF_out
print, 'y pix', YF_out


;specify the data dims for the pixels who's info you want to extract.
pos specifies which band(s) you want to extract from.
;for example, if I have 4 bands and I only want to extract from bands
1 and 4, pos would be [0,3]. Then extract for these pixels/bands.
dims=[-1, XF_out, XF_out, YF_out, YF_out]
pos=[0]
pixdata = ENVI_GET_DATA(fid=fid, dims=dims, pos=pos)

print, pixdata
;open text file to write data to
OPENU, U, 'pixel_value.csv', /get_lun, /append
;write data
printf, U, pixdata
;close LUN
close, U



end
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: SAVE compress and Gzip
Next Topic: trying to export pixel data from .dat files, based on coordinate loc

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 15:33:30 PDT 2025

Total time taken to generate the page: 0.00806 seconds