コード例 #1
0
def plotTimeSerieVariabilityAtWl_mean(ax=None, axmap=None, plotXLabel=True, plotYLabel=True, warmingLev=2.0, bsmp=None):
  yrs, _, _, r8srs_, r4srs_ = loadWlVsScenChange.loadMeanChangesAtWl_nYearsAroundWLYear(warmingLev=warmingLev, nmodels=nmodels, excludedModels=excludedModels)
  r8srs = r8srs_[:, [3], :, :]
  r4srs = r4srs_[:, [3], :, :]

  wlyR8 = getWarmingLevels('rcp85', warmingLev)
  wlyR4 = getWarmingLevels('rcp45', warmingLev)

  srstot = np.concatenate([r8srs, r4srs], 0)
  rallmeanWl, rallsigmaWl = getMeanAndSigma(r8srs)
  cndPosi = rallmeanWl > 0
  cndNega = rallmeanWl < 0
  cnd = cndPosi if positiveChanges else cndNega

  r8meanWl, r8sigmaWl = getMeanAndSigma(r8srs)
  cndMtx = np.tile(cnd, [r8srs.shape[0], r8srs.shape[1], 1, 1])
  r8srs[~cndMtx] = np.nan
  r8mean_ = np.nanmean(r8srs, 0)
  r8mean = r8mean_.reshape(r8mean_.shape[0], r8mean_.shape[1]*r8mean_.shape[2])
 #r8mean = np.nanmedian(r8mean, 1)
  r8mean = np.nanmean(r8mean, 1)

  r8spmean = r8srs.reshape(r8srs.shape[0], r8srs.shape[1], r8srs.shape[2]*r8srs.shape[3])
  r8spmean = np.nanmean(r8spmean, 2)
  r8skew = stats.skew(r8spmean[:, 0])
  r8sigma = np.std(r8spmean[:, 0])
  r8mean = np.nanmean(r8spmean, 0)

  r4meanWl, r4sigmaWl = getMeanAndSigma(r4srs)
  cndMtx = np.tile(cnd, [r4srs.shape[0], r4srs.shape[1], 1, 1])
  r4srs[~cndMtx] = np.nan
  r4mean_ = np.nanmean(r4srs, 0)
  r4mean = r4mean_.reshape(r4mean_.shape[0], r4mean_.shape[1]*r4mean_.shape[2])
 #r4mean = np.nanmedian(r4mean, 1)
  r4mean = np.nanmean(r4mean, 1)

  r4spmean = r4srs.reshape(r4srs.shape[0], r4srs.shape[1], r4srs.shape[2]*r4srs.shape[3])
  r4spmean = np.nanmean(r4spmean, 2)
  r4skew = stats.skew(r4spmean[:, 0])
  r4sigma = np.std(r4spmean[:, 0])
  r4mean = np.nanmean(r4spmean, 0)

  ensTot = np.concatenate([r8spmean, r4spmean], 0)
  stdDevTot = np.std(ensTot, 0)*100.
  meanTot = (r4mean + r8mean)/2.*100.

  yr = [-15, -10, -5, 0, 5, 10, 15]
  if ax is None:
    fg = plt.figure(figsize=[8.46, 4.98])
    ax = fg.gca()
  doPlotChanges(ax, warmingLev, meanTot, stdDevTot, r8spmean, r4spmean, r8mean, r4mean, plotXLabel, plotYLabel)

  if not axmap is None:
    bsmp = doPlotAreaMap(axmap, cnd, bsmp)

  plt.tight_layout()
  if ax is None:
    fg.savefig('./ensemblesVariabilityAround2deg_mean.png', dpi=400)

  return bsmp
コード例 #2
0
def plotAllModelsWarmingLevel(warmingLev, rootDir='/ClimateRun4/multi-hazard/eva/'):
  rcp8, rcp4 = 'rcp85', 'rcp45'
  retPer = 100

  models = """
IPSL-INERIS-WRF331F_BC
SMHI-RCA4_BC_CNRM-CERFACS-CNRM-CM5
SMHI-RCA4_BC_ICHEC-EC-EARTH
SMHI-RCA4_BC_IPSL-IPSL-CM5A-MR
SMHI-RCA4_BC_MOHC-HadGEM2-ES
SMHI-RCA4_BC_MPI-M-MPI-ESM-LR
CLMcom-CCLM4-8-17_BC_CNRM-CERFACS-CNRM-CM5
CLMcom-CCLM4-8-17_BC_ICHEC-EC-EARTH
CLMcom-CCLM4-8-17_BC_MPI-M-MPI-ESM-LR
DMI-HIRHAM5-ICHEC-EC-EARTH_BC
KNMI-RACMO22E-ICHEC-EC-EARTH_BC
"""
  models = models.split()

  wlYearR8 = getWarmingLevels(rcp8, warmingLev)
  wlYearR4 = getWarmingLevels(rcp4, warmingLev)

  outputPngFile = 'scatter_100yrlChange_wl' + str(warmingLev) + '.png'

  nrow = 4
  ncol = 3
  fig = plt.figure(figsize=(8, 9.5))
  gs = gridspec.GridSpec(nrow, ncol)
  lon, lat = [], []
  relChng8lst, relChng4lst = [], []
  mp = None
  for mdl, imdl in zip(models, range(1, len(models) + 1)):
    irow = imdl // ncol
    icol = imdl % ncol
    print('plotting model ' + mdl + ' at ' + str(irow) + ', ' + str(icol))
    ax = plt.subplot(gs[irow, icol])
    
    ncFlNm = '_'.join(['projection_dis', rcp8, mdl, 'wuChang', 'statistics.nc'])
    ncFlPth = os.path.join(rootDir, ncFlNm)
    wrmYear = wlYearR8[mdl]
    wrmYear = int(round(wrmYear/5.)*5.)
    relChngR8 = getRelChngs(ncFlPth, retPer, wrmYear)

    ncFlNm = '_'.join(['projection_dis', rcp4, mdl, 'wuChang', 'statistics.nc'])
    ncFlPth = os.path.join(rootDir, ncFlNm)
    wrmYear = wlYearR4[mdl]
    wrmYear = int(round(wrmYear/5.)*5.)
    relChngR4 = getRelChngs(ncFlPth, retPer, wrmYear)

    plotSingleModel(ax, relChngR8, relChngR4, mdl)
    relChng8lst.append(relChngR8)
    relChng4lst.append(relChngR4)

  ax = plt.subplot(gs[0, 0])
  relChng8 = np.nanmean(np.array(relChng8lst), 0)
  relChng4 = np.nanmean(np.array(relChng4lst), 0)
  plotSingleModel(ax, relChng8, relChng4, 'Ensemble rcp85 vs rcp45,\nwrm. lev. ' + str(warmingLev) + '$^\circ$', bold=True, plotRegression=True)
  plt.tight_layout()

  fig.savefig(outputPngFile, dpi=300)
コード例 #3
0
def plotAllModelsWarmingLevel(warmingLev,
                              rootDir='/ClimateRun4/multi-hazard/eva/'):
    rcp8, rcp4 = 'rcp85', 'rcp45'
    retPer = 100

    models = modelStr.split()

    wlYearR8 = getWarmingLevels(rcp8, warmingLev)
    wlYearR4 = getWarmingLevels(rcp4, warmingLev)

    outputPngFile = 'scatter_100yrlChange_wl' + str(warmingLev) + '.png'

    nrow = 4
    ncol = 3
    fig = plt.figure(figsize=(8, 9.5))
    gs = gridspec.GridSpec(nrow, ncol)
    lon, lat = [], []
    relChng8lst, relChng4lst = [], []
    mp = None
    vlsMask = None
    for mdl, imdl in zip(models, range(1, len(models) + 1)):
        irow = imdl // ncol
        icol = imdl % ncol
        print('plotting model ' + mdl + ' at ' + str(irow) + ', ' + str(icol))
        ax = plt.subplot(gs[irow, icol])

        ncFlNm = '_'.join(
            ['projection_dis', rcp8, mdl, wuStr, 'statistics.nc'])
        ncFlPth = os.path.join(rootDir, ncFlNm)
        wrmYear = wlYearR8[mdl]
        wrmYear = int(round(wrmYear / 5.) * 5.)
        relChngR8, vlsMask = getRelChngs(ncFlPth, retPer, wrmYear, vlsMask)

        ncFlNm = '_'.join(
            ['projection_dis', rcp4, mdl, wuStr, 'statistics.nc'])
        ncFlPth = os.path.join(rootDir, ncFlNm)
        wrmYear = wlYearR4[mdl]
        wrmYear = int(round(wrmYear / 5.) * 5.)
        relChngR4, vlsMask = getRelChngs(ncFlPth, retPer, wrmYear, vlsMask)

        plotSingleModel(ax, relChngR8, relChngR4, mdl)
        relChng8lst.append(relChngR8)
        relChng4lst.append(relChngR4)

    ax = plt.subplot(gs[0, 0])
    relChng8 = np.nanmean(np.array(relChng8lst), 0)
    relChng4 = np.nanmean(np.array(relChng4lst), 0)
    plotSingleModel(ax,
                    relChng8,
                    relChng4,
                    str(retPer) +
                    '-year ret. lev.\nensemble rcp85 vs rcp45,\nwrm. lev. ' +
                    str(warmingLev) + '$^\circ$C',
                    bold=True,
                    plotRegression=True)
    plt.tight_layout()

    fig.savefig(outputPngFile, dpi=300)
コード例 #4
0
def extract1Model(inputNcFlPath, outputNcFlPath):
  outRetPer = [30.]
  outWarmingLevs = [1.5, 2.0, 3.0]

  m = re.match('(.*)/lonlat_projection_windYMax_(.*)_(.*).nc', inputNcFlPath)
  scen = m.group(1)[0]
  mdl = m.group(2)[0]

  wls = [getWarmingLevels(scen, wl) for wl in outWarmingLevs]

  print('  loading the data ...')
  dsin = netCDF4.Dataset(inputNcFlPath)
  x = dsin.variables['lon'][:]
  y = dsin.variables['lat'][:]
  inRetPer = dsin.variables['retper'][:]
  tmnc = dsin.variables['time']
  tm = netCDF4.num2date(tmnc[:], tmnc.units, tmnc.calendar)
  ystart = tm[0].year
  rlYrs = ystart + np.arange(len(tm))
  dsin.close()

  output = []
  for wl in outWarmingLevs:
    wlyrs = wls[wl]
    if wlyrs is None:
      continue
    wlyr = wlyrs[mdl]
    iyr = np.where(rlYrs == wlyr)
    wlRetPer = 
コード例 #5
0
def doCreateAllFiles(diagnosticsDataDir, outdir):
  scenarios = ['rcp85', 'rcp45']
  wls = getWarmingLevels.getWarmingLevels('rcp85', 1.5)
  models = wls.keys()
  for scn in scenarios:
    for mdl in models:
      generateFrqFileForModel(diagnosticsDataDir, scn, mdl, outdir)
コード例 #6
0
def doLoadDiagnosticData(diagnosticDataDir, warmingLev):
    import getWarmingLevels
    print('  loading data at ' + str(warmingLev) + '°')

    fls = [
        f for f in os.listdir(diagnosticsDataDir) if re.match('(.*).npy', f)
    ]
    fls.sort()
    mtchs = [
        re.match('droughtFrqDiagnostics_(.*)_(.*)_(.*)\.npy', f) for f in fls
    ]
    mdls_ = [m.groups(0)[0] for m in mtchs]
    scns_ = [m.groups(0)[1] for m in mtchs]
    yrs = [int(float(m.groups(0)[2])) for m in mtchs]

    dtprot = np.load(os.path.join(diagnosticsDataDir, fls[0]))
    nx, ny = dtprot.shape

    mns = []

    mdls = set(mdls_)
    scns = set(scns_)
    for scn in scns:
        if warmingLev == 0:
            wls = {}
        else:
            wls = getWarmingLevels.getWarmingLevels(scn, warmingLev)
        if wls is None:
            for mdl in mdls:
                mns.append(np.zeros([nx, ny]) * np.nan)
            continue
        for mdl in mdls:
            print('    loading model / scenario == ' + mdl + ' / ' + scn)
            if warmingLev == 0:
                ywl = 1995
            else:
                ywl = wls[mdl]
            ymin = max(ywl - 15, 1981)
            ymax = min(ywl + 14, 2100)
            dt = np.zeros([30, nx, ny]) * np.nan
            yrsMdl = range(ymin, ymax + 1)
            for iyr, yr in zip(range(len(yrsMdl)), yrsMdl):
                flPth = os.path.join(
                    diagnosticsDataDir, '_'.join(
                        ['droughtFrqDiagnostics', mdl, scn,
                         str(yr)])) + '.0.npy'
                if not os.path.isfile(flPth):
                    print('     ... file ' + flPth +
                          ' not found, skipping ...')
                    continue
                dt[iyr] = np.load(flPth)
            mn = np.nanmedian(dt, 0)
            #mn = np.nanmean(dt, 0)
            mns.append(mn)
    mns = np.array(mns)
    frq = np.nanmedian(mns, 0)
    retper = 1 / frq
    return retper, 1 / mns
