OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
sst_dust_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 import os
13 
14 __version__ = '1.0.0_2019-05-18'
15 
16 def main():
17  """
18  Primary driver of the program; get command line arguments, check the files
19  specified and kick off the processing
20  """
21 
22  parser = argparse.ArgumentParser(description=\
23  'Converts ASCII SST dust correction coefficient file to netCDF.')
24  parser.add_argument('--version', action='version', version='%(prog)s ' + __version__)
25  parser.add_argument('input_file', type=str, help='path to the input ASCII LUT')
26  parser.add_argument('output_file', type=str, help='path to the output netCDF LUT')
27  parser.add_argument('--lut_version', type=str, help='LUT version',default='1.0')
28  parser.add_argument('--fields', nargs='+', help="field names for ASCII table; default:['c0','c1','c2','c3','c4','c5','c6','c7','c8','c9','c10','c11','c12','dsdi_thresh']")
29  parser.add_argument('--mission', default="modis-aqua", help="mission for which the LUTs apply (modis-aqua, modis-terra, viirs-npp")
30  parser.add_argument('--verbose', '-v', action='store_true')
31 
32  args = parser.parse_args()
33 
34  numberOfCoefficients = 13
35  inputFilename = args.input_file
36  outputFilename = args.output_file
37  fields = ['c0','c1','c2','c3','c4','c5','c6','c7','c8','c9','c10','c11','c12','dsdi_thresh','dust_thresh']
38  if args.fields:
39  fields = args.fields
40 
41  create_date = datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
42 
43  skiprows = 0
44  with open(inputFilename, 'r') as f:
45  for line in f:
46  if line.startswith('#'):
47  skiprows = skiprows + 1
48 
49  if args.verbose:
50  print("Processing %s ..." % inputFilename)
51  print("...found %d header lines to skip" % skiprows)
52  print("Creating to %s ..." % outputFilename)
53 
54 # data = pd.read_csv(inputFilename, skiprows=skiprows, delim_whitespace=True, names=fields)
55  data = pd.read_csv(inputFilename, skiprows=skiprows, names=fields)
56 
57  #cols = ['month', 'lat1', 'lat2', 'a0', 'a1', 'a2', 'a3', 'a6', 'a5', 'a4', 'count']
58 
59  coefficientList = ['c0','c1','c2','c3','c4','c5','c6','c7','c8','c9','c10','c11','c12' ]
60  if numberOfCoefficients != 13:
61  coefficientList = []
62  for coef in range (0,numberOfCoefficients):
63  coefficientList.append('c'+str(coef))
64  coefficients = data[coefficientList]
65 
66  print(data)
67  #print(bounds[bounds.index.isin([0, 1,2,3, 4,5,6])])
68  #print(coefficients)
69 
70  rootgrp = Dataset(outputFilename, "w", format="NETCDF4")
71  rootgrp.createDimension("coefficient", numberOfCoefficients)
72 
73  instrument = 'MODIS'
74  platform = 'Aqua'
75 
76  if args.mission == 'modis-terra':
77  platform = 'Terra'
78 
79  if args.mission == 'viirs-npp':
80  instrument = 'VIIRS'
81  platform = 'SNPP'
82 
83  rootgrp.title = "%s-%s SST dust correction coefficients" % (instrument,platform)
84  rootgrp.instrument = instrument
85  rootgrp.platform = platform
86  rootgrp.description = "SST dust correction coefficients for %s-%s" % (instrument,platform)
87  if args.lut_version:
88  rootgrp.version = args.lut_version
89  if args.verbose:
90  print("LUT version: %s" % args.lut_version)
91 
92  rootgrp.date_created = create_date
93  rootgrp.product_name = outputFilename
94  rootgrp.dsdi_threshold = data['dsdi_thresh']
95  rootgrp.dust_threshold = data['dust_thresh']
96  history = ' '.join(sys.argv)
97  history = history.replace(os.environ["OCSSWROOT"],"$OCSSWROOT")
98  rootgrp.history = history.encode('ascii')
99 
100 
101  coef = rootgrp.createVariable("coefficients","f4",("coefficient",),fill_value=-32767.0)
102  coef.long_name = "SST dust correction coefficients"
103 # coef.equation = "SST = C0 + C1*BT11 + C2*(BT11-BT12)*bsst + C3*sec(abs(satz)) + C4*satz + C5*satz^2 + C6*mirrorside"
104  coef.description = "SST dust correction coefficients"
105 
106  coef[:] = coefficients.values
107 
108  rootgrp.close()
109  if args.verbose:
110  print("Done!")
111 
112 # The following allows the file to be imported without immediately executing.
113 if __name__ == '__main__':
114  sys.exit(main())
const char * str
Definition: l1c_msi.cpp:35