OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
LutUtils.py
Go to the documentation of this file.
1 
2 import os
3 import seadasutils.JsonUtils as Session
4 import seadasutils.SensorUtils as SensorUtils
5 
6 
7 def lut_version(lut_name):
8  import re
9  f = os.path.basename(lut_name)
10 
11  # lut_name like xcal_<sensor>_<version>_<wavelength>.hdf
12  if re.match('xcal', f):
13  parts = re.split('([_.])', f)
14  return ''.join(parts[4:-4])
15 
16  # lut_name like <stuff>[_.][vV]<version>.<suffix>
17  v = re.search('[_.][vV]\d+', f)
18  if v:
19  parts = re.split('([_.])', f[v.start() + 1:])
20  return ''.join(parts[0:-2])
21 
22  # no version in lut_name
23  return None
24 
25 
26 def any_version(lut_name):
27  import re
28  d = os.path.dirname(lut_name)
29  f = os.path.basename(lut_name)
30  v = lut_version(f)
31  if v:
32  p = re.compile(v)
33  return os.path.join(d, p.sub('*', f))
34  else:
35  return lut_name
36 
37 
38 def old_version(newfile):
39  import glob
40  similar = glob.glob(any_version(newfile))
41  deletable = [f for f in similar if
42  os.path.getmtime(f) < os.path.getmtime(newfile)]
43  return deletable
44 
45 
46 def purge_luts(newfiles, verbose=False):
47  deletable = [old_version(newf) for newf in newfiles]
48  # flatten list of lists into single list
49  deletable = [oldf for sublist in deletable for oldf in sublist]
50  if len(deletable) > 0:
51  if verbose:
52  print('\n...deleting outdated LUTs:')
53  for f in sorted(deletable):
54  if verbose:
55  print('- ' + os.path.basename(f))
56  os.remove(f)
57 
58 
59 class LutUtils:
60  """
61  Utilities to update various LUT files for processing
62  """
63 
64  def __init__(self, mission=None, verbose=False, evalluts=False,
65  timeout=10, clobber=False, dry_run=False):
66 
67  self.mission = mission
68  self.verbose = verbose
69  self.evalluts = evalluts
70  self.timeout = timeout
71  self.clobber = clobber
72  self.dry_run = dry_run
73  self.status = 0
74  self.site_root = 'https://oceandata.sci.gsfc.nasa.gov/Ancillary/LUTs'
75  self.localroot = os.getenv('OCVARROOT')
76  self.sensor = SensorUtils.by_desc(mission)
77 
78  self.session = Session.SessionUtils(timeout=timeout, clobber=clobber, verbose=verbose)
79 
80  def lut_dirs(self):
81  dirs = []
82 
83  # add instrument dir for MODIS utcpole.dat and leapsec.dat
84  if self.sensor['instrument'] == 'MODIS':
85  dirs.append(self.sensor['dir'])
86 
87  # add unique sensor name
88  sensor = self.sensor['sensor'].lower()
89  dirs.append(sensor)
90 
91  # add calibration dirs for MODIS, VIIRS and OCI
92  if self.sensor['instrument'] in ['MODIS', 'VIIRS', 'OCI']:
93  caldirs = ['cal', 'xcal']
94  for caldir in caldirs:
95  dirs.append(os.path.join(sensor, caldir, 'OPER'))
96  if self.evalluts:
97  dirs.append(os.path.join(sensor, caldir, 'EVAL'))
98 
99  '''
100  # Uncomment this section if we ever reorganize $OCVARROOT to match $OCDATAROOT
101  dirs.append(self.sensor['dir'])
102  subdir = self.sensor.get('subdir')
103  if subdir:
104  dirs.append(os.path.join(self.sensor['dir'], subdir))
105  '''
106  return dirs
107 
108  def get_luts(self):
109 
110  # regex for all valid suffixes
111  # suffix = '\.(hdf|h5|nc|dat|txt)$'
112  suffix = '' # take whatever's there
113  query = '?format=json'
114 
115  downloaded = []
116  for d in self.lut_dirs():
117  url = os.path.join(self.site_root, d, '') + query
118  dirpath = os.path.join(self.localroot, d)
119  if self.verbose:
120  print()
121  print('Downloading files into ' + dirpath)
122 
123  newfiles = []
124  if self.mission == "common":
125  newfiles = self.session.download_allfiles(
126  url, dirpath,
127  dry_run=self.dry_run, clobber=self.clobber,
128  regex="(?m)^(?!.*(?:gbr100|gbrReflectance|LandWater15ARC|VIIRS_DARKTARGET_LUT)).*$",
129  check_times=True)
130  if self.session.status:
131  self.status = 1
132  else:
133  # check times for non-versioned files
134  new1 = self.session.download_allfiles(
135  url, dirpath,
136  dry_run=self.dry_run, clobber=self.clobber,
137  regex='^((?!\d+).)*' + suffix, check_times=True)
138  if self.session.status:
139  self.status = 1
140 
141  # check only filesize for others
142  new2 = self.session.download_allfiles(
143  url, dirpath,
144  dry_run=self.dry_run, clobber=self.clobber,
145  regex=suffix, check_times=False)
146  if self.session.status:
147  self.status = 1
148 
149  newfiles = new1 + new2
150 
151 
152  if len(newfiles) == 0:
153  if self.verbose:
154  print('...no new files.')
155  else:
156  downloaded.append(newfiles)
157 
158  # remove outdated LUTs from OPER
159  if 'OPER' in d:
160  purge_luts(newfiles, verbose=self.verbose)
161 
162  return downloaded
163 
164 # end of class LutUtils
def __init__(self, mission=None, verbose=False, evalluts=False, timeout=10, clobber=False, dry_run=False)
Definition: LutUtils.py:64
def lut_version(lut_name)
Definition: LutUtils.py:7
def purge_luts(newfiles, verbose=False)
Definition: LutUtils.py:46
def any_version(lut_name)
Definition: LutUtils.py:26
def old_version(newfile)
Definition: LutUtils.py:38