Created
March 31, 2014 05:06
-
-
Save saadbinakhlaq/9885664 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#from beetle_simulation import * | |
import random | |
import csv | |
import numpy as np | |
def make_array(n, m): | |
'''(int, int) -> list of NoneType | |
Creates and returns an n by m list, filled with None.''' | |
array = [0] * n | |
for i in range(len(array)): | |
array[i] = [0] * m | |
return np.array(array) | |
def how_many_years(filename): | |
with open(filename, 'r') as f: | |
csv_reader = csv.reader(f) | |
a = "" | |
b = [] | |
for row in csv_reader: | |
if not row[0].startswith('Year'): | |
if a.find(row[0]) < 0: | |
a += row[0] | |
b.append(row[0]) | |
return len(b)-1, b | |
def how_many_years(filename): | |
with open(filename, 'r') as f: | |
csv_reader = csv.reader(f) | |
a = "" | |
b = [] | |
for row in csv_reader: | |
if not row[0].startswith('Year'): | |
if a.find(row[0]) < 0: | |
a += row[0] | |
b.append(row[0]) | |
return len(b)-1, b | |
#print(how_many_years("temperatures_whitehorse.csv")) | |
def get_temp_snow(filename): | |
# filename = 'temperatures_whitehorse.csv' | |
data = np.genfromtxt(filename, delimiter = ',', skip_header = 1) | |
total_snowfall = data[:, 8] | |
lowest_temp = data[:, 6] | |
return total_snowfall, lowest_temp | |
def temperature_by_year(filename): | |
'''Complete this function.''' | |
with open(filename, 'r') as f: | |
csv_reader = csv.reader(f, delimiter = ',') | |
(year_num, years) = how_many_years(filename) | |
(snow, temp) = get_temp_snow(filename) | |
# climate_data = make_array(year_num, 3) | |
# for i in range(0, year_num): | |
# climate_data[i, 0] = years[i] | |
# return climate_data | |
year_num_lst = np.arange(0, (year_num +1)) | |
month_lst = np.arange(0, 13) | |
lowest_temp = make_array(year_num, 12) | |
lowest_temp = lowest_temp.astype('float') | |
total_snowfall = make_array(year_num, 12) | |
total_snowfall = total_snowfall.astype('float') | |
for (a, i) in zip(years, year_num_lst): | |
for row in csv_reader: | |
if row[0] == a: | |
try: | |
lowest_temp[i, (int(row[1]))-1] = float(row[6]) | |
total_snowfall[i, (int(row[1]))-1] = float(row[8]) | |
except IndexError: | |
pass | |
f.seek(0) | |
print lowest_temp, total_snowfall | |
return lowest_temp, total_snowfall | |
## lowest_temp.append([]) | |
## total_snowfall.append([]) | |
## if not row[0].startswith('Year'): | |
## if row[0] == a: | |
## lowest_temp[-1].append(row[6]) | |
## total_snowfall[-1].append(row[8]) | |
## return lowest_temp, total_snowfall | |
#print (temperature_by_year('temperatures_whitehorse.csv')) | |
#print(lowest_temp) | |
#print(total_snowfall) | |
# for a in y: | |
# lowest_temp.append([]) | |
# total_snowfall.append([]) | |
# for row in csv_reader: | |
# if not row[0].startswith('Year'): | |
# if row[0] == a: | |
# lowest_temp[-1].append(row[6]) | |
# total_snowfall[-1].append(row[8]) | |
# return lowest_temp, total_snowfall | |
#print(temperature_by_year("temperatures_whitehorse.csv")) | |
if __name__ == '__main__': | |
temperature_by_year('temperatures_whitehorse.csv') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment