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

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

updates to xlst etc

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' )
11shnvo = shelve.open( 'dreq_consol_tables_nvom_shelve_v%s' % vdate, 'r' )
12## from vlsc5b --- includes new names from OMIP
13shold = shelve.open( 'dreq_consol_tables_old_shelve_v20150713', 'r' )
14shlist = [shvg,shnv,shold]
15
16##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
17
18##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
19## vgrp, var, table, freq, comment, shape, levels, tform, mask, priority, mip, recordId
20## e.g. 'PMIP-Lmon', u'rh_c14', u'new', u'mon', '', u'XYT', '', '', '', 2, 'PMIP', '21de1a34-1597-11e5-861b-ac72891c3257')
21#####
22###['group','var','table','frequency','shape',''....
23### Want to map shape onto a "feature type" which will reference a set of dimensions.
24##XYT: latitude, longitude, time
25##'4 element vector' : seaice transect x time.
26## KZT: RFMIP: not clear what K is.
27## '2D vector field': from SIMIP, vector as in list: extra dimension is a list of thickness categories (
28
29##eenv:
30## 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)
31##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']
32
33##eeold:
34##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', '', '']
35##ee_xref
36##hash, uuid list -- list of mip tables for each parameter key (ee_old)
37
38ee_xref = {}
39ee_xref_bck = collections.defaultdict( list )
40ee_miptables_old = collections.defaultdict( list )
41ee_miptables_vars_old = collections.defaultdict( list )
42ee_ovars_old = collections.defaultdict( list )
43ee_mip = {}
44ee0 = {}
45eenv = {}
46eenvo = {}
47## dictionary to carry short names of old variables.
48eeold = {}
49###for ee,shv in [(ee0,shvg),(eenv,shnv),(eeold,shold)]:
50##for ee,shv in [(ee0,shvg),(eenv,shnv)]:
51for k in shnv:
52  eenv[k] = shnv[k]
53for k in shnvo:
54  eenvo[k] = shnvo[k]
55ccl = collections.defaultdict( int )
56for k in shvg:
57  ee0[k] = shvg[k]
58  ccl[len(ee0[k])] += 1
59
60print 'tables ... groups ....',ccl
61 
62
63rmod = 'csv'
64eeoldl = collections.defaultdict( list )
65if rmod == 'xls':
66  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)]:
67    wb = workbook( fn )
68    sh = wb.book.sheet_by_name( 'Sheet1' )
69    for i in range(sh.nrows):
70      r = map( lambda x: string.strip(str(uniCleanFunc(x.value))), sh.row(i) )
71      ee[r[0]] = r
72else:
73  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)]:
74    for i in open(fn).readlines():
75      r = map( lambda x: string.strip(str(uniCleanFunc(x))), string.split( string.strip(i), sep ) )
76      while len(r) < nrmin:
77        r.append( '' )
78      if xxx:
79        ee[r[0]].append( r )
80      else:
81        ee[r[0]] = r
82
83ss = set()
84for k in ee_mip.keys():
85 if len( ee_mip[k][:] ) != 24:
86   print 'ERROR.015.0003: Bad record length %s: %s' % (k,ee_mip[k])
87 mipt = ee_mip[k][22]
88 ss.add( mipt )
89 if mipt == 'OMIP.Oyr':
90   print ee_mip[k]
91
92for h in eeoldl.keys():
93  if len( eeoldl[h] ) == 1:
94    eeold[h] = eeoldl[h][0]
95  else:
96    ll = []
97    for k in range( len( eeoldl[h] ) ):
98      if eeoldl[h][k][8] == '':
99        ll.append(k)
100    if len(ll) == 0:
101      print 'ERROR.004.0001: no unforwarded record in vlsc5b for h=%s' % h
102      eeold[h] = eeoldl[h][0]
103    elif len(ll) > 1:
104      print 'ERROR.004.0002: multiple unforwarded records in vlsc5b for h=%s (%s)' % (h,eeoldl[h][ll[0]][1])
105      eeold[h] = eeoldl[h][ll[0]]
106    else:
107      eeold[h] = eeoldl[h][ll[0]]
108
109k2m = []
110km = []
111for k in ee_xref.keys():
112  if eeold.has_key(k):
113    for k2 in ee_xref[k][1:]:
114      if k2 != '':
115        if ee_mip.has_key(k2):
116          ee_xref_bck[k2].append(k)
117        else:
118          k2m.append( (k2,k) )
119  else:
120    km.append(k)
121
122##
123## had bug here due to extension of ee_mip to include dimensions.
124##
125ee_byvar = collections.defaultdict(list)
126for k in ee_mip.keys():
127  tid = ee_mip[k][-2]
128  ee_miptables_old[tid].append(  ee_mip[k][0] )
129  ee_miptables_vars_old[tid].append(  ee_mip[k][-3] )
130  ee_ovars_old['%s.%s' % (tid,ee_mip[k][-3]) ].append(  k )
131  ee_byvar[ ee_mip[k][-3] ].append( k )
132
133## check cmip5 vs. standard output
134s1 = Set()
135cmip5ByVar = collections.defaultdict( list )
136for k in ee_miptables_vars_old.keys():
137  if k[:6] == 'CMIP5_':
138    s2 = Set( ee_miptables_vars_old[k] )
139    kk = k[6:]
140    s1.add( kk )
141    assert cmip5so.so.has_key(kk ), 'Table %s not found in standard output' % kk
142    lnf = []
143    for v in ee_miptables_vars_old[k]:
144      if not cmip5so.so[kk].a.has_key(v):
145         lnf.append(v)
146    if len(lnf) > 0:
147      print 'ERROR.010.0001: Variables not found in %s:: %s' % (kk,str(lnf))
148
149    lnf = []
150    for v in cmip5so.so[kk].a.keys():
151      if not v in s2:
152         lnf.append(v)
153    if len(lnf) > 0:
154      print 'ERROR.010.0002: SO Variables not found in %s:: %s' % (kk,str(lnf))
155
156for s in cmip5so.so.keys():
157  if not s in s1:
158    print 'ERROR.010.0002: Table %s not in old mip tables' % s
159  for v in cmip5so.so[s].a.keys():
160    cmip5ByVar[v].append(s)
161   
162
163for s in shlist:
164  s.close()
Note: See TracBrowser for help on using the repository browser.