# oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
# Problem:
# We are given a text file containing
# daily measurements of outdoor temperatures
# on a particular meteorological stations.
# Find the coldest day on record.

# oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo


# ----------------------------------------------------------------
# Simple helper formatting function
# turns date format YYYYMMDD into DD.MM.YYYY
# ----------------------------------------------------------------
def getDate( dateStr ):
    # Date YYYYMMDD
    year = dateStr[0:4]
    month = dateStr[4:6]
    day = dateStr[6:8]
    return day + "." + month + "." + year

# ----------------------------------------------------------------
# The coldestDay funcion depends on the exact format of the data.
# Check the data file visually to see how the data are organized.
# File consists of two segments:
'''
<Long complex header>
...
<data>
...
   169,   862,18140101,   36,    0
   169,   862,18140102,   24,    0
   169,   862,18140103,    9,    0
   169,   862,18140104,    7,    0
...
'''
# The data on one line are:
# Station and source ID (169 and 862), # these can be skipped
# date(18140101 = 01.01.1814), 
# mean temperature on that date, in 1/10s of C (36 = 3.6 degrees C),
# quality code for that date (0='valid'; 1='suspect'; 9='missing').
# ----------------------------------------------------------------

def coldestDay( fileName ):
    # named constants do help
    dateColumn = 2
    temperColumn = 3
    qualityColumn = 4
    qualityValid = 0
    qualitySuspect = 1
    qualityMissing = 9

    # start reading
    file = open( fileName, "r" )

    # skip the file header
    while True:
        line = file.readline()
        if line[0:5] == "STAID": break

    minTemper = 10000 # start with big temperature
    dateMinTemper = 0

    # process all lines
    while True:
        line = file.readline()
        # detect possible empty lines to stop
        if line == None or line.strip(" ") == "":
            break

        # extract info from a text line
        lineVals = line.split(",")
        date =  lineVals[dateColumn]
        temper = int( lineVals[temperColumn] )
        quality = int( lineVals[qualityColumn] )

        # do not accept dubious data
        if quality != qualityValid:
            continue

        # just for amusement, temper/10 gives correct value in degrees Celsius
        if temper <= -200:
            print( " cold!", temper/10, date, getDate( date ) )

        if temper <= minTemper:
            minTemper = temper
            dateMinTemper = date
            print( temper, date )

    file.close()
    print("The lowest temperature ", minTemper/10, " was on ", \
          getDate( dateMinTemper ), ".", sep = "'" )
    #end of coldestDay




# oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
#
#               M A I N
#
# ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo


path ="d:\\Iskola\\PGE2024\\data\\"
fname = "TG_STAID000169.txt"
# fname = "TG_STAID000027.txt"

coldestDay( path + fname )
