MHKiT-Software / MHKiT-MATLAB

MHKiT-MATLAB provides the marine renewable energy (MRE) community tools for data processing, visualization, quality control, resource assessment, and device performance.
https://mhkit-software.github.io/MHKiT/
BSD 3-Clause "New" or "Revised" License
15 stars 23 forks source link

MHKiT-MATLAB WPTO Hindcast Returns 503 Service Unavailable while MHKiT-Python Working Just Fine with the same API #106

Open radityadanu opened 11 months ago

radityadanu commented 11 months ago

Describe the bug: I am using the same API for the exact location, requested parameter, and year, as mentioned in the example both using python and MATLAB. The Python module worked just fine, whereas the MATLAB module returned with 503 Service unavailable: Error using Matlab.internal.webservices.HTTPConnector/copyContentToByteArray The server returned the status 503 with message "Service Unavailable" in response to the request to URL https://developer.nrel.gov/api/hsds/?api_key=qiaxEl3sgvbvvolnjTLelLTw2zZHPbvwu7uXJaKh&domain=%2Fnrel%2FUS_wave%2FWest_Coast%2FWest_Coast_wave_1995.h5.

Error in readContentFromWebService (line 46) byteArray = copyContentToByteArray(connection);

Error in webread (line 125) [varargout{1:nargout}] = readContentFromWebService(connection, options);

Error in request_wpto (line 72) root = webread(baseURL);

Unfortunately, I have to use MATLAB to streamline all the data traffic for the current project. Would you happen to have any idea to fix this?

To reproduce: Using the same code provided in the example, just with a different API key:

data_type = '3-hour'; % setting the data type to the 3-hour dataset year = (1995)'; lat_lon = [44.624076,-124.280097]; parameter = "significant_wave_height"; api_key ='qiaxEl3sgvbvvolnjTLelLTw2zZHPbvwu7uXJaKh';

Hs = request_wpto(data_type,parameter,lat_lon,year,api_key);

Expected behavior: Returning struct file with Hs in timeseries format.

Hs.metadata

ans = struct with fields: water_depth: 77.429496765136719 latitude: 44.624298095703125 longitude: -1.242789993286133e+02 distance_to_shore: 1.562217578125000e+04 timezone: -8 jurisdiction: 'Federal'

Desktop: Windows Ver: Win 10 Matlab Ver: 2023a Python Ver: 3.10 Mhkit Ver: 0.6.0

Thank you! Danu

rpauly18 commented 11 months ago

Hi Danu,

Thank you for reporting this issue. We will look into it and develop a fix.

Thanks!

simmsa commented 2 months ago

Hi @radityadanu, we were experiencing a similar issue where our unit tests would fail or take a long time when accessing the hindcast data from developer.nrel.gov.

As a workaround we have implemented a caching feature for all hindcast data calls that makes requesting hindcast data more robust and reliable. Running request_wpto now outputs the status of each request to the user. Example output is below.

Cache is older than 1 day. Redownloading...
Cache of url not found! Performing webread for url:
https://developer.nrel.gov/api/hsds/?api_key=<redacted>&domain=%2Fnrel%2FUS_wave%2Fvirtual_buoy%2FWest_Coast%2FWest_Coast_virtual_buoy_1995.h5

This workaround has two advantages and may improve your individual situation. Firstly, we display the status of the request, giving you visibility into what is being downloaded. Secondly, we cache the result from the hindcast download, speeding up workflows that download the data frequently.

Your code section should now, more reliably, run to completion:

% Start timing
tic;

data_type = '3-hour';
year = (1995)';
lat_lon = [44.624076,-124.280097]; 
parameter = "significant_wave_height";

Hs = request_wpto(data_type,parameter,lat_lon,year,api_key);

% Stop timing
elapsed_time = toc;

fprintf('Elapsed time: %.2f seconds\n', elapsed_time);

I did a test on my local machine and the above code worked and output the following:

Elapsed time: 1139.78 seconds

This seems relatively slow, but it does work. Can you let us know if this fix is sufficient for your use case?