find location of maximum values

I have a netcdf file that contains hourly temperature data for a whole month. I would like to find the maximum temperature within that file and also the corresponding Latitude and Longitude and Time and then plot this. Below is the code I have so far. I think everything is working except for identifying the corresponding latitude and longitude. The latitude and longitude always seems to be in the same area (from file to file) and they are not where the maximum values are occurring. Any feedback will be greatly appreciated. from netCDF4 import Dataset import matplotlib.pyplot as plt import numpy as N from mpl_toolkits.basemap import Basemap from netcdftime import utime from datetime import datetime from numpy import ma as MA import os TSFCall=[] for (path, dirs, files) in os.walk(MainFolder): for dir in dirs: print dir path=path+'/' for ncfile in files: if ncfile[-3:]=='.nc': print "dealing with ncfiles:", path+ncfile ncfile=os.path.join(path,ncfile) ncfile=Dataset(ncfile, 'r+', 'NETCDF4') TSFC=ncfile.variables['T_SFC'][:] TIME=ncfile.variables['time'][:] LAT=ncfile.variables['latitude'][:] LON=ncfile.variables['longitude'][:] ncfile.close() TSFCall.append(TSFC) big_array=N.ma.concatenate(TSFCall) tmax=MA.max(TSFC) print tmax t=TIME[tmax] indexTIME=N.where(TIME==t) TSFCmax=TSFC[tmax] print t, indexTIME print LAT[tmax], LON[tmax], TSFCmax cdftime=utime('seconds since 1970-01-01 00:00:00') ncfiletime=cdftime.num2date(t) print ncfiletime timestr=str(ncfiletime) d = datetime.strptime(timestr, '%Y-%m-%d %H:%M:%S') date_string = d.strftime('%Y%m%d_%H%M') map = Basemap(projection='merc',llcrnrlat=-40,urcrnrlat=-33,llcrnrlon=139.0,urcrnrlon=151.0,lat_ts=0,resolution='i') x,y=map(*N.meshgrid(LON,LAT)) map.tissot(LON[tmax], LAT[tmax], 0.15, 100, facecolor='none', edgecolor='black', linewidth=2) map.readshapefile(shapefile1, 'DSE_REGIONS') map.drawcoastlines(linewidth=0.5) map.drawstates() plt.title('test'+' %s'%date_string) ticks=[-5,0,5,10,15,20,25,30,35,40,45,50] CS = map.contourf(x,y,TSFCmax, 15, cmap=plt.cm.jet) l,b,w,h =0.1,0.1,0.8,0.8 cax = plt.axes([l+w+0.025, b, 0.025, h], ) cbar=plt.colorbar(CS, cax=cax, drawedges=True) plt.show() plt.close()

I'm sorry I don't have time to look closely at your code and this may not be helpful, but just in case... I find it suspicious that you *seem* (by quickly glancing at the code) to be taking TIME[max(temperature)] instead of TIME[argmax(temperature)]. -=- Olivier 2011/12/20 questions anon <questions.anon@gmail.com>
I have a netcdf file that contains hourly temperature data for a whole month. I would like to find the maximum temperature within that file and also the corresponding Latitude and Longitude and Time and then plot this. Below is the code I have so far. I think everything is working except for identifying the corresponding latitude and longitude. The latitude and longitude always seems to be in the same area (from file to file) and they are not where the maximum values are occurring. Any feedback will be greatly appreciated.
from netCDF4 import Dataset import matplotlib.pyplot as plt import numpy as N from mpl_toolkits.basemap import Basemap from netcdftime import utime from datetime import datetime from numpy import ma as MA import os
TSFCall=[]
for (path, dirs, files) in os.walk(MainFolder): for dir in dirs: print dir path=path+'/' for ncfile in files: if ncfile[-3:]=='.nc': print "dealing with ncfiles:", path+ncfile ncfile=os.path.join(path,ncfile) ncfile=Dataset(ncfile, 'r+', 'NETCDF4') TSFC=ncfile.variables['T_SFC'][:] TIME=ncfile.variables['time'][:] LAT=ncfile.variables['latitude'][:] LON=ncfile.variables['longitude'][:] ncfile.close()
TSFCall.append(TSFC)
big_array=N.ma.concatenate(TSFCall) tmax=MA.max(TSFC) print tmax t=TIME[tmax] indexTIME=N.where(TIME==t) TSFCmax=TSFC[tmax] print t, indexTIME print LAT[tmax], LON[tmax], TSFCmax
cdftime=utime('seconds since 1970-01-01 00:00:00') ncfiletime=cdftime.num2date(t) print ncfiletime timestr=str(ncfiletime) d = datetime.strptime(timestr, '%Y-%m-%d %H:%M:%S') date_string = d.strftime('%Y%m%d_%H%M')
map = Basemap(projection='merc',llcrnrlat=-40,urcrnrlat=-33,llcrnrlon=139.0,urcrnrlon=151.0,lat_ts=0,resolution='i') x,y=map(*N.meshgrid(LON,LAT)) map.tissot(LON[tmax], LAT[tmax], 0.15, 100, facecolor='none', edgecolor='black', linewidth=2) map.readshapefile(shapefile1, 'DSE_REGIONS') map.drawcoastlines(linewidth=0.5) map.drawstates() plt.title('test'+' %s'%date_string) ticks=[-5,0,5,10,15,20,25,30,35,40,45,50] CS = map.contourf(x,y,TSFCmax, 15, cmap=plt.cm.jet) l,b,w,h =0.1,0.1,0.8,0.8 cax = plt.axes([l+w+0.025, b, 0.025, h], ) cbar=plt.colorbar(CS, cax=cax, drawedges=True)
plt.show() plt.close()
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

