OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
ParamUtils.py
Go to the documentation of this file.
1 # Utilities used by processing module, ocproc.py
2 
3 __author__="Sean Bailey, Futuretech Corporation"
4 __date__ ="$Apr 10, 2010 4:49:15 PM$"
5 
6 import re
7 import sys
8 
10  """
11  Parameter processing class - build and parse par files
12  """
13  def __init__(self, params=None,parfile=None,parstr='',sensor=None,verbose=False):
14  """
15  Set up parameter processing methods
16  """
17  if params is None: params = {}
18  self.params = params
19  self.parfile = parfile
20  self.parstr = parstr
21  self.sensor = sensor
22  self.verbose = verbose
23 
24 
25  def buildParameterFile(self,prog):
26  """Build a parameter file from a dictionary of parameters.
27  Writes parfile if pfile is defined, else Returns parfile string."""
28 
29  for k in self.params[prog].keys():
30  if re.match('ocproc',k):
31  del self.params[prog][k]
32 
33  filelst = ['ifile','ofile','ofile1','ofile2','ofile3','geofile',
34  'spixl','dpixl','epixl','sline','eline','dline',
35  'north','south','west','east']
36  self.parstr = ''
37  for f in filelst:
38  try:
39  pstr = '='.join([f, self.params[prog][f]])
40  self.parstr = "\n".join([self.parstr,pstr])
41  except Exception:
42  pass
43 
44  for k,v in sorted(self.params[prog].items()):
45  try:
46  try:
47  filelst.index(k)
48  pass
49  except Exception:
50  pstr = '='.join([k, v])
51  self.parstr = "\n".join([self.parstr,pstr])
52  except Exception:
53  pass
54 
55  if self.parfile:
56  if self.verbose:
57  print("Writing parfile %s" % self.parfile)
58  logfile = open(self.parfile, 'w')
59  logfile.write(self.parstr)
60  logfile.write("\n")
61  logfile.close()
62 
63 
64 
65  def parseParFile(self,prog='main'):
66  """
67  Parse a parameter file, returning a dictionary listing
68  """
69  try:
70  if self.verbose:
71  print('PAR',self.parfile)
72  pfile = self.parfile
73  par_file = open(pfile,'r')
74  except Exception:
75  print("File {0} not found!".format(self.parfile))
76  return None
77 
78  try:
79  self.params[prog]
80  except Exception:
81  self.params[prog]={}
82 
83  lines = par_file.readlines()
84  for line in lines:
85  if (len(line) == 0) or re.match('^\s+$',line):
86  continue
87  if line[0] == '#':
88  parts = line.split()
89  try:
90  ix = parts.index('section')
91  prog = parts[ix + 1].strip()
92  try:
93  self.params[prog] #TODO Fix this odd construction
94  continue
95  except Exception:
96  self.params[prog]={}
97  except Exception:
98  pass
99  continue
100  if line.find('=') != -1:
101  line_parts = line.split('#')[0].strip().split('=')
102  if line_parts[0]:
103  if line_parts[0] == 'par':
104  p2 = ParamProcessing(parfile=line_parts[1])
105  p2.parseParFile(prog=prog)
106  self.params[prog].update(p2.params[prog])
107  elif line_parts[0] == 'ifile':
108  self.params[prog]['file'] = line_parts[1]
109  else:
110  self.params[prog][line_parts[0]] = line_parts[1]
111  else:
112  err_msg = 'Error! Entry "{0}" is invalid in par file {1}'.format(line, self.parfile)
113  sys.exit(err_msg)
114 
115 
116  def genOutputFilename(self,prog=None):
117  """Given a program, derive a standard output filename"""
118 
119  ifile = self.params[prog]['ifile']
120  modsen ={'A':'T','P':'A'}
121  ofile = None
122 
123  try:
124  ofile = self.params[prog]['ofile']
125  except Exception:
126  fparts = ifile.split('.')
127  if prog == 'l1agen':
128  if re.search('L0',ifile):
129  ofile = ifile.replace('L0','L1A')
130  elif re.match('M?D*',ifile):
131  type = ifile[6]
132  yrdy = ifile[7:14]
133  hrmn = ifile[15:19]
134  ofile = ''.join([modsen[type],yrdy,hrmn,'00.L1A_LAC'])
135  elif prog == 'l1brsgen':
136  ofile = '.'.join([fparts[0],'L1_BRS'])
137  elif prog == 'l1mapgen':
138  ofile = '.'.join([fparts[0],'L1_MAP'])
139  elif prog == 'l2gen':
140  if re.search('L1[AB]',ifile):
141  of = re.compile( '(L1A|L1B)')
142  ofile = of.sub( 'L2', ifile)
143  else:
144  ofile = '.'.join([fparts[0],'L2'])
145  elif prog == 'l2brsgen':
146  try:
147  prod = self.params[prog]['prod']
148  except Exception:
149  prod = 'chlor_a'
150  ofile = '.'.join([fparts[0],prod,'L2_BRS'])
151  elif prog == 'l2mapgen':
152  try:
153  prod = self.params[prog]['prod']
154  except Exception:
155  prod = 'chlor_a'
156  ofile = '.'.join([fparts[0],prod,'L2_MAP'])
157  elif prog == 'l2bin':
158  ofile = 'output.file'
159  elif prog == 'l3bin':
160  ofile = 'output.file'
161  elif prog == 'smigen':
162  ofile = 'output.file'
163  elif prog == 'smitoppm':
164  ofile = '.'.join([ifile,'ppm'])
165  elif prog == 'l3gen':
166  ofile = 'output.file'
167 
168  else:
169  ofile = '.'.join([prog,'output_file'])
170 
171  self.params[prog]['ofile'] = ofile
def parseParFile(self, prog='main')
Definition: ParamUtils.py:65
def __init__(self, params=None, parfile=None, parstr='', sensor=None, verbose=False)
Definition: ParamUtils.py:13
def genOutputFilename(self, prog=None)
Definition: ParamUtils.py:116