def get_land_ice_mask(fname):
    fland = cdms.open(cmip5.landfrac(fname))
    fglac = cdms.open(cmip5.glacierfrac(fname))
    land = fland("sftlf")
    glacier=fglac("sftgif")

    #mask ocean and ice sheets
    totmask = np.logical_or(land==0,glacier==100.)
   # totmask =np.repeat(totmask.asma()[np.newaxis],12,axis=0)
   

  
    fland.close()
    fglac.close()
    return totmask
def get_tas_and_precip_from_file(fname):
    f = cdms.open(fname)
    tas = cdutil.ANNUALCYCLE.climatology(f("tas",time=('1979-1-1','2005-12-31')))-273.15 #convert from K to C
    
    fpr = cdms.open(get_corresponding_pr(fname))
    pr = cdutil.ANNUALCYCLE.climatology(fpr("pr",time=('1979-1-1','2005-12-31')))*(86400 * 30) #convert to mm
  
    fland = cdms.open(cmip5.landfrac(fname))
    fglac = cdms.open(cmip5.glacierfrac(fname))
    land = fland("sftlf")
    glacier=fglac("sftgif")

    #mask ocean and ice sheets
    totmask = np.logical_or(land==0,glacier==100.)
    totmask =np.repeat(totmask.asma()[np.newaxis],12,axis=0)
    tasmask = MV.masked_where(totmask,tas)
    prmask = MV.masked_where(totmask,pr)

    f.close()
    fpr.close()
    fland.close()
    fglac.close()
    return tasmask,prmask
def PET_from_cmip(fname):
    """
    fname must be a temperature file
    
    Calculate PET from cmip5 data on crunchy
    #Ta      = temperature, degrees K = tas
    #   Rnet    = surface net radiation, W/m2 = (hfls+hfss)
    #  u       = wind speed at 2 meters, m/s = sfcWind (??)
    #   q       = specific humidity, kg/kg = huss

    #  press   = surface pressure, Pascals = ps
     """
    #Get land and ice masks

    temp_variable = fname.split(".")[-4]

    f_ta = cdms.open(fname)
    Ta = f_ta(temp_variable)
    f_ta.close()

    fland = cdms.open(cmip5.landfrac(fname))
    land = fland("sftlf")
    fland.close()
    try:
        fglac = cdms.open(cmip5.glacierfrac(fname))

        glacier = fglac("sftgif")
        fglac.close()
    except:
        glacier = MV.zeros(land.shape)
    #mask ocean and ice sheets
    totmask = np.logical_or(land == 0, glacier == 100.)
    nt = len(Ta.getTime())

    totmask = np.repeat(totmask.asma()[np.newaxis], nt, axis=0)

    if cmip5.get_corresponding_file(fname, "sfcWind") is None:
        #if no surface wind is available, use NCEP reanalyis
        f_wind = cdms.open("wspd.mon.ltm.nc")
        wspd = f_wind("wspd")
        ny = nt / 12
        u = np.repeat(wspd, ny, axis=0)  #Repeat monthly climatologies
        u.setAxisList([Ta.getTime()] + wspd.getAxisList()[1:])
        u.id = "sfcWind"
        f_wind.close()
    else:
        f_wind = cdms.open(cmip5.get_corresponding_file(fname, "sfcWind"))
        u = f_wind("sfcWind")

        f_wind.close()

    f_hfls = cdms.open(cmip5.get_corresponding_file(fname, "hfls"))
    hfls = f_hfls("hfls")
    f_hfls.close()
    # Some models put sfcWind on a different grid to calculate surface fluxes
    if u.shape != hfls.shape:
        u = u.regrid(hfls.getGrid(), regridTool='regrid2')

    f_hfss = cdms.open(cmip5.get_corresponding_file(fname, "hfss"))
    hfss = f_hfss("hfss")
    f_hfss.close()

    Rnet = hfss + hfls

    f_huss = cdms.open(cmip5.get_corresponding_file(fname, "huss"))
    q = f_huss("huss")
    f_huss.close()

    f_ps = cdms.open(cmip5.get_corresponding_file(fname, "ps"))
    press = f_ps("ps")
    f_ps.close()

    corresponding_files = [
        cmip5.get_corresponding_file(fname, var)
        for var in ["hfss", temp_variable, "huss", "ps", "hfls"]
    ]
    L = cmip5.get_common_timeax(corresponding_files)
    if type(L) == type(()):
        PET, VPD, RH = penmont_vpd_SH(Ta(time=L), Rnet(time=L), q(time=L),
                                      press(time=L), u(time=L))
    else:

        PET, VPD, RH = penmont_vpd_SH(Ta[:L], Rnet[:L], q[:L], press[:L],
                                      u[:L])

    PET = MV.masked_where(totmask, PET)
    PET.setAxisList(Ta.getAxisList())
    PET.id = "PET"

    VPD = MV.masked_where(totmask, VPD)
    VPD.setAxisList(Ta.getAxisList())
    VPD.id = "VPD"

    RH = MV.masked_where(totmask, RH)
    RH.setAxisList(Ta.getAxisList())
    RH.id = "RH"

    return PET, VPD, RH
