Exemple #1
0
    def spatial_rmsc(self):
        """ RMS centree pour chaque pas de temps """
        from genutil import statistics
        import cdms2
        import numpy as np
        # centered and biased RMS
        self.spa_rmsc = list() #Initialise une liste

        # Estimation de la std a chaque pas de temps
        tps = self.obs.getTime()
        for i,  t in enumerate(tps):
            minterm = np.reshape(self.model[i, :, :], (self.model.shape[-1]*self.model.shape[-2]))
            ointerm = np.reshape(self.obs[i, :, :], (self.obs.shape[-1]*self.obs.shape[-2]))
            #modif J.Gatti - pour MFS
            if not minterm._grid_  is None:
                minterm._grid_=None
            if not ointerm._grid_  is None:
                ointerm._grid_=None
            #fin modif J.Gatti
            self.spa_rmsc.append(statistics.rms(minterm, ointerm, centered=1))

        # Transformation en variable cdms2
        self.spa_rmsc = cdms2.createVariable(self.spa_rmsc,typecode='f',id='spa_rmsc', attributes=dict(units=self.model.units))


        # Ajout de l'axe des temps correspondant a self...
        self.spa_rmsc.setAxis(0, tps)
Exemple #2
0
    def spatial_rmsc(self):
        """ RMS centree pour chaque pas de temps """
        from genutil import statistics
        import cdms2
        import numpy as np
        # centered and biased RMS
        self.spa_rmsc = list()  #Initialise une liste

        # Estimation de la std a chaque pas de temps
        tps = self.obs.getTime()
        for i, t in enumerate(tps):
            minterm = np.reshape(self.model[i, :, :],
                                 (self.model.shape[-1] * self.model.shape[-2]))
            ointerm = np.reshape(self.obs[i, :, :],
                                 (self.obs.shape[-1] * self.obs.shape[-2]))
            #modif J.Gatti - pour MFS
            if not minterm._grid_ is None:
                minterm._grid_ = None
            if not ointerm._grid_ is None:
                ointerm._grid_ = None
            #fin modif J.Gatti
            self.spa_rmsc.append(statistics.rms(minterm, ointerm, centered=1))

        # Transformation en variable cdms2
        self.spa_rmsc = cdms2.createVariable(
            self.spa_rmsc,
            typecode='f',
            id='spa_rmsc',
            attributes=dict(units=self.model.units))

        # Ajout de l'axe des temps correspondant a self...
        self.spa_rmsc.setAxis(0, tps)
Exemple #3
0
    def temporal_rms(self):
        """ RMS entre modele et observations - calculee en chaque point sur la dimension
            temporelle / Resultat => Carte de RMS uncentered and biased """
        from genutil import statistics
        from vacumm.misc.grid import get_grid, set_grid

        self.temp_rms = ()  #Initialise un tuple

        self.temp_rms = statistics.rms(self.model, self.obs, axis=0)

        gg = get_grid(self.model)
        set_grid(self.temp_rms, gg, axes=True)
Exemple #4
0
    def temporal_rmsc(self):
        """ RMS entre modele et observations - calculee en chaque point sur la dimension
            temporelle / Resultat => Carte de RMS centered and biased """
        from genutil import statistics
        from vacumm.misc.grid import get_grid,  set_grid


        self.temp_rmsc = () #Initialise un tuple

        self.temp_rmsc = statistics.rms(self.model, self.obs, axis = 0,  centered = 1)


        gg = get_grid(self.model)
        set_grid(self.temp_rmsc, gg, axes=True)
Exemple #5
0
#
ncep = os.path.join(sys.prefix, 'sample_data', 'tas_mo.nc')
f = cdms2.open(ncep)

ncep1 = f('tas', time = (a1, b1, 'co'))
ncep2 = f('tas', time = (a2, b2, 'co'))
#
# Note that by using the 'co' option above we specified that the end
# point is 'open'
#

#
# Now we compute the "spatial" rms difference -
# "spatial" is specified by setting the axis='xy' option
#
rms = statistics.rms(ncep1, ncep2, axis='xy')
print 'RMS difference (spatial) between the 2 periods:'
print rms

#
# Similarly the "mean absolute difference" is computed...
#
absd = statistics.meanabsdiff(ncep1,ncep2,axis='xy')
print 'Mean absolute difference:'
print absd


#*******************************************************************************
#
# Example 2:
#           In this example, we compute the spatial correlation between 2 fields
Exemple #6
0
#
ncep = os.path.join(cdat_info.get_prefix(), 'sample_data', 'tas_mo.nc')
f = cdms2.open(ncep)

ncep1 = f('tas', time=(a1, b1, 'co'))
ncep2 = f('tas', time=(a2, b2, 'co'))
#
# Note that by using the 'co' option above we specified that the end
# point is 'open'
#

#
# Now we compute the "spatial" rms difference -
# "spatial" is specified by setting the axis='xy' option
#
rms = statistics.rms(ncep1, ncep2, axis='xy')
print 'RMS difference (spatial) between the 2 periods:'
print rms

#
# Similarly the "mean absolute difference" is computed...
#
absd = statistics.meanabsdiff(ncep1, ncep2, axis='xy')
print 'Mean absolute difference:'
print absd

#*******************************************************************************
#
# Example 2:
#           In this example, we compute the spatial correlation between 2 fields
#           - say the mean surface air temperature for the 1980-1989 period
# global differences
gl_diff = cdutil.averager(diff, axis='xy')

x = vcs.init()
x.setcolormap('default')
fill = x.getisofill('default')
x.plot(z_diff, fill)

x.clear()
x.plot(gl_diff)

cor_t = statistics.correlation(data1, data2, axis='xy')
# temporal correlation map betwen these to time-series
cor_m = statistics.correlation(data1, data2, axis='t')
# temporal rms difference between the two time series
rms = statistics.rms(data1, data2, axis='t')

x.clear()
x.plot(cor_m, fill)

x.clear()
x.plot(cor_t)

x.clear()
x.plot(rms, fill)

slope1, intercept1 = statistics.linearregression(data1, axis='t')
slope2, intercept2 = statistics.linearregression(data2, axis='t')
# set the 'id'
slope1.id = 'linear_slope'
slope2.id = 'linear_slope'
# False data
import numpy as N
# - time axis
t = N.linspace(0., 100., 200)
# - observations
obs = N.sin(t)
# - model
nm = 50
mod = N.resize(obs, (nm, len(t)))
mod += N.random.uniform(-3,  3, mod.shape)+.5

# Make stats
from genutil.statistics import rms
bias = (mod-obs).mean(axis=1)
crms = rms(mod, N.resize(obs, mod.shape), centered=1, axis=1)
stdmod = mod.std(axis=1)
stdref = obs.std()

# Plot
from vacumm.misc.plot import dtarget
dtarget(bias, crms, stdmod, stdref, colors='rms', show=False, units='m', 
    scatter_alpha=0.5, sizes=40, savefigs=__file__, close=True)
# Test taylor diagrom here:
TSstd1, TSstd2 = [], []
TSrms1, TSrms2 = [], []
TScor1, TScor2 = [], []
flag = 1
for idx in range(OBSsp.shape[0]):
    aaa = ERAsp[idx]
    bbb = NOAsp[idx]
    ccc = OBSsp[idx]
    std0 = stat.std(ccc, axis='yx')
    std1 = stat.std(aaa, axis='yx') / std0
    std2 = stat.std(bbb, axis='yx') / std0
    cor1 = stat.correlation(aaa, ccc, axis='yx')
    cor2 = stat.correlation(bbb, ccc, axis='yx')
    rms1 = stat.rms(aaa, ccc, axis='yx', centered=0)
    rms2 = stat.rms(bbb, ccc, axis='yx', centered=0)
    TSstd1.append(std1)
    TSstd2.append(std2)
    TSrms1.append(rms1)
    TSrms2.append(rms2)
    TScor1.append(cor1)
    TScor2.append(cor2)
    if flag == 1:
        sm.taylor_diagram(np.array([1.0, np.mean(TSstd1)]),
                          np.array([0.0, np.mean(TSrms1)]),
                          np.array([1.0, np.mean(TScor1)]),
                          markerColor='firebrick',
                          colOBS='black',
                          markerobs='x')
        flag += 1
