MHKiT-Python icon indicating copy to clipboard operation
MHKiT-Python copied to clipboard

NOAA data request

Open jmcvey3 opened this issue 2 years ago • 0 comments

Describe the bug:

Two bugs in mhkit.tidal.io.noaa.request_noaa_data :

  1. Imprecise error handling on line 139, where this error line appears to be bypassed. It would be useful to raise an Exception instead of a print statement, and pass-through the exact error to the user:
Data request URL:  https://tidesandcurrents.noaa.gov/api/datagetter?begin_date=20171113&end_date=20171118&station=9446484&product=water_level&units=metric&time_zone=gmt&application=web_services&format=xml
Traceback (most recent call last):

  File "C:\Users\mcve343\Anaconda3\lib\site-packages\spyder_kernels\py3compat.py", line 356, in compat_exec
    exec(code, globals, locals)

  File "c:\users\mcve343\onedrive - pnnl\documents\projects\mhkit-dolfyn\coops request issue.py", line 3, in <module>
    noaa.request_noaa_data('9446484', 'water_level', '20171113', '20171118')

  File "c:\users\mcve343\mhkit-python\mhkit\tidal\io\noaa.py", line 76, in request_noaa_data
    dataFrames[date_list[i]], metadata = _xml_to_dataframe(response)

  File "c:\users\mcve343\mhkit-python\mhkit\tidal\io\noaa.py", line 144, in _xml_to_dataframe
    df = pd.DataFrame(data[0].attrib, index=[0])

UnboundLocalError: local variable 'data' referenced before assignment
  1. This function can't handle (i.e. throws the above error) all of the keywords in https://api.tidesandcurrents.noaa.gov/api/prod/, where parameters like "water_level", "water_temperature", and "salinity" could all be useful.

To Reproduce:

This particular request is asking for water level data from the water level station in Tacoma, WA.

from mhkit.tidal.io import noaa

noaa.request_noaa_data('9446484', 'water_level', '20171113', '20171118')

Entering the "Data Request URL" from the error message into a web browser reveals the error code: <error> Wrong Datum: Datum cannot be null or empty ***station=9446484</error>

Adding &datum=MLLW to the end of the url ("Mean Low Low Water" is the standard datum for all tide height measurements) returns the expected response, a clip of which is pasted below:

<data>
  <metadata id="9446484" name="Tacoma" lat="47.2667" lon="-122.4133"/>
    <observations>
    <wl t="2017-11-13 00:00" v="2.551" s="0.022" f="0,0,0,0" q="v"/>
    <wl t="2017-11-13 00:06" v="2.486" s="0.023" f="0,0,0,0" q="v"/>
    <wl t="2017-11-13 00:12" v="2.423" s="0.025" f="0,0,0,0" q="v"/>

Expected behavior:

Pass through the exact error from the website to the user, as well as add the ability to run a number of keywords from the data request website.

Desktop (please complete the following information):

  • OS: windows 10
  • MHKiT Version: 0.6.0

Additional context:

It would also be helpful to update the docstring to include a list of all the metocean variables that the function can call, as well as any extra required parameters those variables require (e.g. - 'water_level' (required args: "datum")

jmcvey3 avatar Feb 14 '23 18:02 jmcvey3