source: CMIP6dreqbuild/trunk/srcMisc/dreqSX.py @ 508

Subversion URL: http://proj.badc.rl.ac.uk/svn/exarch/CMIP6dreqbuild/trunk/srcMisc/dreqSX.py@508
Revision 508, 5.8 KB checked in by mjuckes, 5 years ago (diff)

corrections

Line 
1
2import shelve, string, collections
3from utils_wb import workbook, uniCleanFunc
4import cmip5so
5from sets import Set
6
7vdate = "20150907"
8
9shvg = shelve.open( 'dreq_consol_tables_shelve_v%s' % vdate, 'r' )
10shnv = shelve.open( 'dreq_consol_tables_nv_shelve_v%s' % vdate, 'r' )
11## from vlsc5b --- includes new names from OMIP
12shold = shelve.open( 'dreq_consol_tables_old_shelve_v20150713', 'r' )
13shlist = [shvg,shnv,shold]
14
15##MIP columns: recordid, ['comment', 'deflate_level', 'shuffle', 'ok_max_mean_abs', 'flag_meanings', 'type', 'ok_min_mean_abs', 'standard_name', 'deflate', 'long_name', 'valid_min', 'cell_methods', 'flag_values', 'cell_measures', 'out_name', 'modeling_realm', 'units', '#cell_methods', 'valid_max', 'positive'] , var, mip table, dims
16
17##ee0: Short name of group      Variable short name     Table   Frequency       Description extension (optional)        Shape   Levels  Time mean, point or climatology Mask (optional), priority, mip, uuid
18## vgrp, var, table, freq, comment, shape, levels, tform, mask, priority, mip, recordId
19## e.g. 'PMIP-Lmon', u'rh_c14', u'new', u'mon', '', u'XYT', '', '', '', 2, 'PMIP', '21de1a34-1597-11e5-861b-ac72891c3257')
20#####
21###['group','var','table','frequency','shape',''....
22### Want to map shape onto a "feature type" which will reference a set of dimensions.
23##XYT: latitude, longitude, time
24##'4 element vector' : seaice transect x time.
25## KZT: RFMIP: not clear what K is.
26## '2D vector field': from SIMIP, vector as in list: extra dimension is a list of thickness categories (
27
28##eenv:
29## MIP  standard name check     Review comments Shape etc       Short name      CF standard_name        standard name status    Native grid     units   Long Name       description/comments    Priority        associated observational dataset, ..., recordID (last)
30##e.g. [2, u'FAFMIP', -1.0, '', u'yr|XYZ|mean', u'ocontemppsmadvect', u'tendency_of_sea_water_conservative_temperature_expressed_as_heat_content_due_to_parameterized_submesoscale_advection', -1.0, u'ocean', u'W/m2', '', u'Level 2: priority unclear', 2.0, '', '', '', '', 'b1feb8a2-1598-11e5-92ad-ac72891c3257']
31
32##eeold:
33##e.g. ['ef36eefae77f82455c0a925167a0d4ca', 'rss', 'Net shortwave surface radiation', 'surface_net_downward_shortwave_flux', 'unset', 'W m-2', 'SPECS_day', 'mv.001.0094', 'mv.001.0394', '', '']
34##ee_xref
35##hash, uuid list -- list of mip tables for each parameter key (ee_old)
36
37ee_xref = {}
38ee_xref_bck = collections.defaultdict( list )
39ee_miptables_old = collections.defaultdict( list )
40ee_miptables_vars_old = collections.defaultdict( list )
41ee_ovars_old = collections.defaultdict( list )
42ee_mip = {}
43ee0 = {}
44eenv = {}
45## dictionary to carry short names of old variables.
46eeold = {}
47###for ee,shv in [(ee0,shvg),(eenv,shnv),(eeold,shold)]:
48##for ee,shv in [(ee0,shvg),(eenv,shnv)]:
49for k in shnv:
50  eenv[k] = shnv[k]
51ccl = collections.defaultdict( int )
52for k in shvg:
53  ee0[k] = shvg[k]
54  ccl[len(ee0[k])] += 1
55
56print 'tables ... groups ....',ccl
57 
58
59rmod = 'csv'
60eeoldl = collections.defaultdict( list )
61if rmod == 'xls':
62  for ee,fn in [(ee_xref,'vlsc5_xref_v%s.xls' % vdate),(ee_mip,'vlsc5_mip_v%s.xls' % vdate),(eeold,'vlsc5b_v%s.xls' % vdate)]:
63    wb = workbook( fn )
64    sh = wb.book.sheet_by_name( 'Sheet1' )
65    for i in range(sh.nrows):
66      r = map( lambda x: string.strip(str(uniCleanFunc(x.value))), sh.row(i) )
67      ee[r[0]] = r
68else:
69  for ee,fn,sep,xxx,nrmin in [(ee_xref,'vlsc5_xref_v%s.csv' % vdate,',',False,5),(ee_mip,'vlsc5_mip_v%s.csv' % vdate, '\t',False,24),(eeoldl,'vlsc5b_v%s.csv' % vdate, '\t',True,9)]:
70    for i in open(fn).readlines():
71      r = map( lambda x: string.strip(str(uniCleanFunc(x))), string.split( string.strip(i), sep ) )
72      while len(r) < nrmin:
73        r.append( '' )
74      if xxx:
75        ee[r[0]].append( r )
76      else:
77        ee[r[0]] = r
78
79ss = set()
80for k in ee_mip.keys():
81 if len( ee_mip[k][:] ) != 24:
82   print 'ERROR.015.0003: Bad record length %s: %s' % (k,ee_mip[k])
83 mipt = ee_mip[k][22]
84 ss.add( mipt )
85 if mipt == 'OMIP.Oyr':
86   print ee_mip[k]
87
88for h in eeoldl.keys():
89  if len( eeoldl[h] ) == 1:
90    eeold[h] = eeoldl[h][0]
91  else:
92    ll = []
93    for k in range( len( eeoldl[h] ) ):
94      if eeoldl[h][k][8] == '':
95        ll.append(k)
96    if len(ll) == 0:
97      print 'ERROR.004.0001: no unforwarded record in vlsc5b for h=%s' % h
98      eeold[h] = eeoldl[h][0]
99    elif len(ll) > 1:
100      print 'ERROR.004.0002: multiple unforwarded records in vlsc5b for h=%s (%s)' % (h,eeoldl[h][ll[0]][1])
101      eeold[h] = eeoldl[h][ll[0]]
102    else:
103      eeold[h] = eeoldl[h][ll[0]]
104
105k2m = []
106km = []
107for k in ee_xref.keys():
108  if eeold.has_key(k):
109    for k2 in ee_xref[k][1:]:
110      if k2 != '':
111        if ee_mip.has_key(k2):
112          ee_xref_bck[k2].append(k)
113        else:
114          k2m.append( (k2,k) )
115  else:
116    km.append(k)
117
118##
119## had bug here due to extension of ee_mip to include dimensions.
120##
121ee_byvar = collections.defaultdict(list)
122for k in ee_mip.keys():
123  tid = ee_mip[k][-2]
124  ee_miptables_old[tid].append(  ee_mip[k][0] )
125  ee_miptables_vars_old[tid].append(  ee_mip[k][-3] )
126  ee_ovars_old['%s.%s' % (tid,ee_mip[k][-3]) ].append(  k )
127  ee_byvar[ ee_mip[k][-3] ].append( k )
128
129## check cmip5 vs. standard output
130s1 = Set()
131for k in ee_miptables_vars_old.keys():
132  if k[:6] == 'CMIP5_':
133    s2 = Set( ee_miptables_vars_old[k] )
134    kk = k[6:]
135    s1.add( kk )
136    assert cmip5so.so.has_key(kk ), 'Table %s not found in standard output' % kk
137    lnf = []
138    for v in ee_miptables_vars_old[k]:
139      if not cmip5so.so[kk].a.has_key(v):
140         lnf.append(v)
141    if len(lnf) > 0:
142      print 'ERROR.010.0001: Variables not found in %s:: %s' % (kk,str(lnf))
143
144    lnf = []
145    for v in cmip5so.so[kk].a.keys():
146      if not v in s2:
147         lnf.append(v)
148    if len(lnf) > 0:
149      print 'ERROR.010.0002: SO Variables not found in %s:: %s' % (kk,str(lnf))
150
151for s in cmip5so.so.keys():
152  if not s in s1:
153    print 'ERROR.010.0002: Table %s not in old mip tables' % s
154
155for s in shlist:
156  s.close()
Note: See TracBrowser for help on using the repository browser.