Exemplo n.º 1
0
def Tref(fname='', pctl='', varin='', styr=0, enyr=0):
    # to create a file with 366 time steps and a TNN value for each cell
    # uses a 5-day running mean
    if pctl <= 0 or pctl >= 100:
        raise 'percentile out of range'
    if not styr > 1899 and enyr < 2008 and (enyr > styr):
        raise 'incorrect args passed to Tref %s %d %d' % (fname, styr, enyr)
    if varin == 'tasmin':
        varout = 'TN' + str(pctl) + 'Pref'
    elif varin == 'tasmax':
        varout = 'TX' + str(pctl) + 'Pref'
    elif varin == 'tas':
        varout = 'TG' + str(pctl) + 'Pref'
    else:
        raise "invalid input variable in Tref\n"
    nyrs = enyr - styr + 1
    fn_nodir = split(fname, "/")[-1]
    ofall = OUTROOT + "/" + fn_nodir + str(styr) + "-" + str(enyr) + ".nc"
    ofall = ofall.replace(varin, varout)
    ofbig = OUTTEMP + "/" + fn_nodir + "all." + str(styr) + "-" + str(
        enyr) + ".nc"
    txtcmd = "ncrcat -n " + str(
        nyrs) + ",4,1 " + OUTTEMP + "/" + fn_nodir + str(styr) + ".nc " + ofbig
    if not path.exists(ofbig):
        system(txtcmd)
        print "created combined file ", ofbig
    else:
        print "combined file already exists: ", ofbig
    ofpartflist = []
    #slice big file spatially - 18 zonal slices
    for i in range(18):
        lat1 = -90 + (i * 10)
        lat2 = -90 + ((i + 1) * 10)
        filepart = "sellonlatbox,0,360," + str(lat1) + "," + str(lat2)
        ofjunk = OUTTEMP + "/" + varin + "_ref_ofbigpart.nc"
        ofpart = OUTTEMP + "/" + varin + "_ref_ydrunpctl" + str(
            pctl) + "." + str(i) + ".nc"
        txt = "cdo -m 1e+20 sellonlatbox,0,360," + str(lat1) + "," + str(
            lat2) + " " + ofbig + " " + ofjunk
        system(txt)
        txtcmd = "cdo -m 1e+20 ydrunpctl," + str(
            pctl
        ) + ",5 " + ofjunk + " -ydrunmin,5 " + ofjunk + " -ydrunmax,5 " + ofjunk + " " + ofpart
        system(txtcmd)
        ofpartflist.append(ofpart)
    print "finished separate files, putting them back together..."
    MosaicFiles(ofpartflist, varin, ofall)
    #done with aggregation
    now = datetime.now()
    txthist = "Created on " + now.strftime("%Y-%m-%d %H:%M")
    txtcmd = "ncatted -h -a history,global,o,c,'" + txthist + "' " + ofall
    system(txtcmd)
    txtcmd = "ncatted -h -a institution,global,c,c,'" + txtinst + "' " + ofall
    system(txtcmd)
    txtcmd = "ncrename -h -v " + varin + "," + varout.lower() + " " + ofall
    system(txtcmd)
    return ofall
Exemplo n.º 2
0
def R90ref(fname='', styr=0, enyr=0):
    #to create a file with 366 time steps and a 90pctl value for each cell, cat all files together:
    #uses a 5-day running mean
    if not styr > 1899 and enyr < 2006 and (enyr > styr):
        raise 'incorrect args passed to R90ref %s %d %d' % (fname, styr, enyr)
    nyrs = enyr - styr + 1
    fn_nodir = split(fname, "/")[-1]
    ofall = OUTROOT + "/" + fn_nodir + str(styr) + "-" + str(enyr) + ".nc"
    ofall = ofall.replace('pr', 'R90ref')
    ofbig = OUTTEMP + "/" + fn_nodir + "all." + str(styr) + "-" + str(
        enyr) + ".nc"
    txtcmd = "ncrcat -n " + str(
        nyrs) + ",4,1 " + OUTTEMP + "/" + fn_nodir + str(styr) + ".nc " + ofbig
    if not path.exists(ofbig):
        system(txtcmd)
        print "created combined file ", ofbig
    else:
        print "combined file already exists: ", ofbig
    ofpartflist = []
    #for only rainy days (P>1 mm)
    #slice big file spatially - 18 zonal slices
    for i in range(18):
        lat1 = -90 + (i * 10)
        lat2 = -90 + ((i + 1) * 10)
        filepart = "sellonlatbox,0,360," + str(lat1) + "," + str(lat2)
        ofjunk = OUTTEMP + "/r90pref_ofbigpart.nc"
        ofjunkm = OUTTEMP + "/r90pref_ofbigpart_masked.nc"
        ofpart = OUTTEMP + "/r90pref_ydrunpctl90." + str(i) + ".nc"
        txt = "cdo -m 1e+20 sellonlatbox,0,360," + str(lat1) + "," + str(
            lat2) + " " + ofbig + " " + ofjunk
        system(txt)
        txt1 = "cdo ifthen -gtc,1 " + ofjunk + " " + ofjunk + " " + ofjunkm
        system(txt1)
        txtcmd = "cdo -m 1e+20 ydrunpctl,90,5 " + ofjunkm + " -ydrunmin,5 " + ofjunkm + " -ydrunmax,5 " + ofjunkm + " " + ofpart
        system(txtcmd)
        ofpartflist.append(ofpart)
    print "finished separate files, putting them back together..."
    MosaicFiles(ofpartflist, 'pr', ofall)
    #done with aggregation
    now = datetime.now()
    txthist = "Created on " + now.strftime("%Y-%m-%d %H:%M")
    txtcmd = "ncatted -h -a history,global,o,c,'" + txthist + "' " + ofall
    system(txtcmd)
    txtcmd = "ncatted -h -a institution,global,c,c,'" + txtinst + "' " + ofall
    system(txtcmd)
    txtcmd = "ncrename -h -v pr,r90ref " + ofall
    system(txtcmd)
    return ofall