FV3 Bundle
L95Odb.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 
18 class L95Odb:
19 
20  #=============================================================================
21  # Construct the odb object, read the observations and store them in a map
22  #=============================================================================
23  def __init__(self, odb_file):
24  self.datas = []
25  infile = open(odb_file, "r")
26  nbCols = int(infile.readline())
27  columns = []
28  for i in range(0, nbCols):
29  columns.append(infile.readline().strip())
30  nbRows = int(infile.readline())
31  for i in range(0, nbRows):
32  row = {}
33  col = infile.readline().split();
34  row["stringdate"] = col[1]
35  d = parser.parse(col[1])
36  row["date"] = d - timedelta(minutes=1)
37  row["x"] = col[2]
38  for j in range(0, nbCols) :
39  row[columns[j]] = col[j + 3]
40  self.datas.append(row)
41 
42  #=============================================================================
43  # get the observations at a date
44  #=============================================================================
45  def get(self, date):
46  obs = []
47  for o in self.datas:
48  if o["date"] == date:
49  obs.append(o)
50  return obs
51 
52  #=============================================================================
53  # get the observations at a date
54  #=============================================================================
55  def getXYAt(self, date, colName):
56  obs = {}
57  obs['x'] = []
58  obs['y'] = []
59  for o in self.datas:
60  if o["date"] == date:
61  obs['x'].append(float(o["x"]))
62  obs['y'].append(o[colName])
63  return obs
64 
65  #=============================================================================
66  # get the observations at a date
67  #=============================================================================
68  def getAt(self, stringdate):
69  date = parser.parse(stringdate)
70  return self.get(date)
71 
def getXYAt(self, date, colName)
Definition: L95Odb.py:55
def getAt(self, stringdate)
Definition: L95Odb.py:68
def get(self, date)
Definition: L95Odb.py:45
def __init__(self, odb_file)
Definition: L95Odb.py:23