OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
sst_coef_lut.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 # This script converts the ASCII SST coefficient tables provided by RSMAS
4 # into a netCDF4 file. Since this is a very infrequent occurance, you may
5 # need to modify this as appropritate when new tables are provided.
6 
7 import pandas as pd
8 from netCDF4 import Dataset
9 import datetime
10 import argparse
11 import sys
12 
13 __version__ = '1.0.0_2019-04-05'
14 
15 def main():
16  """
17  Primary driver of the program; get command line arguments, check the files
18  specified and kick off the processing
19  """
20 
21  parser = argparse.ArgumentParser(description=\
22  'Converts ASCII "Latband" SST coefficient LUTs to netCDF.')
23  parser.add_argument('--version', action='version', version='%(prog)s ' + __version__)
24  parser.add_argument('--nlatbands', default=7, type=int, help='number of latbands in LUT')
25  parser.add_argument('--ncoefficients', default=7, type=int, help='number of coefficients in LUT')
26  parser.add_argument('input_file', type=str, help='path to the input ASCII LUT')
27  parser.add_argument('output_file', type=str, help='path to the output netCDF LUT')
28  parser.add_argument('--lut_version', type=str, help='version of the LUT being converted')
29  parser.add_argument('--lut_description', default="11 um", type=str,help='string to identify sst type to apply these coefficients (11 um, 4 um, Triple Window)')
30  parser.add_argument('--fields', nargs='+', help="field names for ASCII table; default:['sensor','month','minlat','maxlat','a0','a1','a2','a3','a4','a5','a6','count']")
31  parser.add_argument('--mission', default="modis-aqua", help="mission for which the LUTs apply (modis-aqua, modis-terra, viirs-npp")
32  parser.add_argument('--verbose', '-v', action='store_true')
33 
34  args = parser.parse_args()
35 
36  numberOfLatbands = args.nlatbands
37  numberOfCoefficients = args.ncoefficients
38  inputFilename = args.input_file
39  outputFilename = args.output_file
40  lutdescription = args.lut_description
41  fields = ['sensor','month','minlat','maxlat','a0','a1','a2','a3','a4','a5','a6','count']
42  if args.fields:
43  fields = args.fields
44 
45  create_date = datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
46 
47  skiprows = 0
48  with open(inputFilename, 'r') as f:
49  for line in f:
50  if line.startswith('#'):
51  skiprows = skiprows + 1
52 
53  if args.verbose:
54  print("Processing %s ..." % inputFilename)
55  print("...found %d header lines to skip" % skiprows)
56  print("Creating to %s ..." % outputFilename)
57  print("with coefficients for %s" % lutdescription)
58 
59  data = pd.read_csv(inputFilename, skiprows=skiprows, delim_whitespace=True, names=fields)
60 
61  #cols = ['month', 'lat1', 'lat2', 'a0', 'a1', 'a2', 'a3', 'a6', 'a5', 'a4', 'count']
62  data['latband'] = data.index % numberOfLatbands
63 
64  bounds = data[['minlat','maxlat'] ]
65  coefficientList = ['a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6' ]
66  if numberOfCoefficients != 7:
67  coefficientList = []
68  for coef in range (0,numberOfCoefficients):
69  coefficientList.append('a'+str(coef))
70  coefficients = data[coefficientList]
71 
72  #print(bounds[bounds.index.isin([0, 1,2,3, 4,5,6])])
73  #print(coefficients)
74 
75  rootgrp = Dataset(outputFilename, "w", format="NETCDF4")
76  rootgrp.createDimension("month", 12)
77  rootgrp.createDimension("latband", numberOfLatbands)
78  rootgrp.createDimension("coefficient", numberOfCoefficients)
79  rootgrp.createDimension("latitude",2)
80 
81  instrument = 'MODIS'
82  platform = 'Aqua'
83 
84  if args.mission == 'modis-terra':
85  platform = 'Terra'
86 
87  if args.mission == 'viirs-npp':
88  instrument = 'VIIRS'
89  platform = 'SNPP'
90 
91  rootgrp.title = "%s-%s SST coefficients" % (instrument,platform)
92  rootgrp.instrument = instrument
93  rootgrp.platform = platform
94  rootgrp.description = "Latitude band derived %s SST coefficients for %s-%s" % (lutdescription,instrument,platform)
95  if args.lut_version:
96  rootgrp.version = args.lut_version
97  if args.verbose:
98  print("LUT version: %s" % args.lut_version)
99 
100  rootgrp.date_created = create_date
101  rootgrp.product_name = outputFilename
102 
103  coef = rootgrp.createVariable("coefficients","f4",("month","latband","coefficient",),fill_value=-32767.0)
104  coef.long_name = "SST (%s) coefficients" % lutdescription
105  coef.equation = "SST = C0 + C1*BT11 + C2*(BT11-BT12)*bsst + C3*sec(abs(satz)) + C4*satz + C5*satz^2 + C6*mirrorside"
106  coef.description = "SST (%s) coefficients based on latitude band and month of the year" % lutdescription
107  coef.comment = 'Month Index: 0=Jan, 1=Feb, 2=Mar, 3=Apr, 4=May, 5=Jun, 6=Jul, 7=Aug, 8=Sep, 9=Oct, 10=Nov, 11=Dec'
108 
109  bound = rootgrp.createVariable("latbands","f4",("latband","latitude",),fill_value=-999.)
110  bound.valid_min = -90.
111  bound.valid_max = 90.
112  bound.long_name = 'Latitude Bands for SST coefficients'
113  bounds.units = 'degrees_north'
114 
115  latbandList = []
116  for latband in range (0,numberOfLatbands):
117  latbandList.append(latband)
118  b = bounds[bounds.index.isin(latbandList)].values
119  bound[:] = b
120 
121  coef[:] = coefficients.values
122 
123  rootgrp.close()
124  if args.verbose:
125  print("Done!")
126 
127 # The following allows the file to be imported without immediately executing.
128 if __name__ == '__main__':
129  sys.exit(main())
const char * str
Definition: l1c_msi.cpp:35