from fcc_utils2 import mipTableScan
from ceda_cc_config.config_c4 import CC_CONFIG_DIR
import re, os, string

ml = ['CORDEX_3h', 'CORDEX_6h', 'CORDEX_Aday', 'CORDEX_day', 'CORDEX_grids', 'CORDEX_mon' ]
ml = ['CORDEX_3h', 'CORDEX_6h', 'CORDEX_fx', 'CORDEX_day', 'CORDEX_mon', 'CORDEX_sem' ]
newMip = 'SPECS'
newMip = 'CORDEX'
newMip = 'CCMI'
ml2 = ['CMIP5_3hr', 'CMIP5_6hrPlev', 'CMIP5_Amon', 'CMIP5_cfDay', 'CMIP5_cfOff', 'CMIP5_day', 'CMIP5_grids', 'CMIP5_Lmon', 'CMIP5_OImon', 'CMIP5_Oyr',
       'CMIP5_6hrLev', 'CMIP5_aero', 'CMIP5_cf3hr', 'CMIP5_cfMon', 'CMIP5_cfSites', 'CMIP5_fx', 'CMIP5_LImon', 'CMIP5_Oclim', 'CMIP5_Omon'] 
ml2 = ['CMIP5_3hr', 'CMIP5_6hrPlev', 'CMIP5_Amon', 'CMIP5_cfDay', 'CMIP5_cfOff', 'CMIP5_day', 'CMIP5_grids', 'CMIP5_Lmon',
       'CMIP5_6hrLev', 'CMIP5_aero', 'CMIP5_cf3hr', 'CMIP5_cfMon', 'CMIP5_cfSites', 'CMIP5_fx', 'CMIP5_LImon'] 

cfsntab = 'cf/cf-standard-name-table.xml'
cordex_dkrz = 'CORDEX_variables_requirement_table_upgedated-1.csv'
cordex_dkrz = 'CORDEX_variables_requirement_table_all.csv'
cordex_dkrz_pat = 'cordex_dkrz/CORDEX_variables_requirement_table_%s.csv'
cordex_dkrz_pat = 'cordex_dkrz_oct/CORDEX_variables_requirement_table_%s.csv'
re_sn = re.compile( 'entry id="(.*)"' )
re_snax = re.compile( '</alias>' )
re_snar = re.compile( '<entry_id>(.*)<' )
re_sna = re.compile( 'alias id="(.*)"' )
##alias id="atmosphere_water_vapor_content"
##entry id="age_of_sea_ice"'
def gen_sn_list( pathn ):
  assert os.path.isfile( pathn ), '%s not found ' % pathn
  inAlias = False
  snl = []
  snla = []
  aliasses = {}
  for l in open(pathn).readlines():
    m = re_sn.findall(l )
    if len(m) > 0:
      for i in m:
        snl.append( i )
    m = re_sna.findall(l )
    if len(m) > 0:
      inAlias = True
      for i in m:
        snla.append( i )
    if inAlias:
      m = re_snax.findall(l )
      if len(m) > 0:
        inAlias = False
      else:
        m = re_snar.findall(l )
        if len(m) > 0:
          aliasses[snla[-1]] = m[0]
          assert len(m) == 1, 'Unexpected length of results, %s [%s]' % (str(m),l)
  ##<alias id="station_wmo_id">
    ##<entry_id>platform_id</entry_id>
  ##</alias>
  return (snl,snla,aliasses)

def tlist_to_dict( ll ):

   ee = {} 
   for l in ll:
     ee[l[0]] = ( l[1], l[2] )
   return ee

class comp(object):

  def __init__(self, snl, snla=None, ec1=None,tag=None):
    self.id = 'comp'
    self.snl = snl
    self.snla = snla
    self.ec1 = ec1
    self.tag=tag


  def comp(self, e1, e2,checkCellMethods=False,tag=None ):
    self.tag=tag
    
    ##e1 = tlist_to_dict( t1 )
    ##e2 = tlist_to_dict( t2 )
    self.nn_sn = 0
    thisSnl = []

    checkAll = True
    keys = e1.keys()
    keys.sort()
    self.nn_var = len(keys)
    for k in keys:
      e0 = 0
      f2 = False
      f3 = False
      f4 = False
      if e1[k][1]['standard_name'] in [None,'None']:
        if e1[k][1].get('long_name',None) not in [None,'None']:
          print 'WARNING[A]: standard name for %s [%s] not set' % (k,e1[k][1].get('long_name') )
        else:
          print 'WARNING[A]: standard name for %s not set' % k
      else:
        if e1[k][1]['standard_name'] not in self.snl:
          if e1[k][1]['standard_name'] not in self.snla:
            print 'ERROR[0]: standard name %s for %s [%s] not in snl or snla' % (e1[k][1]['standard_name'], k, e1[k][2] )
          else:
            print 'WARNING: standard name %s for %s not in snl' % (e1[k][1]['standard_name'], k )
            if e1[k][1]['standard_name'] not in thisSnl:
               thisSnl.append( e1[k][1]['standard_name'] )
        else:
            if e1[k][1]['standard_name'] not in thisSnl:
               thisSnl.append( e1[k][1]['standard_name'] )

      if self.ec1 != None:
        if k not in self.ec1.keys():
          print 'ERROR[1]: variable %s [%s] not in CORDEX variable requirements list' % (k,e1[k][2])
          vrln = None
        else:
          vrln = self.ec1[k][1]
          if e1[k][1]['long_name'] != self.ec1[k][1]:
            f2 = True
          if checkCellMethods:  
            if e1[k][1]['cell_methods'] != self.ec1[k][3]:
              if not (e1[k][1]['cell_methods']=="None" and string.strip(self.ec1[k][3]) == "time:"):
                f3 = True
          if checkAll:  
            if (e1[k][1].has_key( 'positive' ) and self.ec1[k][4] == '') or ( (not e1[k][1].has_key( 'positive' )) and self.ec1[k][4] != ''):
                f4 = True
            elif e1[k][1].has_key( 'positive' ):
              if e1[k][1]['positive'] != self.ec1[k][4]:
                if not (e1[k][1]['positive']=="None" and string.strip(self.ec1[k][4]) == ""):
                  f4 = True
            if (e1[k][1].has_key( 'modeling_realm' ) and self.ec1[k][5] == '') or ( (not e1[k][1].has_key( 'modeling_realm' )) and self.ec1[k][5] != ''):
                f4 = True
            elif e1[k][1].has_key( 'modeling_realm' ):