Excellent, thank you so much. I don't understand all the steps at this stage so I will need some time to go through it carefully but it works perfectly.<br>Thanks again!<br><br><div class="gmail_quote">On Tue, May 8, 2012 at 4:41 PM, Andre' Walker-Loud <span dir="ltr"><<a href="mailto:walksloud@gmail.com" target="_blank">walksloud@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello anonymous questioner,<br>
<br>
first comment - you may want to look into hdf5 data structures<br>
<br>
<a href="http://www.hdfgroup.org/HDF5/" target="_blank">http://www.hdfgroup.org/HDF5/</a><br>
<br>
and the python tools to play with them<br>
<br>
pytables - <a href="http://www.pytables.org/moin" target="_blank">http://www.pytables.org/moin</a><br>
h5py - <a href="http://code.google.com/p/h5py/" target="_blank">http://code.google.com/p/h5py/</a><br>
<br>
I have personally used pytables more - but not for any good reason. If you happen to have the Enthought python distribution - these come with the package, as well as an installation of hdf5<br>
<br>
hdf5 is a very nice file format for storing large amounts of data (binary) with descriptive meta-data. Also, numpy plays very nice with hdf5. Given all your questions here, I suspect you would benefit from learning about these and learning to play with them.<br>
<br>
Now to your specific question.<br>
<div><div class="h5"><br>
> I would like to calculate summary statistics of rainfall based on year and month.<br>
> I have the data in a text file (although could put in any format if it helps) extending over approx 40 years:<br>
> YEAR MONTH MeanRain<br>
> 1972 Jan 12.7083199<br>
> 1972 Feb 14.17007142<br>
> 1972 Mar 14.5659302<br>
> 1972 Apr 1.508517302<br>
> 1972 May 2.780009889<br>
> 1972 Jun 1.609619287<br>
> 1972 Jul 0.138150181<br>
> 1972 Aug 0.214346148<br>
> 1972 Sep 1.322102228<br>
><br>
> I would like to be able to calculate the total rain annually:<br>
><br>
> YEAR Annualrainfall<br>
> 1972 400<br>
> 1973 300<br>
> 1974 350<br>
> ....<br>
> 2011 400<br>
><br>
> and also the monthly mean rainfall for all years:<br>
><br>
> YEAR MonthlyMeanRain<br>
> Jan 13<br>
> Feb 15<br>
> Mar 8<br>
> .....<br>
> Dec 13<br>
><br>
><br>
> Is this something I can easily do?<br>
<br>
</div></div>Yes - this should be very easy. Imagine importing all this data into a numpy array<br>
<br>
===<br>
import numpy as np<br>
<br>
data = open(your_data).readlines()<br>
years = []<br>
for line in data:<br>
if line.split()[0] not in years:<br>
years.append(line.split()[0])<br>
months = ['Jan','Feb',....,'Dec']<br>
<br>
rain_fall = np.zeros([len(n_year),len(months)])<br>
for y,year in enumerate(years):<br>
for m,month in enumerate(months):<br>
rain_fall[y,m] = float(data[ y * 12 + m].split()[2])<br>
<br>
# to get average per year - average over months - axis=1<br>
print np.mean(rain_fall,axis=1)<br>
<br>
# to get average per month - average over years - axis=0<br>
print np.mean(rain_fall,axis=0)<br>
<br>
===<br>
<br>
now you should imagine doing this by setting up dictionaries, so that you can request an average for year 1972 or for month March. That is why I used the enumerate function before to walk the indices - so that you can imagine building the dictionary simultaneously.<br>
<br>
years = {'1972':0, '1973':1, ....}<br>
months = {'Jan':0,'Feb':1,...'Dec':11}<br>
<br>
then you can access and store the data to the array using these dictionaries.<br>
<br>
print rain_fall[int('%(1984)s' % years), int('%(March)s' % months)]<br>
<span class="HOEnZb"><font color="#888888"><br>
<br>
Andre<br>
</font></span><div class="im HOEnZb"><br>
<br>
<br>
<br>
<br>
> I have started by simply importing the text file but data is not represented as time so that is probably my first problem and then I am not sure how to group them by month/year.<br>
><br>
> textfile=r"textfile.txt"<br>
> f=np.genfromtxt(textfile,skip_header=1)<br>
><br>
> Any feedback will be greatly appreciated.<br>
><br>
</div><div class="HOEnZb"><div class="h5">> _______________________________________________<br>
> Tutor maillist - <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
> To unsubscribe or change subscription options:<br>
> <a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
<br>
</div></div></blockquote></div><br>