CSC111 Homework 10 2018

From dftwiki3
Revision as of 21:57, 23 April 2018 by Thiebaut (talk | contribs)
Jump to: navigation, search

D. Thiebaut (talk) 20:49, 15 April 2018 (EDT)



This Homework is due on Thursday evening, 4/26/18, at 11:55 p.m.


Problem #1: Temperatures in the UK

HistoricStationDataUKWeather.png


Assignment

  • Your assignment is to write a program that
  1. reads weather data from a text file,
  2. processes the data, and
  3. finally outputs the answers to several questions.
  • Call your program hw10_1.py

The Weather Data Files



  • The data recorded consists of these quantities
  • Mean daily maximum temperature (tmax)
  • Mean daily minimum temperature (tmin)
  • Days of air frost (af)
  • Total rainfall (rain)
  • Total sunshine duration (sun)
(More information can be found here.)


  • The format for the data is Comma Separated Value, or CSV.
  • The data from the UK Web site have been mirrored on a Smith server: http://cs.smith.edu/~dthiebaut/UKTemperatures/. You will need to download a few files from that site to develop and test your program.
  • The name of a file is simply the name of the U.K. town, all lowercase, with the suffix "data.txt."


Format of the data


  • Use the data files that are located on the Smith server.
  • Download one of the files from http://cs.smith.edu/~dthiebaut/UKTemperatures/, for example armaghdata.txt, and take a look at it. Use Notepad, TextEdit or your favorite text editor to open the file.
  • You will notice that when measurements are missing, they are replaced by "---". Your program should not skip measurements that are missing.
  • Sometimes, especially at the end of the file, measurements are suffixed with an asterisk (*). Your program should discard the asterisk, and treat the data as valid.
  • Some lines have the word "Provisional" at the end. Your program should treat these lines the same as regular line, and should not skip them.


Input


  • Your program should prompt the user for a file name. The user will supply the name of one of the text files that will have been previously downloaded from the Smith Web site. In other words, your program simply needs to read a text file and does not need to access the Web.
  • If the user provides an invalid file name, your program will keep on prompting the user for a new name. Your program will only start processing the data once it has been given a valid file name.


Processing


  • You should use the method illustrated in the Animals and Presidents problems of Lab 11 to process the data.
  • All temperatures should be reported in degrees Celsius which is the system used in the U.K. (so you do not need to transform the temperatures into Fahrenheit).


Questions


Question 1
In what year or years was the coldest temperature recorded? If the coldest temperature appears several times, list all the years and month in which it will have been reported. The format of the output is illustrated in a later section.


Question 2
In what year or years was the warmest temperature recorded? If the warmest temperature appears several times, list all the years and month in which it will have been reported. The format of the output is illustrated in a later section.
Question 3
What are the 5 sunniest months and years for the given city? The format of the output is illustrated below.


Output Format


  • Here is the expected output for armaghdata.txt:


> armagh.txt
Invalid file name, please re-enter
> armaghdata.txt
1, -8.6, 1878, 12
2, 23.8, 1995, 8, 1989, 7
3, 256.0, 1940, 6, 252.9, 1949, 6, 251.6, 1935, 5, 244.1, 1957, 6, 243.8, 1989, 7


and here is the expected output for ballypatrickdata.txt:
> ballypatrickdata.txt
1, -1.8, 1979, 1
2, 20.0, 1995, 8
3, 279.3, 1975, 5, 272.6, 1976, 8, 253.7, 1977, 5, 247.2, 1984, 5, 245.6, 1974, 4

Note that each line is prefixed with a number, identifying the question for which the line is the answer.
The order in which the years of min or max temperature are listed is unimportant.
All the floating point numbers are printed with 1 decimal after the decimal point.
The sunniest months are listed in order of decreasing exposure.
Note, also, that the output is in CSV form. A coma separates all the values. No extra spaces should appear in front of commas.

Moodle Submission

  • Submit your program in the Moodle HW11 PB1 section. You will not be allowed to evaluate this program, so make sure you test it thoroughly.






<showafterdate after="20180427 12:00" before="20180601 00:00">

Solution Programs


Problem 1


# hw11_1.py
# D. Thiebaut
# This program prompts the user for a file name that contains data about
# weather conditions recording a given month in a British city.   The source
# of the data is taken from http://www.metoffice.gov.uk/climate/uk/stationdata/
# This program outputs several quantities, including the years and months when 
# the coldest temperature was recorded, the year the warmest temperature was recorded,
# the 5 sunniest months and years.

def getData():
    #return open( "armagh.txt", "r" ).read()

    fileName = input( "> " )
    while True:
        try:
            text = open( fileName, "r" ).read()
            return text
        except:
            print( "Invalid file name.  Reenter" )
        fileName = input( "> " )

def main():
    text = getData()
    lines = text.split( "\n" )

    # create a list of just year, month, minT, maxT, rain, and sun
    list = []
    for line in lines:
        line = line.strip()
        if len( line ) == 0:
            continue

        # skip line that do not start with a year
        if not line[0] in ['1', '2']:
            continue

        # remove stars and "Provisional" and split
        fields = line.replace( '*', '' ).replace( 'Provisional', '' ).split( )

        # split line into variables
        if len( fields ) == 7:
            year, month, maxT, minT, af, rain, sun = fields
        else:
            continue

        # create tuple and record it in list
        tuple = ( year, month, maxT, minT, rain, sun )
        list.append( tuple )

    #===============================================================
    # Find coldest months and years on record
    #===============================================================
    # create a new list, putting min temperature first
    listMinTemp = []
    for year, month, maxT, minT, rain, sun in list:
        if minT=="---":
            continue
    
        listMinTemp.append( ( float(minT), year, month ) )

    # sort from coldest to warmest min temp
    listMinTemp.sort()

    # find coldest temperature
    coldest = listMinTemp[0][0]

    # find all months and years with that coldest temperature
    print( "1, ", coldest, end="", sep="" )
    for minT, year, month in listMinTemp:
        if minT == coldest:
            print( ", ", year, ", ",  month,  end="", sep="" )
    print()

    #===============================================================
    # Find warmest months and years on record
    #===============================================================
    # create a new list, putting min temperature first
    listMaxTemp = []
    for year, month, maxT, minT, rain, sun in list:
        if maxT=="---":
            continue
    
        listMaxTemp.append( ( float(maxT), year, month ) )

    # sort from coldest to warmest min temp
    listMaxTemp.sort()
    listMaxTemp.reverse()

    # find coldest temperature
    warmest = listMaxTemp[0][0]

    # find all months and years with that coldest temperature
    print( "2, ", warmest, end="", sep="" )
    for maxT, year, month in listMaxTemp:
        if maxT == warmest:
            print( ", ", year, ", ",  month, end="", sep="" )
    print()

    #===============================================================
    # Find the 5 sunniest months, listed in order of sun exposure
    #===============================================================
    # create a new list, putting min temperature first
    sunniestMonthsYears = []
    for year, month, maxT, minT, rain, sun in list:
        if sun=="---":
            continue
        sun = float( sun )
        sunniestMonthsYears.append( (sun, year, month) )

    # sort from coldest to warmest min temp
    sunniestMonthsYears.sort()
    sunniestMonthsYears.reverse()

    # find all months and years with that coldest temperature
    print( "3", end="" )
    for i in range( min( 5, len( sunniestMonthsYears) ) ):
        sun, year, month = sunniestMonthsYears[i]
        print( ", {0:1.1f}, {1:1}, {2:1}".format( sun, year, month), end="", sep="" )
    print()



if __name__=="__main__":
    main()


</showafterdate>