Example #1
0
def genEofVars(infiles, outfile, eobjf=True, latitude=(-30, 30, 'cob'),
                          NEOF=4, season='all', year=None, **kwarg):

    """

    """
    eofobj_endname = kwarg.get('eofobj_endname', None)

    for name, varName, infile in infiles:
        if season == 'all':
            f = cdms2.open(infile)
            if year:
                # add the time axis year statements here in future.
                data = f(varName, time=year, latitude=latitude)
            else:
                data = f(varName, latitude=latitude)
            # end of if year:
        elif season == 'sum':
            data = timeobj.getSummerData(varName, infile, latitude=latitude,
                                                                 year=year)
        elif season == 'win':
            data = timeobj.getWinterData(varName, infile, latitude=latitude,
                                                 year=year, cyclic=True)
        else:
            raise ValueError("arg 'season' must be either 'all/sum/win' only")
        # end of if season == 'all':

        data = data(squeeze=1)
        cdutil.setSlabTimeBoundsDaily(data)

        print "Multiplying coslat with data of %s for %s" % (name, season)
        lat = data.getLatitude()
        coslat = _coslat_weights(lat)
        for l in range(len(lat)):
            data[:, l] *= coslat[l]
        # end of for l in range(len(lat)):
        print "Doing EOF of %s for %s" % (name, season)
        eofobj = Eof(data, weights=None)

        if eobjf:
            # generate the eofobj binary file name and its path
            path = os.path.dirname(outfile)
            eofobj_filename = ['eofobj', 'level1', name, season]
            eofobj_filename = '_'.join(eofobj_filename)
            if eofobj_endname: eofobj_filename += '_' + eofobj_endname
            eofobj_filename += '.pkl'
            # end of if not eofobj_filename:
            eofobj_fpath = os.path.join(path, eofobj_filename)
            # store the eofobj into binary file using pickle module
            objf = open(eofobj_fpath, 'wb')
            pickle.dump(eofobj, objf, 2)
            comment = ''
            pickle.dump(comment, objf, 2)
            objf.close()
            print "Saved the eofobj in", eofobj_fpath
        # end of if eobjf:

        pcts = eofobj.pcs(pcscaling=0, npcs=2)
        eof_data = eofobj.eofs(neofs=NEOF, eofscaling=2)
        per_exp = eofobj.varianceFraction(neigs=NEOF) * 100

        pcts.id = '_'.join(['pcs', name, season])
        pcts.comment = ''
        per_exp.id = '_'.join(['per_exp', name, season])
        per_exp.comment = ''
        eof_data.id = '_'.join(['eof', name, season])
        eof_data.comment = ''

        out = cdms2.open(outfile, 'a')
        out.write(pcts)
        out.write(per_exp)
        out.write(eof_data)
        out.close()

        # make memory free
        del data, pcts, per_exp, eof_data, lat
    # for name, varName, infile in infiles:
    print "Saved the eof_vars_*.nc file in", outfile