Esempio n. 1
0
def handle(infiles, tables, user_input_path, **kwargs):
    """
    Transform MPASO timeMonthly_avg_layerThickness into CMIP.masscello

    Parameters
    ----------
    infiles : dict
        a dictionary with namelist, mesh and time series file names

    tables : str
        path to CMOR tables

    user_input_path : str
        path to user input json file

    Returns
    -------
    varname : str
        the name of the processed variable after processing is complete
    """
    if kwargs.get('simple'):
        msg = f"{VAR_NAME} is not supported for simple conversion"
        print_message(msg)
        return

    msg = 'Starting {name}'.format(name=__name__)
    logging.info(msg)

    namelistFileName = infiles['MPASO_namelist']
    meshFileName = infiles['MPAS_mesh']
    mappingFileName = infiles['MPAS_map']
    timeSeriesFiles = infiles['MPASO']

    namelist = mpas.convert_namelist_to_dict(namelistFileName)
    config_density0 = float(namelist['config_density0'])

    dsMesh = xarray.open_dataset(meshFileName, mask_and_scale=False)
    _, cellMask3D = mpas.get_cell_masks(dsMesh)

    variableList = [
        'timeMonthly_avg_layerThickness', 'xtime_startMonthly',
        'xtime_endMonthly'
    ]

    ds = xarray.Dataset()
    with mpas.open_mfdataset(timeSeriesFiles, variableList) as dsIn:
        ds[VAR_NAME] = config_density0 * \
            dsIn.timeMonthly_avg_layerThickness.where(cellMask3D, 0.)
        ds = mpas.add_time(ds, dsIn)
        ds.compute()

    ds = mpas.add_depth(ds, dsMesh)
    ds.compute()

    ds = mpas.remap(ds, mappingFileName)

    # set masked values (where there are no MPAS grid cells) to zero
    ds[VAR_NAME] = ds[VAR_NAME].where(
        ds[VAR_NAME] != netCDF4.default_fillvals['f4'], 0.)

    mpas.setup_cmor(VAR_NAME, tables, user_input_path, component='ocean')

    # create axes
    axes = [{
        'table_entry': 'time',
        'units': ds.time.units
    }, {
        'table_entry': 'depth_coord',
        'units': 'm',
        'coord_vals': ds.depth.values,
        'cell_bounds': ds.depth_bnds.values
    }, {
        'table_entry': 'latitude',
        'units': 'degrees_north',
        'coord_vals': ds.lat.values,
        'cell_bounds': ds.lat_bnds.values
    }, {
        'table_entry': 'longitude',
        'units': 'degrees_east',
        'coord_vals': ds.lon.values,
        'cell_bounds': ds.lon_bnds.values
    }]
    try:
        mpas.write_cmor(axes, ds, VAR_NAME, VAR_UNITS)
    except Exception:
        return ""
    return VAR_NAME
Esempio n. 2
0
def handle(infiles, tables, user_input_path, **kwargs):
    """
    Transform MPASO timeMonthly_avg_vertVelocityTop into CMIP.wo

    Parameters
    ----------
    infiles : dict
        a dictionary with namelist, mesh and time series file names

    tables : str
        path to CMOR tables

    user_input_path : str
        path to user input json file

    Returns
    -------
    varname : str
        the name of the processed variable after processing is complete
    """
    msg = 'Starting {name}'.format(name=__name__)
    logging.info(msg)

    meshFileName = infiles['MPAS_mesh']
    mappingFileName = infiles['MPAS_map']
    timeSeriesFiles = infiles['MPASO']

    dsMesh = xarray.open_dataset(meshFileName, mask_and_scale=False)
    _, cellMask3D = mpas.get_cell_masks(dsMesh)

    variableList = [
        'timeMonthly_avg_vertVelocityTop', 'xtime_startMonthly',
        'xtime_endMonthly'
    ]

    ds = xarray.Dataset()
    with mpas.open_mfdataset(timeSeriesFiles, variableList) as dsIn:
        ds[VAR_NAME] = dsIn.timeMonthly_avg_vertVelocityTop
        ds = mpas.avg_to_mid_level(ds)
        ds = mpas.add_time(ds, dsIn)
        ds.compute()
    ds = ds.rename({'nVertLevelsP1': 'nVertLevels'})
    ds = mpas.add_mask(ds, cellMask3D)
    ds = mpas.add_depth(ds, dsMesh)
    ds.compute()

    ds = mpas.remap(ds, mappingFileName)

    mpas.setup_cmor(VAR_NAME, tables, user_input_path, component='ocean')

    # create axes
    axes = [{
        'table_entry': 'time',
        'units': ds.time.units
    }, {
        'table_entry': 'depth_coord',
        'units': 'm',
        'coord_vals': ds.depth.values,
        'cell_bounds': ds.depth_bnds.values
    }, {
        'table_entry': 'latitude',
        'units': 'degrees_north',
        'coord_vals': ds.lat.values,
        'cell_bounds': ds.lat_bnds.values
    }, {
        'table_entry': 'longitude',
        'units': 'degrees_east',
        'coord_vals': ds.lon.values,
        'cell_bounds': ds.lon_bnds.values
    }]
    try:
        mpas.write_cmor(axes, ds, VAR_NAME, VAR_UNITS)
    except Exception:
        return ""
    return VAR_NAME
