On Apr 6, 10:34 pm, petersod...@gmail.com wrote:
> I'm going to go ahead and apologize in advance for such a newbie
> question, but I am *slowly* trying to learn the basics of how to write
> ENVI functions. My particular problem is pretty simple, but I've been
> trying to figure it out for far too long and need help. I simply want
> to write a function that 1) opens a spectral library, 2) figures out
> the dimensions of the file, 3) applies a calculation upon each spectra
> and then 4) saves these manipulated spectra into a new spectral
> library which has an appended name (for example, USGSSpecLib_appended)
> and appropriate header file. Could someone give me an example of how
> to do this?
>
> Thanks for your help!
>
> Dave
This should get you pretty close (there may be typos or syntax errors,
this was off the top of my head):
;1) open the spectral library
;specify the filename (or use dialog_picfile(), envi_select, etc.)
in_file = 'spectral_library_file.sli'
;Open the file
envi_open_file, in_file, r_fid=fid
;2) get file dimensions and names of spectra
envi_file_query, fid, spec_names=spec_names, ns=ns, nl=nl,
file_type=file_type
;(you could also ask for nb (#bands) here, but since this is a
spectral
;library it would be 1.
;3) apply a calculation to the spectra
spectra = envi_get_data(fid=fid, pos=0, dims=[-1,0,ns-1, 0, nl-1])
;the variable spectra now contains a 2D array, each row being one of
the
;library spectra. you can now perform any kind of calculation you'd
like.
;Lets assume you wanted to multiply every element of each spectrum by
2
;for the purposes of this exercise
outdata = spectra * 2
;note that if you have a very large spectral library such that it's
not
;efficient to hold them all in memory at once you can set up tiling
routines.
;4)Save new spectral library
outname = file_basename(in_file, '.sli') + '_appended.sli'
openw, lun, outname, /get_lun
writeu, lun, outdata
free_lun, lun
envi_setup_head, fname=outname, ns=ns, nl=nl, nb=1, interleave=0, $
data_type = size(outdata, /type), offset=0, spec_names=spec_names, $
file_type=file_type, /write, /open
|