ok thanks, a quick try at using it resulted in: IndexError: index out of bounds but I may need to do abit more investigating to understand how it works. thanks On Wed, Dec 21, 2011 at 2:20 PM, Olivier Delalleau <shish@keba.be> wrote:
I'm sorry I don't have time to look closely at your code and this may not be helpful, but just in case... I find it suspicious that you *seem* (by quickly glancing at the code) to be taking TIME[max(temperature)] instead of TIME[argmax(temperature)].
-=- Olivier
2011/12/20 questions anon <questions.anon@gmail.com>
I have a netcdf file that contains hourly temperature data for a whole month. I would like to find the maximum temperature within that file and also the corresponding Latitude and Longitude and Time and then plot this. Below is the code I have so far. I think everything is working except for identifying the corresponding latitude and longitude. The latitude and longitude always seems to be in the same area (from file to file) and they are not where the maximum values are occurring. Any feedback will be greatly appreciated.
from netCDF4 import Dataset import matplotlib.pyplot as plt import numpy as N from mpl_toolkits.basemap import Basemap from netcdftime import utime from datetime import datetime from numpy import ma as MA import os
TSFCall=[]
for (path, dirs, files) in os.walk(MainFolder): for dir in dirs: print dir path=path+'/' for ncfile in files: if ncfile[-3:]=='.nc': print "dealing with ncfiles:", path+ncfile ncfile=os.path.join(path,ncfile) ncfile=Dataset(ncfile, 'r+', 'NETCDF4') TSFC=ncfile.variables['T_SFC'][:] TIME=ncfile.variables['time'][:] LAT=ncfile.variables['latitude'][:] LON=ncfile.variables['longitude'][:] ncfile.close()
TSFCall.append(TSFC)
big_array=N.ma.concatenate(TSFCall) tmax=MA.max(TSFC) print tmax t=TIME[tmax] indexTIME=N.where(TIME==t) TSFCmax=TSFC[tmax] print t, indexTIME print LAT[tmax], LON[tmax], TSFCmax
cdftime=utime('seconds since 1970-01-01 00:00:00') ncfiletime=cdftime.num2date(t) print ncfiletime timestr=str(ncfiletime) d = datetime.strptime(timestr, '%Y-%m-%d %H:%M:%S') date_string = d.strftime('%Y%m%d_%H%M')
map = Basemap(projection='merc',llcrnrlat=-40,urcrnrlat=-33,llcrnrlon=139.0,urcrnrlon=151.0,lat_ts=0,resolution='i') x,y=map(*N.meshgrid(LON,LAT)) map.tissot(LON[tmax], LAT[tmax], 0.15, 100, facecolor='none', edgecolor='black', linewidth=2) map.readshapefile(shapefile1, 'DSE_REGIONS') map.drawcoastlines(linewidth=0.5) map.drawstates() plt.title('test'+' %s'%date_string) ticks=[-5,0,5,10,15,20,25,30,35,40,45,50] CS = map.contourf(x,y,TSFCmax, 15, cmap=plt.cm.jet) l,b,w,h =0.1,0.1,0.8,0.8 cax = plt.axes([l+w+0.025, b, 0.025, h], ) cbar=plt.colorbar(CS, cax=cax, drawedges=True)
plt.show() plt.close()
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

