-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcopyRaxpolAttsDimsVarsToNewFile.py
executable file
·65 lines (55 loc) · 2.79 KB
/
copyRaxpolAttsDimsVarsToNewFile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/python3
import netCDF4 as nc
import numpy as np
def copyRaxpolAttsDimsVarsToNewFile(inFile,outFile,excludedAttrs,fields,field,firstFile):
if firstFile:
with nc.Dataset(inFile) as src, nc.Dataset(outFile, "w") as dst:
# copy global attributes
for name in src.ncattrs():
#print(name)
if name == 'MissingData':
fillValue = src.getncattr(name)
if name not in excludedAttrs:
dst.setncattr(name, src.getncattr(name))
# copy dimensions from src
for dimName, dimValue in src.dimensions.items():
#print(dimName, len(dimValue))
dst.createDimension(dimName, len(dimValue) if not dimValue.isunlimited() else None)
# copy variables
for varName, varIn in src.variables.items():
#print(varName)
#print(varIn)
fill_value = fillValue
if hasattr(varIn,'_FillValue'):
fill_value = varIn._FillValue
if varName == fields[field]['inName']:
outVar = dst.createVariable(fields[field]['outName'],varIn.datatype,
varIn.dimensions,fill_value=fill_value)
else:
outVar = dst.createVariable(varName,varIn.datatype,
varIn.dimensions,fill_value=fill_value)
#print(varIn.datatype)
#print(varIn.ncattrs())
outVar.setncatts({k: varIn.getncattr(k) for k in varIn.ncattrs() if k not in ['_FillValue']})
outVar[:] = varIn[:]
else:
with nc.Dataset(inFile) as src, nc.Dataset(outFile, "a") as dst:
# get MissingData global attribute
for name in src.ncattrs():
#print(name)
if name == 'MissingData':
fillValue = src.getncattr(name)
# copy only polarimetric variable
for varName, varIn in src.variables.items():
#print(varName)
#print(fields[field]['inName'])
if varName == fields[field]['inName']:
fill_value = fillValue
if hasattr(varIn,'_FillValue'):
fill_value = varIn._FillValue
outVar = dst.createVariable(fields[field]['outName'],varIn.datatype,
varIn.dimensions,fill_value=fill_value)
#print(varIn.datatype)
#print(varIn.ncattrs())
outVar.setncatts({k: varIn.getncattr(k) for k in varIn.ncattrs() if k not in ['_FillValue']})
outVar[:] = varIn[:]