Programming > Research Paper > Computer Science Engineering & Technology Java Programming. Weather Generator (40 points) (All)

Computer Science Engineering & Technology Java Programming. Weather Generator (40 points)

Document Content and Description Below

Computer Science Engineering & Technology Java Programming Question 3. Weather Generator (40 points) Overview A weather generator produces a "synthetic" time series of weather data for a locati... on 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 YouTube has literally thousands of videos about weather fronts and how they are connected to weather. This one from the UK has graphics that are supportive of the idea of persistence (though that word is not used). As you watch it, consider that whatever is causing weather today (high pressure and a warm mass of air creating a sunny, warm day or low pressure and a cool mass of air creating a cloudy, cool day) is possibly still going to be affecting weather tomorrow. This is the idea of persistence. Time of year and location Weather data depends on both the time of year and the location. This means that the probabilities used in the simulation need to be associated with both a location and a time of year. The table below lists the probabilities that a day will be wet given that the previous day was dry for each month for a weather station near Norman, OK. This table gives the probability of a change from dry to wet. These are "real" numbers that reflect how often the weather changed from dry to wet in that specific location, during the month indicated, over the 30-year period from 1970-2000. JanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember0.270.330.400.460.430.280.120.170.230.210.280.27 The next table lists the probabilities that a day will be wet given that the previous day was wet for the same weather station near Norman, OK. This table gives the probability that the weather remains wet from one day to the next. JanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember0.550.580.610.690.730.620.450.550.580.550.590.55 Armed with these probabilities, we can turn our simulation into a weather generator for this location. Here's what it would look like for July in Norman, OK. 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 If it's a dry day, we want the outcome to simulate "no change" 88% of the time and "change" 12% of the time. A common practice would be to use a random number generator to generate some value between 0 and 1. If the random value is less than .88, then there would be no change, and if it is greater than .88 then the weather changes to rain. If it's a wet day, we want to simulate "no change" 45% of the time and "change" 55% of the time. To have to do with our random number generator, we say there is "no change" if random number is less than .45 and a change to dry if it is greater. Weather generator Now it's time to generate some weather! Imagine you are a farmer. Does knowing the number of wet or dry days tell the whole story? Would the pattern be important? If so, what pattern would you like to see? How would you measure this pattern? The transition probabilities that we have used for Norman, OK are based on historical data, and you might use them to get a sense for the likelihood certain weather phenomena in the near future. For instance, a farmer might want to run many, many simulations to get an idea of the likelihood of going 20 or more days without rain, and the results might influence the crops that he or she plants. Just as we can base the transition probabilities on historical data, we can also base them on future predictions. For instance, the National Center for Atmospheric Research (NCAR) simulates weather as it responds to assumptions about how various "forcings" (e.g, greenhouse gasses) will evolve in the future. Typically, these models couple an atmospheric model with an ocean model, but more recent versions, the so-called Earth system models, incorporate more components including land use, sea and land ice, etc. The models can be used to predict future precipitation patterns and transition probabilities that are based on these forecasts, rather than past data. The weather generator methods you will be writing for this assignment will: 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 Future transition probability table as a 2D array The oneMonthGenerator method receives as arguments the transition probability tables (dry to wet, and wet to wet) as 2D arrays. Each table row corresponds to a location (longitude, latitude) in the USA and contains the transition probabilities for each month of the year. LongitudeLatitudeJanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember-97.5826.020.760.750.770.740.800.860.940.970.890.770.740.77 Following are the methods to be completed in WeatherGenerator.java: 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 longest number of consecutive 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 PolygonTransform.java, Sierpinski.java, andWeatherGenerator.java separately via the web submission system called Autolab. have to do , click the Assignments link from the course website; click the Submit link for that assignment. Comments (1) Answered by Expert Tutors Here is the code WeatherGenerator.java public class WeatherGenerator { static int DRY = 0; static int WET = 1; /* * 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) { int[] monthData = new int[31]; double dryWetProb = getProbability(longitute, latitude, month, drywet); double wetWetProb = getProbability(longitute, latitude, month, wetwet); double prob = StdRandom.uniform(); // first day is wet 50% monthData[0] = prob < 0.5 ? WET : DRY; // generate next for (int i = 1; i < monthData.length; i++) { double rand = StdRandom.uniform(); if (monthData[i - 1] == WET) { monthData[i] = (rand <= wetWetProb) ? WET : DRY; } else { monthData[i] = (rand <= dryWetProb) ? WET : DRY; } } return monthData; } // Helper method to extract the probability from the array private double getProbability(double longitute, double latitude, double month, double[][] array) { for (int i = 0; i < array.length; i++) { //match lat and long if (array[i][0] == longitute && array[i][1] == latitude) { for (int j = 2; j < array[i].length; j++) { //match month if (j == month) { return array[i][j]; } } } } return 0; } // Returns the longest number of consecutive mode (WET or DRY) days in forecast. int numberOfWetDryDays(int[] forecast, int mode) { int maxCount = 0; int count = 0; for (int i = 1; i < forecast.length; i += 2) { //track duplets if (forecast[i - 1] == mode && forecast[i] != mode) { count++; } else { //reset count = 0; } //set max if (count > maxCount) { maxCount = count; } } return maxCount; } /* * 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) { int maxCount = 0; int count = 0; // iterate over all elements for (int i = 0; i < forecast.length; i++) { if (forecast[i] == mode) { count++; } else { //reset count = 0; } // set the max if (count > maxCount) { maxCount = count; return maxCount; } } Driver.java public class Driver { 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]); WeatherGenerator wgen = new WeatherGenerator(); int DRY = 0; int WET = 1; int[] forecast = wgen.oneMonthGenerator(longitute, latitude, month, drywet, wetwet); int drySpell = wgen.lengthOfLongestWetDrySpell(forecast, DRY); int wetSpell = wgen.lengthOfLongestWetDrySpell(forecast, WET); int wetDryDays = wgen.numberOfWetDryDays(forecast, WET); int dryWetDays = wgen.numberOfWetDryDays(forecast, DRY); StdOut.println("There are " + forecast.length + " days in the forecast for month " + month); StdOut.println(drySpell + " days of dry spell."); StdOut.println(wetSpell + " days of dry spell."); StdOut.println( wetDryDays+ " days of wetdry days."); StdOut.println( dryWetDays+ " days of drywet days."); 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); private static void populateTransitionProbabilitiesArrays(double[][] drywet, double[][] wetwet, int numberOfRows) { double[] drywetprobability = {-98.76, 26.7,0.270,0.330,0.400,0.460,0.430,0.280,0.120,0.170,0.230,0.210,0.280,0.27}; double[] wetwetprobability = {-98.76, 26.7,0.550,0.580,0.610,0.690,0.730,0.620,0.450,0.550,0.580,0.550,0.590,0.55}; //iterate over each row for (int i = 0; i < drywet.length; i++) { //iterate over each column drywet[i] = drywetprobability; wetwet[i] =wetwetprobability; [Show More]

Last updated: 10 months ago

Preview 1 out of 10 pages

Reviews( 0 )

$10.50

Add to cart

Instant download

Can't find what you want? Try our AI powered Search

OR

GET ASSIGNMENT HELP
90
0

Document information


Connected school, study & course


About the document


Uploaded On

Mar 20, 2021

Number of pages

10

Written in

Seller


seller-icon
QuizMaster

Member since 4 years

1086 Documents Sold


Additional information

This document has been written for:

Uploaded

Mar 20, 2021

Downloads

 0

Views

 90

Document Keyword Tags

Recommended For You


$10.50
What is Browsegrades

In Browsegrades, a student can earn by offering help to other student. Students can help other students with materials by upploading their notes and earn money.

We are here to help

We're available through e-mail, Twitter, Facebook, and live chat.
 FAQ
 Questions? Leave a message!

Follow us on
 Twitter

Copyright © Browsegrades · High quality services·