Пример #1
0
def check_eb(cf, ystart, yend, cdomain, cid, cver, msg=None):
    """
  Energy balance check 

  Parameters:
  ----------
  Returns:
  -------
  """
    rystart = ystart
    ryend = yend
    vLAT, vLON = e2oU.default_latlon(cf.cdomain)
    nlat = len(vLAT)
    nlon = len(vLON)

    grid_area = e2oU.load_grid_area(fgarea)
    cvarsEB = ["SWnet", "LWnet", "Qh", "Qle", "Qsm", "NET"]
    if msg is None:
        msg = e2oU.init_msg()

    venB = {}
    globM = {}  ## global mean values for information only !
    for cvar in cvarsEB:
        cf = cf.attr2fpath(cfreq="mon", cvar=cvar, cdomain=cdomain, cid=cid, cver=cver)
        print "EB, loading:", cvar

        if cvar == "NET":
            venB[cvar] = np.zeros((nlat, nlon))
            for cc in cvarsEB[:-1]:
                venB[cvar] = venB[cvar] + venB[cc]
        else:
            xdata, xtime = e2oU.load_nc_var(
                cf.fpath, cf.cvar, dstart=dt.datetime(rystart, 1, 1), dend=dt.datetime(ryend, 12, 31)
            )
            if xdata is None:
                venB[cvar] = np.zeros((nlat, nlon))
                msg["Wmsg"].append("EB: Could not find variable: '%s', setting to zero!'" % (cvar))
            else:
                venB[cvar] = np.mean(xdata, 0)
        if cvar == "Qsm":
            venB[cvar] = venB[cvar] * 3.34e5 * -1.0
        globM[cvar] = compute_area_mean(venB[cvar], grid_area)

    for cvar in cvarsEB:
        msg["Dmsg"].append(
            "EB: Global mean of %s %f (W m-2) with %s/%s %f"
            % (cvar, globM[cvar], cvar, cvarsEB[0], globM[cvar] / globM[cvarsEB[0]])
        )
    return msg
Пример #2
0
def check_wb(cf, ystart, yend, cdomain, cid, cver, msg=None):
    """
  Water balance check 

  Parameters:
  ----------
  Returns:
  -------
  """
    rystart = ystart
    ryend = yend
    dstart = dt.datetime(cf.ystart, 1, 1).toordinal()
    dend = dt.datetime(cf.yend, 12, 31).toordinal()
    xTIME = num2date(np.arange(0, dend - dstart + 1, 1), "days since %4i-01-01 00:00:00" % cf.ystart)
    tinD = [
        np.nonzero(xTIME == dt.datetime(rystart, 1, 1))[0][0],
        np.nonzero(xTIME == dt.datetime(ryend, 12, 31))[0][0],
    ]
    ndays = tinD[1] - tinD[0] + 1
    # print tinD
    # print ndays
    vLAT, vLON = e2oU.default_latlon(cf.cdomain)
    nlat = len(vLAT)
    nlon = len(vLON)

    grid_area = e2oU.load_grid_area(fgarea)
    cvarsWB = ["Precip", "Runoff", "Evap", "Stor", "NET"]
    # cvarsWB=['Precip','Runoff','Evap','NET']
    if msg is None:
        msg = e2oU.init_msg()

    venB = {}
    globM = {}  ## global mean values for information only !
    for cvar in cvarsWB:

        print "WB, loading:", cvar

        if cvar == "NET":
            venB[cvar] = np.zeros((nlat, nlon))
            for cc in cvarsWB[:-1]:
                venB[cvar] = venB[cvar] + venB[cc]
        elif cvar == "Stor":
            venB[cvar] = np.zeros((nlat, nlon))
            for svar in ["TotMoist", "SWE", "CanopInt"]:
                cf = cf.attr2fpath(cfreq="day", cvar=svar, cdomain=cdomain, cid=cid, cver=cver)
                xdata, xtime = e2oU.load_nc_var(cf.fpath, cf.cvar, tinD=tinD)
                if xdata is None:
                    xdata = np.zeros((2, nlat, nlon))
                    msg["Wmsg"].append("WB: Could not find variable: '%s', setting to zero!'" % (svar))
                venB[cvar] = venB[cvar] + -1 * (xdata[1, :, :] - xdata[0, :, :]) / (ndays)
        else:
            cf = cf.attr2fpath(cfreq="mon", cvar=cvar, cdomain=cdomain, cid=cid, cver=cver)
            xdata, xtime = e2oU.load_nc_var(
                cf.fpath,
                cf.cvar,
                dstart=dt.datetime(rystart, 1, 1, 0, 0, 0),
                dend=dt.datetime(ryend, 12, 31, 23, 59, 59),
            )
            # if cvar == 'Precip': print xtime
            if xdata is None:
                venB[cvar] = np.zeros((nlat, nlon))
                msg["Wmsg"].append("WB: Could not find variable: '%s', setting to zero!'" % (cvar))
            else:
                venB[cvar] = np.mean(xdata, 0)
            venB[cvar] = venB[cvar] * 86400.0
        globM[cvar] = compute_area_mean(venB[cvar], grid_area)

    for cvar in cvarsWB:
        msg["Dmsg"].append(
            "WB: Global mean of %s %f (mm day-1) with %s/%s %f"
            % (cvar, globM[cvar], cvar, cvarsWB[0], globM[cvar] / globM[cvarsWB[0]])
        )
        if cvar == "NET":
            msg["Dmsg"].append(
                "WB:"
                + " variable %s with gpmin %e, gpmax %e fldmean %e #gp>thr %i"
                % (
                    cvar,
                    np.min(venB[cvar]),
                    np.max(venB[cvar]),
                    globM[cvar],
                    np.sum(np.abs(venB[cvar]) > 5e-6 * 86400.0),
                )
            )
            if np.sum(np.abs(venB[cvar]) > 5e-6 * 86400.0) > 0:
                msg["Wmsg"].append(
                    "WB:"
                    + " variable %s with gpmin %e, gpmax %e fldmean %e #gp>thr %i"
                    % (
                        cvar,
                        np.min(venB[cvar]),
                        np.max(venB[cvar]),
                        globM[cvar],
                        np.sum(np.abs(venB[cvar]) > 5e-6 * 86400.0),
                    )
                )
        # plt.figure()
        # plt.pcolormesh(vLON,vLAT,venB[cvar]);plt.colorbar()   # for plotting
        # plt.title(cvar)
        # if (cvar == "NET"):
        # plt.figure()
        # xx = venB[cvar]
        # xx[np.abs(venB[cvar])<5e-6*86400.] = 0
        # plt.pcolormesh(vLON,vLAT,venB[cvar]);plt.colorbar()   # for plotting
        # plt.title(cvar+' 0 == ok')
    # plt.show()
    return msg
