コード例 #1
0
def main():
    outdir = '/gws/nopw/j04/primavera3/cache/sberthou/'
    country = 'prudence'
    frequency = 'd'
    n512 = False
    other = 'PRIMAVERA'
    time_con = iris.Constraint(
        time=lambda cell: 1950 <= cell.point.year < 2006)
    # CORDEX grid, obtained by executing "cdo griddes CORDEX_file.nc > EUROCORDEX_grid.txt"
    region_name = 'EUROCORDEX'
    new_grid = '{}_grid.txt'.format(region_name)

    runlist, _ = ems.get_runlist_region(frequency, n512, country, other=other)
    for model in runlist:
        #### 1st step: load all the data into one single cube and save it, so that CDO regrid is fast (calculates the weights only once, then applies it to the whole time series. ####
        #### at the very end, once you're happy that everything has been regridded, don't forget to delete the large files, as they are enormous! ####
        large_cube = '{}/pr_{}'.format(outdir, model)

        if not os.path.exists('{}_with_grid.nc'.format(large_cube)):
            if not os.path.exists('{}.nc'.format(large_cube)):
                cubelist = iris.load(runlist[model],
                                     time_con,
                                     callback=callback_overwrite)
                iris.util.unify_time_units(cubelist)
                cube = cubelist.concatenate_cube()
                print('cubes loaded')
                iris.save(cube, large_cube + '.nc')
                print('large cube saved')
            elif 'EC-Earth' in model:
                cube = iris.load_cube('{}.nc'.format(large_cube))
                redefine_spatial_coords(cube)
                iris.save(cube, '{}_tmp.nc'.format(large_cube))
                cmd = 'mv {}_tmp.nc {}.nc'.format(large_cube, large_cube)
                shellcmd(cmd, 'mv ECarth failed')
        #### get the grid from the large netCDF file ####
            cmd = 'cdo griddes {}.nc > init_grid.txt'.format(large_cube)
            shellcmd(cmd, 'cdo griddes didn' 't complete')
            #### set the grid in the file (not sure why you have to do this, I think you need it mostly for the grids which have 1D lat/lon, because cdo requires 2D lat,lons, calculated with cdo griddes ####
            cmd = 'cdo -setgrid,init_grid.txt {}.nc {}_with_grid.nc'.format(
                large_cube, large_cube)
            shellcmd(cmd, 'cdo setgrid didn' 't complete')
        #### remapping itself ####
        if not os.path.exists('{}_regridded_on_{}.nc'.format(
                large_cube, region_name)):
            if not model == 'EC-Earth3P-HR':
                cmd = 'cdo remapcon,{} {}_with_grid.nc {}_regridded_on_{}.nc'.format(
                    new_grid, large_cube, large_cube, region_name)
                shellcmd(cmd, 'cdo remapcon didn' 't complete')
            else:
                cmd = 'cdo remapcon,{} {}.nc {}_regridded_on_{}.nc'.format(
                    new_grid, large_cube, large_cube, region_name)
                shellcmd(cmd, 'cdo remapcon didn' 't complete')
distribution = args.b
hist_type = args.g
wet_or_dry_spells = args.w
thrs = args.t

#ensembles_to_compare = ['PRIMAVERA', 'CORDEX-44', 'obs_cordex50']
ensembles_to_compare = ['PRIMAVERA', 'CORDEX-11_regrid', 'obs_cordex50']
#ensembles_to_compare = [ 'PRIMAVERA']
bootstrap_or_centiles = 'centiles'

### generate ensemble_dict: the dictionary containing all models names in each ensemble.
### The observations are not in this as interannual variability rather than inter-member is calculated
ensemble_dict = {}
for ens in ensembles_to_compare:
    if not 'obs' in ens.lower():
        runlist, _ = ems.get_runlist_region('d', False, 'prudence', other=ens)
        ensemble_dict[ens] = runlist.keys()

## this provides the bootstrap configuration, if bootstrapping is chosen to compare the ensembles
nb_bootstrap_iter = 999999
bootstrap_config = {}
if bootstrap_or_centiles == 'bootstrap':
    bootstrap_config = {
        'PRIMAVERA': {
            'bootstrap_produce_file':
            False,  # True if want to generate bootstrapping random years rather than use exisiting file
            'bootstrap_no_years':
            len(ensemble_dict['PRIMAVERA']),  # number of PRIMAVERA models
            'bootstrap_sample_size':
            nb_bootstrap_iter,  # NB if this goes > nb_bootstrap_iter (3 digits) formatting fails
            'bootstrap_samples_split': [
コード例 #3
0
country = args.c
n512 = bool_from_string(args.r)
frequency = args.f
other = args.o
season_list = args.s
print type(season_list), season_list
bintypes = args.b
run_on_spice = bool_from_string(args.e)
wet_dry_2dhist = args.w
if wet_dry_2dhist == 'None':
    wet_dry_2dhist = None
thrs = args.t

# get the runlist and the region masks from european_masked_subregion.py
runlist, maskedreglist = ems.get_runlist_region(frequency,
                                                n512,
                                                country,
                                                other=other)

# batch job characteristics:
spice_freq = {
    'mem': {
        'h': '40',
        'd': '20'
    },  ### ME: this is where you can change how much memory you use, 20GB by default
    'queue': {
        'h': 'normal',
        'd': 'normal'
    },
    'time': {
        'h': '180',
        'd': '100'
コード例 #4
0
def main():
    datadir = '/home/users/sberthou/PrecipExtremes/'
    frequency = 'd'
    full_ensemble = False
    if full_ensemble:
        runlist, _ = ems.get_runlist_region(frequency, False, 'prudence',
                                            'PRIMAVERA')
        PRIMAVERA_ens = runlist.keys()
        runlist1, _ = ems.get_runlist_region(frequency, False, 'prudence',
                                             'CORDEX-44')
        CORDEX_ens = runlist1.keys()
        runlist2, _ = ems.get_runlist_region(frequency, False, 'prudence',
                                             'CORDEX11')
        CORDEX_ens_11 = runlist2.keys()
        runlist3, _ = ems.get_runlist_region(frequency, False, 'prudence',
                                             'CORDEX-11_regrid')
        CORDEX_ens11_regrid = runlist3.keys()
    else:
        runlist, _ = ems.get_runlist_region(frequency, False, 'prudence',
                                            'PRIMAVERA_reduced')
        PRIMAVERA_ens = runlist.keys()
        runlist1, _ = ems.get_runlist_region(frequency, False, 'prudence',
                                             'CORDEX-44_reduced')
        CORDEX_ens = runlist1.keys()
        runlist3, _ = ems.get_runlist_region(frequency, False, 'prudence',
                                             'CORDEX-11_regrid_reduced')
        CORDEX_ens11_regrid = runlist3.keys()

    obs_ens = [
        'eobs_cordex50',
        'ca_cordex50',
        'france_cordex50',
        'ibp_cordex50',
        'alps_cordex50',
        'uk_cordex50',
    ]
    seasons = ['jja', 'djf', 'mam', 'son']
    ref_grid = iris.load_cube(
        '{}/mean_nc/ibp_cordex50_jja_mean_2000.nc'.format(datadir))
    ensembles = {
        'OBS': obs_ens,
        'PRIMAVERA': PRIMAVERA_ens,
        'EUR-44': CORDEX_ens,  #'CORDEX-11': CORDEX_ens_11, 
        'EUR-11': CORDEX_ens11_regrid
    }
    for season in seasons:
        print season
        cubelist_dict = {}
        for ensemble in ensembles:
            print ensemble
            cubelist = iris.cube.CubeList([])
            for num, simu in enumerate(ensembles[ensemble]):
                print simu
                cubelist0 = iris.load('{}/mean_nc/{}_{}_mean*.nc'.format(
                    datadir, simu, season))
                if len(cubelist0) == 1:
                    cube = cubelist0[0]
                else:
                    cube = cubelist0.extract('precipitation_flux')[0]
                modify_grid(cube, simu)
                coord_names = [coord.name() for coord in cube.dim_coords]
                print cube
                try:
                    lonname, = filter(lambda x: re.search(r'[Ll]on|x', x),
                                      coord_names)
                except:
                    __init__.add_CORDEX_grid(cube, 'EUROCORDEX')

                newcube = cube.regrid(ref_grid, iana.Linear())
                if ensemble is not 'OBS':
                    member_coord = iris.coords.AuxCoord(
                        [num], long_name='member_number', units='1')
                    newcube.add_aux_coord(member_coord)
                    newcube.attributes = None
                    newcube.cell_methods = None
                    try:
                        newcube.remove_coord('time')
                    except:
                        pass
                newcube1 = precip_to_mm(newcube)
                cubelist.append(newcube1)
            cubelist_dict[ensemble] = cubelist.merge()
            print cubelist_dict[ensemble]
        obs = combine_obs(cubelist_dict['OBS'])
        add_reduced = ''
        if not full_ensemble:
            add_reduced = '_reduced'
        figfile = '{}/images/mean_bias{}_{}'.format(datadir, add_reduced,
                                                    season)
        plot_maps(obs,
                  cubelist_dict,
                  plot_order=['PRIMAVERA', 'EUR-11', 'EUR-44'],
                  figfile=figfile,
                  season=season)