tlistener.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 --- tlistener.py (2448B) --- 1 # Copyright (C) 2011, 2012, 2015, 2018 David Maxwell 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 """Contains the abstract base class PlotListener for listeners that 20 create plots of vectors at each iteration. 21 22 """ 23 24 import PISM.logging 25 import PISM.vec 26 27 def pauseListener(*args): 28 """Listener that temporarily halts operation at each iteration waiting for a key press.""" 29 PISM.logging.pause() 30 31 class PlotListener(object): 32 33 """Base class for listeners that create plots of vectors at each iteration. 34 Provides objects for converting :cpp:class:`IceModelVec`'s to ``numpy`` vectors 35 on processor zero, as well as basic ``matplotlib`` figure management.""" 36 37 def __init__(self, grid): 38 self.grid = grid 39 self.figs = {} 40 41 def toproczero(self, *args): 42 """Returns a ``numpy`` vector on processor zero corresponding to an :cpp:class:`IceModelVec`. 43 Takes as input either a single :cpp:class:`IceModelVec` or dictionary of such 44 vectors and the name of an entry. Returns ``None`` on other processors.""" 45 if len(args) == 2: 46 data = args[0] 47 name = args[1] 48 v = data[name] 49 else: 50 v = args[0] 51 if v is None: 52 return None 53 54 return v.numpy() 55 56 def figure(self, name='default'): 57 """Returns a ``matplotlib`` figure based on a string name. If the instance has not yet 58 created a figure with the given name, a new figure is created and associated with the given name.""" 59 fig = self.figs.get(name) 60 if fig is None: 61 import matplotlib.pyplot as pp 62 fig = pp.figure() 63 self.figs[name] = fig 64 return fig.number 65 66 def __call__(self, solver, itr, data): 67 raise NotImplementedError()