Excellent, thank you so much. I don&#39;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&#39; Walker-Loud <span dir="ltr">&lt;<a href="mailto:walksloud@gmail.com" target="_blank">walksloud@gmail.com</a>&gt;</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>
&gt; I would like to calculate summary statistics of rainfall based on year and month.<br>
&gt; I have the data in a text file (although could put in any format if it helps) extending over approx 40 years:<br>
&gt; YEAR MONTH    MeanRain<br>
&gt; 1972 Jan    12.7083199<br>
&gt; 1972 Feb    14.17007142<br>
&gt; 1972 Mar    14.5659302<br>
&gt; 1972 Apr    1.508517302<br>
&gt; 1972 May    2.780009889<br>
&gt; 1972 Jun    1.609619287<br>
&gt; 1972 Jul    0.138150181<br>
&gt; 1972 Aug    0.214346148<br>
&gt; 1972 Sep    1.322102228<br>
&gt;<br>
&gt; I would like to be able to calculate the total rain annually:<br>
&gt;<br>
&gt; YEAR   Annualrainfall<br>
&gt; 1972    400<br>
&gt; 1973    300<br>
&gt; 1974    350<br>
&gt; ....<br>
&gt; 2011     400<br>
&gt;<br>
&gt; and also the monthly mean rainfall for all years:<br>
&gt;<br>
&gt; YEAR  MonthlyMeanRain<br>
&gt; Jan      13<br>
&gt; Feb      15<br>
&gt; Mar       8<br>
&gt; .....<br>
&gt; Dec       13<br>
&gt;<br>
&gt;<br>
&gt; 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 = [&#39;Jan&#39;,&#39;Feb&#39;,....,&#39;Dec&#39;]<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 = {&#39;1972&#39;:0, &#39;1973&#39;:1, ....}<br>
months = {&#39;Jan&#39;:0,&#39;Feb&#39;:1,...&#39;Dec&#39;:11}<br>
<br>
then you can access and store the data to the array using these dictionaries.<br>
<br>
print rain_fall[int(&#39;%(1984)s&#39; % years), int(&#39;%(March)s&#39; % months)]<br>
<span class="HOEnZb"><font color="#888888"><br>
<br>
Andre<br>
</font></span><div class="im HOEnZb"><br>
<br>
<br>
<br>
<br>
&gt; 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>
&gt;<br>
&gt; textfile=r&quot;textfile.txt&quot;<br>
&gt; f=np.genfromtxt(textfile,skip_header=1)<br>
&gt;<br>
&gt; Any feedback will be greatly appreciated.<br>
&gt;<br>
</div><div class="HOEnZb"><div class="h5">&gt; _______________________________________________<br>
&gt; Tutor maillist  -  <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
&gt; To unsubscribe or change subscription options:<br>
&gt; <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>