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
SensorUtils.py
Go to the documentation of this file.
1 import os
2 
3 """
4 Utility functions for determining directories for each sensor.
5 """
6 
7 SensorList = []
8 
9 
10 def load_sensorlist(filename=None):
11  """
12  Read list of sensor definitions from file
13  """
14  global SensorList
15  SensorList = []
16  keys = ['sensor', 'instrument', 'platform', 'dir', 'subdir']
17  try:
18  if not filename:
19  filename = os.path.join(os.getenv('OCDATAROOT'),
20  'common', 'SensorInfo.txt')
21  with open(filename) as infile:
22  for line in infile:
23  line = line.strip()
24  if (len(line) > 0) and (not line.startswith("#")):
25  values = line.rsplit()
26  SensorList.append(dict(zip(keys, values)))
27  except Exception as e:
28  print(e)
29 
30 
31 def by_sensor(name):
32  """
33  Get sensor defs from unique sensor name
34  """
35  global SensorList
36  if len(SensorList) == 0:
38  try:
39  return next(d for d in SensorList if d['sensor'].lower() == name.lower())
40  except StopIteration:
41  return None
42 
43 
44 def by_instplat(inst, plat):
45  """
46  Get sensor defs given the instrument and platform
47  """
48  global SensorList
49  if len(SensorList) == 0:
51  try:
52  return next(d for d in SensorList if
53  (d['instrument'].lower() == inst.lower()) &
54  (d['platform'].lower() == plat.lower()))
55  except StopIteration:
56  return None
57 
58 
59 def by_desc(name):
60  """
61  Get sensor defs given any useful information
62  """
63  global SensorList
64  if len(SensorList) == 0:
66  try:
67  return next(d for d in SensorList if
68  name.lower() in (d['sensor'].lower(),
69  d['platform'].lower(),
70  d.get('subdir'),
71  d['dir'].lower(),
72  d['instrument'].lower()))
73  except StopIteration:
74  return None
75 
76 
77 # end of class SensorUtils
78 
79 # test routines below
80 if __name__ == '__main__':
81  if len(SensorList) == 0:
82 
83  print("\nby sensor:")
84  namelist = ['modisa', 'seawifs', 'bogus']
85  for sensor in namelist:
86  print(sensor, ':\t', by_sensor(sensor))
87 
88  print("\nby inst/plat:")
89  instlist = ['modis', 'VIIRS', 'modis', 'bogus', 'Aquarius']
90  platlist = ['Terra', 'JPSS-1', 'bogus', 'Aqua', 'SAC-D']
91  for inst, plat in zip(instlist, platlist):
92  print(inst, plat, ':\t', by_instplat(inst, plat))
93 
94  print("\nby any name:")
95  namelist = ['seawifs', 'aquarius', 'modisa', 'modist', 'viirsn', 'viirsj1', 'aqua', 'terra', 'npp', 'j1',
96  'bogus']
97  for name in namelist:
98  print(name, ':\t', by_desc(name))
def load_sensorlist(filename=None)
Definition: SensorUtils.py:10
void print(std::ostream &stream, const char *format)
Definition: PrintDebug.hpp:38
def by_instplat(inst, plat)
Definition: SensorUtils.py:44