def run_copy_header(sourceheader, targetheader, keys): """ Copy information from sourceheader to targetheader for given keys. """ # Read header info to dictionaries source_header_dict = envi_header.read_hdr_file(args.sourceheader[0]) target_header_dict = envi_header.read_hdr_file(args.targetheader[0]) copy_header_dict_items(source_header_dict, target_header_dict, args.keys) # Make a backup copy of the origional header try: if os.path.isfile(args.targetheader[0] + ".bak"): print( "Backup file {}.bak already exists. Please remove " "and retry".format(args.targetheader[0]), file=sys.stderr, ) sys.exit(1) shutil.copy2(args.targetheader[0], args.targetheader[0] + ".bak") except IOError: print( "Could create backup copy of header in same destination as source " "- possibly due to folder permissions. Aborting", file=sys.stderr, ) except Exception: raise envi_header.write_envi_header(args.targetheader[0], target_header_dict) print("Copied items to {0}, origional header " "saved as {0}.bak".format(args.targetheader[0]))
def run_copy_header(sourceheader, targetheader, keys): """ Copy information from sourceheader to targetheader for given keys. """ # Read header info to dictionaries source_header_dict = envi_header.read_hdr_file(args.sourceheader[0], keep_case=True) target_header_dict = envi_header.read_hdr_file(args.targetheader[0], keep_case=True) copy_header_dict_items(source_header_dict, target_header_dict, args.keys) # Make a backup copy of the origional header try: if os.path.isfile(args.targetheader[0] + '.bak'): print("Backup file {}.bak already exists. Please remove " "and retry".format(args.targetheader[0]), file=sys.stderr) sys.exit(1) shutil.copy2(args.targetheader[0], args.targetheader[0] + '.bak') except IOError: print( "Could create backup copy of header in same destination as source " "- possibly due to folder permissions. Aborting", file=sys.stderr) except Exception: raise envi_header.write_envi_header(args.targetheader[0], target_header_dict) print("Copied items to {0}, origional header " "saved as {0}.bak".format(args.targetheader[0]))
def get_bands_from_wavelengths(input_image, wavelengths=DEFAULT_WAVELENGTHS): """ Get bands for a list of wavelengths (useful for creating three band composites). Function adapted from one in createimage.py """ input_header = envi_header.find_hdr_file(input_image) if input_header is None: raise Exception("Need to use ENVI header to get bands") header_dict = envi_header.read_hdr_file(input_header) image_wavelengths = header_dict['wavelength'].split(',') out_bands = [] for wavelength in wavelengths: band = min(range(len(image_wavelengths)), key=lambda x:abs(float(image_wavelengths[x])-wavelength)) out_bands.append(band) return out_bands
def get_bands_from_wavelengths(input_image, wavelengths=DEFAULT_WAVELENGTHS): """ Get bands for a list of wavelengths (useful for creating three band composites). Function adapted from one in createimage.py """ input_header = envi_header.find_hdr_file(input_image) if input_header is None: raise Exception("Need to use ENVI header to get bands") header_dict = envi_header.read_hdr_file(input_header) image_wavelengths = header_dict['wavelength'].split(',') out_bands = [] for wavelength in wavelengths: band = min(list(range(len(image_wavelengths))), key=lambda x: abs(float(image_wavelengths[x]) - wavelength)) out_bands.append(band) return out_bands
def apply_elc(input_image, output_image, image_spectra_file, field_spectra_file, scale_factor=DEFAULT_SCALE_FACTOR): """ Derive coefficients for an emprical line calibration and apply to a BIL file. """ print("Getting coefficients") elc_coefficients = get_elc_coefficients(image_spectra_file, field_spectra_file) print("Applying to image") # Read ENVI header input_header_file = envi_header.find_hdr_file(input_image) input_header_dict = envi_header.read_hdr_file(input_header_file) # Open input file in_data = numpy_bin_reader.BilReader(input_image) # Open file for output out_file = open(output_image, "wb") for i, line in enumerate(in_data): print(" Line {0:05}/{1:05}".format(i, in_data.lines), end="\r") # Apply coefficients elc_line = elc_coefficients[:,0] * line.transpose() \ + elc_coefficients[:,1] elc_line = elc_line.transpose() # Set any negative values to 0 elc_line = numpy.where(elc_line < 0, 0, elc_line) elc_line = elc_line * scale_factor # For default scale factor is over 1000 express as integer if scale_factor >= 1000: elc_line = elc_line.astype(numpy.uint16) else: elc_line = elc_line.astype(numpy.float32) elc_line.tofile(out_file) print(" Line {0:05}/{0:05}".format(in_data.lines), end="\n") output_header_dict = input_header_dict # See if need to change data type if scale_factor >= 1000: output_header_dict["data type"] = 12 else: output_header_dict["data type"] = 4 # Remove radiance data units try: output_header_dict.pop("radiance data units") except KeyError: pass # Add scale factor output_header_dict["reflectance scale factor"] = scale_factor procesing_comment = """\nConverted to reflectance using empirical line method. Field spectra: {}, Image spectra: {}\n""".format(field_spectra_file, image_spectra_file) try: output_header_dict["_comments"] = output_header_dict["_comments"] + \ procesing_comment except KeyError: output_header_dict["_comments"] = procesing_comment output_header_dict["description"] = "BIL file created by {} on {}".format(__file__, time.strftime("%Y-%m-%d %H:%M:%S")) # Copy header envi_header.write_envi_header(output_image + ".hdr", output_header_dict) # Close files out_file.close() in_data = None
dates = [] start_times = [] stop_times = [] for each_hdr in args.inputfiles: # Check a header file has been provided. if os.path.splitext(each_hdr)[-1].lower() != '.hdr': print('Must provide header file as input (ending with ".hdr")', file=sys.stderr) sys.exit(1) if not os.path.isfile(each_hdr): print('Could not open input header ({})'.format(each_hdr), file=sys.stderr) sys.exit(1) header_dict = envi_header.read_hdr_file(each_hdr) # Get supplimentary info for csv if available if 'acquisition date' in header_dict.keys(): dates.append(header_dict['acquisition date'].split(' ')[-1]) else: dates.append(float('nan')) if 'gps start time' in header_dict.keys(): start_times.append(header_dict['gps start time'].split(' ')[-1]) else: start_times.append(float('nan')) if 'gps stop time' in header_dict.keys(): stop_times.append(header_dict['gps stop time'].split(' ')[-1]) else: stop_times.append(float('nan')) if "bands" in header_dict.keys():
dates = [] start_times = [] stop_times = [] for each_hdr in args.inputfiles: # Check a header file has been provided. if os.path.splitext(each_hdr)[-1].lower() != '.hdr': print('Must provide header file as input (ending with ".hdr")', file=sys.stderr) sys.exit(1) if not os.path.isfile(each_hdr): print('Could not open input header ({})'.format(each_hdr), file=sys.stderr) sys.exit(1) header_dict = envi_header.read_hdr_file(each_hdr) # Get supplimentary info for csv if available if 'acquisition date' in list(header_dict.keys()): dates.append(header_dict['acquisition date'].split(' ')[-1]) else: dates.append(float('nan')) if 'gps start time' in list(header_dict.keys()): start_times.append(header_dict['gps start time'].split(' ')[-1]) else: start_times.append(float('nan')) if 'gps stop time' in list(header_dict.keys()): stop_times.append(header_dict['gps stop time'].split(' ')[-1]) else: stop_times.append(float('nan')) if "bands" in list(header_dict.keys()):
Latest version available from https://github.com/pmlrsg/arsf_tools/.''') parser.add_argument("inputfile", nargs=1,type=str, help="Input header") parser.add_argument("-o","--outcsv", required=False, type=str, help="Output file to store values for each band to") args = parser.parse_args() # Check a header file has been provided. if os.path.splitext(args.inputfile[0])[-1].lower() != '.hdr': print('Must provide header file as input (ending with ".hdr")', file=sys.stderr) sys.exit(1) if not os.path.isfile(args.inputfile[0]): print('Could not open input header ({})'.format(args.inputfile[0]), file=sys.stderr) sys.exit(1) header_dict = envi_header.read_hdr_file(args.inputfile[0]) band_info_dict = {} nbands = int(header_dict['bands']) print('The header contains the following information:\n') for item in header_dict.keys(): # Check if there are the same number of items as bands, if so don't print # them out, just the number of values if nbands != 1 and header_dict[item].count(',') >= nbands - 1: print(' {} : {} values'.format(item, header_dict['bands'])) item_list = header_dict[item].split(',') band_info_dict[item] = item_list else: print(' {} : {}'.format(item, header_dict[item]))
def _run(): parser = argparse.ArgumentParser(description="Calculates temperature and " "emissivity from owl data", formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument("-f", "--file", help="Full path to processed file", required = True) parser.add_argument("-o", "--outdir", help="Full path to output directory", required = True) parser.add_argument("-p", "--percentile", help="The percentile of the " "spectra used to determine the temperature. Should be set " "to ignore spikes, but not too low that good data are ignored. " "Ideally spikes should be masked and percentile set high.", default = 99) parser.add_argument("-s", "--start_band", help="Start band to use. Bands at " "the edges of the detector are more noisy.", default = 5, type = int) parser.add_argument("-e", "--end_band", help="End band to use. Bands at " "the edges of the detector are more noisy.", default = 95, type = int) args = parser.parse_args() # get filename without path and extension infile = os.path.splitext(os.path.split(args.file)[1])[0] # set output files temp_outfile = os.path.join(args.outdir, infile + "_temperature.bil") em_outfile = os.path.join(args.outdir, infile + "_emissivity.bil") error_outfile = os.path.join(args.outdir, infile + "_temp_error.bil") temp_hdr = temp_outfile[:-4] + ".hdr" em_hdr = em_outfile[:-4] + ".hdr" error_hdr = error_outfile[:-4] + ".hdr" # Read in wavelengths hdr = envi_header.find_hdr_file(args.file) hdrData = envi_header.read_hdr_file(hdr) wavelengths = numpy.asarray(hdrData["wavelength"].split(","), dtype = float)/1000 # check bands are correct if args.start_band < 0 or args.start_band > len(wavelengths): print("Start band specified is out of range ({} bands)".format(hdrData["bands"])) sys.exit() if args.end_band < 0 or args.end_band > len(wavelengths): print("End band specified is out of range ({} bands)".format(hdrData["bands"])) sys.exit() # Open bil file reader in_file = numpy_bin_reader.BilReader(args.file) # open output files temp_out = open(temp_outfile,"wb") em_out = open(em_outfile,"wb") error_out = open(error_outfile,"wb") # Read in data for line, dataline in enumerate(in_file): sys.stdout.write("\r working on line {}".format(line)) # calculate temperature by line spectral_T = planck_T(wavelengths, dataline) # calculate a single temperture from specified bands, ignoring noise line_temp = numpy.percentile(spectral_T[args.start_band:args.end_band], args.percentile, axis = 0).astype(numpy.float32) # estimate the error in temperature line_temp_error = 2.5*numpy.std(spectral_T[args.start_band:args.end_band] , axis = 0).astype(numpy.float32) # if error is more than 3 degrees, black and grey bodies are bad approximations # and the errors are also underestimates # calculate emissivity emissivity = calc_em(wavelengths, dataline, line_temp) # convert NaN to 0s (may be in mapped files) emissivity = numpy.nan_to_num(emissivity).astype(numpy.float32) # write out temp and emissivity bil files line_temp.tofile(temp_out) emissivity.tofile(em_out) line_temp_error.tofile(error_out) sys.stdout.write("\r all lines complete \n") #close input file dataline = None #close output file temp_out.close() em_out.close() error_out.close() # write headers hdr_dict = {"samples": int(hdrData["samples"]), "lines": int(hdrData["lines"]), "bands": 1, "data type": 4} hdr_dict["_comments"] = "file written by owl_temp_emissivity.py" # if mapped file, map info might be useful to keep if "map info" in hdrData: hdr_dict["map info"] = hdrData["map info"] envi_header.write_envi_header(temp_hdr, hdr_dict) envi_header.write_envi_header(error_hdr, hdr_dict) hdr_dict["bands"] = int(hdrData["bands"]) hdr_dict["interleave"] = "bil" hdr_dict["wavelength"] = hdrData["wavelength"] hdr_dict["band names"] = hdrData["wavelength"] envi_header.write_envi_header(em_hdr, hdr_dict)
required=False, type=str, help="Output file to store values for each band to") args = parser.parse_args() # Check a header file has been provided. if os.path.splitext(args.inputfile[0])[-1].lower() != '.hdr': print('Must provide header file as input (ending with ".hdr")', file=sys.stderr) sys.exit(1) if not os.path.isfile(args.inputfile[0]): print('Could not open input header ({})'.format(args.inputfile[0]), file=sys.stderr) sys.exit(1) header_dict = envi_header.read_hdr_file(args.inputfile[0]) band_info_dict = {} nbands = int(header_dict['bands']) print('The header contains the following information:\n') for item in list(header_dict.keys()): # Check if there are the same number of items as bands, if so don't print # them out, just the number of values if nbands != 1 and header_dict[item].count(',') >= nbands - 1: print(' {} : {} values'.format(item, header_dict['bands'])) item_list = header_dict[item].split(',') band_info_dict[item] = item_list else: print(' {} : {}'.format(item, header_dict[item]))