Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Wind Diagnostics API #53

Merged
merged 20 commits into from
Jul 13, 2022
Merged

Wind Diagnostics API #53

merged 20 commits into from
Jul 13, 2022

Conversation

esheehan-gsl
Copy link
Contributor

Define the API for fetching wind speed and direction diagnostics.

Just a basic test to get things rolling, all we do is assert that the
endpoint responds with a 200 (which it fails, because the endpoint
doesn't exist yet).
@esheehan-gsl esheehan-gsl self-assigned this Jul 7, 2022
@esheehan-gsl esheehan-gsl mentioned this pull request Jul 7, 2022
7 tasks
@esheehan-gsl esheehan-gsl temporarily deployed to vlab July 7, 2022 20:54 Inactive
I think I like having access to everything in the mock submodule and
having it namespaced so we call mock.patch instead of just patch.
I created a dataclass to represent diagnostic data for vector data so
that we can benefit from more type checking. This means the
responsibility for converting the dataclass into JSON is the
responsibility of the API. Right now the diag.wind function doesn't do
anything other than return an empty VectorDiag object.
Following the same pattern we used for temperature (which needs to be
refactored), diag.wind takes a minimization loop as an argument and the
route calls it twice to retrieve the guess and analysis loops to build
the response.
@esheehan-gsl esheehan-gsl temporarily deployed to vlab July 12, 2022 15:31 Inactive
I think it makes more sense to send wind data as direction and magnitude
rather than u, v vectors, since most of what we'll be visualizaing is
direction and magnitude. I'm also going to send the obs and forecast
values, instead of obs - forecast, since some of the vector
visualization will actually show both vectors, not just the difference.
It might be a good idea to use a similar format for scalar values, since
it gives us more flexibility; we'll have to see how large these
responses are.
I think having patch namespaced in the mock module help readability,
plus it makes it easier to get to other things from the mock module,
like call or create_autospec if needed.
I've tested the happy path for the diag.wind function to ensure that it
requests the correct diagnostic data and uses the correct dimensions of
the Dataset to properly construct the return value. There's a lot of
mocking going on in here, and it makes me worried that these tests are
fragile, but it does fully isolate this test from its collaborators.
@esheehan-gsl esheehan-gsl temporarily deployed to vlab July 12, 2022 21:22 Inactive
Switch to using pytest's built in tmp_path fixture for defining the
DIAG_DIR config variable. This way we have a temporary directory where
we can write NetCDF files for use in testing.
I've added a function that will take the variable and minimization loop
as parameters and return a Dataset read from the corresponding NetCDF
file. I'll need to refactor get_diagnostic into something that is
specific to fetching temperature and makes use of this function.

I have two placeholder tests for handling missing or bad input files for
open_diagnostic.
Raise a FileNotFoundError in open_diagnostic if the file does not exist.
@esheehan-gsl esheehan-gsl temporarily deployed to vlab July 13, 2022 15:32 Inactive
This might be overkill, but I think it might be worth documenting what
the expected message is just so that we know what to look for in logs.
I don't think we want the diag.wind method to handle exceptions from
diag.open_diagnostic, this way the routes can decide how best to handle
errors in the request with appropriate HTTP status codes.
Since we're letting exceptions bubble up, we can guarantee that if a
function returns, it has a value.
Respond with a 404 if the diagnostics files can't be found and a 500 if
the files can't be read.
I don't want to forget to implement this. I should've added this test
when I stubbed the method.
@esheehan-gsl esheehan-gsl temporarily deployed to vlab July 13, 2022 17:51 Inactive
Calculate direction and magnitude of a vector from the (u, v) vectors we
have in our diag files and create a new VectorVariable object from them.
I've checked my test cases against from the NCL documentation:

http://ncl.ucar.edu/Document/Functions/Contributed/wind_direction.shtml

The only difference is that we end up reporting a direction of 90° for a
vector of (0, 0) instead of a direction of 0°. This may end up
mattering, but I think it will be ok, since we'll be internally
consistent when we calculate the Obs - Fcst.
Rather than dealing with np.arctan2's 0.0 vs -0.0 silliness, We go
through and set the direction for all vectors with a magnitude of 0 to
0°. This is consistent with the NCL wind_function.
@esheehan-gsl esheehan-gsl temporarily deployed to vlab July 13, 2022 20:38 Inactive
@esheehan-gsl esheehan-gsl marked this pull request as ready for review July 13, 2022 20:49
@esheehan-gsl esheehan-gsl merged commit 1056b4b into main Jul 13, 2022
@esheehan-gsl esheehan-gsl deleted the wind-diag-api branch July 13, 2022 20:54
@esheehan-gsl esheehan-gsl temporarily deployed to vlab July 13, 2022 20:54 Inactive
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant