Exemple #1
0
def accumulate24Hourly(data):
    """Returns 12-hourly data accumulated to 24-hours."""
    newTimeValues=[]
    taxis=data.getTime()
    tunits=data.units
    print len(data.getTime())
    newarray=[]

    for i in range((tlen/2)):
        p1=data(time=slice(i,i+1))
        p2=data(time=slice(i+1,i+2))
        accum=p1+p2
        newarray.append(accum)
        newTimeValues.append(p2.getTime()[0])

    array=MA.concatenate(newarray)
    array=MA.array(array, 'f', fill_value=data.getMissing())
    axes=data.getAxisList()
    newTimeAxis=cdms.createAxis(newTimeValues)
    newTimeAxis.units=tunits
    newTimeAxis.designateTime()
    newTimeAxis.id=newTimeAxis.long_name=newTimeAxis.title="time"
    
    newaxes=[newTimeAxis]+axes[1:]
    var=cdms.createVariable(array, axes=newaxes, id=data.id)
    for att in ("units", "long_name"):
        setattr(var, att, getattr(data, att))
    return var 
def flatten2DTimeData(var, time_var):
    """
    Returns a flattened 2D array variable with a recalculated time axies.
    """
    data = MV.ravel(var)
    missing = var.getMissing()
    time_values = time_var._data
    time_unit = time_var.units
    sampling_rate = var.getAxis(1)
    rate = int(sampling_rate.id[3:])
    newtime_values = []

    for t in time_values:
        for i in range(rate):
            tvalue = t + ((1./rate)*i)
            newtime_values.append(tvalue)

    new_time_ax = cdms.createAxis(newtime_values)
    new_time_ax.units = time_unit
    new_time_ax.designateTime()
    new_time_ax.id = new_time_ax.long_name = new_time_ax.standard_name = "time"

    atts = var.attributes
    newAtts = {}
    for att,value in atts.items():
        if type(value) in (type((1,1)), type([1,2]), type(Numeric.array([1.]))) and len(value) == 1:
            value = value[0]
        if type(value) in (type(1), type(1.), type(long(1))):
            newAtts[att] = value
        elif type(value) == type("string"):
            newAtts[att] = value.strip()

    # Now create the variable
    newvar = cdms.createVariable(data, id=var.id, fill_value=missing, axes=[new_time_ax], attributes=newAtts)
    return newvar
Exemple #3
0
def getNewLonAxis(oldLonAxis, startLon=-180):
    assert (oldLonAxis.isLongitude())
    oldLonVals = oldLonAxis.getData()

    newLonVals = (oldLonVals - startLon
                  ) % 360 + startLon  # range "startLon" to "startLon + 360"
    lonValShift = newLonVals - oldLonVals
    nlon = len(newLonVals)
    ##
    ## identify shift by jump in longitude values
    for i in range(1, nlon):
        if newLonVals[i] < newLonVals[i - 1]:
            nShift = i
            break
    else:
        nShift = 0

## create array with new longitude values
    newLonVals = MA.concatenate((newLonVals[nShift:], newLonVals[:nShift]))

    ##    print oldLonVals
    ##    print newLonVals

    newLonAxis = cdms.createAxis(newLonVals)
    newLonAxis.designateLongitude()
    newLonAxis.replace_external_attributes(oldLonAxis.attributes)
    #
    return newLonAxis, nShift, lonValShift
Exemple #4
0
def flatten2DTimeData(var, time_var):
    """
    Returns a flattened 2D array variable with a recalculated time axies.
    """
    data = MV.ravel(var)
    missing = var.getMissing()
    time_values = time_var._data
    time_unit = time_var.units
    sampling_rate = var.getAxis(1)
    rate = int(sampling_rate.id[3:])
    newtime_values = []

    for t in time_values:
        for i in range(rate):
            tvalue = t + ((1. / rate) * i)
            newtime_values.append(tvalue)

    new_time_ax = cdms.createAxis(newtime_values)
    new_time_ax.units = time_unit
    new_time_ax.designateTime()
    new_time_ax.id = new_time_ax.long_name = new_time_ax.standard_name = "time"

    atts = var.attributes
    newAtts = {}
    for att, value in atts.items():
        if type(value) in (type((1, 1)), type([1, 2]), type(Numeric.array(
            [1.]))) and len(value) == 1:
            value = value[0]
        if type(value) in (type(1), type(1.), type(long(1))):
            newAtts[att] = value
        elif type(value) == type("string"):
            newAtts[att] = value.strip()

    # Now create the variable
    newvar = cdms.createVariable(data,
                                 id=var.id,
                                 fill_value=missing,
                                 axes=[new_time_ax],
                                 attributes=newAtts)
    return newvar
