Exemple #1
0
def _cmorize_dataset(in_file, var, cfg, out_dir):
    logger.info("CMORizing variable '%s' from input file '%s'",
                var['short_name'], in_file)
    attributes = deepcopy(cfg['attributes'])
    attributes['mip'] = var['mip']

    cmor_table = cfg['cmor_table']
    definition = cmor_table.get_variable(var['mip'], var['short_name'])

    cube = iris.load_cube(str(in_file),
                          constraint=utils.var_name_constraint(var['raw']))

    # Set correct names
    cube.var_name = definition.short_name
    if definition.standard_name:
        cube.standard_name = definition.standard_name

    cube.long_name = definition.long_name

    # Convert units if required
    cube.convert_units(definition.units)

    # Set global attributes
    utils.set_global_atts(cube, attributes)

    logger.info("Saving CMORized cube for variable %s", cube.var_name)
    utils.save_variable(cube, cube.var_name, out_dir, attributes)

    return in_file
Exemple #2
0
def _cmorize_dataset(in_file, var, cfg, out_dir):
    logger.info("CMORizing variable '%s' from input file '%s'",
                var['short_name'], in_file)
    attributes = deepcopy(cfg['attributes'])
    attributes['mip'] = var['mip']

    cmor_table = cfg['cmor_table']
    definition = cmor_table.get_variable(var['mip'], var['short_name'])

    cube = iris.load_cube(str(in_file),
                          constraint=utils.var_name_constraint(var['raw']))

    # Time has strange values, so use forecast_reference_time instead
    cube.remove_coord('time')
    cube.coord('forecast_reference_time').rename('time')

    # The following lines are essential before applying
    # the common function fix_coords
    # Convert time calendar from proleptic_gregorian to gregorian
    cube.coord('time').units = cf_units.Unit(
        cube.coord('time').units.origin, 'gregorian')

    # Set standard_names for lat and lon
    cube.coord('lat').standard_name = 'latitude'
    cube.coord('lon').standard_name = 'longitude'

    cube = utils.fix_coords(cube)

    # The above command does not return bounds for longitude
    # so explicitly get them here.
    cube.coord('longitude').guess_bounds()

    # Set correct names
    cube.var_name = definition.short_name

    cube.long_name = definition.long_name

    # Convert units if required
    cube.units = '1'
    cube.convert_units(definition.units)

    # Set global attributes
    utils.set_global_atts(cube, attributes)

    logger.info("Saving CMORized cube for variable %s", cube.var_name)
    utils.save_variable(cube, cube.var_name, out_dir, attributes)

    return in_file
def test_set_global_atts_incorrect():
    """Test set global attributes."""
    cube = _create_sample_cube()
    global_attrs = {
        'version': '2',
        'tier': '3',
        'source': '4',
        'reference': 'acknow_author',
        'comment': '6',
        'project_id': '7',
    }
    msg = \
        "".join(["All CMORized datasets need the ",
                 "global attributes 'dataset_id', ",
                 "'version', 'tier', 'source', 'reference', 'comment' and ",
                 "'project_id' specified in the configuration file"])
    with pytest.raises(KeyError) as key_err:
        utils.set_global_atts(cube, global_attrs)
        assert msg in key_err
def test_set_global_atts_correct():
    """Test set global attributes."""
    cube = _create_sample_cube()
    global_attrs = {
        'dataset_id': '1',
        'version': '2',
        'tier': '3',
        'source': '4',
        'reference': 'acknow_author',
        'comment': '6',
        'project_id': '7',
    }
    utils.set_global_atts(cube, global_attrs)
    attrs = cube.attributes
    assert '1 ' in attrs['title']
    assert attrs['version'] == '2'
    assert attrs['tier'] == '3'
    assert attrs['source'] == '4'
    assert attrs['reference'] == 'doi not found'
    assert attrs['comment'] == '6'
    assert attrs['project_id'] == '7'
Exemple #5
0
def _cmorize_dataset(in_file, var, cfg, out_dir):
    logger.info("CMORizing variable '%s' from input file '%s'",
                var['short_name'], in_file)
    attributes = deepcopy(cfg['attributes'])
    attributes['mip'] = var['mip']

    cmor_table = cfg['cmor_table']
    definition = cmor_table.get_variable(var['mip'], var['short_name'])

    cube = iris.load_cube(str(in_file),
                          constraint=utils.var_name_constraint(var['raw']))

    # Set correct names
    cube.var_name = definition.short_name
    if definition.standard_name:
        cube.standard_name = definition.standard_name

    cube.long_name = definition.long_name

    # Convert units if required
    cube.convert_units(definition.units)

    # Set global attributes
    utils.set_global_atts(cube, attributes)

    # Setting time right
    cube = regrid_time(cube, 'mon')

    # Set calendar to gregorian instead of proleptic gregorian
    # matplotlib does not correctly format years in proleptic gregorian
    old_unit = cube.coord('time').units
    new_unit = Unit(old_unit.origin, calendar='gregorian')
    cube.coord('time').units = new_unit

    logger.info("Saving CMORized cube for variable %s", cube.var_name)
    utils.save_variable(cube, cube.var_name, out_dir, attributes)

    return in_file