FV3 Bundle
Browser.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # (C) Copyright 2009-2016 ECMWF.
3 #
4 # This software is licensed under the terms of the Apache Licence Version 2.0
5 # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 # In applying this licence, ECMWF does not waive the privileges and immunities
7 # granted to it by virtue of its status as an intergovernmental organisation nor
8 # does it submit to any jurisdiction.
9 
10 
11 import glob
12 import os
13 import os.path
14 import random
15 import sys
16 import time
17 from datetime import tzinfo, timedelta, datetime
18 from dateutil import parser
19 import gobject
20 
21 try:
22  import pygtk
23  pygtk.require("2.0")
24 except:
25  pass
26 try:
27  import gtk
28  import gtk.glade
29 except:
30  sys.exit(1)
31 
32 from matplotlib.backends.backend_gtkcairo import FigureCanvasGTKCairo as FigureCanvas
33 from matplotlib.figure import Figure
34 
35 from ModelFactory import ModelFactory
36 from Experiment import Experiment
37 
38 class Browser:
39 
40  #===========================================================================
41  # __init__
42  # Constructor:
43  # Load XML which describes the widgets tree, connects signals and
44  # initializes combo-boxes
45  #===========================================================================
46  def __init__(self, factory, expDir, fdbDir):
47  # Initialize
48  self.readyForUpdate = False
49  self.plt = None
50  self.background = None
51  self.fdbdir = fdbDir
52  self.expdir = expDir
53  self.model = factory.id()
54  ModelFactory.addFactory(factory)
55 
56  # Build the GUI
57  gladefile = __file__.replace(".py", ".xml")
58  self.wTree = gtk.glade.XML(gladefile)
59  dic = { "on_quit_clicked" : self.onQuitClicked,
60  "on_png_clicked" : self.onPngClicked,
61  "onSelectExpe": self.onSelectExpe,
62  "onSelectDate": self.onSelectDate,
63  "onSelectTerm": self.onSelectTerm,
64  "onToggleAnim": self.onToggleAnim,
65  }
66  self.wTree.signal_autoconnect(dic)
67  window = self.wTree.get_widget("window")
68  window.set_title(ModelFactory.title(self.model) + " experiment's browser")
69  window.connect("destroy", self.onQuitClicked)
70  self.animToggle = self.wTree.get_widget("toggleAnim")
71 
72  # Initialize the experience's list
73  self.nDates = 1
74  self.nTerms = 1
75 
76  # Fill in experiment's menu
77  comboexpe = self.wTree.get_widget("comboexpe")
79  self.fdb = ModelFactory.createFdb(self.model, self.fdbdir)
80  comboexpe.remove_text(0)
81  for expe in self.experiments.getIds():
82  comboexpe.append_text(expe)
83 
84  # Build a canvas for plotting
85  self.plt = ModelFactory.createPlot(self.model)
86  self.canvas = FigureCanvas(self.plt.getFigure())
87  self.box = self.wTree.get_widget("eventbox1")
88  self.box.add(self.canvas)
89  self.canvas.show()
90 
91  # Open the GUI
92  self.readyForUpdate = True
93  comboexpe.set_active(0)
94  gtk.main()
95 
96 
97  #===========================================================================
98  # onQuitClicked:
99  # Quit the application
100  #===========================================================================
101  def onQuitClicked(self, widget):
102  sys.exit()
103 
104  #===========================================================================
105  # onPngClicked:
106  # Save current plot in a PNG file
107  #===========================================================================
108  def onPngClicked(self, widget):
109  try:
110  now = time.mktime(time.localtime())
111  strNow = time.strftime("%Y-%m-%d-%H:%M:%S", time.gmtime(now))
112  pngFile = os.environ["HOME"] + '/Images/' + strNow + '.png'
113  self.plt.getFigure().savefig(pngFile)
114  except ValueError, Argument:
115  print "Error in export pdf:", Argument
116 
117 
118  #===========================================================================
119  # onToggleAnim:
120  #===========================================================================
121  def anim(self):
122  radio = self.wTree.get_widget("animRefTime")
123  if radio.get_active() is True:
124  combo = self.wTree.get_widget("combodate")
125  n = self.nDates
126  else:
127  combo = self.wTree.get_widget("comboterm")
128  n = self.nTerms
129 
130  if n > 1:
131  num = combo.get_active()
132  combo.set_active(num + 1)
133  return self.animToggle.get_active()
134 
135  def onToggleAnim(self, widget):
136  if widget.get_active() is True:
137  self.timer = gobject.timeout_add(1000, self.anim)
138 
139  #===========================================================================
140  # onSelectExpe:
141  # Select experiment in menu
142  #===========================================================================
143  def onSelectExpe(self, widget):
144  if not self.readyForUpdate:
145  return
146  try:
147  # Remove the date's and term's lists
148  self.readyForUpdate = False
149  combodate = self.wTree.get_widget("combodate")
150  oldDate = combodate.get_active_text()
151  for i in range(0, self.nDates):
152  combodate.remove_text(0)
153  self.nDates = 0
154 
155  # Read observations's file
156  self.idExpe = widget.get_active_text()
157  if self.idExpe is None:
158  self.abort()
159  return
160 
161  self.odb = None
162  obsFile = self.experiments.getObsFile(self.idExpe)
163 
164  if obsFile is not None:
165  self.odb = ModelFactory.createOdb(self.model, self.fdbdir + '/' + os.path.split(obsFile)[1])
166  self.output = self.experiments.getOutputFile(self.idExpe)
167 
168  # Fill in date's menu
169  numOldDate = 0
170  for d in self.fdb.getTimeList(self.output):
171  combodate.append_text(d)
172  if d == oldDate:
173  numOldDate = self.nDates
174  self.nDates += 1
175  self.readyForUpdate = True
176  combodate.set_active(numOldDate)
177  except ValueError, Argument:
178  self.abort()
179  print "Error fired here !!", Argument
180 
181  #===========================================================================
182  # onSelectDate:
183  # Select date in menu
184  #===========================================================================
185  def onSelectDate(self, widget):
186  if not self.readyForUpdate:
187  return
188  try:
189  # Remove term's menu
190  self.readyForUpdate = False
191  comboexpe = self.wTree.get_widget("comboexpe")
192  comboterm = self.wTree.get_widget("comboterm")
193  oldTerm = comboterm.get_active_text()
194 
195  for i in range(0, self.nTerms):
196  comboterm.remove_text(0)
197  self.nTerms = 0
198 
199  date = widget.get_active_text()
200  if date is None:
201  self.abort()
202  return
203 
204  # Fill in the term's menu
205  mData = self.fdb.getMetaData()
206  arr = mData[self.output][date]
207  numOldTerm = 0
208  if len(arr) == 1:
209  comboterm.append_text("/")
210  self.nTerms += 1
211  else:
212  for t in arr:
213  comboterm.append_text(t)
214  if t == oldTerm:
215  numOldTerm = self.nTerms
216  self.nTerms += 1
217  self.readyForUpdate = True
218  comboterm.set_active(numOldTerm)
219 
220  except ValueError, Argument:
221  self.abort()
222  print "Error fired here !!", Argument
223 
224  #===========================================================================
225  # onSelectTerm:
226  # handles the event when a term is chosen, updates the choice of
227  # the background and orders repainting plots.
228  #===========================================================================
229  def onSelectTerm(self, widget):
230  if not self.readyForUpdate:
231  return
232  self.expe = None
233  try:
234  self.readyForUpdate = False
235  comboexpe = self.wTree.get_widget("comboexpe")
236  combodate = self.wTree.get_widget("combodate")
237  if widget.get_active_text() is None:
238  self.abort()
239  return
240  if comboexpe.get_active_text() is None:
241  self.abort()
242  return
243  if combodate.get_active_text() is None:
244  self.abort()
245  return
246 
247  self.termTitle = ''
248  if widget.get_active_text() == "/":
249  expe = self.output + "." + combodate.get_active_text()
250  else:
251  self.termTitle = widget.get_active_text()
252  expe = self.output + "." + combodate.get_active_text() + "." + widget.get_active_text()
253  self.expeData = self.fdb.map[expe]
254 
255  bg = self.experiments.getBackground(self.idExpe)
256  self.background = self.fdb.getExpeAt(bg['expe'], bg['refTime'], self.expeData["validTime"])
257  self.readyForUpdate = True
258  self.update()
259 
260  except ValueError, Argument:
261  self.abort()
262  print "Error fired here !!", Argument
263 
264 
265  #===========================================================================
266  # abort:
267  # Abort current selection
268  #===========================================================================
269  def abort(self):
270  self.readyForUpdate = True
271  self.expeData = None
272  self.update()
273 
274  #===========================================================================
275  # update:
276  # Updates datas in the plot according to the choices done in the GUI.
277  #===========================================================================
278  def update(self):
279  self.plt.clear()
280  self.plot()
281  self.canvas.draw()
282 
283  #===========================================================================
284  # plot:
285  # Plot data on the canvas
286  #===========================================================================
287  def plot(self):
288  if self.expeData is None:
289  return
290 
291  obsData = None
292  if self.odb is not None:
293  obsData = self.odb.getXYAt(self.expeData["validTime"], "ObsVal")
294  expeData = self.fdb.getData(self.expeData)
295 
296  bgData = None
297  if self.background is not None:
298  bgData = self.fdb.getData(self.background)
299 
300  title = "Experiment: " + self.idExpe + " at " + self.expeData["referenceTime"] + ' ' + self.termTitle
301  self.plt.setPlot(title, expeData, bgData, obsData)
def abort(self)
Definition: Browser.py:269
def onPngClicked(self, widget)
Definition: Browser.py:108
def onSelectDate(self, widget)
Definition: Browser.py:185
subroutine, public add(value, cumul, num, wgt)
Definition: tools_func.F90:185
def onToggleAnim(self, widget)
Definition: Browser.py:135
def update(self)
Definition: Browser.py:278
def plot(self)
Definition: Browser.py:287
def __init__(self, factory, expDir, fdbDir)
Definition: Browser.py:46
def onSelectTerm(self, widget)
Definition: Browser.py:229
def onQuitClicked(self, widget)
Definition: Browser.py:101
def anim(self)
Definition: Browser.py:121
def onSelectExpe(self, widget)
Definition: Browser.py:143