On Tuesday, December 20, 2011, questions anon <questions.anon@gmail.com> wrote:
ok thanks, a quick try at using it resulted in: IndexError: index out of bounds but I may need to do abit more investigating to understand how it works. thanks
The assumption is that these arrays are all the same shape. If not, then extra work is needed to figure out how to map indices of the temperature array to the indices of the lat and Lon arrays. Ben Root

Thanks for your responses but I am still having difficuties with this problem. Using argmax gives me one very large value and I am not sure what it is. There shouldn't be any issues with the shape. The latitude and longitude are the same shape always (covering a state) and the temperature (TSFC) data are hourly for a whole month. Are there any other ideas for finding the location and time of the maximum value in an array? Thanks On Wed, Dec 21, 2011 at 3:38 PM, Benjamin Root <ben.root@ou.edu> wrote:
On Tuesday, December 20, 2011, questions anon <questions.anon@gmail.com> wrote:
ok thanks, a quick try at using it resulted in: IndexError: index out of bounds but I may need to do abit more investigating to understand how it works. thanks
The assumption is that these arrays are all the same shape. If not, then extra work is needed to figure out how to map indices of the temperature array to the indices of the lat and Lon arrays.
Ben Root
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

On 04.01.2012, at 5:10AM, questions anon wrote:
Thanks for your responses but I am still having difficuties with this problem. Using argmax gives me one very large value and I am not sure what it is. There shouldn't be any issues with the shape. The latitude and longitude are the same shape always (covering a state) and the temperature (TSFC) data are hourly for a whole month.
There will be an issue if not TSFC.shape == TIME.shape == LAT.shape == LON.shape One needs more information on the structure of these data to say anything definite, but if e.g. your TSFC data have a time and a location dimension, argmax will per default return the index for the flattened array (see the argmax documentation for details, and how to use the axis keyword to get a different output). This might be the very large value you mention, and if your location data have fewer dimensions, the index will easily be out of range. As Ben wrote, you'd need extra work to find the maximum location, depending on what maximum you are actually looking for. As a speculative example, let's assume you have the temperature data in an array(ntime, nloc) and the position data in array(nloc). Then TSFC.argmax(axis=1) would give you the index for the hottest place for each hour of the month (i.e. actually an array of ntime indices, and pointer to so many different locations). To locate the maximum temperature for the entire month, your best way would probably be to first extract the array of (monthly) maximum temperatures in each location as tmax = TSFC.max(axis=0) which would have (in this example) the shape (nloc,), so you could directly use it to index LAT[tmax.argmax()] etc. Cheers, Derek

On Wed, 04 Jan 2012 12:29:36 +0100, Derek Homeier wrote:
On 04.01.2012, at 5:10AM, questions anon wrote:
Thanks for your responses but I am still having difficuties with this problem. Using argmax gives me one very large value and I am not sure what it is.
it is the index in the flattened array. To translate this into a multidimensional index, use numpy.unravel_index(i, original_shape). Cheers, Vincent.

Dear numpy community, I'm trying to create an array of type object. A = empty(9, dtype=object) A[ array(0,1,2) ] = MyObject(1) A[ array(3,4,5) ] = MyObject(2) A[ array(6,7,8) ] = MyObject(3) This has worked well until MyObject has gotten an __getitem__ method. Now python (as it is usually supposed to) assigns A[0] to MyObject(1)[0], [1] to MyObject(1)[1] and so on. Is there any way to just get a reference of the instance of MyObject into every entry of the array slice? Thank you for any help on this problem David

You could try A[...].fill(MyObject(...)). I haven't tried it myself, so not sure it would work though... -=- Olivier 2012/1/6 "David Köpfer" <dkoepfer@gmx.de>
Dear numpy community,
I'm trying to create an array of type object.
A = empty(9, dtype=object) A[ array(0,1,2) ] = MyObject(1) A[ array(3,4,5) ] = MyObject(2) A[ array(6,7,8) ] = MyObject(3)
This has worked well until MyObject has gotten an __getitem__ method. Now python (as it is usually supposed to) assigns A[0] to MyObject(1)[0], [1] to MyObject(1)[1] and so on.
Is there any way to just get a reference of the instance of MyObject into every entry of the array slice?
Thank you for any help on this problem David _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

