Example #1
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)