Due to the lapse in federal government funding, NASA is not updating this website. We sincerely regret this inconvenience.
NASA Logo
Ocean Color Science Software

ocssw V2022
avirispreproc.py
Go to the documentation of this file.
1 #! /usr/bin/env python3
2 
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.
6 
7  aviris_preproc <img file> <output file>
8 
9 R.Healy 11/1/2016 (richard.healy@nasa.gov)
10 
11 '''
12 __version__ = '1.0.0'
13 
14 __author__ = 'rhealy'
15 
16 import argparse
17 import sys, re
18 import glob, os
19 import numpy as np
20 
21 def main():
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.
24 
25  ''', add_help=True)
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 ')
28 
29  #args = parser.parse_args('-ifile /accounts/rhealy/Downloads/aviris/oci/f100828t01p00r11rdn_b/f100828t01p00r11rdn_b_sc01_ort_img.hdr -ofile blah.hdr'.split())
30  #args = parser.parse_args('-ifile /glusteruser/rhealy/aviris/f940921t01p02_r06c/f940921t01p02_r06c_img.hdr -ofile blah.hdr'.split())
31  #args = parser.parse_args('-ifile /glusteruser/rhealy/aviris/f000411t01p03_r04c/f000411t01p03_r04c_img.hdr'.split())
32 
33  args=parser.parse_args()
34 
35  if not args.ifile:
36  parser.error("you must specify an input file")
37 
38  ifile = ''.join(args.ifile)
39  ofile = ''.join(args.ofile)
40  print("HDRFILE =",checkHdrFile(ifile,ofile))
41 
42 
43 def checkHdrFile(ifile,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']
47 
48  avdict = {}
49  avdir=os.path.dirname(ifile)
50  avfile=os.path.basename(ifile)
51  if avdir:
52  try:
53  os.chdir(avdir)
54  except:
55  print("Unable to change to ",avdir)
56  sys.exit()
57 
58  for theLine in open(avfile,'r') :
59  if '=' in theLine:
60  fields = theLine.split('=')
61  fields[0] = fields[0].strip()
62  for f in avfields:
63  pattern = re.compile(f)
64  if re.search(pattern,fields[0]):
65  avdict[f] = fields[1]
66  #print(f, '=',avdict[f])
67  if len(avdict) == len(avfields):
68  return ifile
69 
70  for f in musthave:
71  if f not in avdict.keys():
72  print('Missing data...',f,avdict.keys())
73  sys.exit()
74  infofields=['pilot_st_lat','pilot_end_lat','pilot_st_long','pilot_end_long',
75  'pilot_st_time','pilot_end_time']
76  infodict = {}
77  infofile=''.join(glob.glob("*info"))
78  if not infofile:
79  print("Missing a info file")
80  sys.exit()
81  for theLine in open(infofile,'r'):
82  if '=' in theLine:
83  fields = theLine.split('=')
84  fields[0] = fields[0].strip()
85  for f in infofields:
86  pattern = re.compile(f)
87  if re.search(pattern,fields[0]):
88  infodict[f] = fields[1]
89 
90  #for info,val in infodict.items():
91  #print(info," = ",val)
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())
96  sys.exit()
97  if len(infodict['pilot_st_time'].split()) > 1:
98  time_st = parseMinSec(infodict['pilot_st_time'])
99  else:
100  time_st = np.float(infodict['pilot_st_time'])
101  #print("time start=",time_st)
102  if len(infodict['pilot_end_time'].split()) > 1:
103  time_end = parseMinSec(infodict['pilot_end_time'])
104  else:
105  time_end = np.float(infodict['pilot_end_time'])
106 
107  #print("time end=",time_end)
108  if len(infodict['pilot_st_lat'].split()) > 1:
109  lat_st = parseMinSec(infodict['pilot_st_lat'])
110  else:
111  lat_st = np.float(infodict['pilot_st_lat'])
112  #print("lat_st=",lat_st)
113  if len(infodict['pilot_end_lat'].split()) > 1:
114  lat_end = parseMinSec(infodict['pilot_end_lat'])
115  else:
116  lat_end = np.float(infodict['pilot_end_lat'])
117 
118  #print("lat_end=",lat_end)
119  if len(infodict['pilot_st_long'].split()) > 1:
120  lon_st = parseMinSec(infodict['pilot_st_long'])
121  else:
122  lon_st = np.float(infodict['pilot_st_long'])
123 
124  #print("lon_st=",lon_st)
125  if len(infodict['pilot_st_long'].split()) > 1:
126  lon_end = parseMinSec(infodict['pilot_end_long'])
127  else:
128  lon_end = np.float(infodict['pilot_end_long'])
129  #print("lon_end=",lon_end)
130 
131  sign_lat = np.sign(lat_st)
132  sign_lon = np.sign(lon_st)
133 
134  navfile=''.join(glob.glob("*nav"))
135  #print("navfile <<",navfile,">>")
136  if not navfile:
137  print("Missing a navigation file")
138  sys.exit()
139 
140  navout = ofile.split(".")[0]+"_nav.txt"
141  fnavout = open(navout,"w")
142 
143  fnav = open(navfile,'r')
144  line = fnav.readline()
145  fnav.close()
146  if len(line.split()) == 3:
147  alt,lat,lon = line.split()
148  # correct the sign in the nav file
149  if np.sign(np.float(lat)) == sign_lat:
150  sign_lat = 1.0
151  if np.sign(np.float(lon)) == sign_lon:
152  sign_lon = 1.0
153 
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)))
158  else:
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.
163  alt = nav_data[20]
164  lat = nav_data[4]
165  lon = nav_data[5]
166  fnavout.write("{:.8f} {:.4f} {:.4f} {:.4f}\n".format(avtime,np.float(alt)/1000.,np.float(lat),np.float(lon)))
167 
168  fnavout.close()
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"))
174  if gainfile:
175  fhdr.write("gainfile = {}\n".format(gainfile))
176  imgfile=''.join(glob.glob("*img"))
177  if imgfile:
178  fhdr.write("imgfile = {}\n".format(imgfile))
179  else:
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)
183 
184  fhdr.close()
185  return ofile
186 
187 def parseMinSec(timeFld):
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)
193  sys.exit()
194  shr = ''.join(time_stamp[0].split(':'))
195  smin = ''.join(time_stamp[1].split(':'))
196  ssec = ''.join(time_stamp[2].split(':'))
197  #print("hr={},min={},sec={}".format(shr,smin,ssec))
198  hr = np.float(shr)
199  return hr+np.sign(hr)*(np.float(smin)+np.float(ssec)/60)/60
200 
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.
204 
205  ''', add_help=True)
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())
208 
209  parsedArgs = parser.parse_args(args)
210  return parsedArgs
211 
212 
213 if __name__ == '__main__': main()
def parseMinSec(timeFld)
def ParseCommandLine(args)
void print(std::ostream &stream, const char *format)
Definition: PrintDebug.hpp:38
def checkHdrFile(ifile, ofile)