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

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

updates for python 3

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 = {i.vid for i in self.dq.coll['varChoiceLinkC'].items}
59
60## set of variables in current selection associated with choices
61    v1 = { u for u in cmv if u in s }
62    if len(v1) == 0:
63      ## print 'Nothing to do'
64      return cmv
65
66## set of "rank" choice groups relevant to current selection
67    s1 = {i.cid for i in self.dq.coll['varChoiceLinkC'].items if i.vid in v1}
68
69    self.rejected = set()
70    for s in s1:
71      imr = set()
72##
73## set of choice links in group s which relate to a variable in current selection
74##
75      this = {i for i in self.dq.coll['varChoiceLinkC'].items if i.vid in v1 and i.cid == s}
76##
77## value of configuration option (defaults to True here).
78##
79      testval = cfg.get( s, True )
80      for i in this:
81          if i.cfg != testval:
82            l1 = len(cmv)
83            cmv.remove( i.vid )
84            if len(cmv) == l1:
85              print ( 'Failed to remove i.vid=%s' % i.vid )
86            self.rejected.add( i.vid )
87          else:
88            imr.add( i )
89      ## print self.dq.inx.uid[s].label, len(this), len(imr)
90
91    return cmv
92  def filterByChoiceRank(self,cmv=None):
93    """Filter a set of CMOR variable identifiers by rank as specified in varChoiceLinkR section of the data request.
94       cmv: set of CMOR variable identifiers.
95     
96       Returns the filetered set. The items removed are available in self.rejected."""
97##
98## cmv is a set of CMORvar ids
99##
100    if cmv == None:
101      self.sc.volByMip( 'HighResMIP', pmax=self.defaultPriority )
102      cmv = self.sc.allVars.copy()
103
104## set of vids associated with choices
105    s = {i.vid for i in self.dq.coll['varChoiceLinkR'].items}
106
107## set of variables in current selection associated with choices
108    v1 = { u for u in cmv if u in s }
109    if len(v1) == 0:
110      ## print 'Nothing to do'
111      return cmv
112
113## set of "rank" choice groups relevant to current selection
114    s1 = {i.cid for i in self.dq.coll['varChoiceLinkR'].items if i.vid in v1}
115
116    self.rejected = set()
117    for s in s1:
118      imr = set()
119##
120## set of choice links in group s which relate to a variable in current selection
121##
122      this = {i for i in self.dq.coll['varChoiceLinkR'].items if i.vid in v1 and i.cid == s}
123      if len(this) > 1:
124        mr = max( [i.rank for i in this ] )
125        for i in this:
126          if i.rank < mr:
127            l1 = len(cmv)
128            cmv.remove( i.vid )
129            if len(cmv) == l1:
130              print ( 'Failed to remove i.vid=%s' % i.vid )
131            self.rejected.add( i.vid )
132          else:
133            imr.add( i )
134        ## print self.dq.inx.uid[s].label, len(this), len(imr), mr
135      else:
136        ## print self.dq.inx.uid[s].label, len(this)
137        pass
138
139    return cmv
140
141test = cmvFilter
Note: See TracBrowser for help on using the repository browser.