Esempio n. 3
0
def handle(infiles, tables, user_input_path, **kwargs):
    """
    Transform MPASO timeMonthly_avg_frazilLayerThicknessTendency into
    CMIP.hfsifrazil

    Parameters
    ----------
    infiles : dict
        a dictionary with namelist, mesh and time series file names

    tables : str
        path to CMOR tables

    user_input_path : str
        path to user input json file

    Returns
    -------
    varname : str
        the name of the processed variable after processing is complete
    """
    msg = 'Starting {name}'.format(name=__name__)
    logging.info(msg)

    timeSeriesFiles = infiles['MPASO']
    mappingFileName = infiles['MPAS_map']
    meshFileName = infiles['MPAS_mesh']
    namelistFileName = infiles['MPASO_namelist']

    namelist = mpas.convert_namelist_to_dict(namelistFileName)
    config_density0 = float(namelist['config_density0'])
    config_frazil_heat_of_fusion = \
        float(namelist['config_frazil_heat_of_fusion'])

    dsMesh = xarray.open_dataset(meshFileName, mask_and_scale=False)
    _, cellMask3D = mpas.get_cell_masks(dsMesh)

    variableList = [
        'timeMonthly_avg_frazilLayerThicknessTendency', 'xtime_startMonthly',
        'xtime_endMonthly'
    ]

    ds = xarray.Dataset()
    with mpas.open_mfdataset(timeSeriesFiles, variableList) as dsIn:
        ds[VAR_NAME] = -config_density0 * config_frazil_heat_of_fusion * \
            dsIn.timeMonthly_avg_frazilLayerThicknessTendency

        ds = mpas.add_time(ds, dsIn)
        ds.compute()

    ds = mpas.add_mask(ds, cellMask3D)
    ds = mpas.add_depth(ds, dsMesh)
    ds.compute()

    ds = mpas.remap(ds, mappingFileName)

    mpas.setup_cmor(VAR_NAME, tables, user_input_path, component='ocean')

    # create axes
    axes = [{
        'table_entry': 'time',
        'units': ds.time.units
    }, {
        'table_entry': 'depth_coord',
        'units': 'm',
        'coord_vals': ds.depth.values,
        'cell_bounds': ds.depth_bnds.values
    }, {
        'table_entry': 'latitude',
        'units': 'degrees_north',
        'coord_vals': ds.lat.values,
        'cell_bounds': ds.lat_bnds.values
    }, {
        'table_entry': 'longitude',
        'units': 'degrees_east',
        'coord_vals': ds.lon.values,
        'cell_bounds': ds.lon_bnds.values
    }]
    try:
        mpas.write_cmor(axes, ds, VAR_NAME, VAR_UNITS, positive='down')
    except Exception:
        return ""
    return VAR_NAME
Esempio n. 4
0
def handle(infiles, tables, user_input_path, **kwargs):
    """
    Transform MPASO timeMonthly_avg_layerThickness into CMIP.thkcello

    Parameters
    ----------
    infiles : dict
        a dictionary with namelist, mesh and time series file names

    tables : str
        path to CMOR tables

    user_input_path : str
        path to user input json file

    Returns
    -------
    varname : str
        the name of the processed variable after processing is complete
    """
    if kwargs.get('simple'):
        msg = f"{VAR_NAME} is not supported for simple conversion"
        print_message(msg)
        return

    msg = 'Starting {name}'.format(name=__name__)
    logging.info(msg)

    meshFileName = infiles['MPAS_mesh']
    mappingFileName = infiles['MPAS_map']
    timeSeriesFiles = infiles['MPASO']

    dsMesh = xarray.open_dataset(meshFileName, mask_and_scale=False)
    earth_radius = dsMesh.attrs['sphere_radius']
    _, cellMask3D = mpas.get_cell_masks(dsMesh)

    variableList = [
        'timeMonthly_avg_layerThickness', 'xtime_startMonthly',
        'xtime_endMonthly'
    ]

    ds = xarray.Dataset()
    with mpas.open_mfdataset(timeSeriesFiles, variableList) as dsIn:
        ds[VAR_NAME] = \
            dsIn.timeMonthly_avg_layerThickness.where(cellMask3D, 0.)
        ds = mpas.add_time(ds, dsIn)
        ds.compute()

    ds = mpas.add_depth(ds, dsMesh)
    ds.compute()

    ds = mpas.remap(ds, mappingFileName)

    # set masked values (where there are no MPAS grid cells) to zero
    ds[VAR_NAME] = ds[VAR_NAME].where(
        ds[VAR_NAME] != netCDF4.default_fillvals['f4'], 0.)

    # the result above is just a mask of area fraction.  We need to multiply
    # by the area on the output grid
    dsMap = xarray.open_dataset(mappingFileName)
    area_b = dsMap.area_b.values
    dst_grid_dims = dsMap.dst_grid_dims.values
    area_b = area_b.reshape((dst_grid_dims[1], dst_grid_dims[0]))
    area_b = xarray.DataArray(data=area_b,
                              dims=('lat', 'lon'),
                              coords=(ds.coords['lat'], ds.coords['lon']))

    # area_b is in square radians, so need to multiply by the earth_radius**2
    # multiply variables in this order so they don't get transposed
    ds[VAR_NAME] = ds[VAR_NAME] * earth_radius**2 * area_b

    setup_cmor(var_name=VAR_NAME,
               table_path=tables,
               table_name=TABLE,
               user_input_path=user_input_path)

    # create axes
    axes = [{
        'table_entry': 'time',
        'units': ds.time.units
    }, {
        'table_entry': 'depth_coord',
        'units': 'm',
        'coord_vals': ds.depth.values,
        'cell_bounds': ds.depth_bnds.values
    }, {
        'table_entry': 'latitude',
        'units': 'degrees_north',
        'coord_vals': ds.lat.values,
        'cell_bounds': ds.lat_bnds.values
    }, {
        'table_entry': 'longitude',
        'units': 'degrees_east',
        'coord_vals': ds.lon.values,
        'cell_bounds': ds.lon_bnds.values
    }]
    try:
        mpas.write_cmor(axes, ds, VAR_NAME, VAR_UNITS)
    except Exception:
        return ""
    return VAR_NAME