def monsoon_wang_runner(args):
    # args = P.parse_args(sys.argv[1:])
    modpath = genutil.StringConstructor(args.test_data_path)
    modpath.variable = args.modvar
    outpathdata = args.results_dir
    if isinstance(args.modnames, str):
        mods = eval(args.modnames)
    else:
        mods = args.modnames

    json_filename = args.outnamejson

    if json_filename == "CMIP_MME":
        json_filename = "/MPI_" + args.mip + "_" + args.experiment

    # VAR IS FIXED TO BE PRECIP FOR CALCULATING MONSOON PRECIPITATION INDICES
    var = args.modvar
    thr = args.threshold
    sig_digits = ".3f"

    # Get flag for CMEC output
    cmec = args.cmec

    #########################################
    # PMP monthly default PR obs
    cdms2.axis.longitude_aliases.append("longitude_prclim_mpd")
    cdms2.axis.latitude_aliases.append("latitude_prclim_mpd")
    fobs = cdms2.open(args.reference_data_path)
    dobs_orig = fobs(args.obsvar)
    fobs.close()

    obsgrid = dobs_orig.getGrid()

    ########################################

    # FCN TO COMPUTE GLOBAL ANNUAL RANGE AND MONSOON PRECIP INDEX

    annrange_obs, mpi_obs = mpd(dobs_orig)
    #########################################
    # SETUP WHERE TO OUTPUT RESULTING DATA (netcdf)
    nout = os.path.join(outpathdata,
                        "_".join([args.experiment, args.mip, "wang-monsoon"]))
    try:
        os.makedirs(nout)
    except BaseException:
        pass

    # SETUP WHERE TO OUTPUT RESULTS (json)
    jout = outpathdata
    try:
        os.makedirs(nout)
    except BaseException:
        pass

    gmods = []  # "Got" these MODS
    for i, mod in enumerate(mods):
        modpath.model = mod
        for k in modpath.keys():
            try:
                val = getattr(args, k)
            except Exception:
                continue
            if not isinstance(val, (list, tuple)):
                setattr(modpath, k, val)
            else:
                setattr(modpath, k, val[i])
        l1 = modpath()
        if os.path.isfile(l1) is True:
            gmods.append(mod)

    if len(gmods) == 0:
        raise RuntimeError("No model file found!")
    #########################################

    egg_pth = resources.resource_path()
    globals = {}
    locals = {}
    exec(
        compile(
            open(os.path.join(egg_pth, "default_regions.py")).read(),
            os.path.join(egg_pth, "default_regions.py"),
            "exec",
        ),
        globals,
        locals,
    )
    regions_specs = locals["regions_specs"]
    doms = ["AllMW", "AllM", "NAMM", "SAMM", "NAFM", "SAFM", "ASM", "AUSM"]

    mpi_stats_dic = {}
    for i, mod in enumerate(gmods):
        modpath.model = mod
        for k in modpath.keys():
            try:
                val = getattr(args, k)
            except Exception:
                continue
            if not isinstance(val, (list, tuple)):
                setattr(modpath, k, val)
            else:
                setattr(modpath, k, val[i])
        modelFile = modpath()

        mpi_stats_dic[mod] = {}

        print(
            "******************************************************************************************"
        )
        print(modelFile)
        f = cdms2.open(modelFile)
        d_orig = f(var)

        annrange_mod, mpi_mod = mpd(d_orig)
        annrange_mod = annrange_mod.regrid(obsgrid,
                                           regridTool="regrid2",
                                           regridMethod="conserve",
                                           mkCyclic=True)
        mpi_mod = mpi_mod.regrid(obsgrid,
                                 regridTool="regrid2",
                                 regridMethod="conserve",
                                 mkCyclic=True)

        for dom in doms:

            mpi_stats_dic[mod][dom] = {}

            reg_sel = regions_specs[dom]["domain"]

            mpi_obs_reg = mpi_obs(reg_sel)
            mpi_obs_reg_sd = float(statistics.std(mpi_obs_reg, axis="xy"))
            mpi_mod_reg = mpi_mod(reg_sel)

            cor = float(
                statistics.correlation(mpi_mod_reg, mpi_obs_reg, axis="xy"))
            rms = float(statistics.rms(mpi_mod_reg, mpi_obs_reg, axis="xy"))
            rmsn = rms / mpi_obs_reg_sd

            #  DOMAIN SELECTED FROM GLOBAL ANNUAL RANGE FOR MODS AND OBS
            annrange_mod_dom = annrange_mod(reg_sel)
            annrange_obs_dom = annrange_obs(reg_sel)

            # SKILL SCORES
            #  HIT/(HIT + MISSED + FALSE ALARMS)
            hit, missed, falarm, score, hitmap, missmap, falarmmap = mpi_skill_scores(
                annrange_mod_dom, annrange_obs_dom, thr)

            #  POPULATE DICTIONARY FOR JSON FILES
            mpi_stats_dic[mod][dom] = {}
            mpi_stats_dic[mod][dom]["cor"] = format(cor, sig_digits)
            mpi_stats_dic[mod][dom]["rmsn"] = format(rmsn, sig_digits)
            mpi_stats_dic[mod][dom]["threat_score"] = format(score, sig_digits)

            # SAVE ANNRANGE AND HIT MISS AND FALSE ALARM FOR EACH MOD DOM
            fm = os.path.join(nout, "_".join([mod, dom, "wang-monsoon.nc"]))
            g = cdms2.open(fm, "w")
            g.write(annrange_mod_dom)
            g.write(hitmap, dtype=numpy.int32)
            g.write(missmap, dtype=numpy.int32)
            g.write(falarmmap, dtype=numpy.int32)
            g.close()
        f.close()

    #  OUTPUT METRICS TO JSON FILE
    OUT = pcmdi_metrics.io.base.Base(os.path.abspath(jout), json_filename)

    disclaimer = open(os.path.join(egg_pth, "disclaimer.txt")).read()

    metrics_dictionary = collections.OrderedDict()
    metrics_dictionary["DISCLAIMER"] = disclaimer
    metrics_dictionary["REFERENCE"] = (
        "The statistics in this file are based on" +
        " Wang, B., Kim, HJ., Kikuchi, K. et al. " +
        "Clim Dyn (2011) 37: 941. doi:10.1007/s00382-010-0877-0")
    metrics_dictionary["RESULTS"] = mpi_stats_dic  # collections.OrderedDict()

    OUT.var = var
    OUT.write(
        metrics_dictionary,
        json_structure=["model", "domain", "statistic"],
        indent=4,
        separators=(",", ": "),
    )
    if cmec:
        print("Writing cmec file")
        OUT.write_cmec(indent=4, separators=(",", ": "))
