-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add area integrated annual mean to data iceberg and ice-shelf flux files #836
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
compass/ocean/tests/global_ocean/files_for_e3sm/add_total_iceberg_ice_shelf_melt.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import os | ||
|
||
import numpy as np | ||
import xarray as xr | ||
from mpas_tools.io import write_netcdf | ||
|
||
from compass.io import symlink | ||
from compass.ocean.tests.global_ocean.files_for_e3sm.files_for_e3sm_step import ( # noqa: E501 | ||
FilesForE3SMStep, | ||
) | ||
|
||
|
||
class AddTotalIcebergIceShelfMelt(FilesForE3SMStep): | ||
""" | ||
A step for for adding the total data iceberg and ice-shelf melt rates to | ||
to the data iceberg and ice-shelf melt files and staging them in | ||
``assembled_files`` | ||
""" | ||
def __init__(self, test_case): | ||
""" | ||
Create a new step | ||
|
||
Parameters | ||
---------- | ||
test_case : compass.TestCase | ||
The test case this step belongs to | ||
""" | ||
super().__init__(test_case, name='add_total_iceberg_ice_shelf_melt', | ||
ntasks=1, min_tasks=1) | ||
|
||
filename = 'Iceberg_Climatology_Merino_MPAS.nc' | ||
subdir = 'remap_iceberg_climatology' | ||
self.add_input_file( | ||
filename=filename, | ||
target=f'../{subdir}/{filename}') | ||
|
||
filename = 'prescribed_ismf_paolo2023.nc' | ||
subdir = 'remap_ice_shelf_melt' | ||
self.add_input_file( | ||
filename=filename, | ||
target=f'../{subdir}/{filename}') | ||
|
||
def setup(self): | ||
""" | ||
setup output files based on config options | ||
""" | ||
super().setup() | ||
if self.with_ice_shelf_cavities: | ||
self.add_output_file( | ||
filename='Iceberg_Climatology_Merino_MPAS_with_totals.nc') | ||
self.add_output_file( | ||
filename='prescribed_ismf_paolo2023_with_totals.nc') | ||
|
||
def run(self): | ||
""" | ||
Run this step of the test case | ||
""" | ||
super().run() | ||
|
||
if not self.with_ice_shelf_cavities: | ||
return | ||
|
||
logger = self.logger | ||
|
||
ds_dib = xr.open_dataset('Iceberg_Climatology_Merino_MPAS.nc') | ||
ds_dismf = xr.open_dataset('prescribed_ismf_paolo2023.nc') | ||
ds_mesh = xr.open_dataset('restart.nc') | ||
|
||
area_cell = ds_mesh.areaCell | ||
|
||
days_in_month = np.array( | ||
[31., 28., 31., 30., 31., 30., 31., 31., 30., 31., 30., 31.]) | ||
|
||
weights = xr.DataArray(data=days_in_month / 365., | ||
dims=('Time',)) | ||
|
||
total_dib_flux = (ds_dib.bergFreshwaterFluxData * weights * | ||
area_cell).sum() | ||
|
||
total_dismf_flux = (ds_dismf.dataLandIceFreshwaterFlux * | ||
area_cell).sum() | ||
|
||
total_flux = total_dib_flux + total_dismf_flux | ||
|
||
logger.info(f'total_dib_flux: {total_dib_flux:.1f}') | ||
logger.info(f'total_dismf_flux: {total_dismf_flux:.1f}') | ||
logger.info(f'total_flux: {total_flux:.1f}') | ||
logger.info('') | ||
|
||
for ds in [ds_dib, ds_dismf]: | ||
ntime = ds.sizes['Time'] | ||
field = 'areaIntegAnnMeanDataIcebergFreshwaterFlux' | ||
ds[field] = (('Time',), np.ones(ntime) * total_dib_flux.values) | ||
ds[field].attrs['units'] = 'kg s-1' | ||
field = 'areaIntegAnnMeanDataIceShelfFreshwaterFlux' | ||
ds[field] = (('Time',), np.ones(ntime) * total_dismf_flux.values) | ||
ds[field].attrs['units'] = 'kg s-1' | ||
field = 'areaIntegAnnMeanDataIcebergIceShelfFreshwaterFlux' | ||
ds[field] = (('Time',), np.ones(ntime) * total_flux.values) | ||
ds[field].attrs['units'] = 'kg s-1' | ||
|
||
dib_filename = 'Iceberg_Climatology_Merino_MPAS_with_totals.nc' | ||
write_netcdf(ds_dib, dib_filename) | ||
|
||
dismf_filename = 'prescribed_ismf_paolo2023_with_totals.nc' | ||
write_netcdf(ds_dismf, dismf_filename) | ||
|
||
norm_total_dib_flux = (ds_dib.bergFreshwaterFluxData * weights * | ||
area_cell / total_flux).sum() | ||
|
||
norm_total_dismf_flux = (ds_dismf.dataLandIceFreshwaterFlux * | ||
area_cell / total_flux).sum() | ||
|
||
norm_total_flux = norm_total_dib_flux + norm_total_dismf_flux | ||
|
||
logger.info(f'norm_total_dib_flux: {norm_total_dib_flux:.16f}') | ||
logger.info(f'norm_total_dismf_flux: {norm_total_dismf_flux:.16f}') | ||
logger.info(f'norm_total_flux: {norm_total_flux:.16f}') | ||
logger.info(f'1 - norm_total_flux: {1 - norm_total_flux:.16g}') | ||
logger.info('') | ||
|
||
prefix = 'Iceberg_Climatology_Merino' | ||
suffix = f'{self.mesh_short_name}.{self.creation_date}' | ||
dest_filename = f'{prefix}.{suffix}.nc' | ||
symlink( | ||
os.path.abspath(dib_filename), | ||
f'{self.seaice_inputdata_dir}/{dest_filename}') | ||
|
||
prefix = 'prescribed_ismf_paolo2023' | ||
suffix = f'{self.mesh_short_name}.{self.creation_date}' | ||
dest_filename = f'{prefix}.{suffix}.nc' | ||
symlink( | ||
os.path.abspath(dismf_filename), | ||
f'{self.ocean_inputdata_dir}/{dest_filename}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to use variables for the iceberg and ismf dataset names so it's a little easier to replace them as new datasets come along?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with adding those. There's no huge rush on this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On second thought, I think generalizing Compass so it can handle new iceberg and ISMF datasets based on config options seems like a fairly significant challenge that's beyond the scope of this PR. I think it would make sense to take that on as soon as we have such a new dataset.