Exemple #5
0
    def _flatten2DTimeAxis(self, timevar, samplingRate):
        """
        Returns a flattened 2D time axis.
        """
        if samplingRate!=1:
            raise "Cannot yet deal with sub-sampling to non 1Hz sampling rates!"

        timevalues=timevar._data
        timeunit=timevar.units
        newTimeValues=[]
        rate=samplingRate

        for t in timevalues:
            for i in range(rate):
                tvalue=t+((1./rate)*i)
                newTimeValues.append(tvalue)

        newTimeAx=cdms.createAxis(newTimeValues)
        newTimeAx.units=timeunit
        newTimeAx.designateTime()
        newTimeAx.id=newTimeAx.long_name=newTimeAx.standard_name="time"
        return newTimeAx
    def _flatten2DTimeAxis(self, timevar, samplingRate):
        """
        Returns a flattened 2D time axis.
        """
        if samplingRate != 1:
            raise "Cannot yet deal with sub-sampling to non 1Hz sampling rates!"

        timevalues = timevar._data
        timeunit = timevar.units
        newTimeValues = []
        rate = samplingRate

        for t in timevalues:
            for i in range(rate):
                tvalue = t + ((1.0 / rate) * i)
                newTimeValues.append(tvalue)

        newTimeAx = cdms.createAxis(newTimeValues)
        newTimeAx.units = timeunit
        newTimeAx.designateTime()
        newTimeAx.id = newTimeAx.long_name = newTimeAx.standard_name = "time"
        return newTimeAx
Exemple #7
0
def writeOutput(infile, var, lat, lon, dav, dmax, dmin, sdupper, sdlower, location):
    """
    Writes an output file of the variables generated.
    """

    location=location.replace(" ","_").lower()
    f=cdms.open(infile)
    
    mapit={"apcp":("rainfall","l/m^2"), "tmpk":("temperature","K")} 
    varname=mapit[var][0]
    units=mapit[var][1]
    datetime=os.path.split(infile)[-1].split(".")[1]
    outfile="%s_%s_%s.nc" % (datetime, location, varname)
    outpath=os.path.split(infile)[0]
    outfile=os.path.join(outpath, outfile)
    fout=cdms.open(outfile, "w")
    latax=cdms.createAxis([lat])
    latax.units="degrees_north"
    latax.id=latax.standard_name=latax.long_name="latitude"
    lonax=cdms.createAxis([lon])
    lonax.units="degrees_east"
    lonax.id=lonax.standard_name=lonax.long_name="longitude"   
    tax=f[var].getTime() #f(var, level=slice(0,1), lat=slice(0,1), lon=slice(0,1)).getTime()
    timeax=cdms.createAxis(Numeric.array(tax[0:tlen],'d'))
    timeax.designateTime()
    timeax.units=tax.units
    #timeax.id=timeax.standard_name=timeax.long_name="time"
    timeax.id="time"
    timeax.title=tax.title
    timeax.delta_t=tax.delta_t
    timeax.init_time=tax.init_time
    timeax.actual_range=tax.actual_range
    del timeax.axis
    del timeax.calendar
    metadata=f[var]
    fv=metadata.missing_value
    newshape=(len(timeax), len(latax), len(lonax))
    
    maxFound=20. # Set as our max value if not greater
    
    for v in ((dav, "average"), (dmax, "maximum"), (dmin, "minimum"), \
      (sdupper, "plus_std_dev"), (sdlower, "minus_std_dev"), ("always10", "always10")):
        if type(v[0])==type("jlj") and v[0]=="always10": 
            print "Creating always equal to 10 variable."
            always10=MA.zeros(newshape, 'f')+10.
            #print always10.shape, dav.shape, type(dav)
            newvar=cdms.createVariable(always10,  axes=[timeax, latax, lonax], id=v[1], fill_value=fv)
            newvar.longname="always10"
        else:
            data=v[0]
            name=varname+"_"+v[1]
            if not type(data)==type([1,2]):
                data=data(squeeze=1)
            data=MA.resize(data, newshape)
            newvar=cdms.createVariable(data, axes=[timeax, latax, lonax], id=name, fill_value=fv)
            newvar.long_name="%s - %s" % (varname.title(), v[1].replace("_", " "))
            newvar.units=metadata.units

        (dummy,vmax)=vcs.minmax(newvar)
        if vmax>maxFound:
            maxFound=vmax
        fout.write(newvar)
        fout.sync()
        del newvar

    fout.close()
    return (outfile, varname, datetime, maxFound)
 file_xml = os.path.join(sys.prefix,'sample_data/'+var+'_'+model[i]+'.xml')
 #a=cdms.open('/pcmdi/AMIP3/amip/mo/'+var+'/'+model[i]+'/'+var+'_'+model[i]+'.xml')
 a=cdms.open(file_xml)
 data=a(var,time=(start_time,end_time),squeeze=1)
 a.close()
 ac=cdutil.ANNUALCYCLE.climatology(data(time=(start_time, end_time, 'cob')))
 data_an=cdutil.ANNUALCYCLE.departures(data,ref=ac)
 print i,model[i],data.shape, data_an.shape
 glan[i,:]=cdutil.averager(data_an,axis='xy')

#
# setup metadata and write out to a netcdf file
#
tim=data.getTime()
runs=Numeric.arange(0,len(model))
runs=cdms.createAxis(runs,id='models')
glan=cdms.createVariable(glan,axes=(runs,tim),id='global_'+var+'_anomalies')
#
print 'start write file..'
q=cdms.open('global_anomalies.nc','w')
q.model_designation=model_description
q.write(glan)
q.close()
#
# a simple plot
#
x=vcs.init()
x.setcolormap('default')
x.plot(glan)
#
print ""