8 from netCDF4
import Dataset
14 __version__ =
'1.0.0_2019-05-18'
18 Primary driver of the program; get command line arguments, check the files
19 specified and kick off the processing
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')
32 args = parser.parse_args()
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']
41 create_date = datetime.datetime.utcnow().strftime(
'%Y-%m-%dT%H:%M:%SZ')
44 with open(inputFilename,
'r')
as f:
46 if line.startswith(
'#'):
47 skiprows = skiprows + 1
50 print(
"Processing %s ..." % inputFilename)
51 print(
"...found %d header lines to skip" % skiprows)
52 print(
"Creating to %s ..." % outputFilename)
55 data = pd.read_csv(inputFilename, skiprows=skiprows, names=fields)
59 coefficientList = [
'c0',
'c1',
'c2',
'c3',
'c4',
'c5',
'c6',
'c7',
'c8',
'c9',
'c10',
'c11',
'c12' ]
60 if numberOfCoefficients != 13:
62 for coef
in range (0,numberOfCoefficients):
63 coefficientList.append(
'c'+
str(coef))
64 coefficients = data[coefficientList]
70 rootgrp = Dataset(outputFilename,
"w", format=
"NETCDF4")
71 rootgrp.createDimension(
"coefficient", numberOfCoefficients)
76 if args.mission ==
'modis-terra':
79 if args.mission ==
'viirs-npp':
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)
88 rootgrp.version = args.lut_version
90 print(
"LUT version: %s" % args.lut_version)
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')
101 coef = rootgrp.createVariable(
"coefficients",
"f4",(
"coefficient",),fill_value=-32767.0)
102 coef.long_name =
"SST dust correction coefficients"
104 coef.description =
"SST dust correction coefficients"
106 coef[:] = coefficients.values
113 if __name__ ==
'__main__':