def monsoon_wang_runner(args):
    # args = P.parse_args(sys.argv[1:])
    modpath = args.modpath
    outpathjsons = args.outpathjsons
    outpathdata = args.results_dir
    if isinstance(args.modnames, str):
        mods = eval(args.modnames)
    else:
        mods = args.modnames

    json_filename = args.jsonname

    if json_filename == 'CMIP_MME':
        json_filename = '/MPI_' + args.mip + '_' + args.experiment

    if args.mip == 'CMIP5' and args.experiment == 'historical' and mods is None:
        mods = [
            'ACCESS1-0',
            'ACCESS1-3',
            'bcc-csm1-1',
            'bcc-csm1-1-m',
            'BNU-ESM',
            'CanCM4',
            'CanESM2',
            'CCSM4',
            'CESM1-BGC',
            'CESM1-CAM5',
            'CESM1-FASTCHEM',
            'CESM1-WACCM',
            'CMCC-CESM',
            'CMCC-CM',
            'CMCC-CMS',
            'CNRM-CM5-2',
            'CNRM-CM5',
            'CSIRO-Mk3-6-0',
            'FGOALS-g2',
            'FIO-ESM',
            'GFDL-CM2p1',
            'GFDL-CM3',
            'GFDL-ESM2G',
            'GFDL-ESM2M',
            'GISS-E2-H',
            'GISS-E2-H-CC',
            'GISS-E2-R',
            'GISS-E2-R-CC',
            'HadCM3',
            'HadGEM2-AO',
            'HadGEM2-CC',
            'HadGEM2-ES',
            'inmcm4',
            'IPSL-CM5A-LR',
            'IPSL-CM5A-MR',
            'IPSL-CM5B-LR',
            'MIROC4h',
            'MIROC5',
            'MIROC-ESM',
            'MIROC-ESM-CHEM',
            'MPI-ESM-LR',
            'MPI-ESM-MR',
            'MPI-ESM-P',
            'MRI-CGCM3',
            'MRI-ESM1',
            'NorESM1-M',
            'NorESM1-ME']

    # VAR IS FIXED TO BE PRECIP FOR CALCULATING MONSOON PRECIPITATION INDICES
    var = args.modvar
    thr = args.threshold
    sig_digits = '.3f'

    #########################################
    # PMP monthly default PR obs
    cdms2.axis.longitude_aliases.append("longitude_prclim_mpd")
    cdms2.axis.latitude_aliases.append("latitude_prclim_mpd")
    fobs = cdms2.open(args.reference_data_path)
    dobs_orig = fobs(args.obsvar)
    fobs.close()

    obsgrid = dobs_orig.getGrid()

    ########################################

    # FCN TO COMPUTE GLOBAL ANNUAL RANGE AND MONSOON PRECIP INDEX

    annrange_obs, mpi_obs = mpd(dobs_orig)
    #########################################
    # SETUP WHERE TO OUTPUT RESULTING DATA (netcdf)
    nout = os.path.join(outpathdata, "_".join(
        [args.experiment, args.mip, 'wang-monsoon']))
    try:
        os.makedirs(nout)
    except BaseException:
        pass

    # SETUP WHERE TO OUTPUT RESULTS (json)
    jout = outpathjsons
    try:
        os.makedirs(nout)
    except BaseException:
        pass

    modpathall = modpath.replace('MODS', '*')
    lst = glob.glob(modpathall)
    # CONFIRM DATA FOR MODS IS AVAIL AND REMOVE THOSE IT IS NOT

    gmods = []  # "Got" these MODS
    for mod in mods:
        for l in lst:
            l1 = modpath.replace('MODS', mod)
            if os.path.isfile(l1) is True:
                if mod not in gmods:
                    gmods.append(mod)

    if args.experiment == 'historical' and mods is None:
        gmods = [
            'ACCESS1-0',
            'ACCESS1-3',
            'bcc-csm1-1',
            'bcc-csm1-1-m',
            'BNU-ESM',
            'CanCM4',
            'CanESM2',
            'CCSM4',
            'CESM1-BGC',
            'CESM1-CAM5',
            'CESM1-FASTCHEM',
            'CESM1-WACCM',
            'CMCC-CESM',
            'CMCC-CM',
            'CMCC-CMS',
            'CNRM-CM5-2',
            'CNRM-CM5',
            'CSIRO-Mk3-6-0',
            'FGOALS-g2',
            'FIO-ESM',
            'GFDL-CM2p1',
            'GFDL-CM3',
            'GFDL-ESM2G',
            'GFDL-ESM2M',
            'GISS-E2-H',
            'GISS-E2-H-CC',
            'GISS-E2-R',
            'GISS-E2-R-CC',
            'HadCM3',
            'HadGEM2-AO',
            'HadGEM2-CC',
            'HadGEM2-ES',
            'inmcm4',
            'IPSL-CM5A-LR',
            'IPSL-CM5A-MR',
            'IPSL-CM5B-LR',
            'MIROC4h',
            'MIROC5',
            'MIROC-ESM',
            'MIROC-ESM-CHEM',
            'MPI-ESM-LR',
            'MPI-ESM-MR',
            'MPI-ESM-P',
            'MRI-CGCM3',
            'MRI-ESM1',
            'NorESM1-M',
            'NorESM1-ME']

    #########################################

    try:
        egg_pth = pkg_resources.resource_filename(
            pkg_resources.Requirement.parse("pcmdi_metrics"), "share/pmp")
    except Exception:
        # python 2 seems to fail when ran in home directory of source?
        egg_pth = os.path.join(os.getcwd(), "share", "pmp")
    globals = {}
    locals = {}
    exec(compile(open(os.path.join(egg_pth, "default_regions.py")).read(),
                 os.path.join(egg_pth, "default_regions.py"), 'exec'), globals, locals)
    regions_specs = locals["regions_specs"]
    doms = ['AllMW', 'AllM', 'NAMM', 'SAMM', 'NAFM', 'SAFM', 'ASM', 'AUSM']

    mpi_stats_dic = {}
    for mod in gmods:
        modelFile = modpath.replace('MODS', mod)

        mpi_stats_dic[mod] = {}

        print("******************************************************************************************")
        print(modelFile)
        f = cdms2.open(modelFile)
        d_orig = f(var)

        annrange_mod, mpi_mod = mpd(d_orig)
        annrange_mod = annrange_mod.regrid(obsgrid)
        mpi_mod = mpi_mod.regrid(obsgrid)

        for dom in doms:

            mpi_stats_dic[mod][dom] = {}

            reg_sel = regions_specs[dom]['domain']

            mpi_obs_reg = mpi_obs(reg_sel)
            mpi_obs_reg_sd = float(statistics.std(mpi_obs_reg, axis='xy'))
            mpi_mod_reg = mpi_mod(reg_sel)

            cor = float(
                statistics.correlation(
                    mpi_mod_reg,
                    mpi_obs_reg,
                    axis='xy'))
            rms = float(statistics.rms(mpi_mod_reg, mpi_obs_reg, axis='xy'))
            rmsn = rms / mpi_obs_reg_sd

    #  DOMAIN SELECTED FROM GLOBAL ANNUAL RANGE FOR MODS AND OBS
            annrange_mod_dom = annrange_mod(reg_sel)
            annrange_obs_dom = annrange_obs(reg_sel)

    # SKILL SCORES
    #  HIT/(HIT + MISSED + FALSE ALARMS)
            hit, missed, falarm, score, hitmap, missmap, falarmmap = mpi_skill_scores(
                annrange_mod_dom, annrange_obs_dom, thr)

    #  POPULATE DICTIONARY FOR JSON FILES
            mpi_stats_dic[mod][dom] = {}
            mpi_stats_dic[mod][dom]['cor'] = format(cor, sig_digits)
            mpi_stats_dic[mod][dom]['rmsn'] = format(rmsn, sig_digits)
            mpi_stats_dic[mod][dom]['threat_score'] = format(score, sig_digits)

    # SAVE ANNRANGE AND HIT MISS AND FALSE ALARM FOR EACH MOD DOM
            fm = os.path.join(nout, '_'.join([mod, dom, 'wang-monsoon.nc']))
            g = cdms2.open(fm, 'w')
            g.write(annrange_mod_dom)
            g.write(hitmap, dtype=numpy.int32)
            g.write(missmap, dtype=numpy.int32)
            g.write(falarmmap, dtype=numpy.int32)
            g.close()
        f.close()

    #  OUTPUT METRICS TO JSON FILE
    OUT = pcmdi_metrics.io.base.Base(os.path.abspath(jout), json_filename)

    disclaimer = open(
        os.path.join(
            egg_pth,
            "disclaimer.txt")).read()

    metrics_dictionary = collections.OrderedDict()
    metrics_dictionary["DISCLAIMER"] = disclaimer
    metrics_dictionary["REFERENCE"] = "The statistics in this file are based on" +\
        " Wang, B., Kim, HJ., Kikuchi, K. et al. " +\
        "Clim Dyn (2011) 37: 941. doi:10.1007/s00382-010-0877-0"
    metrics_dictionary["RESULTS"] = mpi_stats_dic  # collections.OrderedDict()

    OUT.var = var
    OUT.write(
        metrics_dictionary,
        json_structure=["model", "domain", "statistic"],
        indent=4,
        separators=(
            ',',
            ': '))
