-
Notifications
You must be signed in to change notification settings - Fork 10
Ship underwater st test #36
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d401865
wip ship st test
surgura 6c34b5e
finish test
surgura 8184c74
explicitly use the trajectory listed in zarr
surgura 68db417
improve comments in test
surgura a790c25
docstring improvements
surgura 8ee267b
fix typo
surgura 4c04e47
fix typo
surgura 250344c
fix typo
surgura 6a936cb
spacetime time now datetime object isntead of numpy
surgura d09b368
use builtin tmpdir pytest
surgura ac87bf8
remove unused iteration variable
surgura dfd52ea
explicitly use outputdt inf
surgura 8b9855b
typo
surgura 1c5c3d2
comment
surgura 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,29 +1,100 @@ | ||
| """Test the simulation of ship salinity temperature measurements.""" | ||
|
|
||
| import datetime | ||
|
|
||
| import numpy as np | ||
| import py | ||
| import xarray as xr | ||
| from parcels import FieldSet | ||
|
|
||
| from virtual_ship import Location, Spacetime | ||
| from virtual_ship.instruments.ship_underwater_st import simulate_ship_underwater_st | ||
|
|
||
|
|
||
| def test_simulate_ship_underwater_st() -> None: | ||
| def test_simulate_ship_underwater_st(tmpdir: py.path.LocalPath) -> None: | ||
| # depth at which the sampling will be done | ||
| DEPTH = -2 | ||
|
|
||
| # arbitrary time offset for the dummy fieldset | ||
| base_time = datetime.datetime.strptime("1950-01-01", "%Y-%m-%d") | ||
|
|
||
| # variabes we are going to compare between expected and actual observations | ||
| variables = ["salinity", "temperature", "lat", "lon"] | ||
|
|
||
| # where to sample | ||
| sample_points = [ | ||
| Spacetime(Location(3, 0), base_time + datetime.timedelta(seconds=0)), | ||
| Spacetime(Location(7, 0), base_time + datetime.timedelta(seconds=1)), | ||
| ] | ||
|
|
||
| # expected observations at sample points | ||
| expected_obs = [ | ||
| { | ||
| "salinity": 1, | ||
| "temperature": 2, | ||
| "lat": 3, | ||
| "lon": 0, | ||
| "time": base_time + datetime.timedelta(seconds=0), | ||
| }, | ||
| { | ||
| "salinity": 5, | ||
| "temperature": 6, | ||
| "lat": 7, | ||
| "lon": 0, | ||
| "time": base_time + datetime.timedelta(seconds=1), | ||
| }, | ||
| ] | ||
|
|
||
| # create fieldset based on the expected observations | ||
| fieldset = FieldSet.from_data( | ||
| {"U": 0, "V": 0, "S": 0, "T": 0}, | ||
| { | ||
| "U": np.zeros((2, 2)), | ||
| "V": np.zeros((2, 2)), | ||
| "salinity": [ | ||
| [expected_obs[0]["salinity"], 0], | ||
| [0, expected_obs[1]["salinity"]], | ||
| ], | ||
| "temperature": [ | ||
| [expected_obs[0]["temperature"], 0], | ||
| [0, expected_obs[1]["temperature"]], | ||
| ], | ||
| }, | ||
| { | ||
| "lon": 0, | ||
| "lat": 0, | ||
| "time": [np.datetime64("1950-01-01") + np.timedelta64(632160, "h")], | ||
| "lat": np.array([expected_obs[0]["lat"], expected_obs[1]["lat"]]), | ||
| "time": np.array( | ||
| [ | ||
| np.datetime64(expected_obs[0]["time"]), | ||
| np.datetime64(expected_obs[1]["time"]), | ||
| ] | ||
| ), | ||
| }, | ||
| ) | ||
|
|
||
| sample_points = [Spacetime(Location(0, 0), 0)] | ||
| out_path = tmpdir.join("out.zarr") | ||
|
|
||
| simulate_ship_underwater_st( | ||
| fieldset=fieldset, | ||
| out_file_name="test", | ||
| out_path=out_path, | ||
| depth=DEPTH, | ||
| sample_points=sample_points, | ||
| ) | ||
|
|
||
| results = xr.open_zarr(out_path) | ||
|
|
||
| # below we assert if output makes sense | ||
| assert len(results.trajectory) == 1 # expect a singe trajectory | ||
| traj = results.trajectory.item() | ||
| assert len(results.sel(trajectory=traj).obs) == len( | ||
| sample_points | ||
| ) # expect as many obs as sample points | ||
|
|
||
| # for every obs, check if the variables match the expected observations | ||
| for obs_i, exp in zip(results.sel(trajectory=traj).obs, expected_obs, strict=True): | ||
| obs = results.sel(trajectory=traj, obs=obs_i) | ||
| for var in variables: | ||
| obs_value = obs[var].values.item() | ||
| exp_value = exp[var] | ||
| assert np.isclose( | ||
| obs_value, exp_value | ||
| ), f"Observation incorrect {obs_i=} {var=} {obs_value=} {exp_value=}." |
This file contains hidden or 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 hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.