-
Notifications
You must be signed in to change notification settings - Fork 10
Ctd cleanup and test #38
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
10 commits
Select commit
Hold shift + click to select a range
b77c397
----use path for ctd
surgura 249e3c7
codetools
surgura 2f59d5d
fix sailship
surgura 987a8a7
wip
surgura fcb2fa1
ctd works
surgura 70e25d4
fix
surgura fc1463b
fix sailship test
surgura 0c98eea
.
surgura 802198c
create test fieldset, fix max depth bug
surgura 7274ed5
fix test
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,38 +1,138 @@ | ||
| """Test the simulation of CTD instruments.""" | ||
| """ | ||
| Test the simulation of CTD instruments. | ||
|
|
||
| Fields are kept static over time and time component of CTD measurements is not tested tested because it's tricky to provide expected measurements. | ||
| """ | ||
|
|
||
| import datetime | ||
| from datetime import timedelta | ||
|
|
||
| import numpy as np | ||
| from parcels import FieldSet | ||
| import py | ||
| import xarray as xr | ||
| from parcels import Field, FieldSet | ||
|
|
||
| from virtual_ship import Location, Spacetime | ||
| from virtual_ship.instruments.ctd import CTD, simulate_ctd | ||
|
|
||
|
|
||
| def test_simulate_ctds() -> None: | ||
| def test_simulate_ctds(tmpdir: py.path.LocalPath) -> None: | ||
| # arbitrary time offset for the dummy fieldset | ||
| base_time = datetime.datetime.strptime("1950-01-01", "%Y-%m-%d") | ||
|
|
||
| # where to cast CTDs | ||
| ctds = [ | ||
| CTD( | ||
| spacetime=Spacetime( | ||
| location=Location(latitude=0, longitude=1), | ||
| time=base_time + datetime.timedelta(hours=0), | ||
| ), | ||
| min_depth=0, | ||
| max_depth=float("-inf"), | ||
| ), | ||
| CTD( | ||
| spacetime=Spacetime( | ||
| location=Location(latitude=1, longitude=0), | ||
| time=base_time + datetime.timedelta(minutes=5), | ||
| ), | ||
| min_depth=0, | ||
| max_depth=float("-inf"), | ||
| ), | ||
| ] | ||
|
|
||
| # expected observations for ctds at surface and at maximum depth | ||
| ctd_exp = [ | ||
| { | ||
| "surface": { | ||
| "salinity": 5, | ||
| "temperature": 6, | ||
| "lat": ctds[0].spacetime.location.lat, | ||
| "lon": ctds[0].spacetime.location.lon, | ||
| }, | ||
| "maxdepth": { | ||
| "salinity": 7, | ||
| "temperature": 8, | ||
| "lat": ctds[0].spacetime.location.lat, | ||
| "lon": ctds[0].spacetime.location.lon, | ||
| }, | ||
| }, | ||
| { | ||
| "surface": { | ||
| "salinity": 5, | ||
| "temperature": 6, | ||
| "lat": ctds[1].spacetime.location.lat, | ||
| "lon": ctds[1].spacetime.location.lon, | ||
| }, | ||
| "maxdepth": { | ||
| "salinity": 7, | ||
| "temperature": 8, | ||
| "lat": ctds[1].spacetime.location.lat, | ||
| "lon": ctds[1].spacetime.location.lon, | ||
| }, | ||
| }, | ||
| ] | ||
|
|
||
| # create fieldset based on the expected observations | ||
| # indices are time, depth, latitude, longitude | ||
| u = np.zeros((2, 2, 2, 2)) | ||
| v = np.zeros((2, 2, 2, 2)) | ||
| t = np.zeros((2, 2, 2, 2)) | ||
| s = np.zeros((2, 2, 2, 2)) | ||
|
|
||
| t[:, 1, 0, 1] = ctd_exp[0]["surface"]["temperature"] | ||
| t[:, 0, 0, 1] = ctd_exp[0]["maxdepth"]["temperature"] | ||
| t[:, 1, 1, 0] = ctd_exp[1]["surface"]["temperature"] | ||
| t[:, 0, 1, 0] = ctd_exp[1]["maxdepth"]["temperature"] | ||
|
|
||
| s[:, 1, 0, 1] = ctd_exp[0]["surface"]["salinity"] | ||
| s[:, 0, 0, 1] = ctd_exp[0]["maxdepth"]["salinity"] | ||
| s[:, 1, 1, 0] = ctd_exp[1]["surface"]["salinity"] | ||
| s[:, 0, 1, 0] = ctd_exp[1]["maxdepth"]["salinity"] | ||
|
|
||
| fieldset = FieldSet.from_data( | ||
| {"U": 0, "V": 0, "T": 0, "S": 0, "bathymetry": 100}, | ||
| {"V": v, "U": u, "T": t, "S": s}, | ||
| { | ||
| "lon": 0, | ||
| "lat": 0, | ||
| "time": [np.datetime64("1950-01-01") + np.timedelta64(632160, "h")], | ||
| "time": [ | ||
| np.datetime64(base_time + datetime.timedelta(hours=0)), | ||
| np.datetime64(base_time + datetime.timedelta(hours=1)), | ||
| ], | ||
| "depth": [-1000, 0], | ||
| "lat": [0, 1], | ||
| "lon": [0, 1], | ||
| }, | ||
| ) | ||
| fieldset.add_field(Field("bathymetry", [-1000], lon=0, lat=0)) | ||
|
|
||
| min_depth = -fieldset.U.depth[0] | ||
| max_depth = -fieldset.U.depth[-1] | ||
|
|
||
| ctds = [ | ||
| CTD( | ||
| spacetime=Spacetime(location=Location(latitude=0, longitude=0), time=0), | ||
| min_depth=min_depth, | ||
| max_depth=max_depth, | ||
| ) | ||
| ] | ||
| # perform simulation | ||
| out_path = tmpdir.join("out.zarr") | ||
|
|
||
| simulate_ctd( | ||
| ctds=ctds, | ||
| fieldset=fieldset, | ||
| out_file_name="test", | ||
| out_path=out_path, | ||
| outputdt=timedelta(seconds=10), | ||
| ) | ||
|
|
||
| # test if output is as expected | ||
| results = xr.open_zarr(out_path) | ||
|
|
||
| assert len(results.trajectory) == len(ctds) | ||
|
|
||
| for ctd_i, (traj, exp_bothloc) in enumerate( | ||
| zip(results.trajectory, ctd_exp, strict=True) | ||
| ): | ||
| obs_surface = results.sel(trajectory=traj, obs=0) | ||
| min_index = np.argmin(results.sel(trajectory=traj)["z"].data) | ||
| obs_maxdepth = results.sel(trajectory=traj, obs=min_index) | ||
|
|
||
| for obs, loc in [ | ||
| (obs_surface, "surface"), | ||
| (obs_maxdepth, "maxdepth"), | ||
| ]: | ||
| exp = exp_bothloc[loc] | ||
| for var in ["salinity", "temperature", "lat", "lon"]: | ||
| obs_value = obs[var].values.item() | ||
| exp_value = exp[var] | ||
| assert np.isclose( | ||
| obs_value, exp_value | ||
| ), f"Observation incorrect {ctd_i=} {loc=} {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
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.
Why is this variable in capitals? We normally use lowercase for
dtThere 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.
it is a constant, and in PEP8 constants have to be all caps