Hi Oliver, thank you very much for your reply, sadly it is not working as you and I hoped. The array still stays at None even after the code. I've also tried A[X] = [MyObject(...)]*len(X) but that just results in a Memory error. So is there really no way to avoid this broadcasting? David -------- Original-Nachricht --------
Datum: Sun, 8 Jan 2012 16:16:33 -0500 Von: Olivier Delalleau <shish@keba.be> An: Discussion of Numerical Python <numpy-discussion@scipy.org> Betreff: Re: [Numpy-discussion] filling an alice of array of object with a reference to an object that has a __getitem__ method
You could try A[...].fill(MyObject(...)). I haven't tried it myself, so not sure it would work though...
-=- Olivier
2012/1/6 "David Köpfer" <dkoepfer@gmx.de>
Dear numpy community,
I'm trying to create an array of type object.
A = empty(9, dtype=object) A[ array(0,1,2) ] = MyObject(1) A[ array(3,4,5) ] = MyObject(2) A[ array(6,7,8) ] = MyObject(3)
This has worked well until MyObject has gotten an __getitem__ method. Now python (as it is usually supposed to) assigns A[0] to MyObject(1)[0], [1] to MyObject(1)[1] and so on.
Is there any way to just get a reference of the instance of MyObject into every entry of the array slice?
Thank you for any help on this problem David _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

Oh, sorry, I hadn't paid enough attention to the way you are indexing A: if you are using an array to index, it creates a copy, so using ".fill" will fill the copy and you won't see the result. Instead, use A[0:3], A[3:6], etc. -=- Olivier 2012/1/9 "David Köpfer" <dkoepfer@gmx.de>
Hi Oliver,
thank you very much for your reply, sadly it is not working as you and I hoped. The array still stays at None even after the code.
I've also tried A[X] = [MyObject(...)]*len(X) but that just results in a Memory error.
So is there really no way to avoid this broadcasting?
David
-------- Original-Nachricht --------
Datum: Sun, 8 Jan 2012 16:16:33 -0500 Von: Olivier Delalleau <shish@keba.be> An: Discussion of Numerical Python <numpy-discussion@scipy.org> Betreff: Re: [Numpy-discussion] filling an alice of array of object with a reference to an object that has a __getitem__ method
You could try A[...].fill(MyObject(...)). I haven't tried it myself, so not sure it would work though...
-=- Olivier
2012/1/6 "David Köpfer" <dkoepfer@gmx.de>
Dear numpy community,
I'm trying to create an array of type object.
A = empty(9, dtype=object) A[ array(0,1,2) ] = MyObject(1) A[ array(3,4,5) ] = MyObject(2) A[ array(6,7,8) ] = MyObject(3)
This has worked well until MyObject has gotten an __getitem__ method. Now python (as it is usually supposed to) assigns A[0] to MyObject(1)[0], [1] to MyObject(1)[1] and so on.
Is there any way to just get a reference of the instance of MyObject into every entry of the array slice?
Thank you for any help on this problem David _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

thanks for the responses. Unfortunately they are not matching shapes
print TSFC.shape, TIME.shape, LAT.shape, LON.shape (721, 106, 193) (721,) (106,) (193,)
So I still receive index out of bounds error:
tmax=TSFC.max(axis=0) numpy array of max values for the month maxindex=tmax.argmax() 2928 maxtemp=tmax.ravel()[maxindex] #or maxtemp=TSFC.max() 35.5 (degrees celcius)
latloc=LAT[tmax.argmax()] IndexError: index out of bounds
lonloc=LON[tmax.argmax()] timeloc=TIME[tmax.argmax()] Any other ideas for this type of situation? thanks On Wed, Jan 4, 2012 at 10:29 PM, Derek Homeier < derek@astro.physik.uni-goettingen.de> wrote:
On 04.01.2012, at 5:10AM, questions anon wrote:
Thanks for your responses but I am still having difficuties with this problem. Using argmax gives me one very large value and I am not sure what it is. There shouldn't be any issues with the shape. The latitude and longitude are the same shape always (covering a state) and the temperature (TSFC) data are hourly for a whole month.
There will be an issue if not TSFC.shape == TIME.shape == LAT.shape == LON.shape
One needs more information on the structure of these data to say anything definite, but if e.g. your TSFC data have a time and a location dimension, argmax will per default return the index for the flattened array (see the argmax documentation for details, and how to use the axis keyword to get a different output). This might be the very large value you mention, and if your location data have fewer dimensions, the index will easily be out of range. As Ben wrote, you'd need extra work to find the maximum location, depending on what maximum you are actually looking for.
As a speculative example, let's assume you have the temperature data in an array(ntime, nloc) and the position data in array(nloc). Then
TSFC.argmax(axis=1)
would give you the index for the hottest place for each hour of the month (i.e. actually an array of ntime indices, and pointer to so many different locations).
To locate the maximum temperature for the entire month, your best way would probably be to first extract the array of (monthly) maximum temperatures in each location as
tmax = TSFC.max(axis=0)
which would have (in this example) the shape (nloc,), so you could directly use it to index
LAT[tmax.argmax()] etc.
Cheers, Derek
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

On Monday, January 9, 2012, questions anon <questions.anon@gmail.com> wrote:
thanks for the responses. Unfortunately they are not matching shapes
print TSFC.shape, TIME.shape, LAT.shape, LON.shape (721, 106, 193) (721,) (106,) (193,)
So I still receive index out of bounds error:
tmax=TSFC.max(axis=0) numpy array of max values for the month maxindex=tmax.argmax() 2928 maxtemp=tmax.ravel()[maxindex] #or maxtemp=TSFC.max() 35.5 (degrees celcius)
latloc=LAT[tmax.argmax()] IndexError: index out of bounds
lonloc=LON[tmax.argmax()] timeloc=TIME[tmax.argmax()]
Any other ideas for this type of situation? thanks
Right, we realize they are not the same shape. When you use argmax on the temperature data, take that index number and use unravel_index(index, TSFC.shape) to get a three-element tuple, each being the index in the TIME, LAT, LON arrays, respectively. Cheers, Ben Root
On Wed, Jan 4, 2012 at 10:29 PM, Derek Homeier <
On 04.01.2012, at 5:10AM, questions anon wrote:
Thanks for your responses but I am still having difficuties with this
There shouldn't be any issues with the shape. The latitude and longitude are the same shape always (covering a state) and the temperature (TSFC) data are hourly for a whole month.
There will be an issue if not TSFC.shape == TIME.shape == LAT.shape == LON.shape
One needs more information on the structure of these data to say anything definite, but if e.g. your TSFC data have a time and a location dimension, argmax will per default return the index for the flattened array (see the argmax documentation for details, and how to use the axis keyword to get a different output). This might be the very large value you mention, and if your location data have fewer dimensions, the index will easily be out of range. As Ben wrote, you'd need extra work to find the maximum location, depending on what maximum you are actually looking for.
As a speculative example, let's assume you have the temperature data in an array(ntime, nloc) and the position data in array(nloc). Then
TSFC.argmax(axis=1)
would give you the index for the hottest place for each hour of the month (i.e. actually an array of ntime indices, and pointer to so many different locations).
To locate the maximum temperature for the entire month, your best way would probably be to first extract the array of (monthly) maximum temperatures in each location as
tmax = TSFC.max(axis=0)
which would have (in this example) the shape (nloc,), so you could
derek@astro.physik.uni-goettingen.de> wrote: problem. Using argmax gives me one very large value and I am not sure what it is. directly use it to index
LAT[tmax.argmax()] etc.
Cheers, Derek
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

