Hi, I am trying to fetch weather data form XML (Finnish weather service - https://en.ilmatieteenlaitos.fi/open-data-manual-fmi-wfs-services) to JMP in a way that the user could define the time period. My code does not work, I always get the "Invalid HTTP request state" error message.
// User-defined start and end dates (modify as needed)
startDate = "2024-07-10";
endDate = "2024-07-24";
// Construct the URL with user-defined dates and parameters
url = "https://opendata.fmi.fi/wfs?service=WFS&version=2.0.0&request=getFeature&storedquery_id=fmi::observations::weather::timevaluepair&place=helsinki-vantaa_airport×tep=1&starttime=" || startDate || "T00:00:00Z&endtime=" || endDate || "T23:59:59Z¶meters=temperature%2Chumidity%2Cpressure";
// Create HTTP request object
httpRequest = New HTTP Request();
// Send HTTP GET request
response = httpRequest << Send("GET", url);
// Check if the request was successful
If(response << Get Status Code == 200,
// Parse the XML response
xmlResponse = response << Get As XML,
// Extract data from XML response
dataNodes = xmlResponse << XPath("//wml2:MeasurementTVP"),
// Initialize lists to store data
temperatureList = {},
humidityList = {},
pressureList = {},
// Loop through each data node and extract values
ForEach(node in dataNodes,
// Extract timestamp
timestamp = node << XPath("wml2:time"),
// Extract temperature value
temperature = node << XPath("wml2:value[@parameter='temperature']"),
temperatureValue = temperature << NodeText,
// Extract humidity value
humidity = node << XPath("wml2:value[@parameter='humidity']"),
humidityValue = humidity << NodeText,
// Extract pressure value
pressure = node << XPath("wml2:value[@parameter='pressure']"),
pressureValue = pressure << NodeText,
// Convert timestamp to JMP DateTime format
timestampDT = Parse DateTime(timestamp, "YYYY-MM-DDThh:mm:ssZ"),
// Append data to lists
Append(temperatureList, {timestampDT, As Number(temperatureValue)}),
Append(humidityList, {timestampDT, As Number(humidityValue)}),
Append(pressureList, {timestampDT, As Number(pressureValue)})
);
// Create a new JMP data table and add columns
dt = New Table("Helsinki-Vantaa Weather Data",
Add Rows(Num Rows(temperatureList)),
New Column("Timestamp", Numeric, "DateTime"),
New Column("Temperature (°C)", Numeric, "Continuous", Format(3)),
New Column("Relative Humidity (%)", Numeric, "Continuous", Format(3)),
New Column("Pressure (hPa)", Numeric, "Continuous", Format(3))
);
// Populate the data table with values
dt:"Timestamp" << Set Values(temperatureList);
dt:"Temperature (°C)" << Set Values(humidityList);
dt:"Relative Humidity (%)" << Set Values(pressureList);
dt:"Pressure (hPa)" << Set Values(pressureList);
// Show the data table
dt << Show;
,
// Display error if request failed
Throw("Failed to fetch data. HTTP status code: " || Char(response << Get Status Code))
);