8 from netCDF4
import Dataset
13 __version__ =
'1.0.0_2019-04-05'
17 Primary driver of the program; get command line arguments, check the files
18 specified and kick off the processing
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')
34 args = parser.parse_args()
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']
45 create_date = datetime.datetime.utcnow().strftime(
'%Y-%m-%dT%H:%M:%SZ')
48 with open(inputFilename,
'r')
as f:
50 if line.startswith(
'#'):
51 skiprows = skiprows + 1
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)
59 data = pd.read_csv(inputFilename, skiprows=skiprows, delim_whitespace=
True, names=fields)
62 data[
'latband'] = data.index % numberOfLatbands
64 bounds = data[[
'minlat',
'maxlat'] ]
65 coefficientList = [
'a0',
'a1',
'a2',
'a3',
'a4',
'a5',
'a6' ]
66 if numberOfCoefficients != 7:
68 for coef
in range (0,numberOfCoefficients):
69 coefficientList.append(
'a'+
str(coef))
70 coefficients = data[coefficientList]
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)
84 if args.mission ==
'modis-terra':
87 if args.mission ==
'viirs-npp':
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)
96 rootgrp.version = args.lut_version
98 print(
"LUT version: %s" % args.lut_version)
100 rootgrp.date_created = create_date
101 rootgrp.product_name = outputFilename
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'
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'
116 for latband
in range (0,numberOfLatbands):
117 latbandList.append(latband)
118 b = bounds[bounds.index.isin(latbandList)].values
121 coef[:] = coefficients.values
128 if __name__ ==
'__main__':