thank you, I seem to have made some progress (with lots of help)!! I still seem to be having trouble with the time. Because it is hourly data for a whole month I assume that is where my problem lies. When I run the following code I alwayes receive the first timestamp of the file. Not sure how to get around this: tmax=TSFC.max(axis=0) maxindex=tmax.argmax() maxtemp=tmax.ravel()[maxindex] #or maxtemp=TSFC.max() print maxindex, maxtemp val=N.unravel_index(maxindex, TSFC.shape) listval=list(val) print listval timelocation=TIME[listval[0]] latlocation=LAT[listval[1]] lonlocation=LON[listval[2]] print latlocation, lonlocation cdftime=utime('seconds since 1970-01-01 00:00:00') ncfiletime=cdftime.num2date(timelocation) print ncfiletime On Tue, Jan 10, 2012 at 10:22 AM, Benjamin Root <ben.root@ou.edu> wrote:
On Monday, January 9, 2012, questions anon <questions.anon@gmail.com> wrote:
thanks for the responses. Unfortunately they are not matching shapes
print TSFC.shape, TIME.shape, LAT.shape, LON.shape (721, 106, 193) (721,) (106,) (193,)
So I still receive index out of bounds error:
tmax=TSFC.max(axis=0) numpy array of max values for the month maxindex=tmax.argmax() 2928 maxtemp=tmax.ravel()[maxindex] #or maxtemp=TSFC.max() 35.5 (degrees celcius)
latloc=LAT[tmax.argmax()] IndexError: index out of bounds
lonloc=LON[tmax.argmax()] timeloc=TIME[tmax.argmax()]
Any other ideas for this type of situation? thanks
Right, we realize they are not the same shape. When you use argmax on the temperature data, take that index number and use unravel_index(index, TSFC.shape) to get a three-element tuple, each being the index in the TIME, LAT, LON arrays, respectively.
Cheers, Ben Root
On Wed, Jan 4, 2012 at 10:29 PM, Derek Homeier <
On 04.01.2012, at 5:10AM, questions anon wrote:
Thanks for your responses but I am still having difficuties with this
There shouldn't be any issues with the shape. The latitude and longitude are the same shape always (covering a state) and the temperature (TSFC) data are hourly for a whole month.
There will be an issue if not TSFC.shape == TIME.shape == LAT.shape == LON.shape
One needs more information on the structure of these data to say anything definite, but if e.g. your TSFC data have a time and a location dimension, argmax will per default return the index for the flattened array (see the argmax documentation for details, and how to use the axis keyword to get a different output). This might be the very large value you mention, and if your location data have fewer dimensions, the index will easily be out of range. As Ben wrote, you'd need extra work to find the maximum location, depending on what maximum you are actually looking for.
As a speculative example, let's assume you have the temperature data in an array(ntime, nloc) and the position data in array(nloc). Then
TSFC.argmax(axis=1)
would give you the index for the hottest place for each hour of the month (i.e. actually an array of ntime indices, and pointer to so many different locations).
To locate the maximum temperature for the entire month, your best way would probably be to first extract the array of (monthly) maximum temperatures in each location as
tmax = TSFC.max(axis=0)
which would have (in this example) the shape (nloc,), so you could
derek@astro.physik.uni-goettingen.de> wrote: problem. Using argmax gives me one very large value and I am not sure what it is. directly use it to index
LAT[tmax.argmax()] etc.
Cheers, Derek
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

Do you mean that listval[0] is systematically equal to 0, or is it something else? -=- Olivier 2012/1/9 questions anon <questions.anon@gmail.com>
thank you, I seem to have made some progress (with lots of help)!! I still seem to be having trouble with the time. Because it is hourly data for a whole month I assume that is where my problem lies. When I run the following code I alwayes receive the first timestamp of the file. Not sure how to get around this:
tmax=TSFC.max(axis=0) maxindex=tmax.argmax() maxtemp=tmax.ravel()[maxindex] #or maxtemp=TSFC.max() print maxindex, maxtemp val=N.unravel_index(maxindex, TSFC.shape) listval=list(val) print listval timelocation=TIME[listval[0]] latlocation=LAT[listval[1]] lonlocation=LON[listval[2]] print latlocation, lonlocation
cdftime=utime('seconds since 1970-01-01 00:00:00') ncfiletime=cdftime.num2date(timelocation) print ncfiletime
On Tue, Jan 10, 2012 at 10:22 AM, Benjamin Root <ben.root@ou.edu> wrote:
On Monday, January 9, 2012, questions anon <questions.anon@gmail.com> wrote:
thanks for the responses. Unfortunately they are not matching shapes
print TSFC.shape, TIME.shape, LAT.shape, LON.shape (721, 106, 193) (721,) (106,) (193,)
So I still receive index out of bounds error:
tmax=TSFC.max(axis=0) numpy array of max values for the month maxindex=tmax.argmax() 2928 maxtemp=tmax.ravel()[maxindex] #or maxtemp=TSFC.max() 35.5 (degrees celcius)
latloc=LAT[tmax.argmax()] IndexError: index out of bounds
lonloc=LON[tmax.argmax()] timeloc=TIME[tmax.argmax()]
Any other ideas for this type of situation? thanks
Right, we realize they are not the same shape. When you use argmax on the temperature data, take that index number and use unravel_index(index, TSFC.shape) to get a three-element tuple, each being the index in the TIME, LAT, LON arrays, respectively.
Cheers, Ben Root
On Wed, Jan 4, 2012 at 10:29 PM, Derek Homeier <
On 04.01.2012, at 5:10AM, questions anon wrote:
Thanks for your responses but I am still having difficuties with
There shouldn't be any issues with the shape. The latitude and longitude are the same shape always (covering a state) and the temperature (TSFC) data are hourly for a whole month.
There will be an issue if not TSFC.shape == TIME.shape == LAT.shape == LON.shape
One needs more information on the structure of these data to say anything definite, but if e.g. your TSFC data have a time and a location dimension, argmax will per default return the index for the flattened array (see the argmax documentation for details, and how to use the axis keyword to get a different output). This might be the very large value you mention, and if your location data have fewer dimensions, the index will easily be out of range. As Ben wrote, you'd need extra work to find the maximum location, depending on what maximum you are actually looking for.
As a speculative example, let's assume you have the temperature data in an array(ntime, nloc) and the position data in array(nloc). Then
TSFC.argmax(axis=1)
would give you the index for the hottest place for each hour of the month (i.e. actually an array of ntime indices, and pointer to so many different locations).
To locate the maximum temperature for the entire month, your best way would probably be to first extract the array of (monthly) maximum temperatures in each location as
tmax = TSFC.max(axis=0)
which would have (in this example) the shape (nloc,), so you could
derek@astro.physik.uni-goettingen.de> wrote: this problem. Using argmax gives me one very large value and I am not sure what it is. directly use it to index
LAT[tmax.argmax()] etc.
Cheers, Derek
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

On Mon, Jan 9, 2012 at 7:59 PM, questions anon <questions.anon@gmail.com>wrote:
thank you, I seem to have made some progress (with lots of help)!! I still seem to be having trouble with the time. Because it is hourly data for a whole month I assume that is where my problem lies. When I run the following code I alwayes receive the first timestamp of the file. Not sure how to get around this:
tmax=TSFC.max(axis=0) maxindex=tmax.argmax()
You are computing max(axis=0) first. So, tmax is an array containing the maximum temperature at each lat/lon grid point, over the set of 721 months. It will be a [106, 193] array. So the argmax of tmax is an element in a shape [106,193] array (the number of latitude/number of longitude) not the original three dimension [721, 106, 193] array. Thus when you unravel it you can only get the first time value. I re-read your original post but I don't understand what number you need. Are you trying to get the single max value over the entire array? Or max value for each month? (a 721 element vector)? or something else? Cheers, Aronne

Thank you, thank you, thank you! I needed to find the max value (and corresponding TIME and LAT, LON) for the entire month but I shouldn't have been using the tmax, instead I needed to use the entire array. Below code works for those needing to do something similar. Thanks for all your help everyone! tmax=TSFC.max(axis=0) maxindex=TSFC.argmax() maxtemp=TSFC.ravel()[maxindex] #or maxtemp=TSFC.max() print maxindex, maxtemp val=N.unravel_index(maxindex, TSFC.shape) listval=list(val) print listval timelocation=TIME[listval[0]] latlocation=LAT[listval[1]] lonlocation=LON[listval[2]] print latlocation, lonlocation cdftime=utime('seconds since 1970-01-01 00:00:00') ncfiletime=cdftime.num2date(timelocation) print ncfiletime On Tue, Jan 10, 2012 at 3:28 PM, Aronne Merrelli <aronne.merrelli@gmail.com>wrote:
On Mon, Jan 9, 2012 at 7:59 PM, questions anon <questions.anon@gmail.com>wrote:
thank you, I seem to have made some progress (with lots of help)!! I still seem to be having trouble with the time. Because it is hourly data for a whole month I assume that is where my problem lies. When I run the following code I alwayes receive the first timestamp of the file. Not sure how to get around this:
tmax=TSFC.max(axis=0) maxindex=tmax.argmax()
You are computing max(axis=0) first. So, tmax is an array containing the maximum temperature at each lat/lon grid point, over the set of 721 months. It will be a [106, 193] array.
So the argmax of tmax is an element in a shape [106,193] array (the number of latitude/number of longitude) not the original three dimension [721, 106, 193] array. Thus when you unravel it you can only get the first time value.
I re-read your original post but I don't understand what number you need. Are you trying to get the single max value over the entire array? Or max value for each month? (a 721 element vector)? or something else?
Cheers, Aronne
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
participants (7)
-
"David Köpfer"
-
Aronne Merrelli
-
Benjamin Root
-
Derek Homeier
-
Olivier Delalleau
-
questions anon
-
Vincent Schut