コード例 #7
0
def plotAllModelsWarmingLevel(scenario, warmingLev, rootDir='/ClimateRun4/multi-hazard/eva/'):
  models = """
IPSL-INERIS-WRF331F_BC
SMHI-RCA4_BC_CNRM-CERFACS-CNRM-CM5
SMHI-RCA4_BC_ICHEC-EC-EARTH
SMHI-RCA4_BC_IPSL-IPSL-CM5A-MR
SMHI-RCA4_BC_MOHC-HadGEM2-ES
SMHI-RCA4_BC_MPI-M-MPI-ESM-LR
CLMcom-CCLM4-8-17_BC_CNRM-CERFACS-CNRM-CM5
CLMcom-CCLM4-8-17_BC_ICHEC-EC-EARTH
CLMcom-CCLM4-8-17_BC_MPI-M-MPI-ESM-LR
DMI-HIRHAM5-ICHEC-EC-EARTH_BC
KNMI-RACMO22E-ICHEC-EC-EARTH_BC
"""
  models = models.split()

  wlYear = getWarmingLevels(scenario, warmingLev)

  outputPngFile = '100yrlChange_' + scenario + '_wl' + str(warmingLev) + '.png'

  nrow = 4
  ncol = 3
  fig = plt.figure(figsize=(8, 9))
  gs = gridspec.GridSpec(nrow, ncol + 1, width_ratios=[1,1,1,.1])
  lon, lat = [], []
  bslnlist, projlist = [], []
  mp = None
  for mdl, imdl in zip(models, range(1, len(models) + 1)):
    irow = imdl // ncol
    icol = imdl % ncol
    print('plotting model ' + mdl + ' at ' + str(irow) + ', ' + str(icol))
    ax = plt.subplot(gs[irow, icol])
    
    wrmYear = wlYear[mdl]
    wrmYear = int(round(wrmYear/5.)*5.)

    ncFlNm = '_'.join(['projection_dis', scenario, mdl, 'wuChang', 'statistics.nc'])
    ncFlPth = os.path.join(rootDir, ncFlNm)
    mdlname = mdl.replace('BC_', '').replace('_BC', '')
    lon, lat, bsln, proj, pcl, mp = plotSingleModel(ax, ncFlPth, mdlname, mp=mp, projYear=wrmYear)
    bslnlist.append(bsln)
    projlist.append(proj)

  ax = plt.subplot(gs[0, 0])
  bsln = np.nanmean(np.array(bslnlist), 0)
  proj = np.nanmean(np.array(projlist), 0)
  projVar = (proj - bsln)/bsln*100
  plotSingleModelVar(ax, 'Ensemble ' + scenario + ', warming lev. ' + str(warmingLev) + '$^\circ$', lon, lat, projVar, mp, bold=True)

  ax = plt.subplot(gs[:, 3])
 #ax.get_xaxis().set_visible(False)
 #ax.get_yaxis().set_visible(False)
  cb = plt.colorbar(pcl, cax=ax)
  cb.set_label(label='100-year event change (%)', fontsize=12)
  plt.tight_layout()

  fig.savefig(outputPngFile, dpi=300)
コード例 #8
0
def plotEnsemblesWarmingLevel(warmingLev=2.0, rootDir='/ClimateRun4/multi-hazard/eva/'):
  scenarios = ['rcp85', 'rcp45']
  models = """
IPSL-INERIS-WRF331F_BC
SMHI-RCA4_BC_CNRM-CERFACS-CNRM-CM5
SMHI-RCA4_BC_ICHEC-EC-EARTH
SMHI-RCA4_BC_IPSL-IPSL-CM5A-MR
SMHI-RCA4_BC_MOHC-HadGEM2-ES
SMHI-RCA4_BC_MPI-M-MPI-ESM-LR
CLMcom-CCLM4-8-17_BC_CNRM-CERFACS-CNRM-CM5
CLMcom-CCLM4-8-17_BC_ICHEC-EC-EARTH
CLMcom-CCLM4-8-17_BC_MPI-M-MPI-ESM-LR
DMI-HIRHAM5-ICHEC-EC-EARTH_BC
KNMI-RACMO22E-ICHEC-EC-EARTH_BC
"""
  models = models.split()

  outputPngFile = '100yrlChange_ensembles_wl.png'

  fig = plt.figure(figsize=(10, 4))
  gs = gridspec.GridSpec(1, 3, width_ratios=[1,1,.1])
  lon, lat = [], []
  mp = None
  for scenario, iscen in zip(scenarios, range(len(scenarios))):
    bslnlist, projlist = [], []
    wlYear = getWarmingLevels(scenario, warmingLev)
    for mdl, imdl in zip(models, range(1, len(models) + 1)):
      print('getting model ' + mdl)
    
      wrmYear = wlYear[mdl]
      wrmYear = int(round(wrmYear/5.)*5.)
      
      ncFlNm = '_'.join(['projection_dis', scenario, mdl, 'wuChang', 'statistics.nc'])
      ncFlPth = os.path.join(rootDir, ncFlNm)
      mdlname = mdl.replace('BC_', '').replace('_BC', '')
      lon, lat, bsln, proj, pcl, mp = plotSingleModel(None, ncFlPth, mdlname, mp=mp, projYear=wrmYear)
      bslnlist.append(bsln)
      projlist.append(proj)

    ax = plt.subplot(gs[iscen])
    bsln = np.nanmean(np.array(bslnlist), 0)
    proj = np.nanmean(np.array(projlist), 0)
    projVar = (proj - bsln)/bsln*100
    plotSingleModelVar(ax, 'Ensemble ' + scenario + '\n wrm. lev. ' + str(warmingLev), lon, lat, projVar, mp, bold=True, titleFontSize=10)

  ax = plt.subplot(gs[2])
 #ax.get_xaxis().set_visible(False)
 #ax.get_yaxis().set_visible(False)
  cb = plt.colorbar(pcl, cax=ax)
  cb.set_label(label='100-year event change (%)', fontsize=12)
  plt.tight_layout()

  fig.savefig(outputPngFile, dpi=300)
コード例 #9
0
def loadRetPerAllYears(ncDir='/ClimateRun4/multi-hazard/eva', rlVarName='rl', retPer=100,
        flpattern='projection_dis_{scen}_{mdl}_wuConst_statistics.nc', nmodels=-1, excludedModels=[]):
  
  wlyR8 = getWarmingLevels('rcp85', 2.0)
  models = wlyR8.keys()
  if nmodels > -1:
    models = models[:nmodels]
  for mdl in excludedModels:
    if mdl in models:
      models.remove(mdl)

  dsuparea = netCDF4.Dataset('upArea.nc')
  upArea = dsuparea.variables['upArea'][:].transpose()
  dsuparea.close()

  tamask, _, _ = getAfricaAndTurkeyMask()
  tamask = tamask.transpose()
  
  vlsR8 = []
  vlsR4 = []  
  for mdl, imdl in zip(models, range(len(models))):
    print('model ' + mdl)

    flr8 = flpattern.format(scen='rcp85', mdl=mdl)
    flr8pth = os.path.join(ncDir, flr8)
    ds = netCDF4.Dataset(flr8pth)
    yr = ds.variables['year'][:]
    upAreaMtx = np.tile(upArea, [yr.shape[0], 1, 1])
    tamaskMtx = np.tile(tamask, [yr.shape[0], 1, 1])
    retper_ = ds.variables['return_period'][:]
    rpIndx = np.where(retper_==retPer)[0][0]
    vl = ds.variables[rlVarName][rpIndx, :, :, :]
    vl[upAreaMtx < 1e9] = np.nan
    vl[~tamaskMtx] = np.nan
    vlsR8.append(vl)
    ds.close()

    flr4 = flpattern.format(scen='rcp45', mdl=mdl)
    flr4pth = os.path.join(ncDir, flr4)
    ds = netCDF4.Dataset(flr4pth)
    vl = ds.variables[rlVarName][rpIndx, :, :, :]
    vl[upAreaMtx < 1e9] = np.nan
    vl[~tamaskMtx] = np.nan
    vlsR4.append(vl)
    ds.close()

  vlsR8 = np.array(vlsR8)
  vlsR4 = np.array(vlsR4)

  return yr, vlsR8, vlsR4, models
コード例 #10
0
def loadFrqDataForModel(diagnosticsDataDir, scn, mdl, warmingLev):
  if warmingLev == 0:
    ywl = 1995 # baseline
  else:
    wls = getWarmingLevels.getWarmingLevels(scn, warmingLev)
    ywl = wls[mdl]
  ymin = max(ywl-15, 1981)
  ymax = min(ywl+14, 2100)
  yrsMdl = range(ymin, ymax+1)
  dt = None
  for iyr, yr in zip(range(len(yrsMdl)), yrsMdl):
    flPth = os.path.join(diagnosticsDataDir, '_'.join(['droughtFrqDiagnostics', mdl, scn, str(yr)])) + '.0.npy'
    if not os.path.isfile(flPth):
      print('     ... file ' + flPth + ' not found, skipping ...')
      continue
    dti = np.load(flPth)
    nx, ny = dti.shape
    dt = np.zeros([30, nx, ny])*np.nan if dt is None else dt
    dt[iyr] = dti
  mn = np.nanmedian(dt, 0)
  return 1/mn
コード例 #11
0
def getRcpEnsembleAtYear(ryear,
                         ncDir='/ClimateRun4/multi-hazard/eva',
                         bslnYear=1995,
                         retPer=100,
                         scen='rcp85'):
    flpattern = 'projection_dis_{scen}_{mdl}_wuConst_statistics.nc'

    wlyR = getWarmingLevels(scen, 2.0)
    #wlyR4 = getWarmingLevels('rcp45', warmingLev)

    models = wlyR.keys()

    rl = []
    for mdl, imdl in zip(models, range(len(models))):
        print('model ' + mdl)
        flr = flpattern.format(scen=scen, mdl=mdl)
        flrpth = os.path.join(ncDir, flr)

        ryearInf = int(np.floor(ryear / float(5)) * 5)

        print('  loading file ' + flrpth)
        ds = netCDF4.Dataset(flrpth)
        retper_ = ds.variables['return_period'][:]
        rpIndx = np.where(retper_ == retPer)[0][0]
        year_ = ds.variables['year'][:]
        yIndxBsln = np.where(year_ == bslnYear)[0][0]
        rlBslnR = ds.variables['rl'][rpIndx, yIndxBsln, :, :]
        yIndx = np.where(year_ == ryearInf)[0][0]
        rlR_ = ds.variables['rl'][rpIndx, yIndx:yIndx + 2, :, :]
        ds.close()
        #rlR8 = interp1d(year_[yIndx:yIndx+2], rlR8_, axis=0)(r8year)
        rlR = interp1d(year_[yIndx:yIndx + 2], rlR_, axis=0)(ryear)
        rRelChng = (rlR - rlBslnR) / rlBslnR
        rl.append(rRelChng)

    rl = np.array(rl)

    ensembleRelChng = np.nanmean(rl, 0)

    return ensembleRelChng
コード例 #12
0
def loadWlVsScenChange2(ncDir='/ClimateRun4/multi-hazard/eva',
                        bslnYear=1995,
                        warmingLev=2,
                        retPer=100,
                        threshold=0,
                        rlVarName='rl'):
    # computes the relative change of the ensemble
    flpattern = 'projection_dis_{scen}_{mdl}_wuConst_statistics.nc'

    wlyR8 = getWarmingLevels('rcp85', warmingLev)
    wlyR4 = getWarmingLevels('rcp45', warmingLev)

    dsuparea = netCDF4.Dataset('upArea.nc')
    upArea = dsuparea.variables['upArea'][:].transpose()
    dsuparea.close()

    models = wlyR8.keys()
    # menta
    #models = [models[0], models[1]]
    tamask, _, _ = getAfricaAndTurkeyMask()
    tamask = tamask.transpose()

    rl_bsln = []
    rl_r4 = []
    rl_r8 = []
    for mdl, imdl in zip(models, range(len(models))):
        print('model ' + mdl)
        flr8 = flpattern.format(scen='rcp85', mdl=mdl)
        flr8pth = os.path.join(ncDir, flr8)
        flr4 = flpattern.format(scen='rcp45', mdl=mdl)
        flr4pth = os.path.join(ncDir, flr4)

        r8year = wlyR8[mdl]
        r8yearInf = int(np.floor(r8year / float(5)) * 5)
        print('  rcp85 w.l. year: ' + str(r8year))
        r4year = wlyR4[mdl]
        r4yearInf = int(np.floor(r4year / float(5)) * 5)
        print('  rcp45 w.l. year: ' + str(r4year))

        print('  loading file ' + flr8pth)
        ds = netCDF4.Dataset(flr8pth)
        retper_ = ds.variables['return_period'][:]
        rpIndx = np.where(retper_ == retPer)[0][0]
        year_ = ds.variables['year'][:]
        yIndxBsln = np.where(year_ == bslnYear)[0][0]
        rlBslnR8 = ds.variables[rlVarName][rpIndx, yIndxBsln, :, :]
        yIndx = np.where(year_ == r8yearInf)[0][0]
        rlR8_ = ds.variables[rlVarName][rpIndx, yIndx:yIndx + 2, :, :]
        ds.close()
        rlR8 = interp1d(year_[yIndx:yIndx + 2], rlR8_, axis=0)(r8year)

        print('  loading file ' + flr4pth)
        ds = netCDF4.Dataset(flr4pth)
        yIndx = np.where(year_ == r4yearInf)[0][0]
        rlR4_ = ds.variables[rlVarName][rpIndx, yIndx:yIndx + 2, :, :]
        ds.close()
        rlR4 = interp1d(year_[yIndx:yIndx + 2], rlR4_, axis=0)(r4year)

        rlR8[upArea < 1e9] = np.nan
        rlR4[upArea < 1e9] = np.nan

        rlR8[~tamask] = np.nan
        rlR4[~tamask] = np.nan

        rl_r8.append(rlR8)
        rl_r4.append(rlR4)
        rl_bsln.append(rlBslnR8)

    rl_r8 = np.array(rl_r8)
    rl_r4 = np.array(rl_r4)
    rl_bsln = np.array(rl_bsln)

    mnbsln = np.nanmean(rl_bsln, 0)
    mnbsln[mnbsln < threshold] = np.nan
    rc_r8 = (np.nanmean(rl_r8, 0) - mnbsln) / mnbsln
    rc_r4 = (np.nanmean(rl_r4, 0) - mnbsln) / mnbsln

    relChngDiff = rc_r8 - rc_r4
    relChngDiff[~tamask] = np.nan

    nmdl = rl_bsln.shape[0]
    std_r8 = np.std(rl_r8 - rl_bsln, 0) / np.sqrt(
        np.sum(rl_bsln**2., 0) / nmdl)
    std_r4 = np.std(rl_r4 - rl_bsln, 0) / np.sqrt(
        np.sum(rl_bsln**2., 0) / nmdl)
    std_diff = np.std(rl_r8 - rl_r4, 0) / np.sqrt(
        np.sum(rl_bsln**2., 0) / nmdl)

    _, pval_r8 = stats.ttest_ind(rl_r8, rl_bsln, 0)
    _, pval_r4 = stats.ttest_ind(rl_r4, rl_bsln, 0)
    _, pval_diff = stats.ttest_ind(rl_r8, rl_r4, 0)

    return relChngDiff, rc_r8, rc_r4, std_r8, std_r4, std_diff, pval_r8, pval_r4, pval_diff
