OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
pixlin_utils.py
Go to the documentation of this file.
1 #! /usr/bin/env python3
2 
3 from seadasutils.setupenv import env
4 import os
5 import subprocess
6 import sys
7 
8 
9 class pixlin:
10 
11  def __init__(self, geofile=None,
12  north=None, south=None, west=None, east=None,
13  verbose=False):
14  # defaults
15  self.geofile = geofile
16  self.ancdir = None
17  self.curdir = False
18  self.verbose = verbose
19  self.dirs = {}
20  self.north = north
21  self.south = south
22  self.west = west
23  self.east = east
24  self.spixl = None
25  self.epixl = None
26  self.sline = None
27  self.eline = None
28  self.status = None
29  env(self)
30 
31  def __setitem__(self, index, item):
32  self.__dict__[index] = item
33 
34  def __getitem__(self, index):
35  return self.__dict__[index]
36 
37  def chk(self):
38  """
39  Check parameters
40  """
41  if not os.path.exists(self.geofile):
42  print("ERROR: File '" + self.geofile + "' does not exist. Exiting.")
43  sys.exit(1)
44 
45  if (0 != self.north and not self.north) or \
46  (0 != self.south and not self.south) or \
47  (0 != self.west and not self.west) or \
48  (0 != self.east and not self.east):
49  print("Error: All four NSWE coordinates required!")
50  sys.exit(1)
51  try:
52  north = float(self.north)
53  except ValueError:
54  err_msg = 'Error! North value "{0}" non-numeric.'.format(self.north)
55  try:
56  south = float(self.south)
57  except ValueError:
58  err_msg = 'Error! South value "{0}" non-numeric.'.format(self.south)
59  try:
60  east = float(self.east)
61  except ValueError:
62  err_msg = 'Error! East value "{0}" non-numeric.'.format(self.east)
63  try:
64  west = float(self.west)
65  except ValueError:
66  err_msg = 'Error! West value "{0}" non-numeric.'.format(self.west)
67 
68  if north <= south:
69  print("Error: North must be greater than South!")
70  sys.exit(1)
71  if (north > 90.0) or (south < -90.0):
72  print("Latitude range outside realistic bounds!")
73  sys.exit(1)
74  if west < -180 or west > 180. or east < -180. or east > 180:
75  print("Longitudes must be between -180.0 and 180.0")
76  sys.exit(1)
77 
78  def lonlat2pixline(self, zero=False):
79  """
80  Run lonlat2pixline
81  """
82  self.chk()
83 
84  if self.verbose:
85  print("")
86  print("Locating pixel/line range ...")
87  exe = os.path.join(self.dirs['bin'], 'lonlat2pixline')
88  pixlincmd = [exe, '-F', self.geofile,
89  str(self.west), str(self.south),
90  str(self.east), str(self.north)]
91  p = subprocess.Popen(pixlincmd, stdout=subprocess.PIPE)
92  line = p.communicate()[0].decode("utf-8")
93 
94  if p.returncode in (0, 110):
95 
96  # get pixel/line endpoints
97  pixlin = line.splitlines()[-5][2:].split() # [spixl,epixl,sline,eline]
98  pixlin = [int(p) for p in pixlin] # bytestring -> int
99  if zero:
100  pixlin = [p - 1 for p in pixlin] # convert to zero-based index
101  self.spixl = pixlin[0]
102  self.epixl = pixlin[1]
103  self.sline = pixlin[2]
104  self.eline = pixlin[3]
105 
106  elif p.returncode == 120:
107  print("No extract necessary:",
108  "entire scene contained within specified region of interest.")
109  else:
110  print("Error locating pixel/line range to extract.")
111 
112  self.status = p.returncode
def lonlat2pixline(self, zero=False)
Definition: pixlin_utils.py:78
def env(self)
Definition: setupenv.py:7
const char * str
Definition: l1c_msi.cpp:35
def __init__(self, geofile=None, north=None, south=None, west=None, east=None, verbose=False)
Definition: pixlin_utils.py:11
def __setitem__(self, index, item)
Definition: pixlin_utils.py:31