Пример #3
0
def check_wb(cf,ystart,yend,cdomain,cid,cver,msg=None):
  """
  Water balance check 

  Parameters:
  ----------
  Returns:
  -------
  """
  rystart=ystart
  ryend=yend
  dstart=dt.datetime(cf.ystart,1,1).toordinal()
  dend=dt.datetime(cf.yend,12,31).toordinal()
  xTIME = num2date(np.arange(0,dend-dstart+1,1),"days since %4i-01-01 00:00:00"%cf.ystart)
  tinD=[np.nonzero(xTIME==dt.datetime(rystart,1,1))[0][0],np.nonzero(xTIME==dt.datetime(ryend,12,31))[0][0]]
  ndays=tinD[1]-tinD[0]+1
  #print tinD
  #print ndays
  vLAT,vLON = e2oU.default_latlon(cf.cdomain)
  nlat = len(vLAT)
  nlon = len(vLON)

  grid_area = e2oU.load_grid_area(fgarea)
  cvarsWB=['Precip','Runoff','Evap','Stor','NET']
  if msg is None:
    msg=e2oU.init_msg()


  venB={}
  globM={}  ## global mean values for information only ! 
  for cvar in cvarsWB:

    print 'WB, loading:',cvar

    if cvar == "NET":
      venB[cvar] = np.zeros((nlat,nlon))
      for cc in cvarsWB[:-1]:
        venB[cvar] = venB[cvar] + venB[cc]
    elif cvar == "Stor":
      venB[cvar] = np.zeros((nlat,nlon))
      for svar in ['TotMoist','SWE','CanopInt','SurfStor']:
        print 'WB, loading:',svar
        cf = cf.attr2fpath(cfreq='day',cvar=svar,cdomain=cdomain,cid=cid,cver=cver)
        xdata,xtime = e2oU.load_nc_var(cf.fpath,cf.cvar,tinD=tinD)
        if xdata is None:
          xdata = np.zeros((2,nlat,nlon))
          msg['Wmsg'].append("WB: Could not find variable: '%s', setting to zero!'"%(svar))
        venB[cvar] = venB[cvar] + -1*(xdata[1,:,:] - xdata[0,:,:])/(ndays)
    else:
      cf = cf.attr2fpath(cfreq='mon',cvar=cvar,cdomain=cdomain,cid=cid,cver=cver)
      xdata,xtime = e2oU.load_nc_var(cf.fpath,cf.cvar,dstart=dt.datetime(rystart,1,1,0,0,0),dend=dt.datetime(ryend,12,31,23,59,59))
      #if cvar == 'Precip': print xtime
      if xdata is None:
        venB[cvar] = np.zeros((nlat,nlon))
        msg['Wmsg'].append("WB: Could not find variable: '%s', setting to zero!'"%(cvar))
      else:
        venB[cvar] = np.mean(xdata,0)
      venB[cvar] = venB[cvar]*86400. 
    globM[cvar] = compute_area_mean(venB[cvar],grid_area)

  for cvar in cvarsWB:
    msg['Dmsg'].append("WB: Global mean of %s %f (mm day-1) with %s/%s %f"%
                          (cvar,globM[cvar],cvar,cvarsWB[0],globM[cvar]/globM[cvarsWB[0]]))
    if cvar == "NET" : 
      msg['Dmsg'].append('WB:'+" variable %s with gpmin %e, gpmax %e fldmean %e #gp>thr %i"%
                      (cvar,np.min(venB[cvar]),np.max(venB[cvar]),globM[cvar],np.sum(np.abs(venB[cvar])>5e-6*86400.)))
      if np.sum(np.abs(venB[cvar])>5e-6*86400.) > 0:
        msg['Wmsg'].append('WB:'+" variable %s with gpmin %e, gpmax %e fldmean %e #gp>thr %i"%
                      (cvar,np.min(venB[cvar]),np.max(venB[cvar]),globM[cvar],np.sum(np.abs(venB[cvar])>5e-6*86400.)))
    
    if LPLOT:
    #produce map with WB residuals
    #requires plot_utils from pyutils
      if (cvar == "NET"):
        from pyutils import plot_utils as pu
        import matplotlib.pyplot as plt

        opts={}
        opts['Clevels']=np.arange(-2,2.5,0.5)*86400*5e-6
        opts['cmap']=plt.cm.get_cmap('RdBu')
        fig=pu.plot_map(vLON,vLAT,venB[cvar],titleC='WB residual',titleL=cf.cid,contourf=False,
                        titleR="%i #gp"%np.sum(np.abs(venB[cvar])>5e-6*86400.),Clabel='[mm/day]',**opts)
        fout='map_wb_res_%s_%s_%s_%i_%i.png'%(cf.cid,cf.cver,cf.cdomain,cf.ystart,cf.yend)
        print "Saving:",fout
        fig[0].savefig(fout,bbox_inches="tight",dpi=200)
        plt.close(fig[0])
      
  return msg 
