About... Prerequisites Examples Home Bugs Download Manual Links

Example 1

Python code that puts the first EOF pattern of an netCDF Sea Level Pressure file into another netCDF file that can be viewed with GrADS:

    from Numeric import *
    from Scientific.IO.NetCDF import *
    from pyclimate.svdeofs import *
    from pyclimate.ncstruct import *
    import math

    def deg2rad(d):
        return d*math.acos(-1)/180.
    inc = NetCDFFile("ncepslp.djf.nc")
    slp = inc.variables["djfslp"]
    areafactor = sqrt(cos(deg2rad(inc.variables["lat"][:])))
    slpdata = slp[:,:,:,:] * areafactor[NewAxis,NewAxis,:,NewAxis]
    oldshape = slpdata.shape
    slpdata.shape = (oldshape[0], oldshape[1]*oldshape[2]*oldshape[3])
    pcs, lambdas, eofs = svdeofs(slpdata)
    dims = ("lat", "lon")
    onc = nccopystruct("firstEOF.nc", inc, dims, dims, dims)
    eof1 = onc.createVariable("eof1", Float32, dims)
    eof1.long_name = "First SLP EOF accounting for %f %% of the variance"%(
    eof1[:,:] = ( reshape(eofs[:,0], oldshape[2:]) / 
                  areafactor[:,NewAxis] ).astype(Float32)

get the code in a file

This graph has been generated by GrADS, using the shell script:
    grads -pb << EOF
    sdfopen firstEOF.nc
    set grads off
    set gxout shaded
    d eof1
    run cbarn
    draw title NAO pattern generated by ejemplo1.py
    enable print eof1.gm
    disable print
    gxgif -i eof1.gm -o ejemplo1.gif
    rm -f eof1.gm

get the script in a file

Contact the