OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
omerc.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 import argparse
3 from math import cos, sin, atan2, radians, degrees
4 import netCDF4 as nc
5 import os
6 
7 def main():
8 
9  parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, description='''\
10  This program returns the projection definition information for an
11  Oblique Mercator projection for the given input L2 file. The default
12  output is a set of keyword=value pairs for input to l3mapgen.''', add_help=True)
13  parser.add_argument('ifile', nargs=1, type=str, help=' input file')
14  parser.add_argument('--oformat', nargs=1, choices=['l3mapgen','proj4','gmt'],type=str,
15  default=['l3mapgen'],help='return l3mapgen, proj4 or gmt projection parameters')
16 
17  args = parser.parse_args()
18  if not args.ifile:
19  parser.error("You must specify a Level 2 input file")
20 
21  phi_c = None # proj4 +lat_1 (single standard parallel)
22  phi_0 = None # proj4 +lat_0 (central latitude of origin)
23  lambda_0 = None # proj4 +lonc (central meridian)
24  lambda_1 = None
25 
26  # Grab the necessary geolocation bits from the L2 file...
27  if os.path.exists(args.ifile[0]):
28  with nc.Dataset(args.ifile[0]) as dataset:
29  nlines = len(dataset.dimensions['number_of_lines'])
30  scan = dataset.groups['scan_line_attributes'].variables
31  mid = nlines / 2
32 
33  # Scene center coordinates
34  clon = scan['clon'][mid]
35  clat = scan['clat'][mid]
36 
37  # Starting (i.e. first scan line) mid-scan coordinates
38  slon = scan['clon'][0]
39  slat = scan['clat'][0]
40 
41  # Ending (i.e. last scan line) mid-scan coordinates
42  elon = scan['clon'][-1]
43  elat = scan['clat'][-1]
44 
45  # Use the above coordinates to define a projection.
46  phi_0 = radians(slat)
47  phi_c = radians(clat)
48  lambda_1 = radians(slon)
49  lambda_0 = radians(clon)
50 
51  y = sin(lambda_1 - lambda_0) * cos(phi_c)
52  x = cos(phi_0) * sin(phi_c) - sin(phi_0)*cos(phi_c)*cos(lambda_1 - lambda_0)
53  alpha = degrees(atan2(y,x))
54 
55  # might need to adjust gamma when azimuth points south?
56  if args.oformat[0] == 'gmt':
57  prj = "gmt mapproject -Job{:f}/{:f}/{:f}/{:f}/1:1 -C -F -R-1/1/-1/1".format(clon,clat,slon,slat)
58  print("GMT def:")
59  print(prj)
60  elif args.oformat[0] == 'proj4':
61  print("proj4 def:")
62  print("+proj=omerc +lat_0=%.9f +lonc=%.9f +alpha=%.9f +gamma=0" %
63  (degrees(phi_c), degrees(lambda_0), alpha-90))
64  else:
65  print("lat_0=%.6f" % degrees(phi_c))
66  print("central_meridian=%.6f" % degrees(lambda_0))
67  print("azimuth=%.6f" % (alpha-90.))
68 
69  else:
70  err_msg = 'Could not find {0} to open'.format(ifile)
71  sys.exit(err_msg)
72 
73 
74 if __name__ == "__main__": main()
a context in which it is NOT documented to do so subscript which cannot be easily calculated when extracting TONS attitude data from the Terra L0 files Corrected several defects in extraction of entrained ephemeris and and as HDF file for both the L1A and Geolocation enabling retrieval of South Polar DEM data Resolved Bug by changing to opent the geolocation file only after a successful read of the L1A and also by checking for fatal errors from not radians
Definition: HISTORY.txt:78
#define degrees(radians)
Definition: niwa_iop.c:30
def main()
Definition: omerc.py:7