def PET_from_cmip(fname, temp_variable="tas"):

    #Ta      = temperature, degrees K = tas
    #   Rnet    = surface net radiation, W/m2 = (hfls+hfss)
    #  u       = wind speed at 2 meters, m/s = sfcWind (??)
    #   q       = specific humidity, kg/kg = huss
    #  press   = surface pressure, Pascals = ps

    #Get land and ice masks
    fland = cdms.open(cmip5.landfrac(fname))
    fglac = cdms.open(cmip5.glacierfrac(fname))
    land = fland("sftlf")
    glacier = fglac("sftgif")

    #mask ocean and ice sheets
    totmask = np.logical_or(land == 0, glacier == 100.)

    f_wind = cdms.open(fname)
    u = f_wind("sfcWind")
    f_wind.close()

    nt = len(u.getTime())

    totmask = np.repeat(totmask.asma()[np.newaxis], nt, axis=0)

    f_hfls = cdms.open(cmip5.get_corresponding_file(fname, "hfls"))
    hfls = f_hfls("hfls")
    f_hfls.close()

    f_hfss = cdms.open(cmip5.get_corresponding_file(fname, "hfss"))
    hfss = f_hfss("hfss")
    f_hfss.close()

    Rnet = hfss + hfls

    f_ta = cdms.open(cmip5.get_corresponding_file(fname, temp_variable))
    Ta = f_ta(temp_variable)
    f_ta.close()

    f_huss = cdms.open(cmip5.get_corresponding_file(fname, "huss"))
    q = f_huss("huss")
    f_huss.close()

    f_ps = cdms.open(cmip5.get_corresponding_file(fname, "ps"))
    press = f_ps("ps")
    f_ps.close()

    PET, VPD, RH = penmont_vpd_SH(Ta, Rnet, q, press, u)

    PET = MV.masked_where(totmask, PET)
    PET.setAxisList(u.getAxisList())
    PET.id = "PET"

    VPD = MV.masked_where(totmask, VPD)
    VPD.setAxisList(u.getAxisList())
    VPD.id = "VPD"

    RH = MV.masked_where(totmask, RH)
    RH.setAxisList(u.getAxisList())
    RH.id = "RH"

    return PET, VPD, RH
Exemple #5
0
def PET_from_cmip(fname, temp_variable="tas"):

    #Ta      = temperature, degrees K = tas
    #   Rnet    = surface net radiation, W/m2 = (hfls+hfss)
    #  u       = wind speed at 2 meters, m/s = sfcWind (??)
    #   q       = specific humidity, kg/kg = huss

    #  press   = surface pressure, Pascals = ps

    #Get land and ice masks
    fland = cdms.open(cmip5.landfrac(fname))
    land = fland("sftlf")
    fland.close()
    try:
        fglac = cdms.open(cmip5.glacierfrac(fname))

        glacier = fglac("sftgif")
        fglac.close()
    except:
        glacier = MV.zeros(land.shape)
    #mask ocean and ice sheets
    totmask = np.logical_or(land == 0, glacier == 100.)

    f_wind = cdms.open(fname)
    u = f_wind("sfcWind")
    f_wind.close()

    nt = len(u.getTime())

    totmask = np.repeat(totmask.asma()[np.newaxis], nt, axis=0)

    f_hfls = cdms.open(cmip5.get_corresponding_file(fname, "hfls"))
    hfls = f_hfls("hfls")
    f_hfls.close()
    # Some models put sfcWind on a different grid to calculate surface fluxes
    if u.shape != hfls.shape:
        u = u.regrid(hfls.getGrid(), regridTool='regrid2')

    f_hfss = cdms.open(cmip5.get_corresponding_file(fname, "hfss"))
    hfss = f_hfss("hfss")
    f_hfss.close()

    Rnet = hfss + hfls

    f_ta = cdms.open(cmip5.get_corresponding_file(fname, temp_variable))
    Ta = f_ta(temp_variable)
    f_ta.close()

    f_huss = cdms.open(cmip5.get_corresponding_file(fname, "huss"))
    q = f_huss("huss")
    f_huss.close()

    f_ps = cdms.open(cmip5.get_corresponding_file(fname, "ps"))
    press = f_ps("ps")
    f_ps.close()

    corresponding_files = [
        cmip5.get_corresponding_file(fname, var)
        for var in ["hfss", temp_variable, "huss", "ps", "hfls"]
    ]
    L = cmip5.get_common_timeax(corresponding_files)

    PET, VPD, RH = penmont_vpd_SH(Ta[:L], Rnet[:L], q[:L], press[:L], u[:L])

    PET = MV.masked_where(totmask, PET)
    PET.setAxisList(Ta.getAxisList())
    PET.id = "PET"

    VPD = MV.masked_where(totmask, VPD)
    VPD.setAxisList(Ta.getAxisList())
    VPD.id = "VPD"

    RH = MV.masked_where(totmask, RH)
    RH.setAxisList(Ta.getAxisList())
    RH.id = "RH"

    return PET, VPD, RH