OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
modis_l1aextract_utils.py
Go to the documentation of this file.
1 #! /usr/bin/env python3
2 
3 from seadasutils.ParamUtils import ParamProcessing
4 
5 class extract:
6  def __init__(self, filename, parfile=None, outfile=None, geofile=None, north=None, south=None, west=None, east=None
7  ,
8  log=False, sensor=None, verbose=False):
9  # defaults
10  self.filename = filename
11  self.parfile = parfile
12  self.geofile = geofile
13  self.outfile = outfile
14  self.log = log
15  self.proctype = 'modisGEO'
16  self.ancdir = None
17  self.curdir = False
18  self.pcf_file = None
19  self.verbose = verbose
20  self.dirs = {}
21  self.sensor = sensor
22  self.north = north
23  self.south = south
24  self.west = west
25  self.east = east
26  # version-specific variables
27  self.collection_id = '061'
28  self.pgeversion = '6.1.9'
29  self.lutversion = None
30 
31  if self.parfile:
32  print(self.parfile)
33  p = ParamProcessing(parfile=self.parfile)
34  p.parseParFile(prog='geogen')
35  print(p.params)
36  phash = p.params['geogen']
37  for param in (list(phash.keys())):
38  print(phash[param])
39  if not self[param]:
40  self[param] = phash[param]
41 
42  def __setitem__(self, index, item):
43  self.__dict__[index] = item
44 
45  def __getitem__(self, index):
46  return self.__dict__[index]
47 
48  def chk(self):
49  """
50  Check parameters
51  """
52  import os
53  import sys
54 
55  if self.filename is None:
56  print("ERROR: No MODIS_L1A_file was specified in either the parameter file or in the argument list. Exiting")
57  sys.exit(1)
58  if not os.path.exists(self.filename):
59  print("ERROR: File '" + self.filename + "' does not exist. Exiting.")
60  sys.exit(1)
61  if self.sensor.find('modis') < 0 and not os.path.exists(self.geofile):
62  print("ERROR: Geolocation file (%s) not found!" % self.geofile)
63  sys.exit(1)
64  if (0 != self.north and not self.north) or \
65  (0 != self.south and not self.south) or \
66  (0 != self.west and not self.west) or \
67  (0 != self.east and not self.east):
68  print("Error: All four NSWE coordinates required!")
69  sys.exit(1)
70  try:
71  north = float(self.north)
72  except ValueError:
73  err_msg = 'Error! North value "{0}" non-numeric.'.format(self.north)
74  try:
75  south = float(self.south)
76  except ValueError:
77  err_msg = 'Error! South value "{0}" non-numeric.'.format(self.south)
78  try:
79  east = float(self.east)
80  except ValueError:
81  err_msg = 'Error! East value "{0}" non-numeric.'.format(self.east)
82  try:
83  west = float(self.west)
84  except ValueError:
85  err_msg = 'Error! West value "{0}" non-numeric.'.format(self.west)
86 
87  if north <= south:
88  print("Error: North must be greater than South!")
89  sys.exit(1)
90  if (north > 90.0) or (south < -90.0):
91  print("Latitude range outside realistic bounds!")
92  sys.exit(1)
93  if west < -180 or west > 180. or east < -180. or east > 180:
94  print("Longitudes must be between -180.0 and 180.0")
95  sys.exit(1)
96 
97  def run(self):
98  """
99  Run lonlat2pixline and l1aextract
100  """
101  import subprocess
102  import os
103 
104  if self.verbose:
105  print("")
106  print("Locating pixel/line range ...")
107  lonlat2pixline = os.path.join(self.dirs['bin'], 'lonlat2pixline')
108  pixlincmd = [lonlat2pixline, self.geofile, str(self.west), str(self.south), str(self.east), str(self.north)]
109  p = subprocess.run(pixlincmd, capture_output=True, text=True, shell=False)
110  line = p.stdout
111  if not p.returncode:
112  pixlin = line.splitlines()[0][2:].split()
113 
114  l1extract = os.path.join(self.dirs['bin'], 'l1aextract_modis')
115  extractcmd = [l1extract, self.filename, pixlin[0], pixlin[1], pixlin[2], pixlin[3], self.outfile]
116  retcode = subprocess.run(extractcmd, shell=False).returncode
117  if retcode:
118  print("Error extracting file %s" % self.filename)
119  return 1
120 
121  else:
122  if p.returncode == 120:
123  print("No extract necessary, entire scene contained within the selected region of interest.")
124  return 120
125  else:
126  print("Error locating pixel/line range to extract.")
127  return 1
128 
129  return 0
130 
131 
132 
133 
list(APPEND LIBS ${PGSTK_LIBRARIES}) add_executable(atteph_info_modis atteph_info_modis.c) target_link_libraries(atteph_info_modis $
Definition: CMakeLists.txt:7
def __init__(self, filename, parfile=None, outfile=None, geofile=None, north=None, south=None, west=None, east=None, log=False, sensor=None, verbose=False)
const char * str
Definition: l1c_msi.cpp:35