tfake-inputtobed.py - pism - [fork] customized build of PISM, the parallel ice sheet model (tillflux branch)
(HTM) git clone git://src.adamsgaard.dk/pism
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) LICENSE
---
tfake-inputtobed.py (1450B)
---
1 #!/usr/bin/env python3
2
3 from PISMNC import PISMDataset as PNC
4 import numpy as np
5 from sys import exit
6
7 # Import all necessary modules here so that if it fails, it fails early.
8 try:
9 import netCDF4 as NC
10 except:
11 print("netCDF4 is not installed!")
12 sys.exit(1)
13
14 inname = "pismnbreen.nc"
15 outname = "fakesummerevent.nc"
16
17 try:
18 innc = NC.Dataset(inname, 'r')
19 except:
20 print("file %s not found" % inname)
21 exit(1)
22
23 try:
24 nc = PNC(outname, 'w', format='NETCDF3_CLASSIC')
25 except:
26 print("can't open file %s for writing" % outname)
27 exit(1)
28
29
30 def get(name):
31 global innc
32 return np.squeeze(innc.variables[name][:])
33
34
35 x = get('x')
36 y = get('y')
37 bmelt = get('basal_melt_rate_grounded')
38 Mx = len(x)
39 My = len(y)
40 zero = np.zeros((My, Mx))
41
42 nc.create_dimensions(x, y, time_dependent=True, use_time_bounds=True)
43
44
45 def drainage(t):
46 """time-dependence of bogus summer runoff event in m/a: a positive wavepacket"""
47 return np.exp(-(t - 180.0) ** 2 / 80.0) * 20.0 * (np.cos(0.2 * t * 2 * 3.14159) + 1.0)
48
49
50 year = 2012
51 nc.variables['time'].units = "days since %d-1-1" % (year)
52
53 # generate space-time bogus summer runoff event; mask where bmelt > 0
54 for a in range(1, 366):
55 nc.append_time(a, (a, a + 1))
56 inputthisday = (zero + drainage(np.double(a))) * (bmelt > 0)
57 nc.write("inputtobed", inputthisday, True)
58
59 # Set attributes
60 inputtobed = nc.variables["inputtobed"]
61 inputtobed.units = "m / year"
62
63 nc.close()
64 innc.close()