1 from copy
import deepcopy
3 from datetime
import datetime, timedelta
4 from netCDF4
import Dataset
10 ncWriteFormat =
'NETCDF3_64BIT_OFFSET'
12 fcHours = os.getenv(
'fcHours',
'0')
13 intervalHours = os.getenv(
'intervalHours',
'6')
14 fcNums = os.getenv(
'fcNums',
'1')
15 initDate = os.getenv(
'start_init',
'2018041500')
16 endDate = os.getenv(
'end_init',
'2018051400')
17 diff2exp = os.getenv(
'diff2exp',
'False')
18 expDirectory = os.getenv(
'TOP_DIR',
'/glade/scratch/$user/pandac/')
20 GFSANA_DIR = os.getenv(
'GFSANA_DIR',
'Please link GFSANA_DIR')
21 expLongNames = os.getenv(
'expLongNames',
'please set expLongNames')
22 expNames = os.getenv(
'expNames',
'please set expNames')
24 EXP_DIR1 = os.getenv(
'FCDIAG_WORK_DIR1',
'FC1DIAG DIR OR FC2DIAG DIR FOR CONTROL')
25 EXP_DIR2 = os.getenv(
'FCDIAG_WORK_DIR2',
'FC1DIAG DIR OR FC2DIAG DIR FOR CURRENT/target exp')
26 exp1Name = os.getenv(
'exp1Name',
'name for control expt')
27 exp2Name = os.getenv(
'exp2Name',
'name for current/target expt')
29 aggregatableFileStats = [
'RMS',
'Mean']
30 allFileStats = aggregatableFileStats
32 varNames2d = [
't2m',
'surface_pressure',
'q2',
'u10',
'v10']
33 varNames3d = [
'theta',
'temperature',
'rho',
'pressure',
'uReconstructZonal',
'uReconstructMeridional',
'qv',
'w']
34 varNames = varNames2d + varNames3d
35 latBands = [
'NXTro',
'Tro',
'SXTro']
36 latBandsBounds = [90.0, 30.0, -30.0, -90.0]
38 inflationVariables = [
41 'uReconstructMeridional',
56 'templateVar':
'2D-c-c',
58 'long_name':
'temperature',
60 'uReconstructZonal': {
61 'templateVar':
'2D-c-c',
63 'long_name':
'Zonal component of reconstructed horizontal velocity at cell centers',
65 'uReconstructMeridional': {
66 'templateVar':
'2D-c-c',
68 'long_name':
'Meridional component of reconstructed horizontal velocity at cell centers',
71 'templateVar':
'2D-c-c',
72 'units':
'kg kg^{-1}',
73 'long_name':
'Specific humidity',
76 'templateVar':
'1D-c',
78 'long_name':
'Diagnosed surface pressure',
81 'templateVar':
'2D-c-c',
82 'units':
'kg kg^{-1}',
83 'long_name':
'Cloud water mixing ratio',
86 'templateVar':
'2D-c-c',
87 'units':
'kg kg^{-1}',
88 'long_name':
'Ice mixing ratio',
91 'templateVar':
'2D-c-c',
92 'units':
'kg kg^{-1}',
93 'long_name':
'Rain mixing ratio',
96 'templateVar':
'2D-c-c',
97 'units':
'kg kg^{-1}',
98 'long_name':
'Snow mixing ratio',
101 'templateVar':
'2D-c-c',
102 'units':
'kg kg^{-1}',
103 'long_name':
'Graupel mixing ratio',
106 'templateVar':
'2D-c-c',
107 'units':
'kg kg^{-1}',
108 'long_name':
'Water vapor mixing ratio',
137 templateVariables = {
138 '1D-c':
'surface_pressure',
152 fcRange = int(fcHours)/24.
153 interval = int(intervalHours)/24
154 SDATE = datetime.strptime(initDate,
'%Y%m%d%H')
155 EDATE = datetime.strptime(endDate,
'%Y%m%d%H')
157 DATEINC = dt.timedelta(days=interval)
158 expLongNames = expLongNames.split()
159 expNames = expNames.split()
162 def getGridFile(date = initDate, gfsAnaDir = GFSANA_DIR, nCells = 40962):
165 d = datetime.strptime(date,
'%Y%m%d%H')
166 filedate= d.strftime(
'%Y-%m-%d_%H.%M.%S')
168 return gfsAnaDir+
'/x1.'+str(nCells)+
'.init.'+filedate+
'.nc'
170 def readGrid(date=initDate, gridFile=None, returnR=False):
172 ncData = Dataset(gridFile,
'r')
173 lats = np.array( ncData.variables[
'latCell'][:] ) * 180.0 / np.pi
174 lons = np.array( ncData.variables[
'lonCell'][:] ) * 180.0 / np.pi
175 R = ncData.__dict__[
'sphere_radius']
179 return (lats, lons, R)
184 ncData = Dataset(ncFile,
'r')
186 return (varName
in ncData.variables)
189 src = Dataset(ncFile,
'r')
190 dims = deepcopy(src.variables[varName].dimensions)
195 src = Dataset(ncFile,
'r')
196 attrs = deepcopy(src[varName].__dict__)
201 src = Dataset(ncFile,
'r')
202 datatype = src.variables[varName].datatype
207 pressure_p = np.array( ncData.variables[
'pressure_p'][0,:,:] )
208 pressure_base = np.array( ncData.variables[
'pressure_base'][0,:,:] )
209 pressure = pressure_p + pressure_base
213 ncData = Dataset(ncFile,
'r')
217 theta = np.array(ncData.variables[
'theta'][0,:,:])
218 tmp1 = (1.e5 / pressure)**(rgas / cp)
219 temperature = np.subtract(np.divide(theta, tmp1), 273.15)
226 if (varName ==
'temperature'):
229 ncData = Dataset(ncFile,
'r')
230 if (varName ==
'pressure'):
232 elif varName
in varNames3d:
233 varVals = np.array( ncData.variables[varName][0,:,:] )
235 varVals = np.array( ncData.variables[varName][0,:] )
237 if (varName ==
'qv' or varName ==
'rho' or varName ==
'q2'):
238 varVals = varVals * 1000.
248 assert os.path.exists(ncFile),
'Only appending to existing file is enabled in varWrite!'
250 dst = Dataset(ncFile,
'a', format=ncWriteFormat)
251 nDims = len(varVals.shape)
253 if not hasVar(varName, ncFile):
254 dst.createVariable(varName, datatype, dims)
264 dst[varName].setncatts(varAttrs)
268 dst[varName][0,:] = np.asarray(varVals[:], dtype=datatype)
270 dst[varName][0,:,:] = np.asarray(varVals[:,:], dtype=datatype)
273 dst[varName][:] = np.asarray(varVals[:], dtype=datatype)
275 dst[varName][:,:] = np.asarray(varVals[:,:], dtype=datatype)
280 assert os.path.exists(infile),
'infile must exist!'
281 src = Dataset(infile,
'r')
282 dst = Dataset(outfile,
'w', format=ncWriteFormat)
285 dstatts = deepcopy(src.__dict__)
287 d = datetime.strptime(date,
'%Y%m%d%H')
288 confdate = d.strftime(
'%Y-%m-%d_%H:%M:%S')
289 dstatts[
'config_start_time'] = confdate
290 dstatts[
'config_stop_time'] = confdate
291 dst.setncatts(dstatts)
294 for name, dimension
in src.dimensions.items():
296 name, (len(dimension)
if not dimension.isunlimited()
else None))
298 requiredMetaVars = [
'xtime']
299 for varname
in requiredMetaVars:
300 srcvar = src.variables[varname]
308 dst[varname].setncatts(src[varname].__dict__)
311 if varname ==
'xtime':
312 xtime_ = src[varname][0][:]
313 xtime = nc.stringtoarr(confdate, len(src[varname][0][:]))
314 dst[varname][0] = xtime
316 if 'Time' in srcvar.dimensions:
317 dst[varname][0,:] = src[varname][0,:]
319 dst[varname][:] = src[varname][:]
325 var1 =
varRead(varName, ncFile1)
326 var2 =
varRead(varName, ncFile2)
330 print (
'This is not a runnable program.')
333 if __name__ ==
'__main__':
main()
def varAttrs(varName, ncFile)
def hasVar(varName, ncFile)
def readGrid(date=initDate, gridFile=None, returnR=False)
def getGridFile(date=initDate, gfsAnaDir=GFSANA_DIR, nCells=40962)
def varDims(varName, ncFile)
def getTemperature(ncFile)
def varRead(varName, ncFile)
def varDatatype(varName, ncFile)
def createHeaderOnlyFile(infile, outfile, date)
def varDiff(varName, ncFile1, ncFile2)
def varWrite(varName, varVals, ncFile, varAttrs, dims, datatype)