How to utilize an existing API to download weather data and use it for calculations in a MATLAB script?

25 vues (au cours des 30 derniers jours)
I want to create a script that can download files from the National Solar Radiation Database using their API (I have an API Key) and convert them to tables, cells, or structures in the workspace so that I can use the data for calculations in MATLAB. (I am designing a solar power system, and want the irradiance data to create an accurate estimate of how much power I can expect specific panels to produce in that location. The data also includes things like temperature, windpseed etc. which are extremely useful for designing the system). The only purpose of this question is to figure out how to pull the data using the API and store it in a structure, cell array, or table.
This link shows an example of the API, and it does achieve my goal of entering coordinates to recieve weather data from the nearest station. I've read some documentation on the "webread" function, but I'm still shaky on how to achieve this in matlab. The only output formats from the API are JSON and XML. I imagine that XLM could be read with "xlmread" function according to the documentation, but I haven't worked with API's enough to set up an example and verify. I believe I have every toolbox for the R2020a release.

Réponse acceptée

millercommamatt
millercommamatt le 8 Sep 2022
Modifié(e) : millercommamatt le 8 Sep 2022
Build a string that is structured like this and use it as the input to webread:
Use the information at the link you provided to specify the input variables you want.
e.g.
%% INPUTS (all are strings/char)
api_key = 'DEMO_KEY';
lat = '40';
lon = '-150';
radius = '32';
%% fetch the data
api_base = 'https://developer.nrel.gov/api/solar/data_query/v1.json?&all=1';
data_out_struct = webread(...
[ api_base '&api_key=' api_key '&lat=' lat '&lon=' lon '&radius=' radius]...
); % you'll get a struct out

Plus de réponses (1)

Vaishak
Vaishak le 25 Jan 2024
Below code is working for me, may be used as a reference:
% Parameters
Latitude = 40.7128; % Replace with actual latitude
Longitude = -74.0060; % Replace with actual longitude
year = '2019';
api_key = 'ENTER YOUR API CODE';
attributes = 'air_temperature,ghi,dhi';
leap_year = 'false';
interval = '15';
utc = 'false';
your_name = 'YOUR NAME';
reason_for_use = 'testing';
your_affiliation = 'researcher';
your_email = 'ENTER YOUR EMAIL';
mailing_list = 'true';
% Construct API URL
url = sprintf('https://developer.nrel.gov/api/nsrdb/v2/solar/msg-iodc-download.csv?wkt=POINT(%f%%20%f)&names=%s&leap_day=%s&interval=%s&utc=%s&full_name=%s&email=%s&affiliation=%s&mailing_list=%s&reason=%s&api_key=%s&attributes=%s', Longitude, Latitude, year, leap_year, interval, utc, your_name, your_email, your_affiliation, mailing_list, reason_for_use, api_key, attributes);
% Download and read CSV file
maxAttempts = 10;
attempt = 1;
while attempt <= maxAttempts
try
% Download CSV file
tempFolderName = tempname;
mkdir(tempFolderName);
outputFilePath = fullfile(tempFolderName, 'downloaded_data.csv');
websave(outputFilePath, url);
% Read the downloaded CSV file
data = readtable(outputFilePath);
% Display or process the data as needed
disp('File content:');
disp(data);
% Successful download and read
disp('CSV file downloaded and read successfully.');
break;
catch ME
fprintf('Error during download or CSV file read: %s\n', ME.message);
% Retry if attempts left
attempt = attempt + 1;
if attempt <= maxAttempts
pause(10);
disp('Retrying...');
else
error('Maximum number of download attempts reached. Unable to download the file.');
end
end
end

Catégories

En savoir plus sur Downloads dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by