This is my code for creating a meta-file for landsat names (with a .hdr which sometimes doesn't work out), you can use it to create your own layer stacking or just make meta-files (which saves some space). But warning, if you create a meta-file don't move your original files, the meta-file only points to their locations.
In my code you have to change the variables image_ext and name_convention at the top to align with how you want your files searched/sorted. Input your source directory in source_dir.You can disable a .hdr using create_hdr_opt
I usually use ENVI classic, which you can start at IDL by just typing ENVI in IDL> prompt.
-----------------------------------------------------------------------------------------------------------------------
;Program to use IDL features to create a meta file for matching file names automatically
;
;Band order is derived from a standard descending sort, so filenames decide band order in the meta file
;
;Created by Kenneth Lynn Dudley, Okinawa Institute of Science and Technology (OIST) 2015
function create_meta_hdr, output_file_name, matching_bands
hdr_fwhm = []
hdr_wavelength = []
foreach band, matching_bands, index do begin ;matching_bands list has been sorted A-Z
;First band will be default for remaining bands for some header items
if index EQ 0 then begin
ENVI_OPEN_FILE, band, /INVISIBLE, /NO_REALIZE, R_FID=b_fid, /NO_INTERACTIVE_QUERY
ENVI_FILE_QUERY, b_fid, Acquisition_time=b_aqc_time, Cloud_cover=b_cloud, DATA_IGNORE_VALUE=b_ignore, $
Data_type=b_data_type, DIMS=b_dims, fwhm=b_fwhm, interleave=b_interleave, NL=bNL, NS=bNS, sensor_type=b_sensor, $
sun_azimuth=b_azimuth, sun_elevation=b_elev, wavelength_units=b_wave_units, WL=bWL
ENVI_FILE_MNG, id=b_fid, /remove
endif else begin
ENVI_OPEN_FILE, band, /INVISIBLE, /NO_REALIZE, R_FID=b_fid, /NO_INTERACTIVE_QUERY
ENVI_FILE_QUERY, b_fid, fwhm=b_fwhm, WL=bWL
ENVI_FILE_MNG, id=b_fid, /remove
endelse
;store band-by-band different headers
hdr_fwhm = [hdr_fwhm, b_fwhm]
hdr_wavelength = [hdr_wavelength, bWL]
endforeach
;Write composite header for ENVI Meta File, Keep in mind, this file doesnt play well with ENVI Classic, so it's better not to have it
;ENVI 5.# handles Meta .hdr files correctly.
ENVI_SETUP_HEAD, Acquisition_time=b_aqc_time, Cloud_cover=b_cloud, DATA_IGNORE_VALUE=b_ignore, $
Data_type=b_data_type, fwhm=hdr_fwhm, interleave=b_interleave, NL=bNL, NS=bNS, sensor_type=b_sensor, $
sun_azimuth=b_azimuth, sun_elevation=b_elev, wavelength_units=b_wave_units, WL=hdr_wavelength, /WRITE, $
NB = (n_elements(matching_bands)), FNAME = output_file_name + '.hdr', File_type = 1
end
pro CreateMetaFile
create_hdr_opt = 1 ; YES=1 or NO=0 on creating a .hdr file for the META file, some attributes are carried over in the meta (wavelength) and others not (acquisition date) and must be put into a .hdr for the META file.
source_dir = 'D:\00_Full_SR_LT_COPY\SOki_NN\LT4-5' ;'Z:\EconomoU\Kenneth\Okinawa Datasets\Remote Sensing Imagery\Landsat Scenes\OperationalScenes\sr_warped'
image_ext = ['*.img'];['*.img','*.tif'] ; image file extension, can use multiple types
name_convention = ['_band1','_band2','_band3','_band4','_band5','_band7'] ; Put in order of default display [0] is blue [1] is green [2] is red [blue, green, red]
output_name_mod = '_Full_META' ; change this to change the output file name "LANDSATNAME+output_name_mod"
name_convention = '*'+name_convention+'*' ;Do not change
find_index_drive = []
foreach ext, image_ext do find_index_drive = [find_index_drive, File_Search(source_dir, ext)] ; create a total index of files and directories
find_index_drive = find_index_drive[where(find_index_drive NE '', /NULL)] ; Get rid of any extra blank spaces
uniq_file_list = list()
foreach file, find_index_drive do uniq_file_list.add, (File_Basename(file)).Substring(0,20) ; This assumes landsat naming convention
uniq_file_list = uniq_file_list.ToArray()
uniq_file_list = uniq_file_list[uniq(uniq_file_list, sort(uniq_file_list))]
foreach file, uniq_file_list do begin
band_matches = find_index_drive[where(strmatch(find_index_drive, '*'+file+'*') EQ 1, /null)]
matching_bands = []
foreach name, name_convention do begin
insert_band = band_matches[where(strmatch(band_matches, name) EQ 1, /NULL)]
matching_bands = [matching_bands, insert_band]
endforeach
check_null = where(matching_bands EQ !NULL, count_null)
if count_null NE 0 then begin
print, 'Error, found ', strtrim(total(check_null),2), ' for file: ', file
endif else begin
matching_bands = matching_bands[(sort(matching_bands))]
;Open first file to pull info from metadata
ENVI_OPEN_FILE, matching_bands[0], /INVISIBLE, /NO_REALIZE, R_FID=match_fid, /NO_INTERACTIVE_QUERY
ENVI_FILE_QUERY, match_fid, DIMS=match_img_dims
ENVI_FILE_MNG, id=match_fid, /remove
;Create output variables
output_file_name = FILE_DIRNAME(matching_bands[0], /MARK_DIRECTORY)+file + output_name_mod
Header_out = 'ENVI META FILE'
Band_out = 'Bands: 1'
Dims_out = 'Dims : 1-'+strtrim(match_img_dims[2],2)+',1-'+strtrim(match_img_dims[4],2)
GET_LUN, out_meta_file
openw, out_meta_file, output_file_name
printf, out_meta_file, "ENVI META FILE"
foreach input_f, matching_bands do begin
printf, out_meta_file, "File : "+input_f
printf, out_meta_file, Band_out
printf, out_meta_file, Dims_out
printf, out_meta_file, ""
endforeach
close, out_meta_file
FREE_LUN, out_meta_file
IF create_hdr_opt then result = create_meta_hdr(output_file_name, matching_bands)
endelse
endforeach
print, 'Done!'
end
|