webread with json parameters

조회 수: 8 (최근 30일)
Filippo Natoli
Filippo Natoli 2020년 12월 9일
댓글: Filippo Natoli 2020년 12월 9일
Hello,
I am trying to dowload climatic data from the ACIS Web Services (http://www.rcc-acis.org/docs_webservices.html). Specifically, I would need data for the average temperatures, at daily frequency, for each US state from 1960 until today.
My json parameter vector should be the following (e.g., for DC),
'{"state":"dc","sdate":"1960-01-01","edate":"2020-10-01","grid":"1","elems":[{"name":"avgt","interval":"dly","duration":"dly","area_reduce":"state_mean"}]}'
In Matlab I tried to use the webread function as follows
url = 'http://data.rcc-acis.org/GridData';
D1 = datetime(1999,01,01,'Format','yyyy-MM-dd');
D2 = datetime(2020,01,01,'Format','yyyy-MM-dd');
opzelem=['name','avgt','interval','dly','duration','dly','area_reduce','state_mean'];
scarico = webread(url,'state','DC','sdate',D1,'edate',D2,'grid','1','elems',opzelem);
but this returns the following error:
Error using readContentFromWebService (line 46)
The server returned the status 400 with message "Bad Request" in response to the request to URL
http://data.rcc-acis.org/GridData?state=DC&sdate=1999-01-01&edate=2020-01-01&grid=1&elems=nameavgtintervaldlydurationdlyarea_reducestate_mean.
Basically, I do not know how to include the "elems" options into webread. Using the jsondecode function to construct a struct array from the json parameter vector does not work either because webread only admits name-value pairs, not structures. Do you have any idea of how to solve it? thank you very much

채택된 답변

Ive J
Ive J 2020년 12월 9일
elems itself should be added as a nested struct to the input data, try this
headers = {'Content-Type' 'application/json'; 'Accept' 'application/json'};
options = weboptions('HeaderFields', headers, 'Timeout', 5000);
data = struct('state', "dc", 'sdate', "1990101", 'edate', "20200101", 'grid', "3");
data.elems = {struct('name', "maxt",'area_reduce', "state_mean")};
scarico = webwrite('http://data.rcc-acis.org/GridData', data, options);
  댓글 수: 1
Filippo Natoli
Filippo Natoli 2020년 12월 9일
Thanks Ive Ive, it works perfectly!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Web Services에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by