def temporal_corr(self): """ Correlation entre modele et observations - calculee en chaque point sur la dimension temporelle / Resultat => Carte de Correlation uncentered and biased """ from genutil import statistics from vacumm.misc.grid import get_grid, set_grid self.temp_corr = () #Initialise un tuple self.temp_corr = statistics.correlation(self.model, self.obs, axis=0) gg = get_grid(self.model) set_grid(self.temp_corr, gg, axes=True)
def temporal_corr(self): """ Correlation entre modele et observations - calculee en chaque point sur la dimension temporelle / Resultat => Carte de Correlation uncentered and biased """ from genutil import statistics from vacumm.misc.grid import get_grid, set_grid self.temp_corr = () #Initialise un tuple self.temp_corr = statistics.correlation(self.model, self.obs, axis = 0) gg = get_grid(self.model) set_grid(self.temp_corr, gg, axes=True)
# Example 2: # In this example, we compute the spatial correlation between 2 fields # - say the mean surface air temperature for the 1980-1989 period # versus each month of the 1980-1989 period. # #******************************************************************************* # # First compute the average for the 1980-1990 period (designated ncep2 above) # mncep2 = MV2.average(ncep2) # # And now correlate this pattern with each month of ncep2 # cor = statistics.correlation(ncep2, mncep2, axis='xy') print 'Correlation:' print cor #******************************************************************************* # # Example 3: # We take the correlation example above a step further and specify # that the correlation be computed with weights based on latitude # area taken into account. Note that the correlation can also be # weighted over axes of our choice. #******************************************************************************* # # To compute the latitude area weights, we take the difference of the sine of
# Example 2: # In this example, we compute the spatial correlation between 2 fields # - say the mean surface air temperature for the 1980-1989 period # versus each month of the 1980-1989 period. # #******************************************************************************* # # First compute the average for the 1980-1990 period (designated ncep2 above) # mncep2 = MV2.average(ncep2) # # And now correlate this pattern with each month of ncep2 # cor = statistics.correlation(ncep2, mncep2, axis='xy') print 'Correlation:' print cor #******************************************************************************* # # Example 3: # We take the correlation example above a step further and specify # that the correlation be computed with weights based on latitude # area taken into account. Note that the correlation can also be # weighted over axes of our choice. #******************************************************************************* # # To compute the latitude area weights, we take the difference of the sine of # the latitude bounds for each latitude point in the grid. First let us get the
# add id to data z_diff.id = 'zonal_diff' print 'Zonal data shape (after): ', z_diff.shape # 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')
var.setAxis(2, lon) solver = Eof(var, weights='area') eofs = solver.eofs(neofs=num, eofscaling=scaling) pc = solver.pcs(npcs=num, pcscaling=scaling) vari = solver.varianceFraction(num) eigv = solver.eigenvalues(num) return eofs, pc, vari, eigv ERAsp.setAxis(0, set_time_axis(ERAsp.shape[0])) NOAsp.setAxis(0, set_time_axis(NOAsp.shape[0])) OBSsp.setAxis(0, set_time_axis(OBSsp.shape[0])) eofs1, pc1, vari1, eigv1 = get_EOFs(ERAsp, num=3, scaling=0) eofs2, pc2, vari2, eigv2 = get_EOFs(NOAsp, num=3, scaling=0) eofs3, pc3, vari3, eigv3 = get_EOFs(OBSsp, num=3, scaling=0) cor11 = np.abs(stat.correlation(eofs1[0], eofs3[0], axis='yx')) cor21 = np.abs(stat.correlation(eofs2[0], eofs3[0], axis='yx')) cor12 = np.abs(stat.correlation(eofs1[1], eofs3[1], axis='yx')) cor22 = np.abs(stat.correlation(eofs2[1], eofs3[1], axis='yx')) cor13 = np.abs(stat.correlation(eofs1[2], eofs3[2], axis='yx')) cor23 = np.abs(stat.correlation(eofs2[2], eofs3[2], axis='yx')) print(cor11, cor21) print(cor12, cor22) print(cor13, cor23) print() print((cor21 - cor11) / cor11 * 100) print((cor22 - cor12) / cor12 * 100) print((cor23 - cor13) / cor13 * 100) plt.bar(np.r_[0, 4, 8], np.r_[cor11, cor12, cor13], width=1) plt.bar(np.r_[1, 5, 9], np.r_[cor21, cor22, cor23], width=1) plt.ylim(0, 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 genDailyCorrelationFiles(dailyAnomalyPath, dailyCorrelationPath, year, hour, **model): """ :func:`genDailyAnomalyFiles` : It should calculate daily anomaly from the daily analysis and daily climatolgy for the current day. i.e. daily model analysis - daily climatolgy Finally stores it as nc files in corresponding directory path which are passed in this function args. Do the same procedure for the model forecast hours anomaly. i.e. daily model forecast - daily climatolgy Inputs : dailyAnomalyPath is the absolute path where the processed mean anomaly nc files are going to store. modelType is either 'a' for 'analysis' or 'f' for 'forecast'. modelHour is the forecast hour. climRegriadir is the absolute path where the regridded monthly mean climatologies (w.r.t the model vertical resolution) nc files were already stored. climPFileName is the partial nc filename of the climatolgy. climatologyYear is the year of the climatolgy to access it. KWargs: modelName, modelXmlPath, modelXmlObj modelName is the model data name which will become part of the process nc files name. modelPath is the absolute path of data where the model xml files are located. modelXmlObj is an instance of the GribXmlAccess class instance. If we are passing modelXmlObj means, it will be optimized one when we calls this same function for same model for different months. We can pass either modelXmlPath or modelXmlObj KWarg is enough. Outputs : It should create daily anomaly for the particular variables which are all set the clim_var option in the vars.txt file. Finally store it as nc file formate in the proper directories structure (modelname, 'Anomaly', year, daily and then 'analysis' or fcst hours hierarchy). Written By : Arulalan.T Date : 02.01.2012 """ modelXmlObj, modelPath = None, None if 'modelName' in model: modelName = model.get('modelName') else: raise RuntimeError("KWarg modelName must be passed") if 'modelXmlObj' in model: modelXmlObj = model.get('modelXmlObj') elif 'modelXmlPath' in model: modelPath = model.get('modelXmlPath') else: raise RuntimeError("you must pass atleast one KWargs of modelXmlPath \ or modelXmlPath ") if not modelXmlObj: xmlobj = xml_data_access.GribXmlAccess(modelPath) else: if isinstance(modelXmlObj, xml_data_access.GribXmlAccess): xmlobj = modelXmlObj else: raise ValueError("Passed modelXmlObj instance %s is not an \ instance of GribXmlAccess class " % type(modelXmlObj)) ncAnoFileDic = {} anomalyDirs = ['Analysis', hour] for adir in anomalyDirs: apath = '/'.join([dailyAnomalyPath, adir]) ncAnoFileDic[adir] = {} # get the nc files name of mean anomaly ncfiles = [f for f in os.listdir(apath) if f.endswith('.nc')] # make ncfiles as dictionary with key as var name for ncfile in ncfiles: var = ncfile.split('_')[0] anomalyFilePath = apath + '/' + ncfile ncAnoFileDic[adir][var] = anomalyFilePath # make memory free del ncfiles ncCorrFileDic = {} # get the nc files name of mean anomaly ncfiles = [f for f in os.listdir(dailyCorrelationPath) if f.endswith('.nc')] # make ncfiles as dictionary with key as var name for ncfile in ncfiles: var = ncfile.split('_')[0] correlationFilePath = dailyCorrelationPath + '/' + ncfile ncCorrFileDic[var] = correlationFilePath # make memory free del ncfiles modelvariables = xmlobj.listvariable(Type = 'a') # get the namedtuple object from the global 'vars.txt' file totalvars = variables.get(modelName) for globalvar in totalvars.itervalues(): # get the model var name mvar = globalvar.model_var # # get the climatolgy var name # cvar = globalvar.clim_var # if not cvar: # print "Climatology var name is empty string. So skipping anomaly \ # correlation process for %s model var name " % mvar # continue # # end of if not cvar: if not mvar in modelvariables: print "The variable %s is not available in the xml anl file object" % mvar print "So skipping the anomaly correlation process \ for this variable %s which is one of the keys of the \ variables dictionary" % mvar continue # end of if not mvar in allvariables: if not mvar in ncAnoFileDic['Analysis']: print "The variable %s is not available in processed anomaly \ analysis directory. So skipping the anomaly correlation process \ for this variable %s " % (mvar, mvar) continue correlationLatestDate = None # generate the nc filename anomalyCorrFileName = mvar + '_'+ modelName + '_' + year anomalyCorrFileName += '_daily_f' + hour + 'hr' anomalyCorrFileName += '_anomaly_correlation.nc' anomalyAnlFilePath = ncAnoFileDic['Analysis'].get(mvar) try: anoAnlFile = cdms2.open(anomalyAnlFilePath, 'r') except: raise ValueError("Couldnt open the anomaly analysis file '%s'.\ so can not perform the daily correlation" % anomalyAnlFilePath) anoAnlTime = anoAnlFile[mvar].getTime().asComponentTime() levAxis = anoAnlFile[mvar].getLevel() if mvar in ncCorrFileDic: anomalyCorrFilePath = ncCorrFileDic.get(mvar) correlationLatestDate = None try: # open nc file in append mode anomalyCorrFile = cdms2.open(anomalyCorrFilePath, 'a') # get the ncfile timeAxis fileTime = anomalyCorrFile[mvar].getTime().asComponentTime() anomalyLatestDate = anoAnlTime[-1] correlationLatestDate = fileTime[-1] # Do check either this month timeAxis is already exists in # the nc file's timeAxis or not. If exists means skip it. if __timeCheck__: if anomalyLatestDate in fileTime: print "The daily anomaly is already exists in the \ file %s. So skipping var '%s' " % \ (anomalyCorrFile, mvar) anomalyCorrFile.close() continue # end of if __timeCheck__: except (AttributeError, cdms2.error.CDMSError): # if it getting this error means, it may not written # properly. So open nc file in write mode freshly. print "Got Problem. nc file is correpted at last time. \ May be lost the previous days data.\ Now creating same nc file '%s' freshly & fully " \ % (anomalyCorrFilePath) anomalyCorrFile = cdms2.open(anomalyCorrFilePath, 'w') else: anomalyCorrFilePath = dailyCorrelationPath + '/' + anomalyCorrFileName # open new nc file in write mode anomalyCorrFile = cdms2.open(anomalyCorrFilePath, 'w') # end of if mvar in ncAnoFileDic: anomalyHrFilePath = ncAnoFileDic[hour].get(mvar) try: anoHrFile = cdms2.open(anomalyHrFilePath, 'r') except: raise ValueError("Couldnt open the anomaly fcst %s hour file '%s'.\ so can not perform the daily correlation" % (hour, anomalyHrFilePath)) anoFirstYear = anoAnlTime[0].year if correlationLatestDate: nextIndex = anoAnlTime.index(correlationLatestDate) + 1 anoAnlTime = anoAnlTime[nextIndex:] print "Writing daily anomaly correlation into %s file \n" % (anomalyCorrFileName) for adate in anoAnlTime: partnerDate = xmlobj.findPartners('a', adate, hour) #print "Extracting the fcst", hour, " partner data on ", partnerDate, "of analysis data on ", adate try: # get the climatolgy data for this month alone anoHrData = anoHrFile(mvar, time = partnerDate) except: print "Coundn't get the anomaly fcst '%s' hour data of %s var \ %s time" % (hour, mvar, partnerDate) print "So skipping the anomaly correlation for the '%s' variable" % mvar continue # end of try: try: # get the model data for this day alone anoAnlData = anoAnlFile(mvar, time = adate) except: print "Coundn't get the anomaly analysis data of %s var %s time" \ % (mvar, adate) print "So skipping the anomaly correlation for the '%s' \ variable on '%s'" % (mvar, adate) continue # end of try: print "Calculating Daily Anomaly Correlation on ", adate # anomaly correlation if levAxis: corrValue = correlation(anoAnlData, anoHrData, axis = "tyx") else: corrValue = correlation(anoAnlData, anoHrData, axis = "tzyx") # make memory free del anoAnlData, anoHrData fcststart = timobj._getDayCountOfYear(partnerDate, year=anoFirstYear) startDayOfYear = '%s-1-1' % anoFirstYear dailyCorrTime = timobj._generateTimeAxis([fcststart], startDayOfYear) corrValue = corrValue.reshape((1, len(corrValue))) corrValue = cdms2.createVariable(corrValue) if levAxis: corrValue.setAxisList([dailyCorrTime, levAxis]) else: corrValue.setAxis(0, dailyCorrTime) corrValue.id = mvar #corrValue.comments = 'monthly mean anomaly of %s model data of %s' % (modelName, year) anomalyCorrFile.write(corrValue) # make memory free del corrValue # end of for mdate in modelTime: anomalyCorrFile.close()
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=( ',', ': '))
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)
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=(',', ': '))
x.setcolormap('default') fill=x.getisofill('default') x.plot(z_diff,fill) print "" 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."