Пример #4
0
def check_eb(cf,ystart,yend,cdomain,cid,cver,msg=None):
  """
  Energy balance check 

  Parameters:
  ----------
  Returns:
  -------
  """
  rystart=ystart
  ryend=yend
  vLAT,vLON = e2oU.default_latlon(cf.cdomain)
  nlat = len(vLAT)
  nlon = len(vLON)

  grid_area = e2oU.load_grid_area(fgarea)
  cvarsEB=['SWnet','LWnet','Qh','Qle','Qsm','NET']
  if msg is None:
    msg=e2oU.init_msg()


  venB={}
  globM={}  ## global mean values for information only ! 
  for cvar in cvarsEB:
    cf = cf.attr2fpath(cfreq='mon',cvar=cvar,cdomain=cdomain,cid=cid,cver=cver)
    print 'EB, loading:',cvar
    
    if cvar == "NET":
      venB[cvar] = np.zeros((nlat,nlon))
      for cc in cvarsEB[:-1]:
        venB[cvar] = venB[cvar] + venB[cc]
    else:
      xdata,xtime = e2oU.load_nc_var(cf.fpath,cf.cvar,dstart=dt.datetime(rystart,1,1),dend=dt.datetime(ryend,12,31))
      if xdata is None:
        venB[cvar] = np.zeros((nlat,nlon))
        msg['Wmsg'].append("EB: Could not find variable: '%s', setting to zero!'"%(cvar))
      else:
        venB[cvar] = np.mean(xdata,0)
    if cvar == "Qsm":
      venB[cvar] = venB[cvar]*3.34e5*-1.
    globM[cvar] = compute_area_mean(venB[cvar],grid_area)

  for cvar in cvarsEB:
    msg['Dmsg'].append("EB: Global mean of %s %f (W m-2) with %s/%s %f"%
                          (cvar,globM[cvar],cvar,cvarsEB[0],globM[cvar]/globM[cvarsEB[0]]))

  if LPLOT:
    #produce map with EB residuals
    #requires plot_utils from pyutils # ask Edutra 
    from pyutils import plot_utils as pu
    import matplotlib.pyplot as plt

    opts={}
    opts['Clevels']=np.linspace(-2,2,10)
    opts['cmap']=plt.cm.get_cmap('RdBu')

    fig=pu.plot_map(vLON,vLAT,venB['NET'],titleC='EB residual',titleL=cf.cid,contourf=False,
                    Clabel='[W m-2]',**opts)
    fout='map_eb_res_%s_%s_%s_%i_%i.png'%(cf.cid,cf.cver,cf.cdomain,cf.ystart,cf.yend)
    print "Saving:",fout
    fig[0].savefig(fout,bbox_inches="tight",dpi=200)
    plt.close(fig[0])
    
  return msg 