Exemple #12
0
    annrange_mod = annrange_mod.regrid(obsgrid)
    mpi_mod = mpi_mod.regrid(obsgrid)

    for dom in doms:

        mpi_stats_dic[mod][dom] = {}

        reg_sel = regions_specs[dom]['domain']

        mpi_obs_reg = mpi_obs(reg_sel)
        mpi_obs_reg_sd = float(statistics.std(mpi_obs_reg, axis='xy'))
        mpi_mod_reg = mpi_mod(reg_sel)

        cor = float(statistics.correlation(mpi_mod_reg, mpi_obs_reg,
                                           axis='xy'))
        rms = float(statistics.rms(mpi_mod_reg, mpi_obs_reg, axis='xy'))
        rmsn = rms / mpi_obs_reg_sd

        #  DOMAIN SELECTED FROM GLOBAL ANNUAL RANGE FOR MODS AND OBS
        annrange_mod_dom = annrange_mod(reg_sel)
        annrange_obs_dom = annrange_obs(reg_sel)

        # SKILL SCORES
        #  HIT/(HIT + MISSED + FALSE ALARMS)
        hit, missed, falarm, score, hitmap, missmap, falarmmap = mpi_skill_scores(
            annrange_mod_dom, annrange_obs_dom, thr)

        #  POPULATE DICTIONARY FOR JSON FILES
        mpi_stats_dic[mod][dom] = {}
        mpi_stats_dic[mod][dom]['cor'] = format(cor, sig_digits)
        mpi_stats_dic[mod][dom]['rmsn'] = format(rmsn, sig_digits)
