Commit 6768b54c authored by Christopher Bohn's avatar Christopher Bohn 🤔
Browse files

Added ability to handle the "forecast" data set

parent 39e3e66a
......@@ -83,9 +83,10 @@ code that uses data from [OpenWeathermap.org](https://openweathermap.org).
- `OpenWeatherConnector` only supports the free, no-cost APIs provided by
[OpenWeathermap.org](https://openweathermap.org). We do not have plans to
support the subscription-based APIs.
- As of this writing, `OpenWeatherConnector` only supports the *"weather"* and
*"air_pollution"* data sets. We plan to add support for the *"onecall"* and
*"forecast"* data sets.
- As of this writing, `OpenWeatherConnector` only supports the *"weather"*,
*"forecast*, *"air_pollution"*, *"air_pollution/forecast"*, and
*"air_pollution/history"* data sets. We plan to add support for the
*"onecall"* data set.
- We may add support for the
[Geocoding API](https://openweathermap.org/api/geocoding-api) (*"direct"* and
*"reverse"* data sets).
......
......@@ -103,7 +103,21 @@ public class Demonstration {
break;
case "forecast":
timestamp = weather.getTimeStamps().get(weather.getTimeStamps().size() / 2);
System.out.println("At " + timestamp + " it will be ");
System.out.println("At " + timestamp + " it will be " + weather.getWeatherCategories(timestamp));
System.out.println("Specifically, it will be " + weather.getWeatherDescriptions(timestamp));
System.out.println("The temperature will be " + weather.getTemperature(timestamp) + "K");
System.out.println("Factoring in the relative humidity of " + weather.getHumidity(timestamp) + "%, " +
"it will feel like " + weather.getFeelsLike(timestamp) + "K");
System.out.println("That probably also factored in winds, which will be " +
weather.getWindSpeed(timestamp) + "m/s from " + weather.getWindDirection(timestamp) +
"˚, gusting to " + weather.getWindGust(timestamp) + "m/s");
System.out.println("Visibility will be " + weather.getVisibility(timestamp) + "m");
System.out.println("The pressure will be " + weather.getPressure(timestamp) + "hPa");
System.out.println("Cloud cover will be " + weather.getCloudCover(timestamp) + "%");
System.out.println("There will be a " + weather.getProbabilityOfPrecipitation(timestamp) * 100 + "% " +
"chance of precipitation, resulting in " + weather.getThreeHourRainfall(timestamp) + "mm of " +
"rain and " + weather.getThreeHourSnowfall(timestamp) + "mm of snow in the three hours before" +
" " + timestamp);
break;
case "air_pollution":
System.out.println("The current air quality is " + weather.getAirQualityIndex() +
......@@ -136,9 +150,9 @@ public class Demonstration {
break;
}
System.out.print(System.lineSeparator() + "Do you wish to save this data to a file (Y)/N? ");
System.out.print(System.lineSeparator() + "Do you wish to save this data to a file Y/(N)? ");
String answer = scanner.nextLine();
if (answer.equals("") || answer.toUpperCase().charAt(0) != 'N') {
if (answer.length() > 0 && answer.toUpperCase().charAt(0) == 'Y') {
System.out.print("What shall the file's name be? ");
String filename = scanner.nextLine();
if (!filename.endsWith(".json")) {
......
package edu.unl.cse.soft160.json_connections;
import edu.unl.cse.soft160.json_connections.connector.OpenWeatherConnector;
import org.json.simple.JSONObject;
import org.junit.Before;
import org.junit.Test;
......@@ -11,7 +10,7 @@ import java.util.List;
import static org.junit.Assert.assertEquals;
public class AirPollution_UnlNov24ToNov27 {
public class AirPollution_UnlNov24ToNov27Test {
private OpenWeatherConnector connector;
private List<Date> timestamps;
......
package edu.unl.cse.soft160.json_connections;
import edu.unl.cse.soft160.json_connections.connector.OpenWeatherConnector;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import static org.junit.Assert.assertEquals;
public class Forecast_UnlNov7ToNov12Test {
private OpenWeatherConnector connector;
private Date Sunday, Monday, Tuesday, Wednesday, Thursday, Friday;
@Before
public void setup() throws IOException {
connector = new OpenWeatherConnector("forecast");
connector.retrieveData("forecasted_weather-unl-nov7-nov12.json");
List<Date> timestamps = connector.getTimeStamps();
Sunday = timestamps.get(0);
Monday = timestamps.get(8);
Tuesday = timestamps.get(16);
Wednesday = timestamps.get(24);
Thursday = timestamps.get(32);
Friday = timestamps.get(39);
}
@Test
public void testLocation() {
double expectedLatitude = 40.8206;
double expectedLongitude = -96.6928;
double actualLatitude = connector.getLatitude();
double actualLongitude = connector.getLongitude();
assertEquals(expectedLatitude, actualLatitude, 0.0001);
assertEquals(expectedLongitude, actualLongitude, 0.0001);
}
@Test
public void testWeather() {
Date timestamp = Sunday;
List<OpenWeatherConnector.WeatherCategory> expectedCategories = List.of(
OpenWeatherConnector.WeatherCategory.CLEAR
);
List<String> expectedDescriptions = List.of("clear sky");
int expectedListSize = 1;
List<OpenWeatherConnector.WeatherCategory> actualCategories = connector.getWeatherCategories(timestamp);
List<String> actualDescriptions = connector.getWeatherDescriptions(timestamp);
assertEquals(expectedListSize, actualCategories.size());
assertEquals(expectedListSize, actualDescriptions.size());
assertEquals(expectedCategories, actualCategories);
assertEquals(expectedDescriptions, actualDescriptions);
}
@Test
public void testMainObservations() {
Date timestamp = Monday;
long expectedVisibility = 10000;
double expectedAmbientTemperature = 285.91;
long expectedHumidity = 70;
double expectedFeelsLikeTemperature = 285.07;
long expectedPressure = 1016;
long actualVisibility = connector.getVisibility(timestamp);
double actualAmbientTemperature = connector.getTemperature(timestamp);
long actualHumidity = connector.getHumidity(timestamp);
double actualFeelsLikeTemperature = connector.getFeelsLike(timestamp);
long actualPressure = connector.getPressure(timestamp);
assertEquals(expectedVisibility, actualVisibility);
assertEquals(expectedAmbientTemperature, actualAmbientTemperature, 0.0001);
assertEquals(expectedHumidity, actualHumidity);
assertEquals(expectedFeelsLikeTemperature, actualFeelsLikeTemperature, 0.0001);
assertEquals(expectedPressure, actualPressure);
}
@Test
public void testWinds() {
Date timestamp = Tuesday;
long expectedDirection = 357;
double expectedSpeed = 5.6;
double expectedGust = 9.73;
long actualDirection = connector.getWindDirection(timestamp);
double actualSpeed = connector.getWindSpeed(timestamp);
double actualGust = connector.getWindGust(timestamp);
assertEquals(expectedDirection, actualDirection);
assertEquals(expectedSpeed, actualSpeed, 0.0001);
assertEquals(expectedGust, actualGust, 0.0001);
}
@Test
public void testClouds() {
Date timestamp = Friday;
long expectedValue = 48;
long actualValue = connector.getCloudCover(timestamp);
assertEquals(expectedValue, actualValue);
}
@Test
public void testPrecipitationWhenNoRain() {
Date timestamp = Thursday;
double expectedProbability = 0.0;
double expected3HourRain = 0.0;
double expected3HourSnow = 0.0;
double actualProbability = connector.getProbabilityOfPrecipitation(timestamp);
double actual3HourRain = connector.getThreeHourRainfall(timestamp);
double actual3HourSnow = connector.getThreeHourSnowfall(timestamp);
assertEquals(expectedProbability, actualProbability, 0.0001);
assertEquals(expected3HourRain, actual3HourRain, 0.0001);
assertEquals(expected3HourSnow, actual3HourSnow, 0.0001);
}
@Test
public void testPrecipitationWhenRainForecasted() {
Date timestamp = Wednesday;
double expectedProbability = 0.27;
double expected3HourRain = 0.18;
double expected3HourSnow = 0.0;
double actualProbability = connector.getProbabilityOfPrecipitation(timestamp);
double actual3HourRain = connector.getThreeHourRainfall(timestamp);
double actual3HourSnow = connector.getThreeHourSnowfall(timestamp);
assertEquals(expectedProbability, actualProbability, 0.0001);
assertEquals(expected3HourRain, actual3HourRain, 0.0001);
assertEquals(expected3HourSnow, actual3HourSnow, 0.0001);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment