<div dir="ltr"><div>Ok, I am continuing to get stuck. I think I was asking the wrong question so I have posted the entire script (see below).</div><div>What I really want to do is find the daily maximum for a dataset (e.g. Temperature) that is contained in monthly netcdf files where the data are separated by hour. </div><div>The major steps are:</div><div>open monthly netcdf files and use the timestamp to extract the hourly data for the first day.</div><div>Append the data for each hour of that day to a list, concatenate, find max and plot</div><div>then loop through and do the following day. </div><div><br></div><div>I can do some of the steps separately but I run into trouble in working out how to loop through each hour and separate the data into each day and then repeat all the steps for the following day.</div><div><br></div><div>Any feedback will be greatly appreciated!</div><div> </div><div><br></div><br><div><div>oneday=[]</div><div>all_the_dates=[]</div><div>onedateperday=[]</div><div><br></div><div><br></div><div>#open folders and open relevant netcdf files that are monthly files containing hourly data across a region</div><div>for (path, dirs, files) in os.walk(MainFolder):</div><div>        for ncfile in files:</div><div>            if ncfile.endswith(ncvariablename+'.nc'):</div><div>                print "dealing with ncfiles:", path+ncfile </div><div>                ncfile=os.path.join(path,ncfile)</div><div>                ncfile=Dataset(ncfile, 'r+', 'NETCDF4')</div><div>                variable=ncfile.variables[ncvariablename][:,:,:]</div><div>                TIME=ncfile.variables['time'][:]</div><div>                ncfile.close()</div><div>                </div><div>                #combine variable and time so I can make calculations based on hours or days</div><div>                for v, time in zip((variable[:,:,:]),(TIME[:])):</div><div><br></div><div>                    cdftime=utime('seconds since 1970-01-01 00:00:00')</div><div>                    ncfiletime=cdftime.num2date(time)</div><div>                    timestr=str(ncfiletime)</div><div>                    utc_dt = dt.strptime(timestr, '%Y-%m-%d %H:%M:%S')</div><div>                    au_tz = pytz.timezone('Australia/Sydney')</div><div>                    local_dt = utc_dt.replace(tzinfo=pytz.utc).astimezone(au_tz)</div><div>                    strp_local=local_dt.strftime('%Y-%m-%d_%H') #strips time down to date and hour</div><div>                    local_date=local_dt.strftime('%Y-%m-%d') #strips time down to just date</div><div><br></div><div>                    all_the_dates.append(local_date)                 </div><div>                                    </div><div>#make a list that strips down the dates to only have one date per day (rather than one for each hour)              </div><div>onedateperday = sorted ( list (set (all_the_dates)))</div><div>                 </div><div>#loop through each day and combine data (v) where the hours occur on the same day                   </div><div>for days in onedateperday:</div><div>    if strp_local.startswith(days):</div><div>        oneday.append(v)</div><div>         </div><div>big_array=np.ma.dstack(oneday) #concatenate data  </div><div>v_DailyMax=big_array.max(axis=2) # find max</div><div><br></div><div>#then go on to plot v_DailyMax for each day </div><div>map = Basemap(projection='merc',llcrnrlat=-40,urcrnrlat=-33,</div><div>        llcrnrlon=139.0,urcrnrlon=151.0,lat_ts=0,resolution='i')</div><div>map.drawcoastlines()</div><div>map.drawstates()</div><div>map.readshapefile(shapefile1, 'REGIONS')</div><div>x,y=map(*np.meshgrid(LON,LAT))</div><div>plottitle=ncvariablename+'v_DailyMax'+days</div><div>cmap=plt.cm.jet</div><div>CS = map.contourf(x,y,v_DailyMax, 15, cmap=cmap)</div><div>l,b,w,h =0.1,0.1,0.8,0.8</div><div>cax = plt.axes([l+w+0.025, b, 0.025, h])</div><div>plt.colorbar(CS,cax=cax, drawedges=True)</div><div>plt.savefig((os.path.join(OutputFolder, plottitle+'.png')))</div><div>plt.show()</div><div>plt.close()</div></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Sep 24, 2014 at 8:27 AM, questions anon <span dir="ltr"><<a href="mailto:questions.anon@gmail.com" target="_blank">questions.anon@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">brilliant, thank you!</div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Sep 23, 2014 at 5:05 PM, Dave Angel <span dir="ltr"><<a href="mailto:davea@davea.name" target="_blank">davea@davea.name</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
Please post in text mode, not html.<br>
<br>
questions anon <<a href="mailto:questions.anon@gmail.com" target="_blank">questions.anon@gmail.com</a>> Wrote in message:<br>
<span>><br>
> lastdate=all_the_dates[1]<br>
> onedateperday.append(lastdate)<br>
> print onedateperday<br>
> for date in all_the_dates:<br>
>      if date !=lastdate:<br>
>           lastdate=date<br>
>          onedateperday.append(lastdate)<br>
<br>
</span>There are a number of things you don't explicitly specify.  But if<br>
 you require the dates in the output list to be in the same order<br>
 as the original list, you have a bug in using element [1]. You<br>
 should be using element zero, or more simply:<br>
<br>
lastdate=None<br>
<span>for date in all_the_dates:<br>
    if date !=lastdate:<br>
</span>        onedateperday.append(date)<br>
        lastdate=date<br>
<br>
But if you specified a bit more, even simpler answers are possible.<br>
<br>
For example,  if output order doesn't matter, try:<br>
     onedateperday = list (set (all_the_dates))<br>
<br>
If output order matters, but the desired order is sorted,<br>
    onedateperday = sorted ( list (set (all_the_dates)))<br>
<br>
Other approaches are possible using library functions,  but this<br>
 should be enough.<br>
<span><font color="#888888"><br>
<br>
<br>
--<br>
DaveA<br>
</font></span><div><div><br>
_______________________________________________<br>
Tutor maillist  -  <a href="mailto:Tutor@python.org" target="_blank">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="https://mail.python.org/mailman/listinfo/tutor" target="_blank">https://mail.python.org/mailman/listinfo/tutor</a><br>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>