tutil.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
---
tutil.py (3290B)
---
1 # Copyright (C) 2011, 2012, 2013, 2015, 2016, 2018, 2019 David Maxwell and Constantine Khroulev
2 #
3 # This file is part of PISM.
4 #
5 # PISM is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # PISM is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with PISM; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19 """Utility functions/objects that don't have a good home elsewhere."""
20
21 import PISM
22 import time
23 import sys
24
25 def convert(value, u1, u2):
26 "Convert value from units u1 to u2."
27 unit_system = PISM.Context().unit_system
28 return PISM.convert(unit_system, value, u1, u2)
29
30 def prepare_output(filename, append_time=True):
31 "Create an output file and prepare it for writing."
32 ctx = PISM.Context()
33 output = PISM.File(ctx.com,
34 filename,
35 PISM.string_to_backend(ctx.config.get_string("output.format")),
36 PISM.PISM_READWRITE_MOVE,
37 ctx.ctx.pio_iosys_id())
38 PISM.define_time(output,
39 ctx.config.get_string("time.dimension_name"),
40 ctx.config.get_string("time.calendar"),
41 ctx.time.units_string(),
42 ctx.unit_system)
43 if append_time:
44 PISM.append_time(output,
45 ctx.config.get_string("time.dimension_name"),
46 ctx.time.current())
47
48 return output
49
50 def writeProvenance(outfile, message=None):
51 """Saves the time and command line arguments (or the provided `message`) to
52 the ``history`` attribute of the :file:`.nc` file `outfile`"""
53
54 com = PISM.Context().com
55
56 ds = PISM.File(com, outfile, PISM.PISM_NETCDF3, PISM.PISM_READWRITE)
57
58 if message is None:
59 message = PISM.timestamp(com) + ": " + PISM.args_string()
60 ds.append_history(message)
61 ds.write_attribute("PISM_GLOBAL", "source", "PISM " + PISM.revision)
62
63 ds.close()
64
65
66 def fileHasVariable(filename, varname):
67 """Returns ``True`` if the :file:`.nc` file `filename` contains an variable named `varname`."""
68
69 return PISM.File(PISM.Context().com, filename,
70 PISM.PISM_NETCDF3, PISM.PISM_READONLY).find_variable(varname)
71
72 # The following was copied from matplotlib, which copied a python recipe.
73
74
75 class Bunch(dict):
76
77 """
78 Often we want to just collect a bunch of stuff together, naming each
79 item of the bunch; a dictionary's OK for that, but a small do- nothing
80 class is even handier, and prettier to use. Whenever you want to
81 group a few variables:
82
83 >>> point = Bunch(datum=2, squared=4, coord=12)
84 >>> point.datum
85 By: Alex Martelli
86 From: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52308"""
87
88 def __init__(self,**kw):
89 dict.__init__(self,kw)
90 self.__dict__ = self