source: CMIP6dreq/trunk/dreqPy/utilities.py @ 489

Subversion URL: http://proj.badc.rl.ac.uk/svn/exarch/CMIP6dreq/trunk/dreqPy/utilities.py@489
Revision 489, 4.8 KB checked in by mjuckes, 5 years ago (diff)

test

Line 
1
2class cmvFilter(object):
3  """Class to filter list of CMOR variables by rank.
4     dq: data request object, as returned by dreq.loadDreq()"""
5
6  def __init__(self,sc,dq=None):
7    self.sc = sc
8    if dq == None:
9      self.dq = self.sc.dq
10    else:
11      self.dq = dq
12
13    self.defaultPriority = 3
14
15  def __test__(self,silentFail=False,silentPass=True):
16    """Test the class against default request document.
17       Returns True if test is passed, False otherwise.
18        silentFail: return without printing a message if test fails.
19        silentPass: return without printing a message if test passes.
20    """
21    cmv = self.filterByChoiceRank()
22    self.cmv = cmv
23    vidt = [i.uid for i in self.dq.coll['var'].items if i.label == 'ta']
24    if len(vidt) != 1:
25      if not silentFail:
26        print ('FAIL: could not identify ta in "var" section' )
27      return False
28
29    vidt = vidt[0]
30    taf = [i for i in cmv if self.dq.inx.uid[i].vid == vidt and self.dq.inx.uid[i].frequency in ['3hr','6hr']]
31    tau = [i for i in self.sc.allVars if self.dq.inx.uid[i].vid == vidt and self.dq.inx.uid[i].frequency in ['3hr','6hr']]
32##
33## NB not easy to separate pressure level from model level variables here
34## at 01.beta.06 do not filter CMIP5 6hrPlev and 27 level HighResMIP ....
35##
36    if len(taf) == 5 and len(tau) == 6:
37      if not silentPass:
38        print ('OK: tests passed')
39      return True
40    else:
41      if not silentFail:
42        print ( 'FAIL: tests failed: len filtered=%s, unfiltered=%s' % (len(taf),len(tau)) )
43      return False
44
45  def filterByChoiceCfg(self,cmv=None,cfg={}):
46    """Filter a set of CMOR variable identifiers by configuration as specified in varChoiceLinkC section of the data request.
47       cmv: set of CMOR variable identifiers.
48     
49       Returns the filetered set. The items removed are available in self.rejected."""
50##
51## cmv is a set of CMORvar ids
52##
53    if cmv == None:
54      self.sc.volByMip( 'HighResMIP', pmax=self.defaultPriority )
55      cmv = self.sc.allVars.copy()
56
57## set of vids associated with choices
58    s = set()
59    for i in self.dq.coll['varChoiceLinkC'].items:
60      s.add( i.vid )
61
62## set of variables in current selection associated with choices
63    v1 = set( [ u for u in cmv if u in s ] )
64    if len(v1) == 0:
65      ## print 'Nothing to do'
66      return cmv
67
68## set of "rank" choice groups relevant to current selection
69    s1 = set( [i.cid for i in self.dq.coll['varChoiceLinkC'].items if i.vid in v1] )
70
71    self.rejected = set()
72    for s in s1:
73      imr = set()
74##
75## set of choice links in group s which relate to a variable in current selection
76##
77      this = set()
78      for i in self.dq.coll['varChoiceLinkC'].items:
79         if i.vid in v1 and i.cid == s:
80           set.add(i)
81##
82## value of configuration option (defaults to True here).
83##
84      testval = cfg.get( s, True )
85      for i in this:
86          if i.cfg != testval:
87            l1 = len(cmv)
88            cmv.remove( i.vid )
89            if len(cmv) == l1:
90              print ( 'Failed to remove i.vid=%s' % i.vid )
91            self.rejected.add( i.vid )
92          else:
93            imr.add( i )
94      ## print self.dq.inx.uid[s].label, len(this), len(imr)
95
96    return cmv
97  def filterByChoiceRank(self,cmv=None):
98    """Filter a set of CMOR variable identifiers by rank as specified in varChoiceLinkR section of the data request.
99       cmv: set of CMOR variable identifiers.
100     
101       Returns the filetered set. The items removed are available in self.rejected."""
102##
103## cmv is a set of CMORvar ids
104##
105    if cmv == None:
106      self.sc.volByMip( 'HighResMIP', pmax=self.defaultPriority )
107      cmv = self.sc.allVars.copy()
108
109## set of vids associated with choices
110    s = set( [i.vid for i in self.dq.coll['varChoiceLinkR'].items] )
111
112## set of variables in current selection associated with choices
113    v1 = set( [ u for u in cmv if u in s ] )
114    if len(v1) == 0:
115      ## print 'Nothing to do'
116      return cmv
117
118## set of "rank" choice groups relevant to current selection
119    s1 = set( [i.cid for i in self.dq.coll['varChoiceLinkR'].items if i.vid in v1] )
120
121    self.rejected = set()
122    for s in s1:
123      imr = set()
124##
125## set of choice links in group s which relate to a variable in current selection
126##
127      this = set( [i for i in self.dq.coll['varChoiceLinkR'].items if i.vid in v1 and i.cid == s] )
128      if len(this) > 1:
129        mr = max( [i.rank for i in this ] )
130        for i in this:
131          if i.rank < mr:
132            l1 = len(cmv)
133            cmv.remove( i.vid )
134            if len(cmv) == l1:
135              print ( 'Failed to remove i.vid=%s' % i.vid )
136            self.rejected.add( i.vid )
137          else:
138            imr.add( i )
139        ## print self.dq.inx.uid[s].label, len(this), len(imr), mr
140      else:
141        ## print self.dq.inx.uid[s].label, len(this)
142        pass
143
144    return cmv
145
146test = cmvFilter
Note: See TracBrowser for help on using the repository browser.