FV3 Bundle
Experiment.py
Go to the documentation of this file.
1 # (C) Copyright 2009-2016 ECMWF.
2 #
3 # This software is licensed under the terms of the Apache Licence Version 2.0
4 # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
5 # In applying this licence, ECMWF does not waive the privileges and immunities
6 # granted to it by virtue of its status as an intergovernmental organisation nor
7 # does it submit to any jurisdiction.
8 
9 #===============================================================================
10 # L950db
11 #
12 # A class to handle the observations used in L95 model
13 #===============================================================================
14 import numpy as np
15 from dateutil import parser
16 from datetime import tzinfo, timedelta, datetime
17 import glob
18 import os
19 import re
20 import os.path
21 import xml.dom.minidom
22 from xml.dom.minidom import Node
23 
24 class Experiment:
25 
26  #=============================================================================
27  # Get some informations from the experiment's files found in the directory
28  #=============================================================================
29  def __init__(self, dir):
30  self.dir = dir
31  self.__map = {}
32  l = glob.glob(dir + '/*.xml')
33  for i in l:
34  filenameSplitted = os.path.split(i)[1].split('.')
35  id = filenameSplitted[0]
36  file = self.getFromXML(i)
37  self.__map[id] = file
38 
39  #=============================================================================
40  # getFromXML
41  #=============================================================================
42  def getFromXML(self, ficXML):
43  ret = {}
44  dom = xml.dom.minidom.parse(ficXML)
45 
46  # Get output filename
47  outputs = dom.getElementsByTagName('output')
48  for output in outputs:
49  ret['datadir'] = output.getElementsByTagName('datadir')[0].firstChild.data
50  ret['exp'] = output.getElementsByTagName('exp')[0].firstChild.data
51  ret['type'] = output.getElementsByTagName('type')[0].firstChild.data
52 
53  # Get background filenames
54  bg = {}
55  backgrounds = dom.getElementsByTagName('background')
56  for background in backgrounds:
57  states = background.getElementsByTagName('state')
58  for state in states:
59  bg['expe'], bg['refTime'] = self.getStateFromXML(state)
60  initials = dom.getElementsByTagName('initial')
61  for initial in initials:
62  bg['expe'], bg['refTime'] = self.getStateFromXML(initial)
63  ret['background'] = bg
64 
65  # Get obs filename
66  obsFiles = dom.getElementsByTagName('ObsFileIn')
67  ret['obsFileIn'] = None
68  for obsFile in obsFiles:
69  ret['obsFileIn'] = obsFile.firstChild.data
70  return ret
71 
72  #=============================================================================
73  # getStateFromXML
74  #=============================================================================
75  def getStateFromXML(self, node):
76  bg = None
77  filename = None
78  try:
79  filename = node.getElementsByTagName('filename')[0].firstChild.data
80  date = node.getElementsByTagName('date')[0].firstChild.data
81  f1 = filename.split('/')
82  f2 = f1[len(f1) - 1]
83  bg = f2.split('.')
84  refTime = parser.parse(bg[2])
85  except:
86  return "xxx", "yyy"
87  return bg[0] + "." + bg[1], refTime
88 
89 
90  #=============================================================================
91  # getIds
92  # Returns an array containing all experiment's Ids
93  #=============================================================================
94  def getIds(self):
95  return self.__map.keys()
96 
97  #=============================================================================
98  # getObsFile
99  #=============================================================================
100  def getObsFile(self, id):
101  return self.__map[id]['obsFileIn']
102 
103  #=============================================================================
104  # getOutputFile
105  # Returns filename without reference time
106  #=============================================================================
107  def getOutputFile(self, id):
108  ret = None
109  try:
110  f = self.__map[id]
111  if "datadir" in f:
112  return f["exp"] + "." + f["type"]
113  except ValueError, Argument:
114  print Argument
115  return ret
116 
117  #=============================================================================
118  # getBackground
119  # Returns the background: an associative array with expe.type and refTime as keys
120  #=============================================================================
121  def getBackground(self, id):
122  return self.__map[id]['background']
123 
def getObsFile(self, id)
Definition: Experiment.py:100
def getBackground(self, id)
Definition: Experiment.py:121
def getOutputFile(self, id)
Definition: Experiment.py:107
def __init__(self, dir)
Definition: Experiment.py:29
def getFromXML(self, ficXML)
Definition: Experiment.py:42
def getStateFromXML(self, node)
Definition: Experiment.py:75