Advertisement
hexalogy

Asg 2

Sep 8th, 2019
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.00 KB | None | 0 0
  1. import sys  # Needed for sys.argv
  2. import csv
  3. from datetime import datetime, timedelta
  4. #from dateutil.relativedelta import relativedelta
  5.  
  6. def get_climate(in_filename, out_filename):
  7.     """Read historical weather from in_filename, write climate to out_filename.
  8.  
  9.    Parameters
  10.    ----------
  11.    in_filename : str
  12.        name of the input file
  13.    out_filename : str
  14.        name of the output file
  15.    """
  16.  
  17.     avg_precip = {} # {"MM/DD":0.15, "MM/DD":0.25}
  18.     avg_low = {}
  19.     avg_high= {}
  20.     min_low = {}
  21.     max_high = {}
  22.     record_low_year = {}
  23.     record_high_year = {}
  24.     count = 1
  25.     base_date = datetime.strptime("1895-07-01", '%Y-%m-%d').date()
  26.    
  27.     #WRITE FILE
  28.     result = open(out_filename, 'w')
  29.     result.write('Day,Avg precip,Avg low,Avg high,Min low,Max high,Min low year,Max high year\n')
  30.  
  31.     #open the file
  32.     with open(in_filename, 'r') as csvfile:
  33.        
  34.         csvreader = csv.reader(csvfile, delimiter=","); next(csvreader) # This skips the first row of the CSV file.
  35.        
  36.         for row in csvreader: # 1895-07-01,0.15,77,57
  37.            
  38.             if row[1] == "":
  39.                 precipitation = 0
  40.             else:
  41.                 precipitation = float(row[1])
  42.                
  43.             try:
  44.                 #date.append(row[0]) # 1895-07-01
  45.                 #precipitation = float(row[1]) #0.15
  46.                 tmax = float(row[2]) # 77
  47.                 tmin = float(row[3]) # 57
  48.            
  49.             except:
  50.                 #SKIPS IF THERES NO TMIN OR TMAX
  51.                 sys.stderr.write('Bad number in line' + str(row) + '\n')
  52.                 continue
  53.            
  54.             #--- process the dates inside list
  55.             for fmt in ('%Y-%m-%d', '%m/%d/%y'):
  56.                 try:
  57.                    
  58.                    #--- PUT INTO THIS FORMAT 1895-07-02
  59.                    example_time =  datetime.strptime(row[0], fmt).date()
  60.                    
  61.                    #-- DEALING WITH CWNTURY YEARS
  62.                    print("example time", example_time)
  63.                   # print("is it after this date?", datetime.strptime("1/1/04", '%m/%d/%y').date())
  64.                    if example_time >= datetime.strptime("1/1/04", '%m/%d/%y').date():
  65.                      
  66.                        print("***")
  67.                        #example_time -= timedelta(days=36525)
  68.                        example_time = example_time.replace(year=example_time.year-100)
  69.  
  70.                    #--- GRAB THE YEAR ONLY, IN THIS CASE ITS 1895
  71.                    year_now = example_time.strftime("%Y")
  72.                    
  73.                    date_now =  example_time #datetime.strptime(row[0], fmt).date()
  74.                    #### print("final output", datetime.strftime(example_time, "%m/%d/%Y"))
  75.                    
  76.                    #-- GETTING ONLY MONTHS AND DAY (12/01)
  77.                    the_month = datetime.strftime(example_time, "%m")
  78.                    the_day = datetime.strftime(example_time, "%d")
  79.                    month_day = the_month + "/" + the_day
  80.  
  81.                    
  82.                 except ValueError:
  83.                    continue
  84.            
  85.             #-- If the year changes, increase count
  86.             print("date now", example_time, "incrementing when it hits", (base_date + timedelta(days=365)))
  87.             if example_time > (base_date + timedelta(days=365)):
  88.                 base_date = example_time
  89.                 print("incrementing count... ")
  90.                 count += 1
  91.                
  92.             else:
  93.                 pass
  94.            
  95.             #-- POPULATE avg_precip DICTIONARY
  96.             if month_day in avg_precip:
  97.                 #-- IF THE DATE IS ALREADY IN THE LIST
  98.                 avg_precip[month_day] += precipitation
  99.                 #count += 1
  100.                
  101.             else:
  102.                 # ADD NEW MONTH_DAY AS THE NEW KEY
  103.                 avg_precip[month_day] = precipitation
  104.                
  105.             #-- POPULATE avg_low DICTIONARY
  106.             if month_day in avg_low:
  107.                 avg_low[month_day] += tmin
  108.             else:
  109.                 avg_low[month_day] = tmin
  110.                
  111.             #-- POPULATE avg_high DICTIONARY
  112.             if month_day in avg_high:
  113.                 avg_high[month_day] += tmax
  114.             else:
  115.                 avg_high[month_day] = tmax
  116.                
  117.             #-- REPLACE min_low DICTIONARY
  118.             if month_day in min_low:
  119.                 if tmin < avg_low[month_day]:
  120.                     min_low[month_day] = tmin
  121.             else:
  122.                 min_low[month_day] = tmin
  123.                
  124.             #-- REPLACE max_high DICTIONARY
  125.             if month_day in max_high:
  126.                 if tmax > avg_high[month_day]:
  127.                     max_high[month_day] = tmax
  128.             else:
  129.                 max_high[month_day] = tmax
  130.                
  131.             #-- REPLACE record_low_year DICTIONARY
  132.             if year_now in record_low_year:
  133.                 if tmin < record_low_year[year_now]:
  134.                     record_low_year[year_now] = tmin
  135.             else:
  136.                 record_low_year[year_now] = tmin
  137.  
  138.             #-- REPLACE record_low_year DICTIONARY
  139.             if year_now in record_high_year:
  140.                 if tmax > record_high_year[year_now]:
  141.                     record_high_year[year_now] = tmax
  142.             else:
  143.                 record_high_year[year_now] = tmax
  144.                
  145.         avg_precip = get_average(avg_precip,count)
  146.         avg_low = get_average(avg_low,count)
  147.         avg_high = get_average(avg_high,count)
  148.        
  149.        
  150.         print("avg_low dict", avg_low)
  151.         print("lowest in a year", record_low_year)
  152.        
  153.         for day_of_year in avg_precip.keys():
  154.             precip = avg_precip[day_of_year]
  155.             lowavg = avg_low[day_of_year]
  156.             highavg = avg_high[day_of_year]
  157.             result.write(day_of_year + "," + str(round(precip, 2)) + "," + str(lowavg) + "," + str(round(highavg, 2)) + "\n")
  158.  
  159.            
  160.  
  161.     """
  162.    What you should do:
  163.    1. Read each line of in_file
  164.    2. Skip the first (header) line
  165.    3. Split each line on commas
  166.    4. Get the year, month, and day
  167.    5. Update the statistics (total precip, total low temp, etc)
  168.    6. When done, open the output file.
  169.    7. for each day of the year:
  170.    8. Compute the climate for the day, write to output file.
  171.    
  172.    python climate.py test.csv result.csv
  173.    """
  174. def get_average(dictionary, count):
  175.     avgDict = {}
  176.     for k,v in dictionary.items():
  177.         #avgDict[k] = sum(v)/ float(len(v))
  178.         avgDict[k] = v / count
  179.     return avgDict
  180.  
  181. def usage():
  182.     """Complain that the user ran the program incorrect ly."""
  183.     sys.stderr.write('Usage:\n')
  184.     sys.stderr.write('  python climate.py <input-file.csv> <output-file.csv>\n')
  185.     sys.exit()
  186.  
  187. def main():
  188.     if len(sys.argv) != 3:
  189.         usage()
  190.         sys.exit()
  191.  
  192.     in_filename = sys.argv[1]
  193.     out_filename = sys.argv[2]
  194.  
  195.     get_climate(in_filename, out_filename)
  196.  
  197. if __name__ == '__main__':
  198.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement