Hun_
Hun_

Reputation: 21

Want to loop file by file in a directory of weather data, then loop through an entire month (day by day) saving precipitation data?

I am trying to make a script that reads in a group of CSV files that I stored in my data directory called 'database', stored on my desktop. In those CSV files are weather data, the last list index 16 of the CSV is perciptitation which I am trying to get and calculate the total monthly precipitation for each Station. Each station contains 12 months of weather data, and in the CSV the last index is precipitation. At the end I want to output a single CSV file that contains 48 rows (12 months x 4 stations) including station ID, month number, and total precipitation for that month at that station.

Here is what I have so far:

import glob

files = glob.glob("database/*.csv")

def totalPercip(list):
    total = 0
    # iterate through the lists of month, each time getting the 16th element, preciptation
    for i in list:
# getting only the 16th index for each day for specific month list
# not sure if this is correct
        total = i[16]
        print(total)


# loop through it one file at a file
for i in files:
    # opening one of the files in the pre-made directory, and reading out the contents
    f = open(i,'r')
    # loop through each line of said specific file at current iteration
    for j in f.readlines():
        # save all those lines into one variable called list
        list = [j]
    # past list as an argument into totalPercip function
    totalPercip(list)
# stuck here, not sure how to get totalPercip to work and get the 16th index

Here is what my folder looks like:

database folder of monthly weather data

And here is what the CSV file looks like, trying to get the last index:

opened CSV file

So I am trying to create a function that calculates the total precipitation given a month worth of data. As it accepts a months worth of weather data, I want to have it return a single float value containing the total amount of precipitation that month. I know i'll need to loop through all the elements int he list (each day of the month), and then split each element by a comma, and then do an inside loop to add up the total for the month. Just not entirely sure how to go about it and what I am doing wrong so far.

Upvotes: 1

Views: 202

Answers (1)

Bill Souvas
Bill Souvas

Reputation: 46

This code should work properly for your totalPercip() function. Your mistakes were 1) you didn't increment the total variable and each loop assigned it to a different, new value, and 2) i[16] would probably give you an List ouf of bounds exception since i[0] = 1st index, i[1] = 2nd index..., i[15] = 16th index.

def totalPercip(list):
    total = 0
    for i in list:
        percip = float(i[15].replace(',','')) #16th index
        total += percip 

    return total

I would also suggest that you do not use list as a variable name since it overshadows a built in python function called by that name.

Hope this helps!

Upvotes: 0

Related Questions