-
Notifications
You must be signed in to change notification settings - Fork 10
Other instrument tests #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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a3b3044
typo
surgura c196f6c
use path for adcp
surgura 9914db4
adcp test
surgura 410b0a1
test adcp at depth
surgura c5218e2
finish adcp test
surgura 074176a
improve st
surgura 4d620a5
cleanup test
surgura 9fdcedf
back to 24
surgura e0b9b3d
comment about indices
surgura 576a17c
repair all tests
surgura dc566c9
scipyparticle comment
surgura b6964bf
formatting
surgura ebd5d6f
fix
surgura 1faf91a
order lat lon instead of lon lat
surgura 8f372d8
forgot something
surgura c5dcc36
typo
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,119 @@ | ||
| """Test the simulation of ADCP instruments.""" | ||
|
|
||
| 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.adcp import simulate_adcp | ||
|
|
||
|
|
||
| def test_simulate_adcp() -> None: | ||
| MAX_DEPTH = -1000 | ||
| MIN_DEPTH = -5 | ||
| BIN_SIZE = 24 | ||
| def test_simulate_adcp(tmpdir: py.path.LocalPath) -> None: | ||
| # maximum depth the ADCP can measure | ||
| MAX_DEPTH = -1000 # -1000 | ||
| # minimum depth the ADCP can measure | ||
| MIN_DEPTH = -5 # -5 | ||
| # How many samples to take in the complete range between max_depth and min_depth. | ||
| NUM_BINS = 40 | ||
|
|
||
| # arbitrary time offset for the dummy fieldset | ||
| base_time = datetime.datetime.strptime("1950-01-01", "%Y-%m-%d") | ||
|
|
||
| # where to sample | ||
| sample_points = [ | ||
| Spacetime(Location(1, 2), base_time + datetime.timedelta(seconds=0)), | ||
| Spacetime(Location(3, 4), base_time + datetime.timedelta(seconds=1)), | ||
| ] | ||
|
|
||
| # expected observations at sample points | ||
| expected_obs = [ | ||
| { | ||
| "V": {"surface": 5, "max_depth": 6}, | ||
| "U": {"surface": 7, "max_depth": 8}, | ||
| "lat": sample_points[0].location.lat, | ||
| "lon": sample_points[0].location.lon, | ||
| "time": base_time + datetime.timedelta(seconds=0), | ||
| }, | ||
| { | ||
| "V": {"surface": 9, "max_depth": 10}, | ||
| "U": {"surface": 11, "max_depth": 12}, | ||
| "lat": sample_points[1].location.lat, | ||
| "lon": sample_points[1].location.lon, | ||
| "time": base_time + datetime.timedelta(seconds=1), | ||
| }, | ||
| ] | ||
|
|
||
| # create fieldset based on the expected observations | ||
| # indices are time, depth, latitude, longitude | ||
| v = np.zeros((2, 2, 2, 2)) | ||
| v[0, 0, 0, 0] = expected_obs[0]["V"]["max_depth"] | ||
| v[0, 1, 0, 0] = expected_obs[0]["V"]["surface"] | ||
| v[1, 0, 1, 1] = expected_obs[1]["V"]["max_depth"] | ||
| v[1, 1, 1, 1] = expected_obs[1]["V"]["surface"] | ||
|
|
||
| u = np.zeros((2, 2, 2, 2)) | ||
| u[0, 0, 0, 0] = expected_obs[0]["U"]["max_depth"] | ||
| u[0, 1, 0, 0] = expected_obs[0]["U"]["surface"] | ||
| u[1, 0, 1, 1] = expected_obs[1]["U"]["max_depth"] | ||
| u[1, 1, 1, 1] = expected_obs[1]["U"]["surface"] | ||
|
|
||
| fieldset = FieldSet.from_data( | ||
| {"U": 0, "V": 0}, | ||
| { | ||
| "lon": 0, | ||
| "lat": 0, | ||
| "time": [np.datetime64("1950-01-01") + np.timedelta64(632160, "h")], | ||
| "V": v, | ||
| "U": u, | ||
| }, | ||
| { | ||
| "lat": np.array([expected_obs[0]["lat"], expected_obs[1]["lat"]]), | ||
| "lon": np.array([expected_obs[0]["lon"], expected_obs[1]["lon"]]), | ||
| "depth": np.array([MAX_DEPTH, MIN_DEPTH]), | ||
| "time": np.array( | ||
| [ | ||
| np.datetime64(expected_obs[0]["time"]), | ||
| np.datetime64(expected_obs[1]["time"]), | ||
| ] | ||
| ), | ||
| }, | ||
| ) | ||
|
|
||
| sample_points = [Spacetime(Location(0, 0), 0)] | ||
| # perform simulation | ||
| out_path = tmpdir.join("out.zarr") | ||
|
|
||
| simulate_adcp( | ||
| fieldset=fieldset, | ||
| out_file_name="test", | ||
| out_path=out_path, | ||
| max_depth=MAX_DEPTH, | ||
| min_depth=MIN_DEPTH, | ||
| bin_size=BIN_SIZE, | ||
| num_bins=NUM_BINS, | ||
| sample_points=sample_points, | ||
| ) | ||
|
|
||
| results = xr.open_zarr(out_path) | ||
|
|
||
| # test if output is as expected | ||
| assert len(results.trajectory) == NUM_BINS | ||
|
|
||
| # for every obs, check if the variables match the expected observations | ||
| # we only verify at the surface and max depth of the adcp, because in between is tricky | ||
| for traj, vert_loc in [ | ||
| (results.trajectory[0], "max_depth"), | ||
| (results.trajectory[-1], "surface"), | ||
| ]: | ||
| obs_all = results.sel(trajectory=traj).obs | ||
| assert len(obs_all) == len(sample_points) | ||
| for i, (obs_i, exp) in enumerate(zip(obs_all, expected_obs, strict=True)): | ||
| obs = results.sel(trajectory=traj, obs=obs_i) | ||
| for var in ["lat", "lon"]: | ||
| obs_value = obs[var].values.item() | ||
| exp_value = exp[var] | ||
| assert np.isclose( | ||
| obs_value, exp_value | ||
| ), f"Observation incorrect {vert_loc=} {obs_i=} {var=} {obs_value=} {exp_value=}." | ||
| for var in ["V", "U"]: | ||
| obs_value = obs[var].values.item() | ||
| exp_value = exp[var][vert_loc] | ||
| assert np.isclose( | ||
| obs_value, exp_value | ||
| ), f"Observation incorrect {vert_loc=} {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.