コード例 #13
0
def getGrossEnsembleAtYear(ryear,
                           ncDir='/ClimateRun4/multi-hazard/eva',
                           bslnYear=1995,
                           retPer=100,
                           threshold=200):
    flpattern = 'projection_dis_{scen}_{mdl}_wuConst_statistics.nc'

    wlyR8 = getWarmingLevels('rcp85', 2.0)
    #wlyR4 = getWarmingLevels('rcp45', warmingLev)

    models = wlyR8.keys()
    tamask, _, _ = getAfricaAndTurkeyMask()
    tamask = tamask.transpose()

    rl_all = []
    for mdl, imdl in zip(models, range(len(models))):
        print('model ' + mdl)
        flr8 = flpattern.format(scen='rcp85', mdl=mdl)
        flr8pth = os.path.join(ncDir, flr8)
        flr4 = flpattern.format(scen='rcp45', mdl=mdl)
        flr4pth = os.path.join(ncDir, flr4)

        #r8year = wlyR8[mdl] + wlOffset
        #r8yearInf = int(np.floor(r8year/float(5))*5)
        #print('  rcp85 w.l. year: ' + str(r8year))
        #r4year = wlyR4[mdl] + wlOffset
        #r4yearInf = int(np.floor(r4year/float(5))*5)
        #print('  rcp45 w.l. year: ' + str(r4year))

        ryearInf = int(np.floor(ryear / float(5)) * 5)

        print('  loading file ' + flr8pth)
        ds = netCDF4.Dataset(flr8pth)
        retper_ = ds.variables['return_period'][:]
        rpIndx = np.where(retper_ == retPer)[0][0]
        year_ = ds.variables['year'][:]
        yIndxBsln = np.where(year_ == bslnYear)[0][0]
        rlBslnR8 = ds.variables['rl'][rpIndx, yIndxBsln, :, :]
        #yIndx = np.where(year_==r8yearInf)[0][0]
        yIndx = np.where(year_ == ryearInf)[0][0]
        rlR8_ = ds.variables['rl'][rpIndx, yIndx:yIndx + 2, :, :]
        ds.close()
        #rlR8 = interp1d(year_[yIndx:yIndx+2], rlR8_, axis=0)(r8year)
        rlR8 = interp1d(year_[yIndx:yIndx + 2], rlR8_, axis=0)(ryear)
        r8RelChng = (rlR8 - rlBslnR8) / rlBslnR8
        if threshold > 0:
            cnd = rlBslnR8 < threshold
            r8RelChng[cnd] = np.nan

        print('  loading file ' + flr4pth)
        ds = netCDF4.Dataset(flr4pth)
        rlBslnR4 = ds.variables['rl'][rpIndx, yIndxBsln, :, :]
        #yIndx = np.where(year_==r4yearInf)[0][0]
        yIndx = np.where(year_ == ryearInf)[0][0]
        rlR4_ = ds.variables['rl'][rpIndx, yIndx:yIndx + 2, :, :]
        ds.close()
        #rlR4 = interp1d(year_[yIndx:yIndx+2], rlR4_, axis=0)(r4year)
        rlR4 = interp1d(year_[yIndx:yIndx + 2], rlR4_, axis=0)(ryear)
        r4RelChng = (rlR4 - rlBslnR4) / rlBslnR4
        if threshold > 0:
            cnd = rlBslnR4 < threshold
            r4RelChng[cnd] = np.nan

        r8RelChng[~tamask] = np.nan
        r4RelChng[~tamask] = np.nan

        rl_all.append(r8RelChng)
        rl_all.append(r4RelChng)

    rl_all = np.array(rl_all)

    ensembleRelChngAll = np.nanmean(rl_all, 0)

    return ensembleRelChngAll
コード例 #14
0
def extract1Model(scen, model, inputHistNcFlPath, inputRcNcFlPath, outputNcFlPath, varnames=['pr']):
  wls = [1.5, 2.0, 3.0]
  bslnYear = 1995

  print('  loading the data ...')
  dsin = netCDF4.Dataset(inputHistNcFlPath)
  x = dsin.variables['x'][:]
  y = dsin.variables['y'][:]
  val = None
  for varname in varnames:
    if varname in dsin.variables:
      val = dsin.variables[varname][:]
      break
  if val is None:
    raise Exception('variables ' + str(varnames) + ' not found in file ' + inputHistNcFlPath)
  valHist = val
  tmnc = dsin.variables['time']
  valYrsHist = [d.year for d in netCDF4.num2date(tmnc[:], tmnc.units, tmnc.calendar)]
  dsin.close()

  dsin = netCDF4.Dataset(inputRcNcFlPath)
  val = None
  for varname in varnames:
    if varname in dsin.variables:
      val = dsin.variables[varname][:]
      break
  if val is None:
    raise Exception('variables ' + str(varnames) + ' not found in file' + inputRcNcFlPath)
  valRc = val
  tmnc = dsin.variables['time']
  valYrsRc = [d.year for d in netCDF4.num2date(tmnc[:], tmnc.units, tmnc.calendar)]
  dsin.close()

  val = np.concatenate([valHist, valRc], 0)
  valYrs = np.concatenate([valYrsHist, valYrsRc], 0)

  def getLonLat():
    lonLatFile = 'lonlat.nc'
    ds = netCDF4.Dataset(lonLatFile)
    lon = ds.variables['lon'][:].transpose()
    lat = ds.variables['lat'][:].transpose()
    return lon, lat

  def getValAtYear(valAll, yearAll, year, halfTmWindowSize=14):
    yindx = np.where(yearAll == year)[0][0]
    val = np.nanmean(valAll[yindx-halfTmWindowSize:yindx+halfTmWindowSize, :], 0).squeeze()
    return val
    
  bslnVal = getValAtYear(val, valYrs, bslnYear)

  print('  elaborating ...')
  noutWls = len(wls)
  shp_ = bslnVal.shape
  shpOut = [noutWls, shp_[0], shp_[1]]
  outYrVal = np.zeros(shpOut)*np.nan
  for wl, iwl in zip(wls, range(noutWls)):
    ywls = getWarmingLevels(scen, wl)
    if ywls == None:
      continue
    outYr = ywls[model]
    yrVal = getValAtYear(val, valYrs, outYr)
    outYrVal[iwl, :, :] = yrVal
    
  bslnVal3d = np.tile(bslnVal, [3, 1, 1])
  dlt = (outYrVal/bslnVal3d - 1)*100.
  absDlt = outYrVal - bslnVal3d

  print('  saving the output ...')
  if os.path.isfile(outputNcFlPath):
    os.remove(outputNcFlPath)
  dsout = netCDF4.Dataset(outputNcFlPath, 'w')
  dsout.createDimension('x', len(x))
  dsout.createDimension('y', len(y))
  dsout.createDimension('warming_lev', noutWls)

  xnc = dsout.createVariable('x', 'f8', ('x'))
  xnc.description = 'x'
  xnc[:] = x

  ync = dsout.createVariable('y', 'f8', ('y'))
  ync.description = 'y'
  ync[:] = y

  lon, lat = getLonLat()
  lon, lat = lon.transpose(), lat.transpose()
  lonnc = dsout.createVariable('lon', 'f8', ('y', 'x'))
  lonnc.description = 'longitude mtx'
  lonnc[:] = lon

  latnc = dsout.createVariable('lat', 'f8', ('y', 'x'))
  latnc.description = 'latitude mtx'
  latnc[:] = lat

  wlsnc = dsout.createVariable('warming_lev', 'f4', ('warming_lev'))
  wlsnc.description = 'warming_lev'
  wlsnc[:] = wls

  bslnYrNc = dsout.createVariable('baseline_year', 'i4')
  bslnYrNc.description = 'baseline year'
  bslnYrNc[:] = bslnYear

  bslnValNc = dsout.createVariable('baseline_val', 'f4', ('y', 'x'))
  bslnValNc.description = 'value at baseline (deg or mm or m3 or ...)'
  bslnValNc[:] = bslnVal

  valNc = dsout.createVariable('value', 'f4', ('warming_lev', 'y', 'x'))
  valNc.description = 'values at warming levels (deg or mm or m3 or ...)'
  valNc[:] = outYrVal

  valNc = dsout.createVariable('value_perc_chng', 'f4', ('warming_lev', 'y', 'x'))
  valNc.description = 'percentage change at warming levels (%)'
  valNc[:] = dlt

  valNc = dsout.createVariable('value_abs_chng', 'f4', ('warming_lev', 'y', 'x'))
  valNc.description = 'absolute change at warming levels (deg or mm or m3 or ...)'
  valNc[:] = absDlt

  dsout.close()
コード例 #15
0
def computeRlChngPValueAtWarmingLev(ncDir='/ClimateRun4/multi-hazard/eva', bslnYear=1995, scen='rcp85', warmingLev=2, retPer=100, minThreshold=0):
  flpattern = 'projection_dis_{scen}_{mdl}_wuChang_statistics.nc'

  wly = getWarmingLevels(scen, warmingLev)

  models = wly.keys()

  rl_bs = []
  serl_bs = []
  rl_rc = [] 
  serl_rc = []
  for mdl, imdl in zip(models, range(len(models))):
    print('model ' + mdl)
    flr = flpattern.format(scen=scen, mdl=mdl)
    flrpth = os.path.join(ncDir, flr)

    rcyear = wly[mdl]
    rcyearInf = int(np.floor(rcyear/float(5))*5)
    print('  ' + scen + ' w.l. year: ' + str(rcyear))

    print('  loading file ' + flrpth)
    ds = netCDF4.Dataset(flrpth)
    retper_ = ds.variables['return_period'][:]
    rpIndx = np.where(retper_==retPer)[0][0]
    year_ = ds.variables['year'][:]
    yIndx = np.where(year_==rcyearInf)[0][0]
    rlR_ = ds.variables['rl'][rpIndx, yIndx:yIndx+2, :, :]
    serlR_ = ds.variables['se_rl'][rpIndx, yIndx:yIndx+2, :, :]
    yBslnIndx = np.where(year_==bslnYear)[0][0]
    rlBsln = ds.variables['rl'][rpIndx, yBslnIndx, :, :]
    serlBsln = ds.variables['se_rl'][rpIndx, yBslnIndx, :, :]
    ds.close()
    rlR = interp1d(year_[yIndx:yIndx+2], rlR_, axis=0)(rcyear)
    serlR = interp1d(year_[yIndx:yIndx+2], serlR_, axis=0)(rcyear)

    rl_rc.append(rlR)
    serl_rc.append(serlR)
    rl_bs.append(rlBsln)
    serl_bs.append(serlBsln)

  rl_rc = np.array(rl_rc)
  rl_rc[rl_rc <= .1] = .1
  serl_rc = np.array(serl_rc)
  serl_rc[serl_rc <= .1] = .1

  rl_bs = np.array(rl_bs)
  rl_bs[rl_bs <= .1] = .1
  serl_bs = np.array(rl_bs)
  serl_bs[serl_bs <= .1] = .1

  cnd = np.nanmean(rl_rc, 0) < minThreshold
  cnd = np.tile(cnd, [len(models), 1, 1])
  rl_rc[cnd] = np.nan
  serl_rc[cnd] = np.nan
  rl_bs[cnd] = np.nan
  serl_bs[cnd] = np.nan

  pvalue = computeRlChngPValueMontecarlo(rl_bs, serl_bs, rl_rc, serl_rc)
 #pvalue = computeRlChngPValueMeansOnly(rl_bs, rl_rc)

  agrMdlCnt, std = countAgreeingModelsAndGetStdDev(rl_bs, rl_rc)

  return pvalue, agrMdlCnt, std
コード例 #16
0
def loadWlVsScenChange(
        ncDir='/ClimateRun4/multi-hazard/eva',
        bslnYear=1995,
        warmingLev=2,
        retPer=100,
        threshold=0,
        rlVarName='rl',
        flpattern='projection_dis_{scen}_{mdl}_wuConst_statistics.nc',
        nmodels=-1,
        shapeParamNcVarName='',
        minNumModels=8):
    # computes the mean relative change

    wlyR8 = getWarmingLevels('rcp85', warmingLev)
    wlyR4 = getWarmingLevels('rcp45', warmingLev)

    dsuparea = netCDF4.Dataset('upArea.nc')
    upArea = dsuparea.variables['upArea'][:].transpose()
    dsuparea.close()

    maskInvalidShapeParamPts = shapeParamNcVarName != ''

    models = wlyR8.keys()
    if nmodels > -1:
        models = models[:nmodels]
    # menta

#models = [models[0], models[1]]
    tamask, _, _ = getAfricaAndTurkeyMask()
    tamask = tamask.transpose()

    if minNumModels > nmodels:
        minNumModels = nmodels

    rl_r4 = []
    rl_r8 = []
    mdlCountR4 = np.zeros(upArea.shape)
    mdlCountR8 = np.zeros(upArea.shape)
    for mdl, imdl in zip(models, range(len(models))):
        print('model ' + mdl)
        flr8 = flpattern.format(scen='rcp85', mdl=mdl)
        flr8pth = os.path.join(ncDir, flr8)
        flr4 = flpattern.format(scen='rcp45', mdl=mdl)
        flr4pth = os.path.join(ncDir, flr4)

        r8year = wlyR8[mdl]
        r8yearInf = int(np.floor(r8year / float(5)) * 5)
        print('  rcp85 w.l. year: ' + str(r8year))
        r4year = wlyR4[mdl]
        r4yearInf = int(np.floor(r4year / float(5)) * 5)
        print('  rcp45 w.l. year: ' + str(r4year))

        print('  loading file ' + flr8pth)
        ds = netCDF4.Dataset(flr8pth)
        retper_ = ds.variables['return_period'][:]
        rpIndx = np.where(retper_ == retPer)[0][0]
        year_ = ds.variables['year'][:]
        yIndxBsln = np.where(year_ == bslnYear)[0][0]
        rlBslnR8 = ds.variables[rlVarName][rpIndx, yIndxBsln, :, :]
        yIndx = np.where(year_ == r8yearInf)[0][0]
        rlR8_ = ds.variables[rlVarName][rpIndx, yIndx:yIndx + 2, :, :]
        rlR8 = interp1d(year_[yIndx:yIndx + 2], rlR8_, axis=0)(r8year)
        if maskInvalidShapeParamPts:
            shapeParam = ds.variables[shapeParamNcVarName][:]
            cnd = shapeParam <= -.4
            rlBslnR8[cnd] = np.nan
            rlR8[cnd] = np.nan
        ds.close()
        r8RelChng = (rlR8 - rlBslnR8) / rlBslnR8
        if threshold > 0:
            cnd = rlBslnR8 < threshold
            r8RelChng[cnd] = np.nan
        #r8RelChng[r8RelChng < -.15] = np.nan

        print('  loading file ' + flr4pth)
        ds = netCDF4.Dataset(flr4pth)
        rlBslnR4 = ds.variables[rlVarName][rpIndx, yIndxBsln, :, :]
        #rlBslnR4 = rlBslnR8
        yIndx = np.where(year_ == r4yearInf)[0][0]
        rlR4_ = ds.variables[rlVarName][rpIndx, yIndx:yIndx + 2, :, :]
        rlR4 = interp1d(year_[yIndx:yIndx + 2], rlR4_, axis=0)(r4year)
        if maskInvalidShapeParamPts:
            shapeParam = ds.variables[shapeParamNcVarName][:]
            cnd = shapeParam <= -.4
            rlBslnR4[cnd] = np.nan
            rlR4[cnd] = np.nan
        ds.close()
        r4RelChng = (rlR4 - rlBslnR4) / rlBslnR4
        if threshold > 0:
            cnd = rlBslnR4 < threshold
            r4RelChng[cnd] = np.nan
        #r4RelChng[r4RelChng < -.15] = np.nan

        r8RelChng[upArea < 1e9] = np.nan
        r4RelChng[upArea < 1e9] = np.nan

        r8RelChng[~tamask] = np.nan
        r4RelChng[~tamask] = np.nan

        rl_r8.append(r8RelChng)
        rl_r4.append(r4RelChng)

        mdlCountR8 += ~np.isnan(r8RelChng)
        mdlCountR4 += ~np.isnan(r4RelChng)

    rl_r8 = np.array(rl_r8)
    rl_r4 = np.array(rl_r4)

    mdlCountMskR8 = mdlCountR8 >= minNumModels
    mdlCountMskR4 = mdlCountR4 >= minNumModels
    mskR8 = np.tile(mdlCountMskR8, [rl_r8.shape[0], 1, 1])
    mskR4 = np.tile(mdlCountMskR4, [rl_r8.shape[0], 1, 1])

    rl_r8[mskR8 != 1] = np.nan
    rl_r4[mskR4 != 1] = np.nan

    relChngDiff = np.nanmean(rl_r8 - rl_r4, 0)
    relChngDiff[~tamask] = np.nan

    return relChngDiff, np.nanmean(rl_r8, 0), np.nanmean(rl_r4,
                                                         0), rl_r8, rl_r4
コード例 #17
0
def plotEnsembles(rootDir='/DATA/JEODPP/eos/projects/CRITECH/ADAPTATION/ClimateRuns/LisfloodEuroCordex/', wuChangStr='wuChang'):
  warmingLevs = [1.5, 2.0]
  rcp8, rcp4 = 'rcp85', 'rcp45'
  retPer = 100

  models = """
IPSL-INERIS-WRF331F_BC
SMHI-RCA4_BC_CNRM-CERFACS-CNRM-CM5
SMHI-RCA4_BC_ICHEC-EC-EARTH
SMHI-RCA4_BC_IPSL-IPSL-CM5A-MR
SMHI-RCA4_BC_MOHC-HadGEM2-ES
SMHI-RCA4_BC_MPI-M-MPI-ESM-LR
CLMcom-CCLM4-8-17_BC_CNRM-CERFACS-CNRM-CM5
CLMcom-CCLM4-8-17_BC_ICHEC-EC-EARTH
CLMcom-CCLM4-8-17_BC_MPI-M-MPI-ESM-LR
DMI-HIRHAM5-ICHEC-EC-EARTH_BC
KNMI-RACMO22E-ICHEC-EC-EARTH_BC
"""
  models = models.split()

  outputPngFile = 'scatter_minDis_' + wuChangStr + '_wls_ensemble.png'

  fig = plt.figure(figsize=(10, 5))
  gs = gridspec.GridSpec(1, 3, width_ratios=[1,1,.1])
  lon, lat = [], []
  for warmingLev, iwl in zip(warmingLevs, range(len(warmingLevs))):

    wlYearR8 = getWarmingLevels(rcp8, warmingLev)
    wlYearR4 = getWarmingLevels(rcp4, warmingLev)
    relChng8lst, relChng4lst = [], []

    for mdl, imdl in zip(models, range(1, len(models) + 1)):
      print('getting model ' + mdl)
      
      fldirHist = os.path.join(rootDir, 'historical', mdl, 'wuConst')
      fldirR8 = os.path.join(rootDir, rcp8, mdl, wuChangStr)
      wrmYear = wlYearR8[mdl]
      cacheIdData = '_'.join([mdl, rcp8, 'wuChang'])
      cacheIdResult = cacheIdData + '_' + str(warmingLev)
      relChngR8 = getRelChngs(cacheIdData, cacheIdResult, fldirHist, fldirR8, wrmYear)
  
      fldirR4 = os.path.join(rootDir, rcp4, mdl, wuChangStr)
      wrmYear = wlYearR4[mdl]
      cacheIdData = '_'.join([mdl, rcp4, 'wuChang'])
      cacheIdResult = cacheIdData + '_' + str(warmingLev)
      relChngR4 = getRelChngs(cacheIdData, cacheIdResult, fldirHist, fldirR4, wrmYear)
  
      relChng8lst.append(relChngR8)
      relChng4lst.append(relChngR4)

    ax = plt.subplot(gs[iwl])
    relChng8 = np.nanmean(np.array(relChng8lst), 0)
    relChng4 = np.nanmean(np.array(relChng4lst), 0)
    plotSingleModel(ax, relChng8, relChng4, 
             'Ensemble rcp85 vs rcp45, min. dis.\nwrm. lev. ' + str(warmingLev) + '$^\circ$', 
             bold=True, plotRegression=False, tickFontSize=10, labelFontSize=12, titleFontSize=13,
             xLabelPad=-25, yLabelPad=-10, xlim=[-150, 150], ylim=[-150, 150])

  plt.tight_layout()

  fig.savefig(outputPngFile, dpi=300)
コード例 #18
0
def extract1Model(scen, model, inputNcFlPath, outputNcFlPath):
    wls = [1.5, 2.0, 3.0, 4.0]
    bslnYear = 1995

    outRetPer = [1.5, 2., 5, 10., 15., 20., 50., 100., 200., 500.]

    print('  loading the data ...')
    dsin = netCDF4.Dataset(inputNcFlPath)
    x = dsin.variables['x'][:]
    y = dsin.variables['y'][:]
    inRetPer = dsin.variables['return_period'][:]
    retLev = dsin.variables['rl_min_ws'][:]
    rlYrs = dsin.variables['year'][:]
    dsin.close()

    def getLonLat():
        lonLatFile = 'lonlat.nc'
        ds = netCDF4.Dataset(lonLatFile)
        lon = ds.variables['lon'][:].transpose()
        lat = ds.variables['lat'][:].transpose()
        return lon, lat

    def getRetLevAtYear(retLevAll, yearAll, year, interpFunc=None):
        interpFunc = interp1d(
            yearAll,
            retLevAll,
            axis=1,
            bounds_error=False,
            fill_value='extrapolate') if interpFunc is None else interpFunc
        retLev = interpFunc(year)
        return retLev, interpFunc

    def getOutRetLev(inRetLev, inRetPer, outRetPer):
        interpFunc = interp1d(inRetPer, inRetLev, axis=0)
        outRetLev = interpFunc(outRetPer)
        return outRetLev

    def getOutRetPer(inRetPer, retLevCurrent, retLevBsln):
        nx = retLevCurrent.shape[1]
        ny = retLevCurrent.shape[2]
        nOutRL = retLevBsln.shape[0]
        outRetPer = np.zeros([nOutRL, nx, ny]) * np.nan
        for ix in range(nx):
            for iy in range(ny):
                rlcri = retLevCurrent[:, ix, iy]
                if np.sum(np.isnan(rlcri)) > 0:
                    continue
                rlbsln = retLevBsln[:, ix, iy]
                outRetPer[:, ix,
                          iy] = interp1d(rlcri,
                                         inRetPer,
                                         axis=0,
                                         fill_value='extrapolate')(rlbsln)
        return outRetPer

    def getSignificanceTest(retLev_KSTest, bslnRetLev_KSTest):
        nx = retLev_KSTest.shape[1]
        ny = retLev_KSTest.shape[2]
        outSigTest = np.zeros([nx, ny]) * np.nan
        for ix in range(nx):
            for iy in range(ny):
                bsln = bslnRetLev_KSTest[:, ix, iy].squeeze()
                if np.sum(np.isnan(bsln)) > 0:
                    continue
                futvl = retLev_KSTest[:, ix, iy].squeeze()
                st, pval = ks_2samp(bsln, futvl)
                outSigTest[ix, iy] = pval <= .05
                pass
        return outSigTest

    interpFunc = None
    bslnRetLev_, interpFunc = getRetLevAtYear(retLev, rlYrs, bslnYear,
                                              interpFunc)
    bslnRetLev = getOutRetLev(bslnRetLev_, inRetPer, outRetPer)

    retPerKSTest = 20
    bslnYears_KSTest = np.arange(bslnYear - 14, bslnYear + 16)
    bslnRetLev_KSTest_, _ = getRetLevAtYear(retLev, rlYrs, bslnYears_KSTest,
                                            interpFunc)
    bslnRetLev_KSTest = getOutRetLev(bslnRetLev_KSTest_, inRetPer,
                                     retPerKSTest)
    del bslnRetLev_KSTest_
    gc.collect()

    print('  elaborating ...')
    noutWls = len(wls)
    shp_ = bslnRetLev.shape
    shpOut = [noutWls, shp_[0], shp_[1], shp_[2]]
    outYrRetPer = np.zeros(shpOut) * np.nan
    outYrRetLev = np.zeros(shpOut) * np.nan
    signTest = np.zeros([noutWls, shp_[1], shp_[2]]) * np.nan
    for wl, iwl in zip(wls, range(noutWls)):
        ywls = getWarmingLevels(scen, wl)
        if ywls == None:
            continue
        outYr = ywls[model]
        yrRetLev, interpFunc = getRetLevAtYear(retLev, rlYrs, outYr,
                                               interpFunc)
        outYrRetLev[iwl, :, :, :] = getOutRetLev(yrRetLev, inRetPer, outRetPer)
        outYrRetPer[iwl, :, :, :] = getOutRetPer(inRetPer, yrRetLev,
                                                 bslnRetLev)

        years_KSTest = np.arange(outYr - 15, outYr + 15)
        retLev_KSTest_, _ = getRetLevAtYear(retLev, rlYrs, years_KSTest)
        retLev_KSTest = getOutRetLev(retLev_KSTest_, inRetPer, retPerKSTest)
        del retLev_KSTest_
        gc.collect()
        signTest[iwl, :, :] = getSignificanceTest(retLev_KSTest,
                                                  bslnRetLev_KSTest)

    outYrRetPer[outYrRetPer < 1] = 1
    print('  saving the output ...')
    if os.path.isfile(outputNcFlPath):
        os.remove(outputNcFlPath)
    dsout = netCDF4.Dataset(outputNcFlPath, 'w')
    dsout.createDimension('x', len(x))
    dsout.createDimension('y', len(y))
    dsout.createDimension('warming_lev', noutWls)
    dsout.createDimension('baseline_rp', len(outRetPer))

    xnc = dsout.createVariable('x', 'f8', ('x'))
    xnc.description = 'x'
    xnc[:] = x

    ync = dsout.createVariable('y', 'f8', ('y'))
    ync.description = 'y'
    ync[:] = y

    lon, lat = getLonLat()
    lonnc = dsout.createVariable('lon', 'f8', ('x', 'y'))
    lonnc.description = 'longitude mtx'
    lonnc[:] = lon

    latnc = dsout.createVariable('lat', 'f8', ('x', 'y'))
    latnc.description = 'latitude mtx'
    latnc[:] = lat

    wlsnc = dsout.createVariable('warming_lev', 'f4', ('warming_lev'))
    wlsnc.description = 'warming_lev'
    wlsnc[:] = wls

    bslnRpNc = dsout.createVariable('baseline_rp', 'f4', ('baseline_rp'))
    bslnRpNc.description = 'return period at the baseline (years)'
    bslnRpNc[:] = outRetPer

    bslnYrNc = dsout.createVariable('baseline_year', 'i4')
    bslnYrNc.description = 'baseline year'
    bslnYrNc[:] = bslnYear

    bslnRetLevNc = dsout.createVariable('baseline_return_level', 'f4',
                                        ('baseline_rp', 'x', 'y'))
    bslnRetLevNc.description = 'return levels at baseline'
    bslnRetLevNc[:] = bslnRetLev

    retPerNc = dsout.createVariable('baseline_rp_shift', 'f4',
                                    ('warming_lev', 'baseline_rp', 'x', 'y'))
    retPerNc.description = 'shifted return periods of the baseline return values (years)'
    retPerNc[:] = outYrRetPer

    retLevNc = dsout.createVariable('return_level', 'f4',
                                    ('warming_lev', 'baseline_rp', 'x', 'y'))
    retLevNc.description = 'return levels at warming levels (m**3)'
    retLevNc[:] = outYrRetLev

    sigNc = dsout.createVariable('significance', 'f4',
                                 ('warming_lev', 'x', 'y'))
    sigNc.description = 'significance of the projected change (ks test on the 20-y return level)'
    sigNc[:] = signTest

    dsout.close()
