NASA Logo
Ocean Color Science Software

ocssw V2022
l1info_spexone.py
Go to the documentation of this file.
1 #! /usr/bin/env python3
2 
3 # l1info for SpexOne L1B. Returns the following information:
4 # 1. Start Date and Time
5 # 2. End Date and Time
6 # 3. Gring information (lon-lats) for upper left, middle and right
7 # points and lower left, middle and right points in clockwise
8 # direction
9 
10 import argparse
11 import datetime
12 from netCDF4 import Dataset as NetCDF
13 
14 __version__ = "1.1 (2024-04-25)"
15 
16 
17 # CONSTANTS
18 MIN_LAT_BOUND = -50.0
19 MAX_LAT_BOUND = 50.0
20 
21 MIN_DELTA_LAT = 5.0
22 
23 
24 '''
25  Given a lon and lat dataset, process the gring of the datasets
26  Return an array of tuples (lon,lat) for the left and right side
27  of the gring
28 '''
29 def getGring(lon_dataset, lat_dataset):
30 
31  # array containing tuples to be returned
32  leftArr = []
33  rightArr = []
34 
35  # tracks prev lon and lat values on the left and right
36  prevLonLeft = None
37  prevLatLeft = None
38  prevLonRight = None
39  prevLatRight = None
40 
41  # go through each line and get left and right points
42  for i in range(num_bins):
43 
44  # only update prev values when points are added
45  updatePrevVals = False
46 
47  # left, right points
48  leftPoint = (lon_dataset[i][num_spatial-1], lat_dataset[i][num_spatial-1])
49  rightPoint = (lon_dataset[i][0], lat_dataset[i][0])
50 
51  # individual left-right lon lats
52  leftLon = leftPoint[0]
53  leftLat = leftPoint[1]
54  rightLon = rightPoint[0]
55  rightLat = rightPoint[1]
56 
57  # first point, record the lon lat values as prev and append the left, middle and
58  # right points
59  if i == 0:
60 
61  # upper middle point needs to be first element in left array bc of gring clock-wise printing.
62  # Left elements are reversed when printing and the last element to print is the upper middle.
63  upperMiddlePoint = (lon_dataset[i][(num_spatial-1)//2], lat_dataset[i][(num_spatial-1)//2])
64  leftArr.append(upperMiddlePoint)
65  leftArr.append(leftPoint)
66  rightArr.append(rightPoint)
67  updatePrevVals = True
68 
69 
70  # last point, add left, middle and right points
71  elif i == num_bins-1:
72  leftArr.append(leftPoint)
73  rightArr.append(rightPoint)
74 
75  # after the right side is done printing, it prints the lower middle
76  # point and then prints the left points in reverse order
77  lowerMiddlePoint = (lon_dataset[i][(num_spatial-1)//2], lat_dataset[i][(num_spatial-1)//2])
78  rightArr.append(lowerMiddlePoint)
79  updatePrevVals = True
80 
81 
82  # above 50.0 and below -50.0 lat bounds, get more points
83  elif (leftLat >= MAX_LAT_BOUND or leftLat <= MIN_LAT_BOUND or rightLat >= MAX_LAT_BOUND or rightLat <= MIN_LAT_BOUND):
84 
85  # check both delta lon and lat >= 10 on both sides
86  leftDeltaLon = abs(leftLon - prevLonLeft)
87  leftDeltaLat = abs(leftLat - prevLatLeft)
88  rightDeltaLon = abs(rightLon - prevLonRight)
89  rightDeltaLat = abs(rightLat - prevLatRight)
90 
91  # if the delta lat is at least MIN_DELTA_LAT, grab the point
92  if ((leftDeltaLat >= MIN_DELTA_LAT and rightDeltaLat >= MIN_DELTA_LAT) or
93  (leftDeltaLon >= 5.0 and rightDeltaLon >= MIN_DELTA_LAT)):
94 
95  leftArr.append(leftPoint)
96  rightArr.append(rightPoint)
97  updatePrevVals = True
98 
99  # in between, check only lat
100  elif (leftLat < 40.0 and leftLat > -40.0) or (rightLat < 40.0 and rightLat > -40.0):
101  deltaLat = abs(leftLat - prevLatLeft)
102  if (deltaLat >= 20.0):
103  leftArr.append(leftPoint)
104  rightArr.append(rightPoint)
105  updatePrevVals = True
106 
107  # update prev values
108  if updatePrevVals:
109  prevLonLeft = leftLon
110  prevLatLeft = leftLat
111  prevLonRight = rightLon
112  prevLatRight = rightLat
113 
114  return rightArr, leftArr
115 
116 
117 # Prints the gring info for both lat and lon.
118 def printGring(rightArr, leftArr, isLon:bool):
119 
120  # flag depending on if it's printing lon or lat gring
121  flag = 0 if isLon else 1
122 
123  # print upper right
124  if (isLon):
125  print("gringpointlongitude=", end="")
126  else:
127  print("gringpointlatitude=", end="")
128 
129 
130  arrSize = len(rightArr)
131 
132  # print all right elements and lower middle
133  for i in range(arrSize):
134  print(rightArr[i][flag], end=",")
135 
136  # print all left elements and upper middle
137  for i in reversed(range(arrSize)):
138  if i == 0:
139  print(leftArr[i][flag], end="\n")
140  else:
141  print(leftArr[i][flag], end=",")
142 
143 
144 
145 def main():
146  print("l1info_spexone", __version__)
147 
148  # parse command line info
149  parser = argparse.ArgumentParser(description="Given a SpexOne L1B file, return its time and gring information")
150  parser.add_argument("iFile", type=str, help="SpexOne L1B NetCDF file")
151  args = parser.parse_args()
152 
153  # open netcdf file
154  ncFile = NetCDF(args.iFile, "r+", format="NETCDF4")
155 
156  # shape of lon and lat dataset. Global for other function access
157  global num_bins # 1st dim
158  global num_spatial # 2nd dim
159  num_bins = len(ncFile.dimensions["bins_along_track"])
160  num_spatial = len(ncFile.dimensions["spatial_samples_per_image"])
161 
162  # get start
163  startTime = ncFile.__dict__["time_coverage_start"]
164  endDatetime = datetime.datetime(int(startTime[:4]), int(startTime[5:7]), int(startTime[8:10]))
165 
166  # get last bin start time, this will be the end time
167  var = ncFile.groups["BIN_ATTRIBUTES"]["image_time"]
168  # ignore the valid_max for this variable
169  var.set_auto_mask(False)
170  lastBinSeconds = float(var[num_bins-1])
171  endTime = datetime.timedelta(seconds=lastBinSeconds)
172  endDatetime += endTime
173  endTimeStr = endDatetime.isoformat()
174  if(len(endTimeStr) > 23):
175  endTimeStr = endTimeStr[:23]
176 
177  # get lon and lat dataset
178  lon_dataset = ncFile.groups["GEOLOCATION_DATA"]["longitude"]
179  lat_dataset = ncFile.groups["GEOLOCATION_DATA"]["latitude"]
180 
181  # get gring items for printing
182  rightArr, leftArr = getGring(lon_dataset, lat_dataset)
183 
184 
185 
186  print(f"Start_Date={startTime[:10]}")
187  print(f"Start_Time={startTime[11:23]}")
188  print(f"End_Date={endTimeStr[:10]}")
189  print(f"End_Time={endTimeStr[11:]}")
190 
191 
192 
193  printGring(rightArr, leftArr, True)
194  printGring(rightArr, leftArr, False)
195 
196 
197 
198  print("gringpointsequence=", end="")
199 
200  # 6 counts for the upper and lower indicies not in the center arr
201  for i in range(len(leftArr) * 2):
202  if i == (len(leftArr) * 2)-1: # last index, dont end with ,
203  print(str(i+1))
204  else:
205  print(str(i+1), end=",")
206 
207 
208 if __name__ == "__main__":
209  main()
def printGring(rightArr, leftArr, bool isLon)
character(len=1000) if
Definition: names.f90:13
void print(std::ostream &stream, const char *format)
Definition: PrintDebug.hpp:38
def getGring(lon_dataset, lat_dataset)
#define abs(a)
Definition: misc.h:90
Definition: aerosol.c:136