20 from netCDF4
import Dataset
as NetCDF
23 __version__ =
"2.1 (2024-09-18)"
37 def __init__(self, west:LonLatPoint, east:LonLatPoint):
48 lons =
list(reversed(lons))
49 lats =
list(reversed(lats))
82 for i
in range(len(lons)-1):
90 diff = (x1*y2) - (x2*y1)
105 for pix
in range(numPixels):
108 if (goodPixelCount == 2):
110 if (~lonData[view][line].mask[pix]
and ~latData[view][line].mask[pix]):
120 return np.any(~lonData[view][line].mask)
and np.any(~latData[view][line].mask)
133 lastGoodLine = numLines-1
136 while (firstGoodLine < lastGoodLine):
143 if (isGoodFirstLine
and isGoodLastLine):
149 if (goodFirstLine
and goodLastLine):
152 elif (goodFirstLine
and not goodLastLine):
155 elif (
not goodFirstLine
and goodLastLine):
162 elif (isGoodFirstLine
and not isGoodLastLine):
164 elif (
not isGoodFirstLine
and isGoodLastLine):
170 return firstGoodLine, lastGoodLine
181 lastGoodLine = numLines-1
184 while (firstGoodLine < lastGoodLine):
191 if (isGoodFirstLine
and isGoodLastLine):
197 if (goodFirstLine
and goodLastLine):
200 elif (goodFirstLine
and not goodLastLine):
203 elif (
not goodFirstLine
and goodLastLine):
210 elif (isGoodFirstLine
and not isGoodLastLine):
212 elif (
not isGoodFirstLine
and isGoodLastLine):
219 if (firstGoodLine >= lastGoodLine):
228 return np.any(~lonData[view][line].mask[pixel])
and np.any(~latData[view][line].mask[pixel])
248 line = lastLine
if isNorth
else firstLine
253 lastGoodPixel = numPixels-1
256 while (firstGoodPixel < lastGoodPixel):
263 if (isGoodFirstPixel
and isGoodLastPixel):
267 elif (isGoodFirstPixel
and not isGoodLastPixel):
271 elif (
not isGoodFirstPixel
and isGoodLastPixel):
281 leftPoint =
LonLatPoint(lonData[view][line][firstGoodPixel], latData[view][line][firstGoodPixel])
282 rightPoint =
LonLatPoint(lonData[view][line][lastGoodPixel], latData[view][line][lastGoodPixel])
287 coordinateObj =
Coordinates(east=rightPoint, west=leftPoint)
293 except Exception
as e:
303 for view
in viewsList:
305 viewCoordinates[view] = coordinate
307 return viewCoordinates
322 global NW_CORNER_VIEWS
323 global NE_CORNER_VIEWS
324 global SW_CORNER_VIEWS
325 global SE_CORNER_VIEWS
331 for view
in NW_CORNER_VIEWS:
332 gringLons.append(northCoordinates[view].west.lon)
333 gringLats.append(northCoordinates[view].west.lat)
336 for view
in NE_CORNER_VIEWS:
337 gringLons.append(northCoordinates[view].east.lon)
338 gringLats.append(northCoordinates[view].east.lat)
341 for view
in reversed(SE_CORNER_VIEWS):
342 gringLons.append(southCoordinates[view].east.lon)
343 gringLats.append(southCoordinates[view].east.lat)
346 for view
in reversed(SW_CORNER_VIEWS):
347 gringLons.append(southCoordinates[view].west.lon)
348 gringLats.append(southCoordinates[view].west.lat)
352 return gringLons, gringLats
368 global NW_CORNER_VIEWS
369 global NE_CORNER_VIEWS
370 global SW_CORNER_VIEWS
371 global SE_CORNER_VIEWS
377 for view
in reversed(NW_CORNER_VIEWS):
378 lons.append(northCoordinates[view].west.lon)
379 lats.append(northCoordinates[view].west.lat)
382 for view
in SW_CORNER_VIEWS:
383 lons.append(southCoordinates[view].west.lon)
384 lats.append(southCoordinates[view].west.lat)
386 for view
in SE_CORNER_VIEWS:
387 lons.append(southCoordinates[view].east.lon)
388 lats.append(southCoordinates[view].east.lat)
391 for view
in reversed(NE_CORNER_VIEWS):
392 lons.append(northCoordinates[view].east.lon)
393 lats.append(northCoordinates[view].east.lat)
404 gringLonStr =
"gringpointlongitude="
405 gringLatStr =
"gringpointlatitude="
407 for i
in range(len(lons)):
410 if (lons[i] == -999
or lats[i] == -999):
412 gringLonStr += f
"{lons[i]},"
413 gringLatStr += f
"{lats[i]},"
416 gringLonStr = gringLonStr[:-1]
417 gringLatStr = gringLatStr[:-1]
421 print(f
"gringpointsequence=1,2,3,4")
441 geospatialBoundsStr =
"geospatial_bounds="
443 for i
in range(len(lons)):
444 if (lons[i] == -999
or lats[i] == -999):
446 geospatialBoundsStr += f
"{lats[i]} {lons[i]},"
449 geospatialBoundsStr = geospatialBoundsStr[:-1]
451 print(geospatialBoundsStr)
457 rightPoint = len(allViews)-1
459 while(leftPoint < rightPoint):
464 if (goodLeftPoint
and goodRightPoint):
465 goodLeftViewNum = allViews[leftPoint]
466 goodRightViewNum = allViews[rightPoint]
467 return [goodLeftViewNum, goodRightViewNum]
470 elif (goodLeftPoint
and not goodRightPoint):
473 elif (
not goodLeftPoint
and goodRightPoint):
482 global emptyCornerCount
490 print(
"l1info_harp2", __version__)
495 helpMessage =
"""Given a Harp2 L1B file, return time, gring and geospatial bounds. \n
497 0: Everything is fine,
499 110: Bad Geolocation,
500 120: Bad Geospacial bounds,
501 130: Bad file, missing more than 1 corner of the polygon or wrong file.
505 parser = argparse.ArgumentParser(
506 description=helpMessage,
509 formatter_class=argparse.RawDescriptionHelpFormatter
511 parser.add_argument(
"iFile", type=str, help=
"HARP2 L1B NetCDF file")
512 args = parser.parse_args()
516 if (args.iFile.split(
".")[2].upper() !=
"L1B"):
517 print(
"Input file is not L1B. Exiting...")
520 ncFile = NetCDF(args.iFile,
"r+", format=
"NETCDF4")
523 start_time = ncFile.__dict__[
"time_coverage_start"]
524 end_time = ncFile.__dict__[
"time_coverage_end"]
529 lonData = ncFile.groups[
"geolocation_data"][
"longitude"][:]
530 latData = ncFile.groups[
"geolocation_data"][
"latitude"][:]
534 global numViews, numLines, numPixels
535 numViews = len(ncFile.dimensions[
"views"])
536 numLines = len(ncFile.dimensions[
"swath_lines"])
537 numPixels = len(ncFile.dimensions[
"swath_pixels"])
566 global ALL_NE_VIEWS, ALL_NE_VIEWS, ALL_SW_VIEWS, ALL_SE_VIEWS
567 ALL_NW_VIEWS = [17, 71, 16, 15, 14, 13, 12, 14, 13, 12, 80, 11, 70, 10]
568 ALL_NE_VIEWS = [10, 70, 11, 80, 12, 13, 14, 15, 16, 71, 17, 81, 18, 1, 19, 20, 21, 22]
569 ALL_SW_VIEWS = [62, 63, 64, 65, 66, 67, 79, 68]
570 ALL_SE_VIEWS = [79, 67, 9, 66, 65, 64, 63, 62, 78, 61, 88, 59, 58, 57, 56]
574 global emptyCornerCount
579 global NW_CORNER_VIEWS
585 global NE_CORNER_VIEWS
590 global SW_CORNER_VIEWS
594 global SE_CORNER_VIEWS
600 if emptyCornerCount > 0:
607 northViews =
list(
set(NW_CORNER_VIEWS + NE_CORNER_VIEWS))
611 southViews =
list(
set(SW_CORNER_VIEWS + SE_CORNER_VIEWS))
617 gringLons, gringLats =
ConstructGring(northViewCoordinates, southViewCoordinates)
631 print(f
"Start_Date={start_time[:10]}")
632 print(f
"Start_Time={start_time[11:-1]}")
633 print(f
"End_Date={end_time[:10]}")
634 print(f
"End_Time={end_time[11:-1]}")
645 if __name__ ==
"__main__":