コード例 #19
0
def extract1Model(scen, model, inputNcFlPath, outputNcFlPath):
    wls = [1.5, 2.0, 3.0, 4.0]
    bslnYear = 1995

    print('  loading the data ...')
    dsin = netCDF4.Dataset(inputNcFlPath)
    x = dsin.variables['x'][:]
    y = dsin.variables['y'][:]
    vals = dsin.variables['year_mean'][:]
    yrs = dsin.variables['year_all'][:]
    dsin.close()

    def getLonLat():
        lonLatFile = 'lonlat.nc'
        ds = netCDF4.Dataset(lonLatFile)
        lon = ds.variables['lon'][:].transpose()
        lat = ds.variables['lat'][:].transpose()
        return lon, lat

    def getValAtYear(vlsAll, yearAll, year):
        iyear = np.where(yearAll == year)[0][0]
        minIndx = max(iyear - 14, 0)
        vly = np.nanmean(vlsAll[minIndx:iyear + 15, :, :], 0).squeeze()
        return vly

    bslnVal = getValAtYear(vals, yrs, bslnYear)

    print('  elaborating ...')
    noutWls = len(wls)
    shp_ = bslnVal.shape
    shpOut = [noutWls, shp_[0], shp_[1]]
    outYrVal = np.zeros(shpOut) * np.nan
    for wl, iwl in zip(wls, range(noutWls)):
        ywls = getWarmingLevels(scen, wl)
        if ywls == None:
            continue
        outYr = ywls[model]
        yrVal = getValAtYear(vals, yrs, outYr)
        outYrVal[iwl, :, :] = yrVal

    print('  saving the output ...')
    if os.path.isfile(outputNcFlPath):
        os.remove(outputNcFlPath)
    dsout = netCDF4.Dataset(outputNcFlPath, 'w')
    dsout.createDimension('x', len(x))
    dsout.createDimension('y', len(y))
    dsout.createDimension('warming_lev', noutWls)

    xnc = dsout.createVariable('x', 'f8', ('x'))
    xnc.description = 'x'
    xnc[:] = x

    ync = dsout.createVariable('y', 'f8', ('y'))
    ync.description = 'y'
    ync[:] = y

    lon, lat = getLonLat()
    lonnc = dsout.createVariable('lon', 'f8', ('x', 'y'))
    lonnc.description = 'longitude mtx'
    lonnc[:] = lon

    latnc = dsout.createVariable('lat', 'f8', ('x', 'y'))
    latnc.description = 'latitude mtx'
    latnc[:] = lat

    wlsnc = dsout.createVariable('warming_lev', 'f4', ('warming_lev'))
    wlsnc.description = 'warming_lev'
    wlsnc[:] = wls

    bslnYrNc = dsout.createVariable('baseline_year', 'i4')
    bslnYrNc.description = 'baseline year'
    bslnYrNc[:] = bslnYear

    bslnValNc = dsout.createVariable('baseline_mean_dis', 'f4', ('x', 'y'))
    bslnValNc.description = 'mean discharge at baseline (m**3)'
    bslnValNc[:] = bslnVal

    valNc = dsout.createVariable('mean_dis', 'f4', ('warming_lev', 'x', 'y'))
    valNc.description = 'mean discharge at warming levels (m**3)'
    valNc[:] = outYrVal

    prcChngNc = dsout.createVariable('perc_change', 'f4',
                                     ('warming_lev', 'x', 'y'))
    prcChngNc.description = 'change of mean discharge at warming levels (%)'
    prcChngNc[:] = (outYrVal - bslnVal) / bslnVal * 100

    dsout.close()
コード例 #20
0
def plotTimeSerie30YVariability_hiExt(ax=None,
                                      plotXLabel=True,
                                      warmingLev=2.0):
    yrs, rpR8Srs, rpR4Srs, models = loadWlVsScenChange.loadRetPerAllYears(
        nmodels=nmodels)
    rpR8 = getRelChng(yrs, rpR8Srs)
    rpR4 = getRelChng(yrs, rpR4Srs)

    wlyR8 = getWarmingLevels('rcp85', warmingLev)
    r8srs = getNYrsTimeSeriesAroundWl(yrs, rpR8, wlyR8, models)
    r8medianWl, r8sigmaWl = getMeanAndSigma(r8srs)
    cndSignR8 = np.abs(r8medianWl) > r8sigmaWl
    cndPosi = r8medianWl > 0
    #cnd = np.logical_and(cndSignR8, cndPosi)
    cnd = cndPosi
    cndMtx = np.tile(cnd, [r8srs.shape[0], r8srs.shape[1], 1, 1])
    r8srs[~cndMtx] = np.nan
    r8median_ = np.median(r8srs, 0)
    r8median = r8median_.reshape(r8median_.shape[0],
                                 r8median_.shape[1] * r8median_.shape[2])
    r8median = np.nanmedian(r8median, 1)
    #r8median = np.nanmean(r8median, 1)

    r8spmean = r8srs.reshape(r8srs.shape[0], r8srs.shape[1],
                             r8srs.shape[2] * r8srs.shape[3])
    r8spmean = np.nanmedian(r8spmean, 2)
    #r8spmean = np.nanmean(r8spmean, 2)
    r8skew = stats.skew(r8spmean[:, 3])
    r8sigma = np.std(r8spmean[:, 3])
    r8median = np.nanmedian(r8spmean, 0)

    wlyR4 = getWarmingLevels('rcp45', 2.0)
    r4srs = getNYrsTimeSeriesAroundWl(yrs, rpR4, wlyR4, models)
    r4medianWl, r4sigmaWl = getMeanAndSigma(r4srs)
    cndSignR4 = np.abs(r4medianWl) > r4sigmaWl
    cndPosi = r4medianWl > 0
    cnd = cndPosi
    cndMtx = np.tile(cnd, [r4srs.shape[0], r4srs.shape[1], 1, 1])
    r4srs[~cndMtx] = np.nan
    r4median_ = np.median(r4srs, 0)
    r4median = r4median_.reshape(r4median_.shape[0],
                                 r4median_.shape[1] * r4median_.shape[2])
    r4median = np.nanmedian(r4median, 1)
    #r4median = np.nanmean(r4median, 1)

    r4spmean = r4srs.reshape(r4srs.shape[0], r4srs.shape[1],
                             r4srs.shape[2] * r4srs.shape[3])
    r4spmean = np.nanmedian(r4spmean, 2)
    #r4spmean = np.nanmean(r4spmean, 2)
    r4skew = stats.skew(r4spmean[:, 3])
    r4sigma = np.std(r4spmean[:, 3])
    r4median = np.nanmedian(r4spmean, 0)

    yr = [-15, -10, -5, 0, 5, 10, 15]
    if ax is None:
        fg = plt.figure(figsize=[8.46, 4.98])

#pmdlr8 = plt.plot(yr, r8spmean.transpose()*100, 'peachpuff', label='RCP8.5 models ($\sigma={sgm:1.2f}\%, skw.={s:1.2f}$)'.format(sgm=r8sigma*100, s=r8skew));
#pmdlr4 = plt.plot(yr, r4spmean.transpose()*100, 'skyblue', label='RCP4.5 models ($\sigma={sgm:1.2f}\%, skw.={s:1.2f})$'.format(sgm=r4sigma*100, s=r4skew));
    pmdlr8 = plt.plot(yr,
                      r8spmean.transpose() * 100,
                      'peachpuff',
                      label='RCP8.5 models ($\sigma={sgm:1.2f}\%$'.format(
                          sgm=r8sigma * 100, s=r8skew))
    pmdlr4 = plt.plot(yr,
                      r4spmean.transpose() * 100,
                      'skyblue',
                      label='RCP4.5 models ($\sigma={sgm:1.2f}\%$'.format(
                          sgm=r4sigma * 100, s=r4skew))
    pmedr8 = plt.plot(yr,
                      r8median * 100,
                      'firebrick',
                      linewidth=6,
                      label='RCP8.5 mean')
    pmedr4 = plt.plot(yr,
                      r4median * 100,
                      'royalblue',
                      linewidth=6,
                      label='RCP4.5 mean')
    plt.grid('on')
    plt.legend(handles=[pmdlr8[0], pmedr8[0], pmdlr4[0], pmedr4[0]])
    if plotXLabel:
        plt.xlabel('years to $2^\circ C w.l.$', fontsize=14)
    plt.ylabel('$Q_{H100}$ % change', fontsize=14)
    plt.tight_layout()
    if ax is None:
        fg.savefig('./ensemblesVariabilityAround2deg.png', dpi=400)
