0

I would like to spatially subset LANDSAT photos in ENVI using an IDL program. I have over 150 images that I would like to subset, so I'd like to run the program in batch mode (with no interaction). I know how to do it manually, but what command would I use to spatially subset the image via lat/long coordinates in IDL code?

Jim Lewis
  • 43,505
  • 7
  • 82
  • 96
Carly Hyatt
  • 1
  • 1
  • 2

1 Answers1

2

Here is some inspiration, for a single file. You can do the same for a large number of files by building up a list of filenames and looping over it.

; define the image to be opened (could be in a loop), I believe it can also be a tif, img...
img_file='path/to/image.hdr'
envi_open_file,img_file,r_fid=fid
if (fid eq -1) then begin
    print, 'Error when opening file ',img_file
    return
  endif

; let's define some coordinates
XMap=[-70.0580916, -70.5006694]
YMap=[-32.6030694, -32.9797194]
; now convert coordinates into pixel position:
; the transformation function uses the image geographic information:
ENVI_CONVERT_FILE_COORDINATES, FID, XF, YF, XMap, YMap
; we must consider integer. Think twice here, maybe you need to floor() or ceil()
XF=ROUND(XF)
YF=ROUND(YF)

; read the image
envi_file_query, fid, DIMS=DIMS, NB=NB, NL=NL, NS=NS
pos  = lindgen(nb)
; and store it in an array
image=fltarr(NS, NL, NB)
; read each band sequentially
FOR i=0, NB-1 DO BEGIN
   image[*,*,i]= envi_get_data(fid=fid, dims=dims, pos=pos[i])
endfor

; simply crop the data with array-indexing function
imagen= image[XF[0]:XF[1],YF[0]:YF[1]]

nl2=YF[1]-YF[0]
ns2=XF[1]-XF[0]

; read mapinfo to save it in the final file
map_info=envi_get_map_info(fid=fid)

envi_write_envi_file, imagen, data_type=4, $
  descrip = 'cropped', $
  map_info = map_info, $
  nl=nl2, ns=ns2, nb=nb, r_fid=r_fid, $
  OUT_NAME = 'path/to/cropped.hdr'
Bruno von Paris
  • 882
  • 1
  • 7
  • 26