3 ''' This is the aviris preprocessing script.
4 Run this on the *img.hdr if it fails to run under l2gen
5 or the aviris file converters.
7 aviris_preproc <img file> <output file>
9 R.Healy 11/1/2016 (richard.healy@nasa.gov)
22 '''Specifies command line arguments and parses command line accordingly.'''
23 parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, description=
''' Read an Aviris hdr file and generate a parameter file as input to the converter if needed.
26 parser.add_argument(
'-ifile', nargs=1, type=str, help=
' Input Aviris hdr file ')
27 parser.add_argument(
'-ofile', nargs=1, type=str, default=(
'aviris_preproc.hdr'),help=
' output file ')
33 args=parser.parse_args()
36 parser.error(
"you must specify an input file")
38 ifile =
''.join(args.ifile)
39 ofile =
''.join(args.ofile)
44 avfields=[
'lines',
'samples',
'bands',
'interleave',
'rotation angle',
45 'pixel size',
'Northing',
'Easting',
'UTM zone',
'wavelength',
'fwhm']
46 musthave=[
'lines',
'samples',
'bands',
'fwhm',
'wavelength']
49 avdir=os.path.dirname(ifile)
50 avfile=os.path.basename(ifile)
55 print(
"Unable to change to ",avdir)
58 for theLine
in open(avfile,
'r') :
60 fields = theLine.split(
'=')
61 fields[0] = fields[0].strip()
63 pattern = re.compile(f)
64 if re.search(pattern,fields[0]):
67 if len(avdict) == len(avfields):
71 if f
not in avdict.keys():
72 print(
'Missing data...',f,avdict.keys())
74 infofields=[
'pilot_st_lat',
'pilot_end_lat',
'pilot_st_long',
'pilot_end_long',
75 'pilot_st_time',
'pilot_end_time']
77 infofile=
''.join(glob.glob(
"*info"))
79 print(
"Missing a info file")
81 for theLine
in open(infofile,
'r'):
83 fields = theLine.split(
'=')
84 fields[0] = fields[0].strip()
86 pattern = re.compile(f)
87 if re.search(pattern,fields[0]):
88 infodict[f] = fields[1]
92 if len(infodict) < len(infofields):
93 print(
"Incomplete information in info file:",infofile)
94 print(
"Need these fields: ",infofields)
95 print(
"But only have: ",infodict.keys())
97 if len(infodict[
'pilot_st_time'].split()) > 1:
100 time_st = np.float(infodict[
'pilot_st_time'])
102 if len(infodict[
'pilot_end_time'].split()) > 1:
105 time_end = np.float(infodict[
'pilot_end_time'])
108 if len(infodict[
'pilot_st_lat'].split()) > 1:
111 lat_st = np.float(infodict[
'pilot_st_lat'])
113 if len(infodict[
'pilot_end_lat'].split()) > 1:
116 lat_end = np.float(infodict[
'pilot_end_lat'])
119 if len(infodict[
'pilot_st_long'].split()) > 1:
122 lon_st = np.float(infodict[
'pilot_st_long'])
125 if len(infodict[
'pilot_st_long'].split()) > 1:
128 lon_end = np.float(infodict[
'pilot_end_long'])
131 sign_lat = np.sign(lat_st)
132 sign_lon = np.sign(lon_st)
134 navfile=
''.join(glob.glob(
"*nav"))
137 print(
"Missing a navigation file")
140 navout = ofile.split(
".")[0]+
"_nav.txt"
141 fnavout = open(navout,
"w")
143 fnav = open(navfile,
'r')
144 line = fnav.readline()
146 if len(line.split()) == 3:
147 alt,lat,lon = line.split()
149 if np.sign(np.float(lat)) == sign_lat:
151 if np.sign(np.float(lon)) == sign_lon:
154 time_ar = np.arange(time_st,time_end,(time_end-time_st)/np.float(avdict[
'lines']))
155 for theLine, avtime
in zip(open(navfile,
'r'), time_ar):
156 alt,lat,lon = theLine.split()
157 fnavout.write(
"{:.8f} {:.4f} {:.4f} {:.4f}\n".format(avtime,np.float(alt),sign_lat*np.float(lat),sign_lon*np.float(lon)))
159 for theLine
in open(navfile,
'r'):
160 nav_data = theLine.split()
161 stime_ar = nav_data[3].split(
':')
162 avtime = np.float(stime_ar[1])+np.float(stime_ar[2])/60.+np.float(stime_ar[3])/3600.
166 fnavout.write(
"{:.8f} {:.4f} {:.4f} {:.4f}\n".format(avtime,np.float(alt)/1000.,np.float(lat),np.float(lon)))
169 fhdr = open(ofile,
"w")
170 fhdr.write(
'AVIRIS_PREPROC_HDR\n')
171 fhdr.write(
"hdrfile = {}\n".format(avfile))
172 fhdr.write(
"navfile = {}\n".format(navout))
173 gainfile=
''.join(glob.glob(
"*gain"))
175 fhdr.write(
"gainfile = {}\n".format(gainfile))
176 imgfile=
''.join(glob.glob(
"*img"))
178 fhdr.write(
"imgfile = {}\n".format(imgfile))
180 print(
"WARNING!! No img file\n")
181 print(
"It is extremely likely the aviris reader will not work unless you add imgfile=the_name_of_the_img_file \n")
182 print(
"into the file ",ofile)
188 time_stamp =timeFld.split()
189 if len(time_stamp) < 3:
190 time_stamp = timeFld.split()
191 if len(time_stamp) < 3:
192 print(
"Can't parse Time Field: ",timeFld)
194 shr =
''.join(time_stamp[0].split(
':'))
195 smin =
''.join(time_stamp[1].split(
':'))
196 ssec =
''.join(time_stamp[2].split(
':'))
199 return hr+np.sign(hr)*(np.float(smin)+np.float(ssec)/60)/60
202 '''Specifies command line arguments and parses command line accordingly.'''
203 parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, description=
''' Read an Aviris hdr file and generate a parameter file as input to the converter if needed.
206 parser.add_argument(
'-ifile', nargs=1, type=str, help=
' Aviris hdr file ')
207 args = parser.parse_args(
'-ifile=/accounts/rhealy/Downloads/aviris/oci/f100828t01p00r11rdn_b/f100828t01p00r11rdn_b_sc01_ort_img.hdr'.split())
209 parsedArgs = parser.parse_args(args)
213 if __name__ ==
'__main__':
main()