コード例 #21
0
def plotTimeSerie30YVariability_hiExt(ax=None, plotXLabel=True, plotYLabel=True, warmingLev=2.0):
  yrs, rpR8Srs, rpR4Srs, models = loadWlVsScenChange.loadRetPerAllYears(nmodels=nmodels, excludedModels=excludedModels)
  rpR8 = getRelChng(yrs, rpR8Srs)
  rpR4 = getRelChng(yrs, rpR4Srs)

  wlyR8 = getWarmingLevels('rcp85', warmingLev)
  r8srs = getNYrsTimeSeriesAroundWl(yrs, rpR8, wlyR8, models)
  r8meanWl, r8sigmaWl = getMeanAndSigma(r8srs)
  cndPosi = r8meanWl > 0
  cndNega = r8meanWl < 0
  cnd = cndPosi if positiveChanges else cndNega
  cndMtx = np.tile(cnd, [r8srs.shape[0], r8srs.shape[1], 1, 1])
  r8srs[~cndMtx] = np.nan
  r8mean_ = np.nanmean(r8srs, 0)
  r8mean = r8mean_.reshape(r8mean_.shape[0], r8mean_.shape[1]*r8mean_.shape[2])
 #r8mean = np.nanmedian(r8mean, 1)
  r8mean = np.nanmean(r8mean, 1)

  r8spmean = r8srs.reshape(r8srs.shape[0], r8srs.shape[1], r8srs.shape[2]*r8srs.shape[3])
  r8spmean = np.nanmean(r8spmean, 2)
 #r8spmean = np.nanmean(r8spmean, 2)
  r8skew = stats.skew(r8spmean[:, 3])
  r8sigma = np.std(r8spmean[:, 3])
  r8mean = np.nanmean(r8spmean, 0)

  wlyR4 = getWarmingLevels('rcp45', warmingLev)
  r4srs = getNYrsTimeSeriesAroundWl(yrs, rpR4, wlyR4, models)
  r4meanWl, r4sigmaWl = getMeanAndSigma(r4srs)
  cndPosi = r4meanWl > 0
  cndNega = r8meanWl < 0
  cnd = cndPosi if positiveChanges else cndNega
  cndMtx = np.tile(cnd, [r4srs.shape[0], r4srs.shape[1], 1, 1])
  r4srs[~cndMtx] = np.nan
  r4mean_ = np.nanmean(r4srs, 0)
  r4mean = r4mean_.reshape(r4mean_.shape[0], r4mean_.shape[1]*r4mean_.shape[2])
 #r4mean = np.nanmedian(r4mean, 1)
  r4mean = np.nanmean(r4mean, 1)

  r4spmean = r4srs.reshape(r4srs.shape[0], r4srs.shape[1], r4srs.shape[2]*r4srs.shape[3])
  r4spmean = np.nanmean(r4spmean, 2)
 #r4spmean = np.nanmean(r4spmean, 2)
  r4skew = stats.skew(r4spmean[:, 3])
  r4sigma = np.std(r4spmean[:, 3])
  r4mean = np.nanmean(r4spmean, 0)

  ensTot = np.concatenate([r8spmean, r4spmean], 0)
  stdDevTot = np.std(ensTot, 0)*100.
  meanTot = (r4mean + r8mean)/2.*100.

  yr = [-15, -10, -5, 0, 5, 10, 15]
  if ax is None:
    fg = plt.figure(figsize=[8.46, 4.98])
  pstdDev = plt.fill_between(yr, meanTot+stdDevTot, meanTot-stdDevTot, color='gainsboro', label='$\sigma={sgm:1.0f}\%$'.format(sgm=stdDevTot[int(np.floor(len(stdDevTot)/2))]))
 #pmdlr8 = plt.plot(yr, r8spmean.transpose()*100, 'sandybrown', label='RCP8.5 models ($\sigma={sgm:1.2f}\%, skw.={s:1.2f}$)'.format(sgm=r8sigma*100, s=r8skew)); 
 #pmdlr4 = plt.plot(yr, r4spmean.transpose()*100, 'skyblue', label='RCP4.5 models ($\sigma={sgm:1.2f}\%, skw.={s:1.2f})$'.format(sgm=r4sigma*100, s=r4skew)); 
 #pmdlr8 = plt.plot(yr, r8spmean.transpose()*100, 'sandybrown', label='RCP8.5 models ($\sigma_{{RCP8.5}}={sgm:1.0f}\%$)'.format(sgm=r8sigma*100, s=r8skew)); 
 #pmdlr4 = plt.plot(yr, r4spmean.transpose()*100, 'skyblue', label='RCP4.5 models ($\sigma_{{RCP4.5}}={sgm:1.0f}\%$)'.format(sgm=r4sigma*100, s=r4skew)); 
  pmdlr8 = plt.plot(yr, r8spmean.transpose()*100, 'sandybrown', label='RCP8.5 models'); 
  pmdlr4 = plt.plot(yr, r4spmean.transpose()*100, 'skyblue', label='RCP4.5 models'); 
 #pmedr8 = plt.plot(yr, r8mean*100, 'firebrick', linewidth=6, label='RCP8.5 mean'); 
 #pmedr4 = plt.plot(yr, r4mean*100, 'royalblue', linewidth=6, label='RCP4.5 mean'); 
  pmn = plt.plot(yr, (r4mean+r8mean)/2.*100, 'k', linewidth=6, label='ens. mean'); 
  plt.grid('on')
  lgndLoc = 2 if positiveChanges else 3
 #plt.legend(handles=[pmdlr8[0], pmedr8[0], pmdlr4[0], pmedr4[0], pstdDev], fontsize=12, loc=lgndLoc)
  plt.legend(handles=[pmdlr8[0], pmdlr4[0], pmn[0], pstdDev], fontsize=12, loc=lgndLoc)
  if plotXLabel:
    plt.xlabel('years to $' + str(warmingLev) + '^\circ$C w.l.', fontsize=18)
  else:
    ax.set_xticklabels([])
  if plotYLabel:
    plt.ylabel('$Q_{H100}$ % change', fontsize=17)
  else:
    ax.set_yticklabels([])
  ax.tick_params(axis="x", labelsize=axFontSize)
  ax.tick_params(axis="y", labelsize=axFontSize)

  ylm = ax.get_ylim()
  dlt = max(ylm) - min(ylm)
  pzr = plt.plot([0, 0], [min(ylm)-dlt*4, max(ylm)+dlt*4], 'k', linewidth=2)
  pzr[0].set_zorder(1)
  ax.set_ylim(ylm)
  plt.tight_layout()

  if ax is None:
    fg.savefig('./ensemblesVariabilityAround2deg.png', dpi=400)
コード例 #22
0
def extract1Model(model, inputNcFlPath, outputNcFlPath):
    bslnYear = 1995
    wls = [1.5, 2.0, 3.0]
    noutWls = len(wls)

    outRetPer = [10., 20., 50., 100., 200., 500.]

    print('  loading the data ...')
    dsin = netCDF4.Dataset(inputNcFlPath)
    lon = dsin.variables['lon'][:]
    lat = dsin.variables['lat'][:]
    inRetPer = dsin.variables['return_period'][:]
    retLev = dsin.variables['rl'][:]
    rlYrs = dsin.variables['year'][:]
    dsin.close()

    def getRetLevAtYear(retLevAll, yearAll, year, interpFunc=None):
        interpFunc = interp1d(yearAll, retLevAll,
                              axis=1) if interpFunc is None else interpFunc
        retLev = interpFunc(year)
        return retLev, interpFunc

    def getOutRetLev(inRetLev, inRetPer, outRetPer):
        interpFunc = interp1d(inRetPer, inRetLev, axis=0)
        outRetLev = interpFunc(outRetPer)
        return outRetLev

    def getOutRetPer(inRetPer, retLevCurrent, retLevBsln):
        nx = retLevCurrent.shape[1]
        ny = retLevCurrent.shape[2]
        nOutRL = retLevBsln.shape[0]
        outRetPer = np.zeros([nOutRL, nx, ny]) * np.nan
        for ix in range(nx):
            for iy in range(ny):
                rlcri = retLevCurrent[:, ix, iy]
                if np.sum(np.isnan(rlcri)) > 0:
                    continue
                rlbsln = retLevBsln[:, ix, iy]
                outRetPer[:, ix,
                          iy] = interp1d(rlcri,
                                         inRetPer,
                                         axis=0,
                                         fill_value='extrapolate')(rlbsln)
        return outRetPer

    interpFunc = None
    bslnRetLev_, interpFunc = getRetLevAtYear(retLev, rlYrs, bslnYear,
                                              interpFunc)
    bslnRetLev = getOutRetLev(bslnRetLev_, inRetPer, outRetPer)

    print('  elaborating ...')
    shp_ = bslnRetLev.shape
    shpOut = [noutWls, shp_[0], shp_[1], shp_[2]]
    outYrRetPer = np.zeros(shpOut) * np.nan
    outYrRetLev = np.zeros(shpOut) * np.nan
    for wl, iwl in zip(wls, range(len(wls))):
        wly = getWarmingLevels('rcp85', wl)
        outYr = wly[model]
        yrRetLev, interpFunc = getRetLevAtYear(retLev, rlYrs, outYr,
                                               interpFunc)
        outYrRetLev[iwl, :, :, :] = getOutRetLev(yrRetLev, inRetPer, outRetPer)
        outYrRetPer[iwl, :, :, :] = getOutRetPer(inRetPer, yrRetLev,
                                                 bslnRetLev)

    outYrRetPer[outYrRetPer < 1] = 1
    print('  saving the output ...')
    if os.path.isfile(outputNcFlPath):
        os.remove(outputNcFlPath)
    dsout = netCDF4.Dataset(outputNcFlPath, 'w')
    dsout.createDimension('lat', len(lat))
    dsout.createDimension('lon', len(lon))
    dsout.createDimension('warming_lev', noutWls)
    dsout.createDimension('baseline_rp', len(outRetPer))

    xnc = dsout.createVariable('lon', 'f8', ('lon'))
    xnc.description = 'longitude'
    xnc.long_name = 'longitude'
    xnc.standard_name = 'longitude'
    xnc.units = 'degrees_east'
    xnc[:] = lon

    ync = dsout.createVariable('lat', 'f8', ('lat'))
    ync.description = 'latitude'
    ync.description = 'latitude'
    ync.long_name = 'latitude'
    ync.standard_name = 'latitude'
    ync.units = 'degrees_north'
    ync[:] = lat

    wlnc = dsout.createVariable('warming_lev', 'f4', ('warming_lev'))
    wlnc.description = 'warming level'
    wlnc[:] = wls

    bslnRpNc = dsout.createVariable('baseline_rp', 'f4', ('baseline_rp'))
    bslnRpNc.description = 'return period at the baseline (years)'
    bslnRpNc[:] = outRetPer

    bslnYrNc = dsout.createVariable('baseline_year', 'i4')
    bslnYrNc.description = 'baseline year'
    bslnYrNc[:] = bslnYear

    bslnRetLevNc = dsout.createVariable('baseline_return_level', 'f4',
                                        ('baseline_rp', 'lon', 'lat'))
    bslnRetLevNc.description = 'return levels at baseline'
    bslnRetLevNc[:] = bslnRetLev

    retPerNc = dsout.createVariable(
        'baseline_rp_shift', 'f4',
        ('warming_lev', 'baseline_rp', 'lon', 'lat'))
    retPerNc.description = 'shifted return periods of the baseline return values (years)'
    retPerNc[:] = outYrRetPer
    retPerNc.coordinates = 'warming_lev baseline_rp lon lat'

    retLevNc = dsout.createVariable(
        'return_level', 'f4', ('warming_lev', 'baseline_rp', 'lon', 'lat'))
    retLevNc.description = 'return levels at warming levels (m**3)'
    retLevNc[:] = outYrRetLev
    retLevNc.coordinates = 'warming_lev baseline_rp lon lat'

    dsout.close()
コード例 #23
0
def loadWlVsScenChangeYMax(
        ncDir='/ClimateRun4/multi-hazard/eva',
        bslnYear=1995,
        warmingLev=2,
        retPer=100,
        threshold=0,
        rlVarName='year_max',
        windowHalfSize=10,
        flpattern='projection_dis_{scen}_{mdl}_wuConst_statistics.nc'):
    # computes the mean relative change

    wlyR8 = getWarmingLevels('rcp85', warmingLev)
    wlyR4 = getWarmingLevels('rcp45', warmingLev)

    dsuparea = netCDF4.Dataset('upArea.nc')
    upArea = dsuparea.variables['upArea'][:].transpose()
    dsuparea.close()

    models = wlyR8.keys()
    # menta
    #models = [models[0], models[1]]
    tamask, _, _ = getAfricaAndTurkeyMask()
    tamask = tamask.transpose()

    rl_r4 = []
    rl_r8 = []
    for mdl, imdl in zip(models, range(len(models))):
        print('model ' + mdl)
        flr8 = flpattern.format(scen='rcp85', mdl=mdl)
        flr8pth = os.path.join(ncDir, flr8)
        flr4 = flpattern.format(scen='rcp45', mdl=mdl)
        flr4pth = os.path.join(ncDir, flr4)

        r8year = wlyR8[mdl]
        print('  rcp85 w.l. year: ' + str(r8year))
        r4year = wlyR4[mdl]
        print('  rcp45 w.l. year: ' + str(r4year))

        hsz = windowHalfSize

        print('  loading file ' + flr8pth)
        ds = netCDF4.Dataset(flr8pth)
        year_ = ds.variables['year_all'][:]
        yIndxBsln = np.where(year_ == bslnYear)[0][0]
        rlBslnR8 = np.nanmean(
            ds.variables[rlVarName][yIndxBsln - hsz:yIndxBsln + hsz, :, :], 0)
        yIndx = np.where(year_ == r8year)[0][0]
        rlR8 = np.nanmean(
            ds.variables[rlVarName][yIndx - hsz:yIndx + hsz, :, :], 0)
        ds.close()
        r8RelChng = (rlR8 - rlBslnR8) / rlBslnR8
        if threshold > 0:
            cnd = rlBslnR8 < threshold
            r8RelChng[cnd] = np.nan
        #r8RelChng[r8RelChng < -.15] = np.nan

        print('  loading file ' + flr4pth)
        ds = netCDF4.Dataset(flr4pth)
        #rlBslnR4 = ds.variables[rlVarName][rpIndx, yIndxBsln, :, :]
        rlBslnR4 = rlBslnR8
        yIndx = np.where(year_ == r4year)[0][0]
        rlR4 = np.nanmean(
            ds.variables[rlVarName][yIndx - hsz:yIndx + hsz, :, :], 0)
        ds.close()
        r4RelChng = (rlR4 - rlBslnR4) / rlBslnR4
        if threshold > 0:
            cnd = rlBslnR4 < threshold
            r4RelChng[cnd] = np.nan
        #r4RelChng[r4RelChng < -.15] = np.nan

        r8RelChng[upArea < 1e9] = np.nan
        r4RelChng[upArea < 1e9] = np.nan

        r8RelChng[~tamask] = np.nan
        r4RelChng[~tamask] = np.nan

        rl_r8.append(r8RelChng)
        rl_r4.append(r4RelChng)

    rl_r8 = np.array(rl_r8)
    rl_r4 = np.array(rl_r4)

    relChngDiff = np.nanmean(rl_r8 - rl_r4, 0)
    relChngDiff[~tamask] = np.nan

    return relChngDiff, np.nanmean(rl_r8, 0), np.nanmean(rl_r4,
                                                         0), rl_r8, rl_r4