Пример #5
0
def check_wb(cf, ystart, yend, cdomain, cid, cver, msg=None):
    """
  Water balance check 

  Parameters:
  ----------
  Returns:
  -------
  """
    rystart = ystart
    ryend = yend
    dstart = dt.datetime(cf.ystart, 1, 1).toordinal()
    dend = dt.datetime(cf.yend, 12, 31).toordinal()
    xTIME = num2date(np.arange(0, dend - dstart + 1, 1),
                     "days since %4i-01-01 00:00:00" % cf.ystart)
    tinD = [
        np.nonzero(xTIME == dt.datetime(rystart, 1, 1))[0][0],
        np.nonzero(xTIME == dt.datetime(ryend, 12, 31))[0][0]
    ]
    ndays = tinD[1] - tinD[0] + 1
    #print tinD
    #print ndays
    vLAT, vLON = e2oU.default_latlon(cf.cdomain)
    nlat = len(vLAT)
    nlon = len(vLON)

    grid_area = e2oU.load_grid_area(fgarea)
    cvarsWB = ['Precip', 'Runoff', 'Evap', 'Stor', 'NET']
    if msg is None:
        msg = e2oU.init_msg()

    venB = {}
    globM = {}  ## global mean values for information only !
    for cvar in cvarsWB:

        print 'WB, loading:', cvar

        if cvar == "NET":
            venB[cvar] = np.zeros((nlat, nlon))
            for cc in cvarsWB[:-1]:
                venB[cvar] = venB[cvar] + venB[cc]
        elif cvar == "Stor":
            venB[cvar] = np.zeros((nlat, nlon))
            for svar in ['TotMoist', 'SWE', 'CanopInt', 'SurfStor']:
                print 'WB, loading:', svar
                cf = cf.attr2fpath(cfreq='day',
                                   cvar=svar,
                                   cdomain=cdomain,
                                   cid=cid,
                                   cver=cver)
                xdata, xtime = e2oU.load_nc_var(cf.fpath, cf.cvar, tinD=tinD)
                if xdata is None:
                    xdata = np.zeros((2, nlat, nlon))
                    msg['Wmsg'].append(
                        "WB: Could not find variable: '%s', setting to zero!'"
                        % (svar))
                venB[cvar] = venB[cvar] + -1 * (xdata[1, :, :] -
                                                xdata[0, :, :]) / (ndays)
        else:
            cf = cf.attr2fpath(cfreq='mon',
                               cvar=cvar,
                               cdomain=cdomain,
                               cid=cid,
                               cver=cver)
            xdata, xtime = e2oU.load_nc_var(
                cf.fpath,
                cf.cvar,
                dstart=dt.datetime(rystart, 1, 1, 0, 0, 0),
                dend=dt.datetime(ryend, 12, 31, 23, 59, 59))
            #if cvar == 'Precip': print xtime
            if xdata is None:
                venB[cvar] = np.zeros((nlat, nlon))
                msg['Wmsg'].append(
                    "WB: Could not find variable: '%s', setting to zero!'" %
                    (cvar))
            else:
                venB[cvar] = np.mean(xdata, 0)
            venB[cvar] = venB[cvar] * 86400.
        globM[cvar] = compute_area_mean(venB[cvar], grid_area)

    for cvar in cvarsWB:
        msg['Dmsg'].append(
            "WB: Global mean of %s %f (mm day-1) with %s/%s %f" %
            (cvar, globM[cvar], cvar, cvarsWB[0],
             globM[cvar] / globM[cvarsWB[0]]))
        if cvar == "NET":
            msg['Dmsg'].append(
                'WB:' +
                " variable %s with gpmin %e, gpmax %e fldmean %e #gp>thr %i" %
                (cvar, np.min(venB[cvar]), np.max(venB[cvar]), globM[cvar],
                 np.sum(np.abs(venB[cvar]) > 5e-6 * 86400.)))
            if np.sum(np.abs(venB[cvar]) > 5e-6 * 86400.) > 0:
                msg['Wmsg'].append(
                    'WB:' +
                    " variable %s with gpmin %e, gpmax %e fldmean %e #gp>thr %i"
                    %
                    (cvar, np.min(venB[cvar]), np.max(venB[cvar]), globM[cvar],
                     np.sum(np.abs(venB[cvar]) > 5e-6 * 86400.)))

        if LPLOT:
            #produce map with WB residuals
            #requires plot_utils from pyutils
            if (cvar == "NET"):
                from pyutils import plot_utils as pu
                import matplotlib.pyplot as plt

                opts = {}
                opts['Clevels'] = np.arange(-2, 2.5, 0.5) * 86400 * 5e-6
                opts['cmap'] = plt.cm.get_cmap('RdBu')
                fig = pu.plot_map(vLON,
                                  vLAT,
                                  venB[cvar],
                                  titleC='WB residual',
                                  titleL=cf.cid,
                                  contourf=False,
                                  titleR="%i #gp" %
                                  np.sum(np.abs(venB[cvar]) > 5e-6 * 86400.),
                                  Clabel='[mm/day]',
                                  **opts)
                fout = 'map_wb_res_%s_%s_%s_%i_%i.png' % (
                    cf.cid, cf.cver, cf.cdomain, cf.ystart, cf.yend)
                print "Saving:", fout
                fig[0].savefig(fout, bbox_inches="tight", dpi=200)
                plt.close(fig[0])

    return msg
