NASA Logo
Ocean Color Science Software

ocssw V2022
run_local.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 import os
3 import csv
4 import numpy as np
5 from geo_eval import get_lat_lon_shift, read_file_l1b
6 from scipy.spatial import ConvexHull
7 from shapely.geometry.polygon import Polygon
8 from shapely.geometry import Point
9 import sys
10 import argparse
11 
12 
13 def get_polygon(lat, lon):
14  coord = np.column_stack((lat.flatten(), lon.flatten()))
15  hull = ConvexHull(coord)
16  points = []
17  for lat_p, lon_p in zip(coord[hull.vertices, 0], coord[hull.vertices, 1]):
18  points.append((lat_p, lon_p))
19  points.append((coord[hull.vertices, 0][0], coord[hull.vertices, 1][0]))
20  polygon = Polygon(points)
21  return polygon
22 
23 
24 def get_matching_chips(feature_file, polygon):
25 
26  feature_names = []
27  feature_lats = []
28  feature_lons = []
29 
30  with open(feature_file, mode='r') as file:
31  csvFile = csv.DictReader(file)
32  for lines in csvFile:
33  feature_lat = float(lines["featureLat"])
34  feature_lon = float(lines["featureLon"])
35  south = float(lines["south"])
36  north = float(lines["north"])
37  west = float(lines["west"])
38  east = float(lines["east"])
39  point = Point(feature_lat, feature_lon)
40  if polygon.contains(point):
41  p_0 = (north, west)
42  p_1 = (north, east)
43  p_2 = (south, east)
44  p_3 = (south, west)
45  polygon_chip = Polygon([p_0, p_1, p_2, p_3, p_0])
46  if polygon.contains(polygon_chip):
47  feature_names.append(lines["filename"])
48  feature_lats.append(feature_lat)
49  feature_lons.append(feature_lon)
50  return feature_names
51 
52 
53 if __name__ == "__main__":
54  parser = argparse.ArgumentParser()
55  parser.add_argument(
56  "--ifile", help="input L1B netcdf file", type=str, required=True)
57  parser.add_argument(
58  "--idir", help="directory with chips", type=str, required=True)
59  parser.add_argument(
60  "--odir", help="output directory", type=str, required=True)
61  parser.add_argument('--debug', action='store_true')
62  parser.add_argument('--no-debug', dest='debug', action='store_false')
63  parser.set_defaults(debug=False)
64  if len(sys.argv) == 1:
65  parser.print_help()
66  sys.exit(0)
67  args = parser.parse_args()
68  l1b_path = args.ifile
69  lat, lon, _, _, _ = read_file_l1b(l1b_path)
70  polygon = get_polygon(lat, lon)
71  dir_chips = args.idir
72  feature_file = os.path.join(dir_chips, "chipindex.csv")
73  debug_mode = False
74  feature_names = get_matching_chips(feature_file, polygon)
75  feature_names = [os.path.join(dir_chips, x) for x in feature_names]
76  outfile = os.path.join(args.odir, f"gcpm-{os.path.basename(l1b_path)}.csv")
77  if args.debug:
78  with open("chip_lists.txt","w") as ilist:
79  ilist.write('\n'.join(feature_names))
80  ret_code = get_lat_lon_shift(
81  l1b_path, feature_names, outfile, debug_mode=args.debug)
82  sys.exit(ret_code)
def read_file_l1b(str path)
Definition: geo_eval.py:86
def get_polygon(lat, lon)
Definition: run_local.py:13
def get_lat_lon_shift(str l1b_path, List[str] chips_list, str output_file_name, debug_mode=False, **kwargs)
Definition: geo_eval.py:354
K::Point_2 Point
Definition: cgal_interp.cpp:16
def get_matching_chips(feature_file, polygon)
Definition: run_local.py:24