コード例 #24
0
def plotEnsembles_means(rootDir='/ClimateRun4/multi-hazard/eva/',
                        gs=None,
                        showLegend=True):
    warmingLevs = [1.5, 2.0]
    rcp8, rcp4 = 'rcp85', 'rcp45'

    models = modelStr.split()
    #models = [models[0], models[1]]

    if nTestModels != -1:
        models = models[:nTestModels]

    outputPngFile = 'scatter_100yrlChange_mean_wls_ensemble_retPer.png'

    if gs is None:
        fig = plt.figure(figsize=(11, 5))
        gs = gridspec.GridSpec(1, 3, width_ratios=[1, 1, .04])
        ownsFig = True
    else:
        ownsFig = False
    lon, lat = [], []
    axs = []
    lgndShown = not showLegend
    # lims = {1.5: [-20, 50], 2.0: [-39, 80]}
    # lims = {1.5: [-24, 60], 2.0: [-39, 80]}
    densityBins = {1.5: 130, 2.0: 130}
    lims = {1.5: [-10, 35], 2.0: [-10, 35]}
    for warmingLev, iwl in zip(warmingLevs, range(len(warmingLevs))):

        wlYearR8 = getWarmingLevels(rcp8, warmingLev)
        wlYearR4 = getWarmingLevels(rcp4, warmingLev)
        relChng8lst, relChng4lst = [], []

        vlsMask = None
        for mdl, imdl in zip(models, range(1, len(models) + 1)):
            print('getting model ' + mdl)

            ncFlNm = '_'.join(
                ['projection_dis', rcp8, mdl, wuStr, 'statistics.nc'])
            ncFlPth = os.path.join(rootDir, ncFlNm)
            wrmYear = wlYearR8[mdl]
            wrmYear = int(round(wrmYear / 5.) * 5.)
            relChngR8, vlsMask = getRelChngs(ncFlPth,
                                             None,
                                             wrmYear,
                                             ncvar='year_mean',
                                             vlsMask=vlsMask)

            ncFlNm = '_'.join(
                ['projection_dis', rcp4, mdl, wuStr, 'statistics.nc'])
            ncFlPth = os.path.join(rootDir, ncFlNm)
            wrmYear = wlYearR4[mdl]
            wrmYear = int(round(wrmYear / 5.) * 5.)
            relChngR4, vlsMask = getRelChngs(ncFlPth,
                                             None,
                                             wrmYear,
                                             ncvar='year_mean',
                                             vlsMask=vlsMask)

            relChng8lst.append(relChngR8)
            relChng4lst.append(relChngR4)

        ax = plt.subplot(gs[iwl])
        #relChng8 = np.nanmean(np.array(relChng8lst), 0)
        #relChng4 = np.nanmean(np.array(relChng4lst), 0)
        relChng8 = np.nanmedian(np.array(relChng8lst), 0)
        relChng4 = np.nanmedian(np.array(relChng4lst), 0)
        showLegend = not lgndShown
        dp = plotSingleModel(ax,
                             relChng8,
                             relChng4,
                             'mean discharge\nwrm. lev. ' + str(warmingLev) +
                             '$^\circ$C',
                             bold=True,
                             plotRegression=False,
                             tickFontSize=13,
                             labelFontSize=15,
                             titleFontSize=14,
                             xLabelPad=-30,
                             yLabelPad=-15,
                             showLegend=showLegend,
                             lim=lims[warmingLev])
        plt.xlim(lims[warmingLev])
        plt.ylim(lims[warmingLev])
        lgndShown = True
        axs.append(ax)

    cax = plt.subplot(gs[2])
    cb = plt.colorbar(dp[-1], ax=ax, cax=cax)
    cax.tick_params(labelsize=13, rotation=90)
    cb.set_label('density (pixel count)', fontsize=13)
    [ax.set_aspect('auto') for ax in axs]
    cax.set_aspect('auto')

    plt.tight_layout()

    if ownsFig:
        fig.savefig(outputPngFile, dpi=300)
    return axs
コード例 #25
0
def plotTimeSerieVariabilityAtWl_hiExt(ax=None, axmap=None, plotXLabel=True, plotYLabel=True, warmingLev=2.0, bsmp=None):
  yrs, rpR8Srs, rpR4Srs, models = loadWlVsScenChange.loadRetPerAllYears(nmodels=nmodels, excludedModels=excludedModels)
  rpR8 = getRelChng(yrs, rpR8Srs)
  rpR4 = getRelChng(yrs, rpR4Srs)

  wlyR8 = getWarmingLevels('rcp85', warmingLev)
  r8srs = getNYrsTimeSeriesAroundWl(yrs, rpR8, wlyR8, models)
  wlyR4 = getWarmingLevels('rcp45', warmingLev)
  r4srs = getNYrsTimeSeriesAroundWl(yrs, rpR4, wlyR4, models)

  srstot = np.concatenate([r8srs, r4srs], 0)
  rallmeanWl, rallsigmaWl = getMeanAndSigma(r8srs)
  cndPosi = rallmeanWl > 0
  cndNega = rallmeanWl < 0
  cnd = cndPosi if positiveChanges else cndNega

  r8meanWl, r8sigmaWl = getMeanAndSigma(r8srs)
  cndMtx = np.tile(cnd, [r8srs.shape[0], r8srs.shape[1], 1, 1])
  r8srs[~cndMtx] = np.nan
  r8mean_ = np.nanmean(r8srs, 0)
  r8mean = r8mean_.reshape(r8mean_.shape[0], r8mean_.shape[1]*r8mean_.shape[2])
 #r8mean = np.nanmedian(r8mean, 1)
  r8mean = np.nanmean(r8mean, 1)

  r8spmean = r8srs.reshape(r8srs.shape[0], r8srs.shape[1], r8srs.shape[2]*r8srs.shape[3])
  r8spmean = np.nanmean(r8spmean, 2)
 #r8spmean = np.nanmean(r8spmean, 2)
  r8skew = stats.skew(r8spmean[:, 0])
  r8sigma = np.std(r8spmean[:, 0])
  r8mean = np.nanmean(r8spmean, 0)

  r4meanWl, r4sigmaWl = getMeanAndSigma(r4srs)
  cndMtx = np.tile(cnd, [r4srs.shape[0], r4srs.shape[1], 1, 1])
  r4srs[~cndMtx] = np.nan
  r4mean_ = np.nanmean(r4srs, 0)
  r4mean = r4mean_.reshape(r4mean_.shape[0], r4mean_.shape[1]*r4mean_.shape[2])
 #r4mean = np.nanmedian(r4mean, 1)
  r4mean = np.nanmean(r4mean, 1)

  r4spmean = r4srs.reshape(r4srs.shape[0], r4srs.shape[1], r4srs.shape[2]*r4srs.shape[3])
  r4spmean = np.nanmean(r4spmean, 2)
 #r4spmean = np.nanmean(r4spmean, 2)
  r4skew = stats.skew(r4spmean[:, 0])
  r4sigma = np.std(r4spmean[:, 0])
  r4mean = np.nanmean(r4spmean, 0)

  ensTot = np.concatenate([r8spmean, r4spmean], 0)
  stdDevTot = np.std(ensTot, 0)*100.
  meanTot = (r4mean + r8mean)/2.*100.

  if ax is None:
    fg = plt.figure(figsize=[5, 5])
    ax = fg.gca()
  doPlotChanges(ax, warmingLev, meanTot, stdDevTot, r8spmean, r4spmean, r8mean, r4mean, plotXLabel, plotYLabel)

  if not axmap is None:
    rallmeanWlCnd = rallmeanWl.copy()
    rallmeanWlCnd[~cnd] = np.nan
    bsmp = doPlotAreaMap(axmap, rallmeanWlCnd, bsmp)
  plt.tight_layout()

  if ax is None:
    fg.savefig('./ensemblesVariabilityAround2deg.png', dpi=400)
  return bsmp
コード例 #26
0
def loadMdlsAtWl(ncDir='/ClimateRun4/multi-hazard/eva',
                 bslnYear=1995,
                 warmingLev=2,
                 rlVarName='rl',
                 numberOfModels=-1):
    flpattern = 'projection_dis_{scen}_{mdl}_wuConst_statistics.nc'

    wlyR8 = getWarmingLevels('rcp85', warmingLev)
    wlyR4 = getWarmingLevels('rcp45', warmingLev)

    models = wlyR8.keys()
    models.sort()
    numberOfModels = len(models) if numberOfModels == -1 else numberOfModels
    #tamask, _, _ = getAfricaAndTurkeyMask()
    #tamask = tamask.transpose()

    bsln = []
    rl_r4 = []
    rl_r8 = []
    for mdl, imdl in zip(models[:numberOfModels], range(numberOfModels)):
        print('model ' + mdl)
        flr8 = flpattern.format(scen='rcp85', mdl=mdl)
        flr8pth = os.path.join(ncDir, flr8)
        flr4 = flpattern.format(scen='rcp45', mdl=mdl)
        flr4pth = os.path.join(ncDir, flr4)

        r8year = wlyR8[mdl]
        r8yearInf = int(np.floor(r8year / float(5)) * 5)
        print('  rcp85 w.l. year: ' + str(r8year))
        r4year = wlyR4[mdl]
        r4yearInf = int(np.floor(r4year / float(5)) * 5)
        print('  rcp45 w.l. year: ' + str(r4year))

        print('  loading file ' + flr8pth)
        ds = netCDF4.Dataset(flr8pth)
        retper_ = ds.variables['return_period'][:]
        year_ = ds.variables['year'][:]
        yIndxBsln = np.where(year_ == bslnYear)[0][0]
        rlBslnR8 = ds.variables[rlVarName][:, yIndxBsln, :, :]
        yIndx = np.where(year_ == r8yearInf)[0][0]
        rlR8_ = ds.variables[rlVarName][:, yIndx:yIndx + 2, :, :]
        ds.close()
        rlR8 = interp1d(year_[yIndx:yIndx + 2], rlR8_, axis=1)(r8year)

        print('  loading file ' + flr4pth)
        ds = netCDF4.Dataset(flr4pth)
        rlBslnR4 = ds.variables[rlVarName][:, yIndxBsln, :, :]
        yIndx = np.where(year_ == r4yearInf)[0][0]
        rlR4_ = ds.variables[rlVarName][:, yIndx:yIndx + 2, :, :]
        ds.close()
        rlR4 = interp1d(year_[yIndx:yIndx + 2], rlR4_, axis=1)(r4year)

        rlBsln = rlBslnR8
        #rlBsln[~tamask] = np.nan
        #rlR4[~tamask] = np.nan
        #rlR8[~tamask] = np.nan

        bsln.append(rlBsln)
        rl_r8.append(rlR8)
        rl_r4.append(rlR4)

    bsln = np.array(bsln)
    rl_r8 = np.array(rl_r8)
    rl_r4 = np.array(rl_r4)

    return bsln, rl_r8, rl_r4, retper_
コード例 #27
0
def plotEnsembles(rootDir='/ClimateRun4/multi-hazard/eva/', retPer=100):
    warmingLevs = [1.5, 2.0]
    rcp8, rcp4 = 'rcp85', 'rcp45'

    models = """
IPSL-INERIS-WRF331F_BC
SMHI-RCA4_BC_CNRM-CERFACS-CNRM-CM5
SMHI-RCA4_BC_ICHEC-EC-EARTH
SMHI-RCA4_BC_IPSL-IPSL-CM5A-MR
SMHI-RCA4_BC_MOHC-HadGEM2-ES
SMHI-RCA4_BC_MPI-M-MPI-ESM-LR
CLMcom-CCLM4-8-17_BC_CNRM-CERFACS-CNRM-CM5
CLMcom-CCLM4-8-17_BC_ICHEC-EC-EARTH
CLMcom-CCLM4-8-17_BC_MPI-M-MPI-ESM-LR
DMI-HIRHAM5-ICHEC-EC-EARTH_BC
KNMI-RACMO22E-ICHEC-EC-EARTH_BC
"""
    models = models.split()

    outputPngFile = 'scatter_100yrlChange_wls_ensemble_retPer' + str(
        retPer) + '.png'

    fig = plt.figure(figsize=(11, 5))
    gs = gridspec.GridSpec(1, 3, width_ratios=[1, 1, .04])
    lon, lat = [], []
    axs = []
    lgndShown = False
    for warmingLev, iwl in zip(warmingLevs, range(len(warmingLevs))):

        wlYearR8 = getWarmingLevels(rcp8, warmingLev)
        wlYearR4 = getWarmingLevels(rcp4, warmingLev)
        relChng8lst, relChng4lst = [], []

        for mdl, imdl in zip(models, range(1, len(models) + 1)):
            print('getting model ' + mdl)

            ncFlNm = '_'.join(
                ['projection_dis', rcp8, mdl, 'wuChang', 'statistics.nc'])
            ncFlPth = os.path.join(rootDir, ncFlNm)
            wrmYear = wlYearR8[mdl]
            wrmYear = int(round(wrmYear / 5.) * 5.)
            relChngR8 = getRelChngs(ncFlPth, retPer, wrmYear)

            ncFlNm = '_'.join(
                ['projection_dis', rcp4, mdl, 'wuChang', 'statistics.nc'])
            ncFlPth = os.path.join(rootDir, ncFlNm)
            wrmYear = wlYearR4[mdl]
            wrmYear = int(round(wrmYear / 5.) * 5.)
            relChngR4 = getRelChngs(ncFlPth, retPer, wrmYear)

            relChng8lst.append(relChngR8)
            relChng4lst.append(relChngR4)

        ax = plt.subplot(gs[iwl])
        relChng8 = np.nanmean(np.array(relChng8lst), 0)
        relChng4 = np.nanmean(np.array(relChng4lst), 0)
        showLegend = not lgndShown
        dp = plotSingleModel(
            ax,
            relChng8,
            relChng4,
            str(retPer) +
            '-year ret. lev.\nensemble rcp85 vs rcp45,\nwrm. lev. ' +
            str(warmingLev) + '$^\circ$',
            bold=True,
            plotRegression=False,
            tickFontSize=13,
            labelFontSize=15,
            titleFontSize=14,
            xLabelPad=-30,
            yLabelPad=-15,
            showLegend=showLegend)
        lgndShown = True
        axs.append(ax)

    cax = plt.subplot(gs[2])
    cb = plt.colorbar(dp[-1], ax=ax, cax=cax)
    cax.tick_params(labelsize=13, rotation=90)
    cb.set_label('density (pixel count)', fontsize=13)
    [ax.set_aspect('auto') for ax in axs]
    cax.set_aspect('auto')

    plt.tight_layout()

    fig.savefig(outputPngFile, dpi=300)
