Weather Generator – 50 points
This assignment consists of writing a library of static methods to generate and analyze weather forecast.
Refer to our Programming Assignments FAQ for instructions on how to install VSCode, how to use the command line and how to submit your assignments.
Start your assignment early.
Programming
We provide this ZIP FILE containing WeatherGenerator.java. For each problem update and submit on Autolab.
Observe the following rules:
- DO NOT use System.exit().
- DO NOT add the project or package statements.
- DO NOT change the class name.
- DO NOT change the headers of ANY of the given methods.
- DO NOT add any new class fields.
- ONLY display the result as specified by the example for each problem.
- DO NOT print other messages, follow the examples for each problem.
- USE StdIn, StdOut, StdRandom, and StdDraw libraries.
Overview
A weather generator produces a “synthetic” time series of weather data for a location based on
the statistical characteristics of observed weather at that location. You can think of a weather
generator as being a simulator of future weather based on observed past weather. A time series is
a collection of observations generated sequentially through time. The special feature of a time
series is that successive observations are usually expected to be dependent. In fact, this dependence
is often exploited in forecasting.
Since we are just beginning as weather forecasters, we will simplify our predictions to just
whether measurable precipitation will fall from the sky. If there is measurable precipitation,
we call it a “wet” day. Otherwise, we call it a “dry” day.
Weather Persistence
To help with understanding relationships and sequencing events through time, here’s a simple
pseudocode that shows what it means for precipitation to be persistent from one day to the next.
READ "Did it rain today?" IF answer is yes THEN There is a greater chance that tomorrow will be wet rather than dry ELSE There is a greater chance that tommorrow will be dry rather than wet ENDIF
Time of year and location
| January | February | March | April | May | June | July | August | September | October | November | December |
| 0.27 | 0.33 | 0.40 | 0.46 | 0.43 | 0.28 | 0.12 | 0.17 | 0.23 | 0.21 | 0.28 | 0.27 |
| January | February | March | April | May | June | July | August | September | October | November | December |
| 0.55 | 0.58 | 0.61 | 0.69 | 0.73 | 0.62 | 0.45 | 0.55 | 0.58 | 0.55 | 0.59 | 0.55 |
READ "Did it rain today?"
IF answer is yes THEN
READ a random value between 0 and 1
IF the random value is less than or equal to 0.45 THEN
No change! It is a wet day
ELSE
Change! It is a dry day
ENDIF
ELSE
READ a random value between 0 and 1
IF the random value is less than or equal 0.12 THEN
Change! It is a wet day
ELSE
No change! It is a dry day
ENDIF
ENDIF
- predict future precipitation pattern for one month: oneMonthGenerator
- find the number of wet or dry days in a given month’s forecast: numberOfWetDryDays
- find the longest wet or dry spell in a given month’s forecast: lengthOfLongestWetDrySpell
| Longitude | Latitude | January | February | March | April | May | June | July | August | September | October | November | December |
| -97.58 | 26.02 | 0.76 | 0.75 | 0.77 | 0.74 | 0.80 | 0.86 | 0.94 | 0.97 | 0.89 | 0.77 | 0.74 | 0.77 |
public class WeatherGenerator {
/* Given a location (longitude, latitude) in the USA and a month of the year, the method
* returns the forecast for the month based on the drywet and wetwet transition
* probabilities tables.
*
* month will be a value between 2 and 13: 2 corresponds to January, 3 corresponds to February
* and so on. These are the column indexes of each month in the transition probabilities tables.
*
* The first day of the month has a 50% chance to be a wet day, 0-0.49 (wet), 0.50-0.99 (dry)
*
* Use StdRandom.uniform() to generate a real number uniformly in [0,1)
*/
int[] oneMonthGenerator(double longitute, double latitude, int month, double[][] drywet, double[][] wetwet)
/* Returns the number of mode (WET or DRY) days in forecast. */
int numberOfWetDryDays (int[] forecast, int mode)
/*
* Analyzes the forecast array and returns the longest number of
* consecutive mode (which can be WET or DRY) days in forecast.
*/
int lengthOfLongestWetDrySpell (int[] forecast, int mode)
}
Use the main method as a driver to test your methods. To generate the weather for location at longitude -98.76 and latitude 26.70 for the month of February do:
java WeatherGenerator111 -98.76 26.70 3
public static void main (String[] args) {
int numberOfRows = 4001; // Total number of locations
int numberOfColumns = 14; // Total number of 14 columns in file
// File format: longitude, latitude, 12 months of transition probabilities
// Allocate and populate arrays that hold the transition probabilities
double[][] drywet = new double[numberOfRows][numberOfColumns];
double[][] wetwet = new double[numberOfRows][numberOfColumns];
populateTransitionProbabilitiesArrays(drywet, wetwet, numberOfRows);
/*** WRITE YOUR CODE BELLOW THIS LINE. DO NOT erase any of the lines above. ***/
// Read command line inputs
double longitute = Double.parseDouble(args[0]);
double latitude = Double.parseDouble(args[1]);
int month = Integer.parseInt(args[2]);
int[] forecast = oneMonthGenerator(longitute, latitude, month, drywet, wetwet);
int drySpell = lengthOfLongestSpell(forecast, DRY);
int wetSpell = lengthOfLongestSpell(forecast, WET);
StdOut.println("There are " + forecast.length + " days in the forecast for month " + month);
StdOut.println(drySpell + " days of dry spell.");
for ( int i = 0; i < forecast.length; i++ ) {
// This is the ternary operator. (conditional) ? executed if true : executed if false
String weather = (forecast[i] == WET) ? "Wet" : "Dry";
StdOut.println("Day " + (i+1) + " is forecasted to be " + weather);
}
}
Always read the main() method first, in any code. Here is a video to clarify the assignment.
Before submission
- Collaboration policy. Read our collaboration policy here.
- Update @author. Update the @author tag of the files with your name, email and netid.
- Submitting the assignment. Submit WeatherGenerator.java separately via the web submission system called Autolab. To do this, click the Assignments link from the course website; click the Submit link for that assignment.
Getting help
If anything is unclear, don’t hesitate to drop by office hours or post a question on Piazza. Find instructors office hours by clicking the Staff link from the course website. In addition to office hours we have the CAVE (Collaborative Academic Versatile Environment), a community space staffed with lab assistants which are undergraduate students further along the CS major to answer questions.