Reputation: 873
I'm reading in data for three separate cities and I want to keep each set of data in a two-dimensional array, but as I get past a part of my code, loops keep writing over things from my first two cities as I only have a one-dimensional array. Where should I set up these 2-D arrays to keep my files organized and what function and arguments should I use to do so?
Arrays should be 3X54 (3 for each city, 54 for each year of data)
EDIT: All the initial variables in the code below (i.e. precip, tmin, tmax) will have over 19,000 elements in them at the beginning which I end up averaging over each year later in the code.
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
city = ['Lubbock.txt','Erie.txt','Oslo.txt']
years = np.arange(1960,2014,1)
months_summer = range(5,8,1)
for x in range(0,len(city),1):
data = np.genfromtxt(city[x], skip_header=2, usecols=(1), dtype=('S8'))
data2 = np.genfromtxt(city[x], skip_header=2, usecols=(2,3,4))
#ONLY GET 1-D ARRAY WHEN I ASK FOR SHAPE OF VARIABLE AFTER THIS POINT
dates = pd.DatetimeIndex(data[:])
year = dates.year
month = dates.month
day = dates.day
precip = data2[:,0]/10.
tmax = data2[:,1]/10.
tmin = data2[:,2]/10.
tmaxF = (tmax*(9./5.))+32.
tminF = (tmin*(9./5.))+32.
precipinches = precip*0.03937007874
tmax_avg = []
JJA3tmax_avg = []
JJAtmax_avg = []
DJFtmax_avg = []
for yr in years:
toavg = np.where(year == yr)
tmax_avg.append(np.average(tmax[toavg]))
for mo in months_summer:
sumtoavg = np.where(month == mo)
JJA3tmax_avg.append(np.average(tmax[sumtoavg]))
JJAtmax_avg.append(np.average(JJA3tmax_avg))
JJA3tmax_avg = []
dec_this_year = (year == yr) & (month == 12)
jan_next_year = (year == (yr+1)) & (month == 1)
feb_next_year = (year == (yr+1)) & (month == 2)
wintoavg = np.where(dec_this_year & jan_next_year & feb_next_year)
DJFtmax_avg.append(np.average(tmax[wintoavg]))
tmaxmean30 = np.average(tmax_avg[1:31])
JJAtmaxmean30 = np.average(JJAtmax_avg[1:31])
DJFtmaxmean30 = np.average(DJFtmax_avg[1:31])
#THIS IS THE DATA THAT I'M PLOTTING
tmax_avg_dep = tmax_avg - tmaxmean30
JJAtmax_avg_dep = JJAtmax_avg - JJAtmaxmean30
DJFtmax_avg_dep = DJFtmax_avg - DJFtmaxmean30
Upvotes: 1
Views: 433
Reputation: 23231
This line:
data[:]
creates a shallow copy. You need a deep copy:
import copy
copy.deepcopy(data)
From the docs:
The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):
- A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.
- A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.
Upvotes: 5