-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathnetcdf_functions.py
executable file
·45 lines (43 loc) · 1.29 KB
/
netcdf_functions.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
#Python utilities for reading and writing variables to a netcdf file
# using Scientific Python OR scipy, whichever available
def getvar(fname, varname):
usescipy = False
try:
import Scientific.IO.NetCDF as netcdf
except ImportError:
import scipy
from scipy.io import netcdf
usescipy = True
if (usescipy):
nffile = netcdf.netcdf_file(fname,"r",mmap=False)
var = nffile.variables[varname]
varvals = var[:].copy() #works for vector only?
nffile.close()
else:
nffile = netcdf.NetCDFFile(fname,"r")
var = nffile.variables[varname]
varvals = var.getValue()
nffile.close()
return varvals
def putvar(fname, varname, varvals):
usescipy = False
try:
import Scientific.IO.NetCDF as netcdf
except ImportError:
import scipy
from scipy.io import netcdf
usescipy = True
if (usescipy):
nffile = netcdf.netcdf_file(fname,"a",mmap=False)
var = nffile.variables[varname]
if (len(varvals) > 1):
var[:] = varvals
else:
nffile.close()
else:
nffile = netcdf.NetCDFFile(fname,"a")
var = nffile.variables[varname]
var.assignValue(varvals)
nffile.close()
ierr = 0
return ierr