def model_files(multi):
    """
    Computes respective metric and returns single model files and multi-model cube at different aggregations
    """
    #this is necessary to make multiprocessing an option
    variable = multi[0]
    scenario = multi[1]
    bc_and_resolution = multi[2]
    inpath = multi[3]
    outpath = multi[4]
    season = multi[5]
    metric = multi[6]
    region = multi[7]
    overwrite = multi[8]

    ###region box
    box = region[2]
    xmin = box[0]
    xmax = box[1]
    ymin = box[2]
    ymax = box[3]

    for sc, bc, seas, var in itertools.product(scenario, bc_and_resolution, season, variable):


        if metric == 'pet' and var == 'multivars':
            var = 'rsds'

        files_good, modelID = load_file_names(inpath, var, sc, bc)

        if files_good == []:
            print inpath, sc, bc, seas, var
            print 'No files found. Check your input directory!!!'
            continue
            #sys.exit('No files found. Check your input directory!!!')

        out = outpath + os.sep + bc

        calc_to_call = getattr(calc, metric) # calls the metric calc function from calc.py
        file_searcher = out + os.sep + str(metric) + '_' +str(var) + '_' + str(bc) + \
                        '_' + str(sc) + '_' + str(seas) +'_' + str(region[0])

        for file, nme in zip(files_good, modelID):

            cubeout=None
            print('Doing:', nme, metric, var)
            if metric == 'pet' and var == 'rsds':
                print(nme, 'in')
                # This is a different case because PET needs multiple variables, rather than one
                # NB: We use rsds, because generally, less models have this variable as opposed to tasmin or tasmax

                tasmin = atlas_utils.load_data(file.replace(var, 'tasmin'), xmin, xmax, ymin, ymax)
                tasmax = atlas_utils.load_data(file.replace(var, 'tasmax'), xmin, xmax, ymin, ymax)
                rsds = atlas_utils.load_data(file.replace(var, 'rsds'), xmin, xmax, ymin, ymax)
                cubeout = iris.cube.CubeList([tasmin, tasmax, rsds])
                file_searcher = file_searcher.replace('rsds', 'multivars')

                print cubeout

            nc_file = file_searcher + '_' + str(nme) + '_singleModel_tseries.nc'

            if not os.path.isfile(nc_file) or (overwrite == 'Yes'):
                print 'nc_file: ' + nc_file
                print 'Load file: '+ file

                if not cubeout:
                    print(nme, 'no cubeout yet, loading file')
                    print 'newcube'

                    cubeout = atlas_utils.load_data(file, xmin, xmax, ymin, ymax)
                    
                calc_to_call(cubeout, seas, nc_file)  # saves single model netcdf
                print '#######################################'
                print 'Saving data for: '
                print nme, metric, var, seas, region[0]
                print '#######################################'

        # runs big_cube for all available aggregations
        for agg in cnst.AGGREGATION:
            big_cube(file_searcher, agg)
def wfdei(multi):

    # this is necessary to make multiprocessing an option
    variable = multi[0]
    outpath =  multi[1]
    season =  multi[2]
    metric =  multi[3]
    region = multi[4]
    overwrite = multi[5]

    for seas, var in itertools.product(season, variable):

        bc = 'WFDEI'
        sc = 'historical'

        if metric == 'pet' and var == 'multivars':
            var = 'rsds'

        filepath = cnst.DATADIR + os.sep + bc + os.sep + str(var) + '_daily*.nc'
        file = glob.glob(filepath)

        if (len(file) != 1):
            print('No or too many WFDEI files found, path problem')
            return
        file = file[0]

        box = region[2]
        xmin = box[0]
        xmax = box[1]
        ymin = box[2]
        ymax = box[3]

        if metric == 'pet' and var == 'multivars':
            var = 'rsds'

        out = outpath + os.sep + bc

        calc_to_call = getattr(calc, metric)  # calls the metric calc function from calc.py
        file_searcher = out + os.sep + str(metric) + '_' + str(var) + '_' + str(bc) + \
                        '_' + str(sc) + '_' + str(seas) + '_' + str(region[0])

        cubeout = None

        # Check if we have any missing files for all aggregation types, if so, run the metric calculation again
        # Note: the calc functions run for 2 or 3 aggregation methods

        if metric == 'pet' and var == 'rsds':
            # This is a different case because PET needs multiple variables, rather than one
            # NB: We use rsds, because generally, less models have this variable as opposed to tasmin or tasmax
            tasmin = atlas_utils.load_data(file.replace(var, 'tasmin'), xmin, xmax, ymin, ymax)
            tasmax = atlas_utils.load_data(file.replace(var, 'tasmax'), xmin, xmax, ymin, ymax)
            rsds = atlas_utils.load_data(file.replace(var, 'rsds'), xmin, xmax, ymin, ymax)
            cubeout = iris.cube.CubeList([tasmin, tasmax, rsds])

            file_searcher = file_searcher.replace('rsds', 'multivars')

            print cubeout

        nc_file = file_searcher + '_WFDEI_tseries.nc'

        if not os.path.isfile(nc_file) or (overwrite == 'Yes'):
            print 'nc_file: ' + nc_file
            print 'Load file: ' + file

            if not cubeout:
                print 'newcube'
                cubeout = atlas_utils.load_data(file, xmin, xmax, ymin, ymax)

            calc_to_call(cubeout, seas, nc_file)  # saves single model netcdf
            print '#######################################'
            print 'Saving data for: '
            print metric, var, seas, region[0]
            print '#######################################'
Example #3
0
import atlas_utils, calc, constants as cnst

#### We only need these seven strings to create clean output files.
# Please give the correct variable and scenario information, the rest is less important
metric = 'metricname'  # identifies the metric you're calculating
var = 'tas'  # the CMIP5 variable name
bc = 'dummy'  # we use this, you need to provide a random string (it's a bias correction flag)
sc = 'rcp45'  #  scenario, could form a loop from cnst.SCENARIO
seas = 'jas'  # season
reg = 'WestAfrica'  # regional flag
aggregation = cnst.AGGREGATION[0]

lonlatbox = [-18, 25, 4, 25]
cmip5_file = '/users/global/cornkle/CMIP/CMIP5_Africa/BC_mdlgrid/GFDL-CM3/rcp45/' + var + '_WFDEI_1979-2013_mdlgrid_day_GFDL-CM3_west-africa_rcp45_r1i1p1_full.nc'

outfilename = str(metric) + '_' +str(var) + '_' + str(bc) + \
                        '_' + str(sc) + '_' + str(seas) +'_' + str(reg) +'_singleModel_' + aggregation +'.nc'

outpath = '/users/global/cornkle/test/' + outfilename

cube = atlas_utils.load_data(cmip5_file, lonlatbox[0], lonlatbox[1],
                             lonlatbox[2], lonlatbox[3])

calc.annualHotDays(cube, 'jas', outpath)