Ignore:
Timestamp:
12/02/16 13:06:56 (4 years ago)
Author:
mjuckes
Message:

Updated setup for tag 01.beta.20

File:
1 edited

Legend:

Unmodified
Added
Removed
  • CMIP6dreq/trunk/dreqPy/makeTables.py

    r586 r594  
    1818  def close(self): 
    1919    self.wb.close() 
     20 
     21def vfmt( x ): 
     22            if x < 1.e9: 
     23              s = '%sM' % int( x*1.e-6 ) 
     24            elif x < 1.e12: 
     25              s = '%sG' % int( x*1.e-9 ) 
     26            elif x < 1.e13: 
     27              s = '%3.1fT' % ( x*1.e-12 ) 
     28            elif x < 1.e15: 
     29              s = '%3iT' % int( x*1.e-12 ) 
     30            elif x < 1.e18: 
     31              s = '%3iP' % int( x*1.e-15 ) 
     32            else: 
     33              s = '{:,.2f}'.format( x*1.e-9 ) 
     34            return s 
    2035 
    2136#priority       long name       units   comment         questions & notes       output variable name    standard name   unconfirmed or proposed standard name   unformatted units       cell_methods    valid min       valid max       mean absolute min       mean absolute max       positive        type    CMOR dimensions CMOR variable name      realm   frequency       cell_measures   flag_values     flag_meanings 
     
    89104       
    90105class makeTab(object): 
    91   def __init__(self, subset=None, dest='tables/test.xlsx', skipped=set()): 
     106  def __init__(self, dq, subset=None, dest='tables/test.xlsx', skipped=set(), collected=None): 
    92107    if subset != None: 
    93108      cmv = [x for x in dq.coll['CMORvar'].items if x.uid in subset] 
     
    104119    hdr_cell_format = wb.wb.add_format({'text_wrap': True, 'font_size': 14, 'font_color':'#0000ff', 'bold':1, 'fg_color':'#aaaacc'}) 
    105120    hdr_cell_format.set_text_wrap() 
     121    sect_cell_format = wb.wb.add_format({'text_wrap': True, 'font_size': 14, 'font_color':'#0000ff', 'bold':1, 'fg_color':'#ccccbb'}) 
     122    sect_cell_format.set_text_wrap() 
    106123    cell_format = wb.wb.add_format({'text_wrap': True, 'font_size': 11}) 
    107124    cell_format.set_text_wrap() 
     
    109126    mode = 'c' 
    110127    sht = wb.newSheet( 'Notes' ) 
    111     tableNotes = [('MIPs (...)','The last two columns in each row list MIPs associated with each variable. The first column in this pair lists the MIPs which are requesting the variable in one or more experiments. The second column lists the MIPs proposing experiments in which this variable is requested. E.g. If a variable is requested in a DECK experiment by HighResMIP, then HighResMIP appears in the forst column and DECK in the second')] 
    112     sht.set_row(0,40) 
     128    tableNotes = [('MIPs (...)','The last two columns in each row list MIPs associated with each variable. The first column in this pair lists the MIPs which are requesting the variable in one or more experiments. The second column lists the MIPs proposing experiments in which this variable is requested. E.g. If a variable is requested in a DECK experiment by HighResMIP, then HighResMIP appears in the first column and DECK in the second')] 
     129    sht.write( 0,0, '', hdr_cell_format ) 
    113130    sht.write( 0,1, 'Notes on tables', hdr_cell_format ) 
    114131    ri = 0 
    115     sht.set_column(0,0,40) 
    116     sht.set_column(0,1,200) 
     132    sht.set_column(0,0,30) 
     133    sht.set_column(1,1,60) 
    117134    for t in tableNotes: 
    118135      ri += 1 
     
    120137          sht.write( ri,i, t[i], cell_format ) 
    121138 
     139    if collected != None: 
     140      ri += 2 
     141      sht.write( ri, 0, 'Table', sect_cell_format ) 
     142      sht.write( ri, 1, 'Reference Volume (1 deg. atmosphere, 0.5 deg. ocean)', sect_cell_format ) 
     143      for k in sorted( collected.keys() ): 
     144        ri += 1 
     145        sht.write( ri, 0, k ) 
     146        sht.write( ri, 1, vfmt( collected[k]*2. ) ) 
     147 
    122148    ncga = 'NetCDF Global Attribute' 
     149    withoo = False 
    123150    for t in tables: 
    124       oo = open( 'tables/test_%s.csv' % t, 'w' ) 
     151      if withoo: 
     152        oo = open( 'tables/test_%s.csv' % t, 'w' ) 
    125153      sht = wb.newSheet( t ) 
    126154      j = 0 
     
    142170        hrec.append( 'MIPs (by experiment)' ) 
    143171 
    144       sht.set_row(0,40) 
     172      ##sht.set_row(0,40) 
    145173      for i in range(len(hrec)): 
    146174          sht.write( j,i, hrec[i], hdr_cell_format ) 
     
    183211              orec.append( string.join( sorted( list( thismips2) ),',') ) 
    184212 
    185             oo.write( string.join(orec, '\t' ) + '\n' ) 
     213            if withoo: 
     214              oo.write( string.join(orec, '\t' ) + '\n' ) 
    186215            j+=1 
    187216            for i in range(len(orec)): 
    188217              sht.write( j,i, orec[i], cell_format ) 
    189       oo.close() 
     218      if withoo: 
     219        oo.close() 
    190220    wb.close() 
    191221 
     
    317347 
    318348  def cmvLink(self,targ,frm='',ann=''): 
    319     return '<li>%s {%s}: %s [%s]</li>' % (  targ.__href__(odir='../u/', label=targ.label), targ.mipTable, targ.title, targ.frequency ) 
     349    t2 = targ._inx.uid[targ.stid] 
     350    return '<li>%s {%s}: %s [%s: %s]</li>' % (  targ.__href__(odir='../u/', label=targ.label), targ.mipTable, targ.title, targ.frequency, t2.title ) 
    320351 
    321352  def objLink(self,targ,frm='',ann=''): 
     
    344375 
    345376class tables(object): 
    346   def __init__(self,sc, mips): 
     377  def __init__(self,sc, mips, odir='xls'): 
    347378      self.sc = sc 
    348379      self.dq = sc.dq 
    349380      self.mips = mips 
    350  
    351   def doTable(self,m,l1,m2,pmax,collector,acc=True): 
    352       """acc allows accumulation of values to be switched off when called in single expt mode""" 
     381      self.odir = odir 
     382 
     383  def setMlab( self, m ): 
     384      if type(m) == type(''): 
     385        mlab = m 
     386      else: 
     387        ll = sorted( list(m) ) 
     388        if len(ll) == 1: 
     389          mlab = list(m)[0] 
     390        else: 
     391          mlab=string.join( [ x[:2].lower() for x in m ], '.' ) 
     392      return mlab 
     393 
     394  def doTable(self,m,l1,m2,pmax,collector,acc=True, mlab=None): 
     395      """*acc* allows accumulation of values to be switched off when called in single expt mode""" 
     396 
     397      if mlab == None: 
     398        mlab = self.setMlab( m ) 
    353399         
     400 
    354401      x = self.sc.volByExpt( l1, m2, expFullEx=(m2 in self.mips), pmax=pmax ) 
    355402      if x[0] > 0: 
    356         collector[m].a[m2] += x[0] 
    357403        im2 = self.dq.inx.uid[m2] 
     404        collector[mlab].a[im2.label] += x[0] 
    358405# 
    359406# create sum for each table 
    360407# 
    361408        xs = 0 
     409        kkc = '_%s_%s' % (mlab,im2.label) 
    362410        for k in x[2].keys(): 
    363411          i = self.dq.inx.uid[k] 
     
    365413          xs += xxx 
    366414          if xxx > 0: 
    367             collector['_%s_%s' % (m,m2)].a[i.mipTable] += xxx 
     415            collector[kkc].a[i.mipTable] += xxx 
    368416        assert x[0] == xs, 'ERROR.0088: consistency problem %s  %s %s %s' % (m,m2,x[0],xs) 
    369417        if x[0] == 0: 
     
    373421 
    374422        if acc: 
    375           collector[m].a['TOTAL'] += x[0] 
     423          collector[mlab].a['TOTAL'] += x[0] 
     424 
    376425        dd = collections.defaultdict( list ) 
    377426        lll = set() 
     
    383432            dd[t].append( (f,t,l,tt,d,u) ) 
    384433        if len( dd.keys() ) > 0: 
    385           collector[m].dd[m2] = dd 
     434          collector[mlab].dd[im2.label] = dd 
    386435          if im2._h.label == 'experiment': 
    387436            dothis = self.sc.tierMax >= im2.tier 
     
    390439###         need the variable list itself ..... 
    391440### 
    392           makeTab( subset=lll, dest='tab2/%s-%s_%s_%s.xlsx' % (m,m2,self.sc.tierMax,pmax) ) 
     441          makeTab( self.sc.dq, subset=lll, dest='%s/%s-%s_%s_%s.xlsx' % (self.odir,mlab,im2.label,self.sc.tierMax,pmax), collected=collector[kkc].a ) 
    393442 
    394443styls = styles() 
    395  
    396 assert os.path.isdir( 'html' ), 'Before running this script you need to create "html", "html/index" and "html/u" sub-directories, or edit the call to dq.makeHtml' 
    397 assert os.path.isdir( 'html/u' ), 'Before running this script you need to create "html", "html/index" and "html/u" sub-directories, or edit the call to dq.makeHtml, and refernces to "u" in style lines below' 
    398 assert os.path.isdir( 'html/index' ), 'Before running this script you need to create "html", "html/index" and "html/u" sub-directories, or edit the call to dq.makeHtml, and refernces to "u" in style lines below' 
    399 assert os.path.isdir( 'tables' ), 'Before running this script you need to create a "tables" sub-directory, or edit the makeTab class' 
    400444 
    401445htmlStyle = {} 
     
    415459htmlStyle['structure'] = {'getIrefs':['__all__']} 
    416460htmlStyle['standardname'] = {'getIrefs':['__all__']} 
    417 dq = dreq.loadDreq( htmlStyles=htmlStyle) 
     461 
     462if __name__ == "__main__": 
     463  assert os.path.isdir( 'html' ), 'Before running this script you need to create "html", "html/index" and "html/u" sub-directories, or edit the call to dq.makeHtml' 
     464  assert os.path.isdir( 'html/u' ), 'Before running this script you need to create "html", "html/index" and "html/u" sub-directories, or edit the call to dq.makeHtml, and refernces to "u" in style lines below' 
     465  assert os.path.isdir( 'html/index' ), 'Before running this script you need to create "html", "html/index" and "html/u" sub-directories, or edit the call to dq.makeHtml, and refernces to "u" in style lines below' 
     466  assert os.path.isdir( 'tables' ), 'Before running this script you need to create a "tables" sub-directory, or edit the makeTab class' 
     467 
     468  dq = dreq.loadDreq( htmlStyles=htmlStyle) 
    418469## 
    419470## add special styles to dq object "itemStyle" dictionary. 
    420471## 
    421472 
    422 dq.itemStyles['standardname'] = styls.snLink 
    423 dq.itemStyles['var'] = styls.varLink 
    424 dq.itemStyles['mip'] = styls.mipLink 
    425 dq.itemStyles['CMORvar'] = styls.cmvLink 
    426 dq.itemStyles['objective'] = styls.objLink 
    427 dq.itemStyles['structure'] = styls.strLink 
    428 dq.itemStyles['objectiveLink'] = styls.objLnkLink 
    429 dq.itemStyles['requestVarGroup'] = styls.vgrpLink 
    430 dq.itemStyles['requestLink'] = styls.rqlLink02 
    431 dq.itemStyles['spatialShape'] = styls.labTtl 
    432 dq.coll['var'].items[0].__class__._linkAttrStyle['sn'] = styls.snLink01 
     473  dq.itemStyles['standardname'] = styls.snLink 
     474  dq.itemStyles['var'] = styls.varLink 
     475  dq.itemStyles['mip'] = styls.mipLink 
     476  dq.itemStyles['CMORvar'] = styls.cmvLink 
     477  dq.itemStyles['objective'] = styls.objLink 
     478  dq.itemStyles['structure'] = styls.strLink 
     479  dq.itemStyles['objectiveLink'] = styls.objLnkLink 
     480  dq.itemStyles['requestVarGroup'] = styls.vgrpLink 
     481  dq.itemStyles['requestLink'] = styls.rqlLink02 
     482  dq.itemStyles['spatialShape'] = styls.labTtl 
     483  dq.coll['var'].items[0].__class__._linkAttrStyle['sn'] = styls.snLink01 
    433484##dq.coll['requestVarGroup'].items[0].__class__._linkAttrStyle['requestVar'] = styls.rqvLink01 
    434 dq.itemStyles['requestVar'] = styls.rqvLink01 
    435  
    436 if __name__ == "__main__": 
     485  dq.itemStyles['requestVar'] = styls.rqvLink01 
     486 
    437487  ht = htmlTrees(dq) 
    438488  dq.makeHtml( annotations={'var':ht.anno} ) 
    439489  try: 
    440490    import xlsxwriter 
    441     mt = makeTab() 
     491    mt = makeTab( dq) 
    442492  except: 
    443493    print ('Could not make tables ...') 
Note: See TracChangeset for help on using the changeset viewer.