Skip to content

fix (ship): use 'is None' so Boat.get_weather_data() actually caches the weather dataset - #214

Closed
amanbn8 wants to merge 1 commit into
52North:mainfrom
amanbn8:fix/weather-data-cache-none-check
Closed

amanbn8 wants to merge 1 commit into
52North:mainfrom
amanbn8:fix/weather-data-cache-none-check

Conversation

@amanbn8

@amanbn8 amanbn8 commented Sep 30, 2026 •

Copy link
Copy Markdown

Related Issue / Discussion:

Fixes Issue #213

Changes:

  • Modified WeatherRoutingTool/ship/ship.py: Boat.get_weather_data() now checks self.weather_data is None instead of == None.
  • Modified tests/test_ship.py: new test test_get_weather_data_opens_file_only_once that wraps xr.open_dataset with a counter, calls get_weather_data() twice on a ConstantFuelBoat, and checks that the file is opened once and the same Dataset object is returned both times.

Further Details:

Summary:

get_weather_data() is documented as opening the weather NetCDF once and caching it. The guard compared an xarray.Dataset to None with ==, which xarray evaluates element-wise and returns a Dataset that is truthy. So after the first call the condition stayed true and the file was reopened and fully loaded on every call, from every routing step, for both the isobased and genetic algorithms.

Before: xr.open_dataset is called once per get_weather_data() call, and each call returns a new Dataset.

After: the file is opened once per Boat instance and the cached Dataset is returned on later calls, which is what the docstring already describes.

The new test fails on main (two calls to open_dataset, two different objects) and passes with the fix. flake8 runs clean on both files with the repo's settings.

Dependencies:

None.

PR Checklist:

In the context of this PR, I:

…the dataset

Comparing an xarray.Dataset to None with '==' is element-wise and yields a
truthy Dataset, so the cache guard was true on every call and the weather
file was reopened and fully loaded on every routing step.

Add a regression test that counts xr.open_dataset calls across repeated
get_weather_data() calls.
@kdemmich

Copy link
Copy Markdown
Collaborator

Dear user,
thank you for your interest in contributing to our WRT! I would like to point you to our guidelines for software contributions which require to follow 52 North's CLA guidelines for all PRs and, in particular, fill the 52°North Contributor License Agreement. As long as this has not been filled, I can not assign you to any issues or merge any PRs. Please don't hesitate to contact me in case of related questions.

@kdemmich kdemmich added the question Further information is requested label Oct 12, 2026
@amanbn8

amanbn8 commented Oct 16, 2026

Copy link
Copy Markdown
Author

Hi @kdemmich, thanks for the pointer and for taking a look.

I read through the CLA guidelines, and as far as I can tell the agreement has to be printed, signed and sent by post to 52°North in Germany. I'm a student in India and that's unfortunately not really feasible for me, so I won't be able to complete the CLA in its current form. If an electronically signed scan sent by email would be acceptable, let me know and I'll send one right away.

Otherwise, feel free to close this PR. Thanks again!

@amanbn8
amanbn8 force-pushed the fix/weather-data-cache-none-check branch from 9e927a0 to 02fee5c Compare October 16, 2026 19:18
@kdemmich

Copy link
Copy Markdown
Collaborator

Dear @amanbn8,
thanks a lot for your enthusiasm in contributing to our routing tool. As stated in the CLA guidelines, the printed copies of the CLA are required. I will thus close your PR.

@kdemmich kdemmich closed this Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

question Further information is requested

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants