Ejemplo n.º 1
0
def initialize(path, expname, tabledir, prefix, refdate):
    """initialize the cmorization for TM5
    Description:
        
    Input variables:
        path, String: path to TM5 files
        expname, string: name of the experiment
        tabledir, string: path to tables
        prefix, string: table prefix
    Returns:
        boolean: success
    """
    global log, tm5_files_, exp_name_, table_root_, ref_date_, plev39_, plev19_, areacella_, path_
    exp_name_ = expname
    path_ = path
    table_root_ = os.path.join(tabledir, prefix)
    # select all TM5 files with expname from path
    tm5_files_ = cmor_utils.find_tm5_output(path, expname)
    if len(tm5_files_) == 0:
        log.error('no TM5 varibles found, exiting!')
        exit()
    areacella_file = cmor_utils.find_tm5_output(path, expname, 'areacella',
                                                'fx')
    if len(areacella_file) == 0:
        log.error('Areacella not found!')
        exit()
    else:
        areacella_ = netCDF4.Dataset(areacella_file[0],
                                     'r').variables['areacella'][:]
    cal = None
    ref_date_ = refdate

    # read pressure level definitions from CMIP6_coordante file
    # and save globally
    coordfile = os.path.join(tabledir, prefix + "_coordinate.json")
    if os.path.exists(coordfile):
        with open(coordfile) as f:
            data = json.loads(f.read())
        axis_entries = data.get("axis_entry", {})
        axis_entries = {k.lower(): v for k, v in axis_entries.iteritems()}
        plev19 = numpy.array([
            numpy.float(value) for value in axis_entries['plev19']['requested']
        ])
        plev19_ = plev19
        plev39 = numpy.array([
            numpy.float(value) for value in axis_entries['plev39']['requested']
        ])
        plev39_ = plev39
    else:
        log.warning('Using default pressure level definitions')

    cmor.load_table(table_root_ + "_grids.json")
    return True
Ejemplo n.º 2
0
def get_ps_tasks(tasks):
    """ find ps (surface preseure) tasks for different tables
    Args:
        tasks (list): list of tasks
    Returns:
        result (dictionary): dictionary based on the frequencies of different tasks with corresponding ps-tasks as values.

    """
    global exp_name_,path_
    tasks_by_freq = cmor_utils.group(tasks, lambda task: task.target.frequency)
    result = {}
    for freq, task_group in tasks_by_freq.iteritems():
        tasks3d = [t for t in task_group if ("alevel" in getattr(t.target, cmor_target.dims_key).split()  or "plev19" in getattr(t.target, cmor_target.dims_key).split() or
            "alevhalf" in getattr(t.target, cmor_target.dims_key).split()  or "plev39"  in getattr(t.target, cmor_target.dims_key).split() )]
        if not any(tasks3d):
            continue
        ps_tasks = [t for t in task_group if t.source.variable() == "ps" and
                               getattr(t, "time_operator", "point") in ["mean", "point"]]
        ps_task = ps_tasks[0] if any(ps_tasks) else None
        if ps_task:
            result[freq]=ps_task
        else:
            source = cmor_source.tm5_source("ps")
            ps_task = cmor_task.cmor_task(source, cmor_target.cmor_target("ps", freq))
            setattr(ps_task.target, cmor_target.freq_key, freq)
            setattr(ps_task.target, "time_operator", ["point"])
            freqid=set_freqid(freq)
            filepath=cmor_utils.find_tm5_output(path_,exp_name_,"ps",freqid)
            setattr(ps_task, cmor_task.output_path_key, filepath[0])
            result[freq]=ps_task
        for task3d in tasks3d:

            setattr(task3d, "ps_task", ps_task)
    return result
Ejemplo n.º 3
0
def select_files(path, expname, start, length):
    allfiles = cmor_utils.find_tm5_output(path, expname)
    starttime = cmor_utils.make_datetime(start)
    stoptime = cmor_utils.make_datetime(start + length)
    return [
        f for f in allfiles if cmor_utils.get_tm5_interval(f)[0] <= stoptime
        and cmor_utils.get_tm5_interval(f)[1] >= starttime
    ]