python代写

Assignment 4 Week 1

Northern California Temperature Anomaly

In this assignment, we’ll look at some climate change data from the National Oceanic and Atmospheric Administration (the US government agency in charge of weather and climate data, among other things). In particular, we’ll look at one of the “headline” datasets, global temperature anomaly over the last century. By “anomaly” they just mean the difference between the annual average temperature and a single baseline temperature. In this case they use the average from 1981-2010 as a baseline (although the data goes from 1880-2018).

The following figure, illustrating the trends in temperature anomalies in California, is from a recent article (Links to an external site.)Links to an external site. citing the extreme overnight low temperatures in much of California.

 

Note that they use an average as the baseline for calculating the anomaly values, rather than a single year, because the temperature varies from year to year. If you average over a number of years these variations smooth out.

Our program will calculate a moving average of the temperature anomaly data for Sacramento (the latitude and longitude used in the query) during the month of July.

You will use this linked file as input: Sacramento-1880-2018.NOAA.csv

Week 1

During the first week we will write the main loop, input and output components of the program.

To begin you can download the provided file of temperature anomaly data for our region: Sacramento-1880-2018.NOAA.csv  You should be able to look at the data in your favorite text editor.  This is what the beginning of the file should look like. Your program should read but ignore the first five lines of the file.

38.6 deg N, 121.5 deg E Temperature Anomalies, July
Units: Degrees Celsius
Base Period: 1981-2010
Missing: -9999
Year,Value
1880,-1.56
1881,-0.08
1882,-0.30
1883,-1.44
1884,-2.29

Your program will be asking the user for only one piece of information, the number of years over which to average. This number must be an integer between 0 and 60. In this assignment, your program will keep asking until the user enters valid input.

The output of your program will be to the screen and an output file called tempAnomaly.txt. The output data will be in two columns separated by tabs. This allows programs like Microsoft Excel or Google Docs to read the data and make a chart.

Week 1 Deliverables

Solutions will be posted after the first week for those students who could not complete this part. Therefore, you can not receive any credit for this part after the due date. Week 1 deliverables are in three parts listed below.

Part 1 (15 pts)

  • Have your program ignore the first five lines of the input file. Then make a loop that reads in the data file, and prints out every line.
  • Change the block under the loop so that the newline is removed after each line of input. The last 5 lines of the printed output should then look exactly like this:

2014,-0.06
2015,-0.40
2016,0.48
2017,2.63
2018,0.18

  • Submit this program to Kodethon as py

Part 2 (20 pts)

  • In the loop block for the program you submitted for Part 1, use the split() string method to extract the year and the temperature into variables. Change the temperature to a floating point number.
  • Implementation Note: our implementation we store temperature and year as separate lists, tempList and yearLists as we read through the input file. If you are not yet comfortable with lists you can postpone implementing them until the second week of the assignment by concurrently reading and writing the input and output in the same loop block. If you do implement lists, it will be better at this point if you use seperate loops for reading and printing/writing, since you will eventually analyze the whole dataset in-between these phases.
  • Change your program so that it prints out the year followed by the temperature separated by a tab (instead of the original comma). You will now also include a simple tab separated header “Year\tValue” similar to the fifth line of the input file as the first line of output. The temperature should be formatted using four fixed decimal places using the format method: “{:.4f}”.format(average)

The last five lines of your output will look like:
2014 -0.0600
2015 -0.4000
2016 0.4800
2017 2.6300
2018 0.1800

  • Finally, change your program so that it also writes its output to a file called “tempAnomaly.txt”.
  • When you are sure the output in the file is the same as what was printed, you should comment out the print statements before submission.
  • Submit this program as py

Part 3 (15 pts)

  • Finally we will update the program in part 2, let the user pick the integer k. Before your program reads the input file you will prompt the user for an integer between 0 and 60 (including the integers 0 and 60).

    The prompt string will be: “Enter an integer between 0 and 60:”

    Use the pymodule to check the user’s input can be converted to an integer. Make sure it is between 0 and 60. You will simply re-prompt (using a loop as shown in class) until the user enters correct input.

  • Submit this program to Kodethon as py