def handle(infiles, tables, user_input_path, **kwargs): """ Transform MPASO timeMonthly_avg_activeTracers_temperature into CMIP.tosga 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'] timeSeriesFiles = infiles['MPASO'] dsMesh = xarray.open_dataset(meshFileName, mask_and_scale=False) cellMask2D, _ = mpas.get_cell_masks(dsMesh) variableList = [ 'timeMonthly_avg_activeTracers_temperature', 'xtime_startMonthly', 'xtime_endMonthly' ] ds = xarray.Dataset() with mpas.open_mfdataset(timeSeriesFiles, variableList) as dsIn: thetao = dsIn.timeMonthly_avg_activeTracers_temperature tos = thetao.isel(nVertLevels=0).squeeze(drop=True).where(cellMask2D) areaCell = dsMesh.areaCell.where(cellMask2D) ds[VAR_NAME] = ((tos * areaCell).sum(dim='nCells') / areaCell.sum(dim='nCells')) ds = mpas.add_time(ds, dsIn) ds.compute() ds.compute() mpas.setup_cmor(VAR_NAME, tables, user_input_path, component='ocean') # create axes axes = [{'table_entry': 'time', 'units': ds.time.units}] try: mpas.write_cmor(axes, ds, VAR_NAME, VAR_UNITS) except Exception: return "" return VAR_NAME
def handle(infiles, tables, user_input_path, **kwargs): """ Transform MPASO timeMonthly_avg_layerThickness and timeMonthly_avg_activeTracers_temperature into CMIP.thetaoga 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'] timeSeriesFiles = infiles['MPASO'] dsMesh = xarray.open_dataset(meshFileName, mask_and_scale=False) _, cellMask3D = mpas.get_cell_masks(dsMesh) variableList = [ 'timeMonthly_avg_layerThickness', 'timeMonthly_avg_activeTracers_temperature', 'xtime_startMonthly', 'xtime_endMonthly' ] ds = xarray.Dataset() with mpas.open_mfdataset(timeSeriesFiles, variableList) as dsIn: layerThickness = dsIn.timeMonthly_avg_layerThickness.where(cellMask3D) thetao = dsIn.timeMonthly_avg_activeTracers_temperature.where( cellMask3D) vol = layerThickness * dsMesh.areaCell volo = (vol).sum(dim=['nVertLevels', 'nCells']) ds[VAR_NAME] = (vol * thetao).sum(dim=['nVertLevels', 'nCells']) / volo ds = mpas.add_time(ds, dsIn) ds.compute() mpas.setup_cmor(VAR_NAME, tables, user_input_path, component='ocean') # create axes axes = [{'table_entry': 'time', 'units': ds.time.units}] try: mpas.write_cmor(axes, ds, VAR_NAME, VAR_UNITS) except Exception: return "" return VAR_NAME
def handle(infiles, tables, user_input_path, **kwargs): """ Transform MPASO timeMonthly_avg_normalVelocity, timeMonthly_avg_normalGMBolusVelocity, timeMonthly_avg_vertVelocityTop, timeMonthly_avg_vertGMBolusVelocityTop, and timeMonthly_avg_layerThickness into CMIP.msftmz 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'] timeSeriesFiles = infiles['MPASO'] regionMaskFileName = infiles['MPASO_MOC_regions'] namelistFileName = infiles['MPASO_namelist'] namelist = mpas.convert_namelist_to_dict(namelistFileName) config_density0 = float(namelist['config_density0']) dsMesh = xarray.open_dataset(meshFileName, mask_and_scale=False) dsMesh = dsMesh.isel(Time=0) dsMasks = xarray.open_dataset(regionMaskFileName, mask_and_scale=False) variableList = [ 'timeMonthly_avg_normalVelocity', 'timeMonthly_avg_normalGMBolusVelocity', 'timeMonthly_avg_vertVelocityTop', 'timeMonthly_avg_vertGMBolusVelocityTop', 'timeMonthly_avg_layerThickness', 'xtime_startMonthly', 'xtime_endMonthly' ] with mpas.open_mfdataset(timeSeriesFiles, variableList) as dsIn: showProgress = 'serial' in kwargs and kwargs['serial'] ds = config_density0 * mpas.compute_moc_streamfunction( dsIn, dsMesh, dsMasks, showProgress=showProgress) ds = ds.rename({'moc': VAR_NAME}) mpas.setup_cmor(VAR_NAME, tables, user_input_path, component='ocean') region = ['global_ocean', 'atlantic_arctic_ocean'] # create axes axes = [{ 'table_entry': 'time', 'units': ds.time.units }, { 'table_entry': 'basin', 'units': '', 'coord_vals': region }, { '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 }] try: mpas.write_cmor(axes, ds, VAR_NAME, VAR_UNITS) except Exception: return "" return VAR_NAME
def handle(infiles, tables, user_input_path, **kwargs): """ Transform MPASO timeMonthly_avg_dThreshMLD into CMIP.mlotst 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) cellMask2D, _ = mpas.get_cell_masks(dsMesh) variableList = [ 'timeMonthly_avg_dThreshMLD', 'xtime_startMonthly', 'xtime_endMonthly' ] ds = xarray.Dataset() with mpas.open_mfdataset(timeSeriesFiles, variableList) as dsIn: ds[VAR_NAME] = dsIn.timeMonthly_avg_dThreshMLD.where(cellMask2D) ds = mpas.add_time(ds, dsIn) ds.compute() ds = mpas.add_mask(ds, cellMask2D) 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': '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
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
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
def handle(infiles, tables, user_input_path, **kwargs): """ Transform MPASO timeMonthly_avg_windStressMeridional into CMIP.tauvo 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) mappingFileName = infiles['MPAS_map'] timeSeriesFiles = infiles['MPASO'] variableList = [ 'timeMonthly_avg_windStressMeridional', 'xtime_startMonthly', 'xtime_endMonthly' ] ds = xarray.Dataset() with mpas.open_mfdataset(timeSeriesFiles, variableList) as dsIn: ds[VAR_NAME] = dsIn.timeMonthly_avg_windStressMeridional ds = mpas.add_time(ds, dsIn) 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': '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
def handle(infiles, tables, user_input_path, **kwargs): """ Transform MPASO timeMonthly_avg_seaIceHeatFlux, timeMonthly_avg_latentHeatFlux ,timeMonthly_avg_sensibleHeatFlux, timeMonthly_avg_shortWaveHeatFlux, timeMonthly_avg_longWaveHeatFluxUp, and timeMonthly_avg_longWaveHeatFluxDown into CMIP.hfds 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) mappingFileName = infiles['MPAS_map'] timeSeriesFiles = infiles['MPASO'] variableList = ['timeMonthly_avg_seaIceHeatFlux', 'timeMonthly_avg_latentHeatFlux', 'timeMonthly_avg_sensibleHeatFlux', 'timeMonthly_avg_shortWaveHeatFlux', 'timeMonthly_avg_longWaveHeatFluxUp', 'timeMonthly_avg_longWaveHeatFluxDown', 'xtime_startMonthly', 'xtime_endMonthly'] ds = xarray.Dataset() with mpas.open_mfdataset(timeSeriesFiles, variableList) as dsIn: ds[VAR_NAME] = (dsIn.timeMonthly_avg_seaIceHeatFlux + dsIn.timeMonthly_avg_latentHeatFlux + dsIn.timeMonthly_avg_sensibleHeatFlux + dsIn.timeMonthly_avg_shortWaveHeatFlux + dsIn.timeMonthly_avg_longWaveHeatFluxUp + dsIn.timeMonthly_avg_longWaveHeatFluxDown) ds = mpas.add_time(ds, dsIn) 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': '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
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
def handle(infiles, tables, user_input_path, **kwargs): """ Transform MPASSI timeMonthly_avg_iceAreaCell and timeMonthly_avg_iceVolumeCell into CMIP.sithick 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['MPASSI'] dsMesh = xarray.open_dataset(meshFileName, mask_and_scale=False) cellMask2D, _ = mpas.get_cell_masks(dsMesh) variableList = [ 'timeMonthly_avg_iceAreaCell', 'timeMonthly_avg_iceVolumeCell', 'xtime_startMonthly', 'xtime_endMonthly' ] ds = xarray.Dataset() with mpas.open_mfdataset(timeSeriesFiles, variableList) as dsIn: ds[VAR_NAME] = dsIn.timeMonthly_avg_iceVolumeCell ds['siconc'] = dsIn.timeMonthly_avg_iceAreaCell ds = mpas.add_time(ds, dsIn) ds.compute() ds = mpas.add_si_mask(ds, cellMask2D, ds.siconc) ds['cellMask'] = ds.siconc * ds.cellMask ds.compute() ds = mpas.remap(ds, mappingFileName) mpas.setup_cmor(VAR_NAME, tables, user_input_path, component='seaice') # create axes axes = [{ 'table_entry': 'time', 'units': ds.time.units }, { '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
def handle(infiles, tables, user_input_path, **kwargs): """ Transform MPASO timeMonthly_avg_seaIceFreshWaterFlux, timeMonthly_avg_riverRunoffFlux, timeMonthly_avg_iceRunoffFlux, timeMonthly_avg_rainFlux, and timeMonthly_avg_snowFlux into CMIP.wfo 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) mappingFileName = infiles['MPAS_map'] timeSeriesFiles = infiles['MPASO'] variableList = [ 'timeMonthly_avg_seaIceFreshWaterFlux', 'timeMonthly_avg_riverRunoffFlux', 'timeMonthly_avg_iceRunoffFlux', 'timeMonthly_avg_rainFlux', 'timeMonthly_avg_snowFlux', 'xtime_startMonthly', 'xtime_endMonthly' ] ds = xarray.Dataset() with mpas.open_mfdataset(timeSeriesFiles, variableList) as dsIn: ds[VAR_NAME] = \ dsIn.timeMonthly_avg_seaIceFreshWaterFlux + \ dsIn.timeMonthly_avg_riverRunoffFlux + \ dsIn.timeMonthly_avg_iceRunoffFlux + \ dsIn.timeMonthly_avg_rainFlux + \ dsIn.timeMonthly_avg_snowFlux ds = mpas.add_time(ds, dsIn) 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': '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, comment='Computed as the water flux into the ocean ' 'divided by the area of the ocean portion of ' 'the grid cell. This is the sum of sea-ice' 'freshwater, river runoff, ice runoff, rain,' 'and snow fluxes.') except Exception: return "" return VAR_NAME
def handle(infiles, tables, user_input_path, **kwargs): """ Transform MPASO timeMonthly_avg_seaIceFreshWaterFlux into CMIP.fsitherm 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'): print_message(f'Simple CMOR output not supported for {VAR_NAME}', 'error') return None logging.info(f'Starting {VAR_NAME}') mappingFileName = infiles['MPAS_map'] timeSeriesFiles = infiles['MPASO'] variableList = [ 'timeMonthly_avg_seaIceFreshWaterFlux', 'xtime_startMonthly', 'xtime_endMonthly' ] ds = xarray.Dataset() with mpas.open_mfdataset(timeSeriesFiles, variableList) as dsIn: ds[VAR_NAME] = dsIn.timeMonthly_avg_seaIceFreshWaterFlux ds = mpas.add_time(ds, dsIn) 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': '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
def handle(infiles, tables, user_input_path, **kwargs): """ Transform MPASO timeMonthly_avg_pressureAdjustedSSH, timeMonthly_avg_ssh, timeMonthly_avg_density, timeMonthly_avg_layerThickness, and EAM PSL into CMIP.pbo 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) namelistFileName = infiles['MPASO_namelist'] meshFileName = infiles['MPAS_mesh'] mappingFileName = infiles['MPAS_map'] timeSeriesFiles = infiles['MPASO'] pslFileNames = infiles['PSL'] namelist = mpas.convert_namelist_to_dict(namelistFileName) config_density0 = float(namelist['config_density0']) gravity = 9.80616 dsMesh = xarray.open_dataset(meshFileName, mask_and_scale=False) cellMask2D, cellMask3D = mpas.get_cell_masks(dsMesh) variableList = [ 'timeMonthly_avg_pressureAdjustedSSH', 'timeMonthly_avg_ssh', 'timeMonthly_avg_layerThickness', 'timeMonthly_avg_density', 'xtime_startMonthly', 'xtime_endMonthly' ] ds = xarray.Dataset() with mpas.open_mfdataset(timeSeriesFiles, variableList) as dsIn: seaIcePressure = config_density0 * gravity * \ (dsIn.timeMonthly_avg_pressureAdjustedSSH - dsIn.timeMonthly_avg_ssh) ds[VAR_NAME] = seaIcePressure.where(cellMask2D) + gravity * \ (dsIn.timeMonthly_avg_density * dsIn.timeMonthly_avg_layerThickness).where(cellMask3D).sum( dim='nVertLevels') ds = mpas.add_time(ds, dsIn) ds.compute() ds = mpas.remap(ds, mappingFileName) with xarray.open_mfdataset(pslFileNames, concat_dim='time') as dsIn: ds[VAR_NAME] = ds[VAR_NAME] + dsIn.PSL.values mpas.setup_cmor(VAR_NAME, tables, user_input_path, component='ocean') # create axes axes = [{ 'table_entry': 'time', 'units': ds.time.units }, { '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
def handle(infiles, tables, user_input_path, **kwargs): """ Transform MPASO timeMonthly_avg_layerThickness into CMIP.zhalfo 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_layerThickness', 'xtime_startMonthly', 'xtime_endMonthly' ] nVertLevels = dsMesh.sizes['nVertLevels'] ds = xarray.Dataset() with mpas.open_mfdataset(timeSeriesFiles, variableList) as dsIn: layerThickness = dsIn.timeMonthly_avg_layerThickness layerThickness = layerThickness.where(cellMask3D) thicknessSum = layerThickness.sum(dim='nVertLevels') mask = cellMask3D.isel(nVertLevels=0) zSurface = (-dsMesh.bottomDepth + thicknessSum).where(mask) zSurface.compute() # print('done zSurface') slices = [zSurface] maskSlices = [mask] zLayerBot = zSurface for zIndex in range(nVertLevels): mask = cellMask3D.isel(nVertLevels=zIndex) zLayerBot = (zLayerBot - layerThickness.isel(nVertLevels=zIndex)).where(mask) zLayerBot.compute() # print('done zLayerBot {}/{}'.format(zIndex+1, nVertLevels)) slices.append(zLayerBot) maskSlices.append(mask) ds[VAR_NAME] = xarray.concat(slices, dim='olevhalf') mask = xarray.concat(maskSlices, dim='olevhalf') ds = mpas.add_mask(ds, mask) ds = ds.transpose('Time', 'olevhalf', 'nCells') ds = mpas.add_time(ds, dsIn) ds.compute() ds = mpas.remap(ds, mappingFileName) depth_coord_half = numpy.zeros(nVertLevels + 1) depth_coord_half[1:] = dsMesh.refBottomDepth.values 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_half', 'units': 'm', 'coord_vals': depth_coord_half }, { '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