Пример #6
0
def check_eb(cf, ystart, yend, cdomain, cid, cver, msg=None):
    """
  Energy balance check 

  Parameters:
  ----------
  Returns:
  -------
  """
    rystart = ystart
    ryend = yend
    vLAT, vLON = e2oU.default_latlon(cf.cdomain)
    nlat = len(vLAT)
    nlon = len(vLON)

    grid_area = e2oU.load_grid_area(fgarea)
    cvarsEB = ['SWnet', 'LWnet', 'Qh', 'Qle', 'Qsm', 'NET']
    if msg is None:
        msg = e2oU.init_msg()

    venB = {}
    globM = {}  ## global mean values for information only !
    for cvar in cvarsEB:
        cf = cf.attr2fpath(cfreq='mon',
                           cvar=cvar,
                           cdomain=cdomain,
                           cid=cid,
                           cver=cver)
        print 'EB, loading:', cvar

        if cvar == "NET":
            venB[cvar] = np.zeros((nlat, nlon))
            for cc in cvarsEB[:-1]:
                venB[cvar] = venB[cvar] + venB[cc]
        else:
            xdata, xtime = e2oU.load_nc_var(cf.fpath,
                                            cf.cvar,
                                            dstart=dt.datetime(rystart, 1, 1),
                                            dend=dt.datetime(ryend, 12, 31))
            if xdata is None:
                venB[cvar] = np.zeros((nlat, nlon))
                msg['Wmsg'].append(
                    "EB: Could not find variable: '%s', setting to zero!'" %
                    (cvar))
            else:
                venB[cvar] = np.mean(xdata, 0)
        if cvar == "Qsm":
            venB[cvar] = venB[cvar] * 3.34e5 * -1.
        globM[cvar] = compute_area_mean(venB[cvar], grid_area)

    for cvar in cvarsEB:
        msg['Dmsg'].append("EB: Global mean of %s %f (W m-2) with %s/%s %f" %
                           (cvar, globM[cvar], cvar, cvarsEB[0],
                            globM[cvar] / globM[cvarsEB[0]]))

    if LPLOT:
        #produce map with EB residuals
        #requires plot_utils from pyutils # ask Edutra
        from pyutils import plot_utils as pu
        import matplotlib.pyplot as plt

        opts = {}
        opts['Clevels'] = np.linspace(-2, 2, 10)
        opts['cmap'] = plt.cm.get_cmap('RdBu')

        fig = pu.plot_map(vLON,
                          vLAT,
                          venB['NET'],
                          titleC='EB residual',
                          titleL=cf.cid,
                          contourf=False,
                          Clabel='[W m-2]',
                          **opts)
        fout = 'map_eb_res_%s_%s_%s_%i_%i.png' % (cf.cid, cf.cver, cf.cdomain,
                                                  cf.ystart, cf.yend)
        print "Saving:", fout
        fig[0].savefig(fout, bbox_inches="tight", dpi=200)
        plt.close(fig[0])

    return msg