Skip to content

Commit

Permalink
Add remapping and viz steps to cosine bell
Browse files Browse the repository at this point in the history
  • Loading branch information
xylar committed Apr 13, 2023
1 parent ceec172 commit 01f2195
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 0 deletions.
12 changes: 12 additions & 0 deletions polaris/ocean/tests/global_convergence/cosine_bell/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
)
from polaris.ocean.tests.global_convergence.cosine_bell.forward import Forward
from polaris.ocean.tests.global_convergence.cosine_bell.init import Init
from polaris.ocean.tests.global_convergence.cosine_bell.viz import Viz, VizMap
from polaris.testcase import TestCase
from polaris.validate import compare_variables

Expand Down Expand Up @@ -128,5 +129,16 @@ def _setup_steps(self, config):
self.add_step(Forward(test_case=self, resolution=resolution,
mesh_name=mesh_name))

name = f'{mesh_name}_map'
subdir = f'{mesh_name}/map'
viz_map = VizMap(test_case=self, name=name, subdir=subdir,
mesh_name=mesh_name)
self.add_step(viz_map)

name = f'{mesh_name}_viz'
subdir = f'{mesh_name}/viz'
self.add_step(Viz(test_case=self, name=name, subdir=subdir,
mapping_file_step=viz_map, mesh_name=mesh_name))

self.add_step(Analysis(test_case=self, resolutions=resolutions,
icosahedral=self.icosahedral))
24 changes: 24 additions & 0 deletions polaris/ocean/tests/global_convergence/cosine_bell/cosine_bell.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,27 @@ icos_conv_thresh = 1.8

# Convergence rate above which a warning is issued for icosahedral meshes
icos_conv_max = 2.2


# options for visualization for the cosine bell convergence test case
[cosine_bell_viz]

# visualization latitude and longitude resolution
dlon = 0.5
dlat = 0.5

# remapping method ('bilinear', 'neareststod', 'conserve')
remap_method = conserve

# colormap options
# colormap
colormap_name = viridis

# the type of norm used in the colormap
norm_type = linear

# A dictionary with keywords for the norm
norm_args = {'vmin': 0., 'vmax': 1.}

# We could provide colorbar tick marks but we'll leave the defaults
# colorbar_ticks = np.linspace(0., 1., 9)
142 changes: 142 additions & 0 deletions polaris/ocean/tests/global_convergence/cosine_bell/viz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import cmocean # noqa: F401
import xarray as xr

from polaris.remap import MappingFileStep
from polaris.step import Step
from polaris.viz.globe import plot_global


class VizMap(MappingFileStep):
"""
A step for making a mapping file for cosine bell viz
Attributes
----------
mesh_name : str
The name of the mesh
"""
def __init__(self, test_case, name, subdir, mesh_name):
"""
Create the step
Parameters
----------
test_case : polaris.TestCase
The test case this step belongs to
name : str
The name of the step
subdir : str
The subdirectory in the test case's work directory for the step
mesh_name : str
The name of the mesh
"""
super().__init__(test_case=test_case, name=name, subdir=subdir,
ntasks=128, min_tasks=1)
self.mesh_name = mesh_name
self.add_input_file(filename='mesh.nc', target='../mesh/mesh.nc')

def setup(self):
"""
Set up the source and destination grids for this step
"""
config = self.config
section = config['cosine_bell_viz']
dlon = section.getfloat('dlon')
dlat = section.getfloat('dlat')
method = section.get('remap_method')
self.src_from_mpas(filename='mesh.nc', mesh_name=self.mesh_name)
self.dst_global_lon_lat(dlon=dlon, dlat=dlat)
self.method = method

super().setup()


class Viz(Step):
"""
A step for plotting fields from the cosine bell output
Attributes
----------
mapping_file_step : polaris.remap.MappingFileStep
The step for creating a mapping files, also used to remap data
from the MPAS mesh to a lon-lat grid
mesh_name : str
The name of the mesh
"""
def __init__(self, test_case, name, subdir, mapping_file_step, mesh_name):
"""
Create the step
Parameters
----------
test_case : polaris.TestCase
The test case this step belongs to
name : str
The name of the step
subdir : str
The subdirectory in the test case's work directory for the step
mapping_file_step : polaris.remap.MappingFileStep
The step for creating a mapping files, also used to remap data
from the MPAS mesh to a lon-lat grid
mesh_name : str
The name of the mesh
"""
super().__init__(test_case=test_case, name=name, subdir=subdir)
self.add_input_file(
filename='initial_state.nc',
target='../init/initial_state.nc')
self.add_input_file(
filename='output.nc',
target='../forward/output.nc')
self.mapping_file_step = mapping_file_step
self.mesh_name = mesh_name
self.add_output_file('init.png')
self.add_output_file('final.png')

def setup(self):
"""
Set up the source and destination grids for this step
"""
super().setup()
map_filename = self.mapping_file_step.map_filename
self.add_input_file(filename=map_filename,
target=f'../map/{map_filename}')

def run(self):
"""
Run this step of the test case
"""
config = self.config
mesh_name = self.mesh_name
period = config.getfloat('cosine_bell', 'vel_pd')

remapper = self.mapping_file_step.get_remapper()

ds_init = xr.open_dataset('initial_state.nc')
ds_init = ds_init[['tracer1', ]].isel(Time=0, nVertLevels=0)
ds_init = remapper.remap(ds_init)

plot_global(ds_init.lon.values, ds_init.lat.values,
ds_init.tracer1.values,
out_filename='init.png', config=config,
colormap_section='cosine_bell_viz',
title=f'{mesh_name} tracer at init', plot_land=True)

ds_out = xr.open_dataset('output.nc')
ds_out = ds_out[['tracer1', ]].isel(Time=-1, nVertLevels=0)
ds_out = remapper.remap(ds_out)

plot_global(ds_out.lon.values, ds_out.lat.values,
ds_out.tracer1.values,
out_filename='final.png', config=config,
colormap_section='cosine_bell_viz',
title=f'{mesh_name} tracer after {period} days',
plot_land=False)

0 comments on commit 01f2195

Please sign in to comment.