Exemple #13
0
def monsoon_wang_runner(args):
    # args = P.parse_args(sys.argv[1:])
    modpath = args.modpath
    outpathjsons = args.outpathjsons
    outpathdata = args.results_dir
    if isinstance(args.modnames, str):
        mods = eval(args.modnames)
    else:
        mods = args.modnames

    json_filename = args.jsonname

    if json_filename == 'CMIP_MME':
        json_filename = '/MPI_' + args.mip + '_' + args.experiment

    if args.mip == 'CMIP5' and args.experiment == 'historical' and mods is None:
        mods = [
            'ACCESS1-0', 'ACCESS1-3', 'bcc-csm1-1', 'bcc-csm1-1-m', 'BNU-ESM',
            'CanCM4', 'CanESM2', 'CCSM4', 'CESM1-BGC', 'CESM1-CAM5',
            'CESM1-FASTCHEM', 'CESM1-WACCM', 'CMCC-CESM', 'CMCC-CM',
            'CMCC-CMS', 'CNRM-CM5-2', 'CNRM-CM5', 'CSIRO-Mk3-6-0', 'FGOALS-g2',
            'FIO-ESM', 'GFDL-CM2p1', 'GFDL-CM3', 'GFDL-ESM2G', 'GFDL-ESM2M',
            'GISS-E2-H', 'GISS-E2-H-CC', 'GISS-E2-R', 'GISS-E2-R-CC', 'HadCM3',
            'HadGEM2-AO', 'HadGEM2-CC', 'HadGEM2-ES', 'inmcm4', 'IPSL-CM5A-LR',
            'IPSL-CM5A-MR', 'IPSL-CM5B-LR', 'MIROC4h', 'MIROC5', 'MIROC-ESM',
            'MIROC-ESM-CHEM', 'MPI-ESM-LR', 'MPI-ESM-MR', 'MPI-ESM-P',
            'MRI-CGCM3', 'MRI-ESM1', 'NorESM1-M', 'NorESM1-ME'
        ]

    # VAR IS FIXED TO BE PRECIP FOR CALCULATING MONSOON PRECIPITATION INDICES
    var = args.modvar
    thr = args.threshold
    sig_digits = '.3f'

    #########################################
    # PMP monthly default PR obs
    cdms2.axis.longitude_aliases.append("longitude_prclim_mpd")
    cdms2.axis.latitude_aliases.append("latitude_prclim_mpd")
    fobs = cdms2.open(args.reference_data_path)
    dobs_orig = fobs(args.obsvar)
    fobs.close()

    obsgrid = dobs_orig.getGrid()

    ########################################

    # FCN TO COMPUTE GLOBAL ANNUAL RANGE AND MONSOON PRECIP INDEX

    annrange_obs, mpi_obs = mpd(dobs_orig)
    #########################################
    # SETUP WHERE TO OUTPUT RESULTING DATA (netcdf)
    nout = os.path.join(outpathdata,
                        "_".join([args.experiment, args.mip, 'wang-monsoon']))
    try:
        os.makedirs(nout)
    except BaseException:
        pass

    # SETUP WHERE TO OUTPUT RESULTS (json)
    jout = outpathjsons
    try:
        os.makedirs(nout)
    except BaseException:
        pass

    modpathall = modpath.replace('MODS', '*')
    lst = glob.glob(modpathall)
    # CONFIRM DATA FOR MODS IS AVAIL AND REMOVE THOSE IT IS NOT

    gmods = []  # "Got" these MODS
    for mod in mods:
        for l in lst:
            l1 = modpath.replace('MODS', mod)
            if os.path.isfile(l1) is True:
                if mod not in gmods:
                    gmods.append(mod)

    if args.experiment == 'historical' and mods is None:
        gmods = [
            'ACCESS1-0', 'ACCESS1-3', 'bcc-csm1-1', 'bcc-csm1-1-m', 'BNU-ESM',
            'CanCM4', 'CanESM2', 'CCSM4', 'CESM1-BGC', 'CESM1-CAM5',
            'CESM1-FASTCHEM', 'CESM1-WACCM', 'CMCC-CESM', 'CMCC-CM',
            'CMCC-CMS', 'CNRM-CM5-2', 'CNRM-CM5', 'CSIRO-Mk3-6-0', 'FGOALS-g2',
            'FIO-ESM', 'GFDL-CM2p1', 'GFDL-CM3', 'GFDL-ESM2G', 'GFDL-ESM2M',
            'GISS-E2-H', 'GISS-E2-H-CC', 'GISS-E2-R', 'GISS-E2-R-CC', 'HadCM3',
            'HadGEM2-AO', 'HadGEM2-CC', 'HadGEM2-ES', 'inmcm4', 'IPSL-CM5A-LR',
            'IPSL-CM5A-MR', 'IPSL-CM5B-LR', 'MIROC4h', 'MIROC5', 'MIROC-ESM',
            'MIROC-ESM-CHEM', 'MPI-ESM-LR', 'MPI-ESM-MR', 'MPI-ESM-P',
            'MRI-CGCM3', 'MRI-ESM1', 'NorESM1-M', 'NorESM1-ME'
        ]

    #########################################

    try:
        egg_pth = pkg_resources.resource_filename(
            pkg_resources.Requirement.parse("pcmdi_metrics"), "share/pmp")
    except Exception:
        # python 2 seems to fail when ran in home directory of source?
        egg_pth = os.path.join(os.getcwd(), "share", "pmp")
    globals = {}
    locals = {}
    exec(
        compile(
            open(os.path.join(egg_pth, "default_regions.py")).read(),
            os.path.join(egg_pth, "default_regions.py"), 'exec'), globals,
        locals)
    regions_specs = locals["regions_specs"]
    doms = ['AllMW', 'AllM', 'NAMM', 'SAMM', 'NAFM', 'SAFM', 'ASM', 'AUSM']

    mpi_stats_dic = {}
    for mod in gmods:
        modelFile = modpath.replace('MODS', mod)

        mpi_stats_dic[mod] = {}

        print(
            "******************************************************************************************"
        )
        print(modelFile)
        f = cdms2.open(modelFile)
        d_orig = f(var)

        annrange_mod, mpi_mod = mpd(d_orig)
        annrange_mod = annrange_mod.regrid(obsgrid)
        mpi_mod = mpi_mod.regrid(obsgrid)

        for dom in doms:

            mpi_stats_dic[mod][dom] = {}

            reg_sel = regions_specs[dom]['domain']

            mpi_obs_reg = mpi_obs(reg_sel)
            mpi_obs_reg_sd = float(statistics.std(mpi_obs_reg, axis='xy'))
            mpi_mod_reg = mpi_mod(reg_sel)

            cor = float(
                statistics.correlation(mpi_mod_reg, mpi_obs_reg, axis='xy'))
            rms = float(statistics.rms(mpi_mod_reg, mpi_obs_reg, axis='xy'))
            rmsn = rms / mpi_obs_reg_sd

            #  DOMAIN SELECTED FROM GLOBAL ANNUAL RANGE FOR MODS AND OBS
            annrange_mod_dom = annrange_mod(reg_sel)
            annrange_obs_dom = annrange_obs(reg_sel)

            # SKILL SCORES
            #  HIT/(HIT + MISSED + FALSE ALARMS)
            hit, missed, falarm, score, hitmap, missmap, falarmmap = mpi_skill_scores(
                annrange_mod_dom, annrange_obs_dom, thr)

            #  POPULATE DICTIONARY FOR JSON FILES
            mpi_stats_dic[mod][dom] = {}
            mpi_stats_dic[mod][dom]['cor'] = format(cor, sig_digits)
            mpi_stats_dic[mod][dom]['rmsn'] = format(rmsn, sig_digits)
            mpi_stats_dic[mod][dom]['threat_score'] = format(score, sig_digits)

            # SAVE ANNRANGE AND HIT MISS AND FALSE ALARM FOR EACH MOD DOM
            fm = os.path.join(nout, '_'.join([mod, dom, 'wang-monsoon.nc']))
            g = cdms2.open(fm, 'w')
            g.write(annrange_mod_dom)
            g.write(hitmap, dtype=numpy.int32)
            g.write(missmap, dtype=numpy.int32)
            g.write(falarmmap, dtype=numpy.int32)
            g.close()
        f.close()

    #  OUTPUT METRICS TO JSON FILE
    OUT = pcmdi_metrics.io.base.Base(os.path.abspath(jout), json_filename)

    disclaimer = open(os.path.join(egg_pth, "disclaimer.txt")).read()

    metrics_dictionary = collections.OrderedDict()
    metrics_dictionary["DISCLAIMER"] = disclaimer
    metrics_dictionary["REFERENCE"] = "The statistics in this file are based on" +\
        " Wang, B., Kim, HJ., Kikuchi, K. et al. " +\
        "Clim Dyn (2011) 37: 941. doi:10.1007/s00382-010-0877-0"
    metrics_dictionary["RESULTS"] = mpi_stats_dic  # collections.OrderedDict()

    OUT.var = var
    OUT.write(metrics_dictionary,
              json_structure=["model", "domain", "statistic"],
              indent=4,
              separators=(',', ': '))
Exemple #14
0
print "Press the Return key to see next plot."
sys.stdin.readline()


x.clear()
x.plot(gl_diff)
print ""
print "Press the Return key to see next plot."
sys.stdin.readline()


cor_t=statistics.correlation(data1,data2,axis='xy')
# temporal correlation map betwen these to time-series
cor_m=statistics.correlation(data1,data2,axis='t')
# temporal rms difference between the two time series
rms=statistics.rms(data1,data2,axis='t')

x.clear()
x.plot(cor_m,fill)
print ""
print "Press the Return key to see next plot."
sys.stdin.readline()

x.clear()
x.plot(cor_t)
print ""
print "Press the Return key to see next plot."
sys.stdin.readline()

x.clear()
x.plot(rms,fill)