source: CCCC/tags/0.1/unitTestsS1.py @ 111

Subversion URL: http://proj.badc.rl.ac.uk/svn/exarch/CCCC/tags/0.1/unitTestsS1.py@283
Revision 111, 6.3 KB checked in by mjuckes, 6 years ago (diff)

more checks and unit tests implemented for SPECS

Line 
1
2
3import logging, time, string
4import utils_c4
5import config_c4 as config
6
7
8#### set up log file ####
9tstring2 = '%4.4i%2.2i%2.2i' % time.gmtime()[0:3]
10testLogFile = '%s__qclog_%s.txt' % ('unitTests',tstring2)
11log = logging.getLogger(testLogFile)
12fHdlr = logging.FileHandler(testLogFile,mode='w')
13fileFormatter = logging.Formatter('%(message)s')
14fHdlr.setFormatter(fileFormatter)
15log.addHandler(fHdlr)
16log.setLevel(logging.INFO)
17
18class dummy:
19
20  def __init__(self):
21     pass
22
23p = dummy()
24ps = dummy()
25p.log = log
26ps.log = log
27p.pcfg = config.projectConfig( "CORDEX" )
28ps.pcfg = config.projectConfig( "SPECS" )
29p.abortMessageCount = -1
30ps.abortMessageCount = -1
31
32
33module = 'checkFileName'
34c = utils_c4.checkFileName(parent=p)
35
36fn = 'ps_AFR-44_ECMWF-ERAINT_evaluation_r1i1p1_SMHI-RCA4_x1_day_20060101-20101231.nc'
37testId = '#01.001'
38c.check( fn )
39if c.errorCount == 0:
40  print 'Passed [%s] %s: valid file name' % (module,fn)
41else:
42  print 'Failed [%s] %s: valid file name' % (module,fn)
43
44testId = '#01.002'
45fn = 'ps_AFR-44_ECMWF-ERAINT_evaluation_r1i1p1_SMHI-RCA4_x1_fx.nc'
46c.check(fn)
47if c.errorCount == 0 and c.isFixed:
48  print 'Passed [%s] %s: valid file name' % (module,fn)
49else:
50  print 'Failed [%s] %s: valid file name' % (module,fn)
51
52testId = '#01.003'
53fn = 'ps_AFR-44_ECMWF-ERAINT_evaluation_r1i1p1_SMHI-RCA4_x1_3hr_2006010100-2010123100.nc'
54c.check(fn)
55if c.errorCount == 0:
56  print 'Passed [%s] %s: valid file name' % (module,fn)
57else:
58  print 'Failed [%s] %s: valid file name' % (module,fn)
59
60testId = '#01.004'
61fn = 'ps_AFR-44_ECMWF-ERAINT_evaluation_r1i1p1_SMHI-RCA4_x1_3hr_200601010030-201012310030.nc'
62c.check(fn)
63if c.errorCount == 0:
64  print 'Passed [%s] %s: valid file name' % (module,fn)
65else:
66  print 'Failed [%s] %s: valid file name' % (module,fn)
67
68testId = '#01.005'
69fn = 'ps_AFR-44_ECMWF-ERAINT_evaluation_r1i1p1_SMHI-RCA4_x1_day_200601010030-201012310030.nc'
70c.check(fn)
71if c.errorCount == 0:
72  print 'Failed to detect bad file name: [%s] %s ' % (module,fn)
73else:
74  print 'OK -- detected bad file name: [%s] %s' % (module,fn)
75
76c = utils_c4.checkStandardDims(parent=p)
77module = 'checkStandardDims'
78c.check( 'tas', 'day', {},{}, False )
79if c.errorCount == 0:
80  print 'Failed [%s]: failed to detect empty dictionaries' % module
81else:
82  print 'OK -- detected error in standard dims'
83
84c = utils_c4.checkByVar(parent=p)
85module = 'checkByVar (regex)'
86c.check( norun=True )
87import re
88r1 = re.compile( c.pats['subd'][0] )
89for x in ['200401010000','2004010100']:
90  m = r1.match( x )
91  if m:
92     print 'OK -- passed [%s] %s for sub-daily data' % (module,x)
93  else:
94     print 'Failed to match correct sub-daily time range element [%s] %s' % (module,x)
95
96for x in ['200401010040','2004010200']:
97  m = r1.match( x )
98  if not m:
99     print 'OK -- correctly failed [%s] %s for sub-daily data' % (module,x)
100  else:
101     print 'Failed to detect bad sub-daily time range element [%s] %s' % (module,x)
102
103r1 = re.compile( c.pats['sem'][0] )
104for x in ['199012','199101']:
105  m = r1.match( x )
106  if m:
107     print 'OK -- passed [%s] %s for seasonal data' % (module,x)
108  else:
109     print 'Failed to match correct seasonal time range element [%s] %s' % (module,x)
110
111for x in ['199011','199112']:
112  m = r1.match( x )
113  if not m:
114     print 'OK -- correctly failed [%s] %s for seasonal data' % (module,x)
115  else:
116     print 'Failed to detect bad seasonal time range element [%s] %s' % (module,x)
117
118r1 = re.compile( c.pats['mon'][0] )
119for x in ['199101']:
120  m = r1.match( x )
121  if m:
122     print 'OK -- passed [%s] %s for daily data' % (module,x)
123  else:
124     print 'Failed to match correct daily time range element [%s] %s -- %s' % (module,x,c.pats['day'][0])
125
126
127c = utils_c4.checkGrids(parent=p)
128c.interpolatedGrids = config.interpolatedGrids
129
130lat = map( lambda x: -46.25 + x*0.5, range(185) )
131lon = map( lambda x: -25.25 + x*0.5, range(173) )
132da = {'lat':{'_data':lat,'units':'degrees_north','long_name':'latitude','standard_name':'latitude','_type':'float64'}, 'lon':{'_data':lon,'units':'degrees_east','long_name':'longitude','standard_name':'longitude','_type':'float64'} }
133c.check( 'tas','AFR-44i', da, {'tas':{} } )
134if c.errorCount == 0:
135  print 'OK -- passed a correct grid'
136else:
137  print 'Failed -- reported errors on correct grid'
138lat = map( lambda x: -46.25 + x*0.5, range(180) )
139lon = map( lambda x: -25.25 + x*0.5, range(172) )
140da = {'lat':{'_data':lat,'units':'degrees_north','long_name':'latitude','standard_name':'latitude','_type':'float64'}, 'lon':{'_data':lon,'units':'degrees_east','long_name':'longitude','standard_name':'longitude','_type':'float64'} }
141c.check( 'tas','AFR-44i', da, {'tas':{} } )
142if c.errorCount == 0:
143  print 'Failed -- passed a bad grid'
144else:
145  print 'OK -- detected a bad grid'
146
147ii = open( 'specs_vocabs/globalAtsSample001.txt' )
148fn = string.strip( ii.readline() )
149res = string.strip( ii.readline() )
150testId = '#04.001'
151## switch to project = SPECS
152c = utils_c4.checkFileName(parent=ps)
153c.check( fn )
154if c.errorCount == 0:
155  print 'Passed [%s] %s: valid file name' % (module,fn)
156else:
157  print 'Failed [%s] %s: valid file name' % (module,fn)
158
159
160## note test is done on string'ed values.
161ga = {}
162for l in ii.readlines():
163  bits = string.split( string.strip(l) )
164  if bits[1] == '@int':
165    ga[bits[0]] = int( bits[2] )
166  else:
167    ga[bits[0]] = string.join( bits[1:] )
168
169va = { "tas":{ "_type":"float32", "standard_name":"air_temperature", 'long_name':'Air Temperature', 'units':'K', 'cell_methods':'time: mean'} }
170
171cga = utils_c4.checkGlobalAttributes( parent=ps,cls='SPECS')
172cga.check( ga, va, "tas", "day", ps.pcfg.vocabs, c.fnParts )
173
174ii = open( 'specs_vocabs/globalAtsSample002.txt' )
175fn = string.strip( ii.readline() )
176res = string.strip( ii.readline() )
177testId = '#04.002'
178## switch to project = SPECS
179c = utils_c4.checkFileName(parent=ps)
180c.check( fn )
181if c.errorCount == 0:
182  print 'Passed [%s] %s: valid file name' % (module,fn)
183else:
184  print 'Failed [%s] %s: valid file name' % (module,fn)
185
186
187## note test is done on string'ed values.
188ga = {}
189for l in ii.readlines():
190  bits = string.split( string.strip(l) )
191  if bits[1] == '@int':
192    ga[bits[0]] = int( bits[2] )
193  else:
194    ga[bits[0]] = string.join( bits[1:] )
195
196va = { "tas":{ "_type":"float32", "standard_name":"air_temperature", 'long_name':'Air Temperature', 'units':'K', 'cell_methods':'time: mean'} }
197
198cga = utils_c4.checkGlobalAttributes( parent=ps,cls='SPECS')
199cga.check( ga, va, "tas", "day", ps.pcfg.vocabs, c.fnParts )
Note: See TracBrowser for help on using the repository browser.