Skip to content

Commit a7d50b0

Browse files
committed
Added NetCDF interface
NetCDF files can be processed with pdd.nc('i.nc', 'o.nc').
1 parent d6b0466 commit a7d50b0

File tree

1 file changed

+31
-33
lines changed

1 file changed

+31
-33
lines changed

pdd.py

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env python
22

33
import numpy as np
4-
from netCDF4 import Dataset as NC
54

65
# PDD model class
76

@@ -56,12 +55,38 @@ def smb(self, snow, pdd):
5655
return np.where(snow_acc > 0, snow_acc, snow*self.pdd_refreeze
5756
+ snow_acc*self.pdd_factor_ice/self.pdd_factor_snow)
5857

58+
def nc(self, i_file, o_file):
59+
"""NetCDF interface"""
60+
61+
from netCDF4 import Dataset as NC
62+
63+
# open netcdf files
64+
i = NC(i_file, 'r')
65+
o = NC(o_file, 'w')
66+
67+
# read input data
68+
temp = i.variables['air_temp'][:]
69+
prec = i.variables['precipitation'][:]
70+
71+
# create dimensions
72+
o.createDimension('x', len(i.dimensions['x']))
73+
o.createDimension('y', len(i.dimensions['y']))
74+
75+
# create variables
76+
smbvar = o.createVariable('smb', 'f4', ('x', 'y'))
77+
smbvar[:] = self(temp, prec)
78+
79+
# close netcdf files
80+
i.close()
81+
o.close()
82+
5983
# netCDF IO
6084

6185
def init():
6286
"""Create an artificial PISM atmosphere file"""
6387

6488
from math import cos, pi
89+
from netCDF4 import Dataset as NC
6590

6691
# open netcdf file
6792
nc = NC('atm.nc', 'w')
@@ -93,43 +118,16 @@ def init():
93118
# close netcdf file
94119
nc.close()
95120

96-
def main():
97-
"""Read atmosphere file and output surface mass balance"""
98-
99-
# open netcdf files
100-
i = NC('atm.nc', 'r')
101-
o = NC('clim.nc', 'w')
102-
103-
# read input data
104-
temp = i.variables['air_temp'][:]
105-
prec = i.variables['precipitation'][:]
106-
107-
# create dimensions
108-
for dimname, dim in i.dimensions.items():
109-
o.createDimension(dimname, len(dim))
110-
111-
# initiate PDD model
112-
pdd=PDDModel()
113-
114-
# create variables
115-
#pddvar = o.createVariable('pdd', 'f4', ('x', 'y'))
116-
#snowvar = o.createVariable('snow', 'f4', ('x', 'y'))
117-
smbvar = o.createVariable('smb', 'f4', ('x', 'y'))
118-
119-
# compute surface mass balance
120-
smbvar[:] = pdd(temp, prec)
121-
122-
# close netcdf files
123-
i.close()
124-
o.close()
125-
126121
# Called at execution
127122

128123
if __name__ == "__main__":
129124

130125
# prepare dummy input dataset
131126
init()
132127

133-
# run the mass balance model
134-
main()
128+
# initiate PDD model
129+
pdd=PDDModel()
130+
131+
# compute surface mass balance
132+
pdd.nc('atm.nc', 'clim.nc')
135133

0 commit comments

Comments
 (0)