Skip to content

Commit

Permalink
Ensure data_soruce argument is passed to Mediator by objects of type …
Browse files Browse the repository at this point in the history
…regridder (#154)

* Pass data_source to Mediator on creation when regridding

* Fix regridding notebook

* Flag that this version has not been officially released
  • Loading branch information
jimc101 authored Sep 19, 2024
1 parent c30a6b2 commit 0476b6d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
4 changes: 3 additions & 1 deletion doc/source/documentation/cookbook/regridding.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
"output_type": "stream",
"text": [
"Creating FVCOM grid metrics file ./regridding/grid_metrics.nc\n",
"\n",
"Calculating element areas ... done\n",
"Grid has 45 nodes on the open boundary\n"
]
}
Expand Down Expand Up @@ -53,7 +55,7 @@
"grid_metrics_file_name = f'{regridding_dir}/grid_metrics.nc'\n",
"\n",
"# Generate the file\n",
"create_fvcom_grid_metrics_file(fvcom_file_name, obc_file_name,\n",
"create_fvcom_grid_metrics_file(fvcom_file_name, obc_file_name=obc_file_name,\n",
" grid_metrics_file_name=grid_metrics_file_name)"
]
},
Expand Down
59 changes: 49 additions & 10 deletions pylag/regrid/regridder.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ include "constants.pxi"

import numpy as np

try:
import configparser
except ImportError:
import ConfigParser as configparser

from libcpp.vector cimport vector

# Data types used for constructing C data structures
Expand All @@ -33,6 +38,10 @@ from pylag.particle cimport Particle
from pylag.data_reader cimport DataReader
from pylag.particle_cpp_wrapper cimport ParticleSmartPtr

# Exceptions
from pylag.exceptions import PyLagRuntimeError, PyLagValueError


cdef class Regridder:
""" A Regridder object
Expand Down Expand Up @@ -111,20 +120,51 @@ cdef class Regridder:
self.datetime_start = datetime_start
self.datetime_end = datetime_end

# At present, we only support the regridding of ocean data. If other data
# types are listed in the config file, raise an error.
try:
ocean_product_name = config.get("OCEAN_DATA",
"name").strip()
except (configparser.NoSectionError, configparser.NoOptionError):
raise PyLagRuntimeError("No ocean data specified in the config file. This "
"should be speficied using the OCEAN_DATA section of the config "
"file.")

# If atmosphere data is specified, raise a warning that this will not be read or used.
try:
atmos_product_name = config.get("ATMOSPHERE_DATA", "name").strip()
except (configparser.NoSectionError, configparser.NoOptionError):
atmos_product_name = "none"

if atmos_product_name.lower() != "none":
print(f'Warning: Atmosphere data specified in config file. This will not be read or used '
f'during regridding.')

# If wave data is specified, raise a warning that this will not be read or used.
try:
wave_product_name = config.get("WAVE_DATA", "name").strip()
except (configparser.NoSectionError, configparser.NoOptionError):
wave_product_name = "none"

if wave_product_name.lower() != "none":
print(f'Warning: Wave data specified in config file. This will not be read or used '
f'during regridding.')

# Intialise the data reader
if config.get("OCEAN_DATA", "name") == "ArakawaA":
mediator = SerialMediator(config, datetime_start, datetime_end)
data_source = 'ocean'
if ocean_product_name == "ArakawaA":
mediator = SerialMediator(config, data_source, datetime_start, datetime_end)
self.data_reader = ArakawaADataReader(config, mediator)
elif config.get("OCEAN_DATA", "name") == "FVCOM":
mediator = SerialMediator(config, datetime_start, datetime_end)
elif ocean_product_name == "FVCOM":
mediator = SerialMediator(config, data_source, datetime_start, datetime_end)
self.data_reader = FVCOMDataReader(config, mediator)
elif config.get("OCEAN_DATA", "name") == "ROMS":
mediator = SerialMediator(config, datetime_start, datetime_end)
elif ocean_product_name == "ROMS":
mediator = SerialMediator(config, data_source, datetime_start, datetime_end)
self.data_reader = ROMSDataReader(config, mediator)
else:
raise ValueError('Unsupported ocean circulation model.')
raise PyLagValueError('Unsupported ocean data product.')

self.coordinate_system = config.get("SIMULATION", "coordinate_system")
self.coordinate_system = self.config.get("SIMULATION", "coordinate_system").strip().lower()

# Generate particle set, including interpolation coefficients
print('Computing weights ', end='... ')
Expand Down Expand Up @@ -188,9 +228,8 @@ cdef class Regridder:

# Check that the coordinate system is supported
# TODO if support for Cartesian input grids is added, will need to apply xmin and xmax offsets
self.coordinate_system = self.config.get("OCEAN_DATA", "coordinate_system").strip().lower()
if not self.coordinate_system == "geographic":
raise ValueError('Input oordinate sytem {} is not supported in regridding tasks.'.format(self.coordinate_system))
raise ValueError(f'Input oordinate sytem {self.coordinate_system} is not supported in regridding tasks.')

# Initialise variables involved in creating the particle set
host_elements = None
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
MAJOR = 0
MINOR = 7
PATCH = 1
ISRELEASED = True
ISRELEASED = False
VERSION = '{}.{}.{}'.format(MAJOR, MINOR, PATCH)

build_type = 'prod'
Expand Down

0 comments on commit 0476b6d

Please sign in to comment.