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

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

misc

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)]:
48for ee,shv in [(ee0,shvg),(eenv,shnv)]:
49  for k in shv.keys():
50    ee[k] = shv[k]
51
52rmod = 'csv'
53eeoldl = collections.defaultdict( list )
54if rmod == 'xls':
55  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)]:
56    wb = workbook( fn )
57    sh = wb.book.sheet_by_name( 'Sheet1' )
58    for i in range(sh.nrows):
59      r = map( lambda x: string.strip(str(uniCleanFunc(x.value))), sh.row(i) )
60      ee[r[0]] = r
61else:
62  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)]:
63    for i in open(fn).readlines():
64      r = map( lambda x: string.strip(str(uniCleanFunc(x))), string.split( string.strip(i), sep ) )
65      while len(r) < nrmin:
66        r.append( '' )
67      if xxx:
68        ee[r[0]].append( r )
69      else:
70        ee[r[0]] = r
71
72for k in ee_mip.keys():
73 if len( ee_mip[k][:] ) != 24:
74   print 'ERROR.015.0003: Bad record length %s: %s' % (k,ee_mip[k])
75
76for h in eeoldl.keys():
77  if len( eeoldl[h] ) == 1:
78    eeold[h] = eeoldl[h][0]
79  else:
80    ll = []
81    for k in range( len( eeoldl[h] ) ):
82      if eeoldl[h][k][8] == '':
83        ll.append(k)
84    if len(ll) == 0:
85      print 'ERROR.004.0001: no unforwarded record in vlsc5b for h=%s' % h
86      eeold[h] = eeoldl[h][0]
87    elif len(ll) > 1:
88      print 'ERROR.004.0002: multiple unforwarded records in vlsc5b for h=%s (%s)' % (h,eeoldl[h][ll[0]][1])
89      eeold[h] = eeoldl[h][ll[0]]
90    else:
91      eeold[h] = eeoldl[h][ll[0]]
92
93k2m = []
94km = []
95for k in ee_xref.keys():
96  if eeold.has_key(k):
97    for k2 in ee_xref[k][1:]:
98      if k2 != '':
99        if ee_mip.has_key(k2):
100          ee_xref_bck[k2].append(k)
101        else:
102          k2m.append( (k2,k) )
103  else:
104    km.append(k)
105
106##
107## had bug here due to extension of ee_mip to include dimensions.
108##
109ee_byvar = collections.defaultdict(list)
110for k in ee_mip.keys():
111  tid = ee_mip[k][-2]
112  ee_miptables_old[tid].append(  ee_mip[k][0] )
113  ee_miptables_vars_old[tid].append(  ee_mip[k][-3] )
114  ee_ovars_old['%s.%s' % (tid,ee_mip[k][-3]) ].append(  k )
115  ee_byvar[ ee_mip[k][-3] ].append( k )
116
117## check cmip5 vs. standard output
118s1 = Set()
119for k in ee_miptables_vars_old.keys():
120  if k[:6] == 'CMIP5_':
121    s2 = Set( ee_miptables_vars_old[k] )
122    kk = k[6:]
123    s1.add( kk )
124    assert cmip5so.so.has_key(kk ), 'Table %s not found in standard output' % kk
125    lnf = []
126    for v in ee_miptables_vars_old[k]:
127      if not cmip5so.so[kk].a.has_key(v):
128         lnf.append(v)
129    if len(lnf) > 0:
130      print 'ERROR.010.0001: Variables not found in %s:: %s' % (kk,str(lnf))
131
132    lnf = []
133    for v in cmip5so.so[kk].a.keys():
134      if not v in s2:
135         lnf.append(v)
136    if len(lnf) > 0:
137      print 'ERROR.010.0002: SO Variables not found in %s:: %s' % (kk,str(lnf))
138
139for s in cmip5so.so.keys():
140  if not s in s1:
141    print 'ERROR.010.0002: Table %s not in old mip tables' % s
142
143for s in shlist:
144  s.close()
Note: See TracBrowser for help on using the repository browser.