コード例 #28
0
def computeRlChngPValueAtWarmingLevBtwScen(ncDir='/ClimateRun4/multi-hazard/eva', warmingLev=2, retPer=100, minThreshold=0):
  flpattern = 'projection_dis_{scen}_{mdl}_wuChang_statistics.nc'

  wlyR8 = getWarmingLevels('rcp85', warmingLev)
  wlyR4 = getWarmingLevels('rcp45', warmingLev)

  models = wlyR8.keys()

  rl_r4 = []
  serl_r4 = []
  rl_r8 = [] 
  serl_r8 = []
  for mdl, imdl in zip(models, range(len(models))):
    print('model ' + mdl)
    flr8 = flpattern.format(scen='rcp85', mdl=mdl)
    flr8pth = os.path.join(ncDir, flr8)
    flr4 = flpattern.format(scen='rcp45', mdl=mdl)
    flr4pth = os.path.join(ncDir, flr4)

    r8year = wlyR8[mdl]
    r8yearInf = int(np.floor(r8year/float(5))*5)
    print('  rcp85 w.l. year: ' + str(r8year))
    r4year = wlyR4[mdl]
    r4yearInf = int(np.floor(r4year/float(5))*5)
    print('  rcp45 w.l. year: ' + str(r4year))

    print('  loading file ' + flr8pth)
    ds = netCDF4.Dataset(flr8pth)
    retper_ = ds.variables['return_period'][:]
    rpIndx = np.where(retper_==retPer)[0][0]
    year_ = ds.variables['year'][:]
    yIndx = np.where(year_==r8yearInf)[0][0]
    rlR8_ = ds.variables['rl'][rpIndx, yIndx:yIndx+2, :, :]
    serlR8_ = ds.variables['se_rl'][rpIndx, yIndx:yIndx+2, :, :]
    ds.close()
    rlR8 = interp1d(year_[yIndx:yIndx+2], rlR8_, axis=0)(r8year)
    serlR8 = interp1d(year_[yIndx:yIndx+2], serlR8_, axis=0)(r8year)

    print('  loading file ' + flr4pth)
    ds = netCDF4.Dataset(flr4pth)
    yIndx = np.where(year_==r4yearInf)[0][0]
    rlR4_ = ds.variables['rl'][rpIndx, yIndx:yIndx+2, :, :]
    serlR4_ = ds.variables['se_rl'][rpIndx, yIndx:yIndx+2, :, :]
    ds.close()
    rlR4 = interp1d(year_[yIndx:yIndx+2], rlR4_, axis=0)(r4year)
    serlR4 = interp1d(year_[yIndx:yIndx+2], serlR4_, axis=0)(r4year)

    rl_r8.append(rlR8)
    serl_r8.append(serlR8)
    rl_r4.append(rlR4)
    serl_r4.append(serlR4)

  rl_r8 = np.array(rl_r8)
  rl_r8[rl_r8 <= .1] = .1
  serl_r8 = np.array(serl_r8)
  serl_r8[serl_r8 <= .1] = .1

  rl_r4 = np.array(rl_r4)
  rl_r4[rl_r4 <= .1] = .1
  serl_r4 = np.array(rl_r4)
  serl_r4[serl_r4 <= .1] = .1

  cnd = np.nanmean(rl_r8, 0) < minThreshold
  cnd = np.tile(cnd, [len(models), 1, 1])
  rl_r8[cnd] = np.nan
  serl_r8[cnd] = np.nan
  rl_r4[cnd] = np.nan
  serl_r4[cnd] = np.nan

  pvalue = computeRlChngPValueMontecarlo(rl_r4, serl_r4, rl_r8, serl_r8)
 #pvalue = computeRlChngPValueMeansOnly(rl_r4, rl_r8)

  agrMdlCnt, _ = countAgreeingModelsAndGetStdDev(rl_r4, rl_r8)

  return pvalue, agrMdlCnt
コード例 #29
0
def plotEnsembleChangVsConstWaterUse(rootDir='/DATA/JEODPP/eos/projects/CRITECH/ADAPTATION/ClimateRuns/LisfloodEuroCordex/'):
  warmingLevs = [1.5, 2.0]
  rcp8, rcp4 = 'rcp85', 'rcp45'
  scenarios = [rcp8, rcp4]
  wuConst, wuChang = 'wuConst', 'wuChang'

  models = """
IPSL-INERIS-WRF331F_BC
SMHI-RCA4_BC_CNRM-CERFACS-CNRM-CM5
SMHI-RCA4_BC_ICHEC-EC-EARTH
SMHI-RCA4_BC_IPSL-IPSL-CM5A-MR
SMHI-RCA4_BC_MOHC-HadGEM2-ES
SMHI-RCA4_BC_MPI-M-MPI-ESM-LR
CLMcom-CCLM4-8-17_BC_CNRM-CERFACS-CNRM-CM5
CLMcom-CCLM4-8-17_BC_ICHEC-EC-EARTH
CLMcom-CCLM4-8-17_BC_MPI-M-MPI-ESM-LR
DMI-HIRHAM5-ICHEC-EC-EARTH_BC
KNMI-RACMO22E-ICHEC-EC-EARTH_BC
"""
  models = models.split()

  outputPngFile = 'scatter_minDis_wuChngVSwuConst_wls_ensemble.png'

  fig = plt.figure(figsize=(4, 4))
  gs = gridspec.GridSpec(2, 2)

  
  for warmingLev, iwl in zip(warmingLevs, range(len(warmingLevs))):
    print('elaborating wl ' + str(warmingLev))
    for scen, iscen in zip(scenarios, range(len(scenarios))):
      print('  scenario ' + scen)
      wlYear = getWarmingLevels(scen, warmingLev)

      relChngWuChangLst, relChngWuConstLst = [], []

      for mdl, imdl in zip(models, range(1, len(models) + 1)):
        print('    getting model ' + mdl)
      
        fldirHist = os.path.join(rootDir, 'historical', mdl, wuConst)

        fldirScenWuChang = os.path.join(rootDir, scen, mdl, wuChang)
        wrmYear = wlYear[mdl]
        cacheIdData = '_'.join([mdl, scen, wuChang])
        cacheIdResult = cacheIdData + '_' + str(warmingLev)
        relChngWuChang = getRelChngs(cacheIdData, cacheIdResult, fldirHist, fldirScenWuChang, wrmYear)

        fldirScenWuConst = os.path.join(rootDir, scen, mdl, wuConst)
        cacheIdData = '_'.join([mdl, scen, wuConst])
        cacheIdResult = cacheIdData + '_' + str(warmingLev)
        relChngWuConst = getRelChngs(cacheIdData, cacheIdResult, fldirHist, fldirScenWuConst, wrmYear)
      
        relChngWuChangLst.append(relChngWuChang)
        relChngWuConstLst.append(relChngWuConst)
        
      ax = plt.subplot(gs[iwl, iscen])
      relChngWuChang = np.nanmean(np.array(relChngWuChangLst), 0)
      relChngWuConst = np.nanmean(np.array(relChngWuConstLst), 0)
      plotSingleModel(ax, relChngWuChang, relChngWuConst, 
               'Ensemble, constant vs changing w.u.,\n minimum discharge\n' + scen + ', wrm. lev. ' + str(warmingLev) + '$^\circ$', 
               bold=True, plotRegression=False, plotIdealFit=False, tickFontSize=5, labelFontSize=7, titleFontSize=5,
               xLabelPad=-25, yLabelPad=-10, xlabel='const. w.u. ($\Delta$ %)', ylabel='chng. w.u. ($\Delta$ %)', xlim=[-80, 170], ylim=[-80, 170])

  plt.tight_layout()

  fig.savefig(outputPngFile, dpi=300)
コード例 #30
0
def loadMeanPrecipitationChangesAtWl(
        ncRootDir='/DATA/ClimateData/cordexEurope/yearlymeans/',
        bslnYear=1995,
        warmingLev=2,
        threshold=0,
        flname='pr.nc',
        nmodels=-1,
        timeWindowHalfSize=15):
    # computes the mean relative change

    wlyR8 = getWarmingLevels('rcp85', warmingLev)
    wlyR4 = getWarmingLevels('rcp45', warmingLev)

    models = wlyR8.keys()
    if nmodels > -1:
        models = models[:nmodels]

    tamask, _, _ = getAfricaAndTurkeyMask()
    tamask = tamask

    rlVarName1 = 'pr'
    rlVarname2 = 'prAdjust'

    rl_r4 = []
    rl_r8 = []
    for mdl, imdl in zip(models, range(len(models))):
        print('model ' + mdl)
        flhistpth = os.path.join(ncRootDir, mdl, 'historical', flname)
        flr8pth = os.path.join(ncRootDir, mdl, 'rcp85', flname)
        flr4pth = os.path.join(ncRootDir, mdl, 'rcp45', flname)

        r8year = wlyR8[mdl]
        r4year = wlyR4[mdl]

        print('  loading file ' + flhistpth)
        ds = netCDF4.Dataset(flhistpth)
        rlVarName = 'pr' if 'pr' in ds.variables else 'prAdjust'
        tmnc = ds.variables['time']
        tm = netCDF4.num2date(tmnc[:], tmnc.units, tmnc.calendar)
        yearHist = np.array([t.year for t in tm])
        yIndxBsln = np.where(yearHist == bslnYear)[0][0]
        minIndx = np.max([yIndxBsln - timeWindowHalfSize, 0])
        maxIndx = np.min([yIndxBsln + timeWindowHalfSize, len(yearHist)])
        rlBsln = np.nanmean(ds.variables[rlVarName][minIndx:maxIndx, :, :], 0)
        ds.close()

        print('  loading file ' + flr8pth)
        ds = netCDF4.Dataset(flr8pth)
        rlVarName = 'pr' if 'pr' in ds.variables else 'prAdjust'
        tmnc = ds.variables['time']
        tm = netCDF4.num2date(tmnc[:], tmnc.units, tmnc.calendar)
        yearR = np.array([t.year for t in tm])
        yIndx = np.where(yearR == r8year)[0][0]
        minIndx = np.max([yIndx - timeWindowHalfSize, 0])
        maxIndx = np.min([yIndx + timeWindowHalfSize, len(yearR)])
        rlR8 = np.nanmean(ds.variables[rlVarName][minIndx:maxIndx, :, :], 0)
        r8RelChng = (rlR8 - rlBsln) / rlBsln
        if threshold > 0:
            cnd = rlBslnR8 < threshold
            r8RelChng[cnd] = np.nan
        #r8RelChng[r8RelChng < -.15] = np.nan
        ds.close()

        print('  loading file ' + flr4pth)
        ds = netCDF4.Dataset(flr4pth)
        rlVarName = 'pr' if 'pr' in ds.variables else 'prAdjust'
        tmnc = ds.variables['time']
        tm = netCDF4.num2date(tmnc[:], tmnc.units, tmnc.calendar)
        yearR = np.array([t.year for t in tm])
        yIndx = np.where(yearR == r4year)[0][0]
        minIndx = np.max([yIndx - timeWindowHalfSize, 0])
        maxIndx = np.min([yIndx + timeWindowHalfSize, len(yearR)])
        rlR4 = np.nanmean(ds.variables[rlVarName][minIndx:maxIndx, :, :], 0)
        r4RelChng = (rlR4 - rlBsln) / rlBsln
        if threshold > 0:
            cnd = rlBslnR4 < threshold
            r4RelChng[cnd] = np.nan
        #r4RelChng[r4RelChng < -.15] = np.nan
        ds.close()

        #r8RelChng[upArea < 1e9] = np.nan
        #r4RelChng[upArea < 1e9] = np.nan

        r8RelChng[~tamask] = np.nan
        r4RelChng[~tamask] = np.nan

        rl_r8.append(r8RelChng)
        rl_r4.append(r4RelChng)

    rl_r8 = np.array(rl_r8)
    rl_r4 = np.array(rl_r4)

    relChngDiff = np.nanmean(rl_r8 - rl_r4, 0)
    relChngDiff[~tamask] = np.nan

    return relChngDiff, np.nanmean(rl_r8, 0), np.nanmean(rl_r4,
                                                         0), rl_r8, rl_r4