|
1 | 1 | #!/usr/bin/env python |
2 | 2 |
|
3 | 3 | import numpy as np |
4 | | -from netCDF4 import Dataset as NC |
5 | 4 |
|
6 | 5 | # PDD model class |
7 | 6 |
|
@@ -56,12 +55,38 @@ def smb(self, snow, pdd): |
56 | 55 | return np.where(snow_acc > 0, snow_acc, snow*self.pdd_refreeze |
57 | 56 | + snow_acc*self.pdd_factor_ice/self.pdd_factor_snow) |
58 | 57 |
|
| 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 | + |
59 | 83 | # netCDF IO |
60 | 84 |
|
61 | 85 | def init(): |
62 | 86 | """Create an artificial PISM atmosphere file""" |
63 | 87 |
|
64 | 88 | from math import cos, pi |
| 89 | + from netCDF4 import Dataset as NC |
65 | 90 |
|
66 | 91 | # open netcdf file |
67 | 92 | nc = NC('atm.nc', 'w') |
@@ -93,43 +118,16 @@ def init(): |
93 | 118 | # close netcdf file |
94 | 119 | nc.close() |
95 | 120 |
|
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 | | - |
126 | 121 | # Called at execution |
127 | 122 |
|
128 | 123 | if __name__ == "__main__": |
129 | 124 |
|
130 | 125 | # prepare dummy input dataset |
131 | 126 | init() |
132 | 127 |
|
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') |
135 | 133 |
|
0 commit comments