Plotting grid data

Mauro Cavalcanti wrote:

Dear Jeff,

2008/12/9 Jeff Whitaker <jswhit@...146...>:
  

Mauro: The shape of x, y, and dat must all be (nlats, nlons).
    
I tried to do:

coords = numpy.loadtxt('grid.dat')
lon = coords[:,0]
lat = coords[:,1]
dat = coords[:,2]
X, Y = numpy.meshgrid(lon, lat)
nlons = X.shape[1]
nlats = X.shape[0]
Z = dat.reshape(nlats,nlons)

(so that I could proceed to plotting with the X,Y,Z arrays), but got
another error:

"ValueError: total size of new array must be unchanged"

and with this I get lost.... :frowning:

I tried to follow the guidelines you provided for Mr. Tim Michelsen on
a previous post
(http://www.nabble.com/plotting-a-contour-map-from-CSV-file-td18440459.html)
but it didn't help (however, that provided me with a solution to
another problem!).

Best regards,

Mauro: This will only work is dat is length(nlons*nlats). Is it?

-Jeff

···

--
Jeffrey S. Whitaker Phone : (303)497-6313
NOAA/OAR/CDC R/PSD1 FAX : (303)497-6449
325 Broadway Boulder, CO, USA 80305-3328

Mauro Cavalcanti wrote:
I tried to do:

coords = numpy.loadtxt('grid.dat')
lon = coords[:,0]
lat = coords[:,1]
dat = coords[:,2]
X, Y = numpy.meshgrid(lon, lat)
nlons = X.shape[1]
nlats = X.shape[0]
Z = dat.reshape(nlats,nlons)

(so that I could proceed to plotting with the X,Y,Z arrays), but got
another error:

"ValueError: total size of new array must be unchanged"

and with this I get lost.... :frowning:

Hi Mauro,

As Jeff noted earlier, the number of elements in an array can't be
increased simply by reshaping it, so the error message isn't too
mysterious, if carefully considered.

This might be a good time to step back from the code for a moment and
consider the problem that needs to be solved.

As I understand it:

1. You have a small number of scattered data values in 'grid.dat' with
their locations specified by lat/lon co-ordinates.
2. You want to create a regular square/rectangular grid that spans
your region of interest and assign the values in 'grid.dat' to the
grid box in which they fall, with all other regions of the grid being
masked.
3. You then want to plot the masked rectangular grid onto a Basemap instance.

The attached script shows one way to do this (using the imshow
method), I've left the task assigning the data values to the correct
grid box for you..

Cheers,
Scott

temp.py (1.87 KB)

Dear Scott & Jeff,

Sorry for the delay in replying, I was off office for a while.

2008/12/9 Scott Sinclair <scott.sinclair.za@...287...>:

As Jeff noted earlier, the number of elements in an array can't be
increased simply by reshaping it, so the error message isn't too
mysterious, if carefully considered.

Sure, I got lost because I tried to follow the solution previously
presented in the posting I referred to, since the example data I
provided are identical in format to that used in that solution (a
3-column space delimited file, with the first two representing the
longitude and latitude, and the 3rd representing the variable of
interest).

1. You have a small number of scattered data values in 'grid.dat' with
their locations specified by lat/lon co-ordinates.
2. You want to create a regular square/rectangular grid that spans
your region of interest and assign the values in 'grid.dat' to the
grid box in which they fall, with all other regions of the grid being
masked.
3. You then want to plot the masked rectangular grid onto a Basemap instance.

You did understood it very correctly, and of course offered a much
better description than I could provide myself using my pidgin English
:-). Indeed, there is just one detail to add to the above problem
description, and this is the fact that the "region of interest" may
span the entire world (land or sea, depending on the biological
organism under study).

The attached script shows one way to do this (using the imshow
method), I've left the task assigning the data values to the correct
grid box for you..

Thank you very much, I feel somewhat ashamed! Anyway, the final
product of this project will be made freely available under GPL, so it
will belong to everyone (and due credit will be given to all of you
meteorologists, physicists, mathematicians, etc. that have been so
helpful!)

Returning to the techical aspects of the problem, it is indeed the
assignment of data values to the correct grid box that lays in the
core of the second part of my question -- it may indeed be not so
difficult as the previous requirement, that is to build the data file
(with the data values of interest) *before* the grid analysis, from
simple lat-long files for each species under study (usually up to 20,
but may be more). But I will develop this further.

One more detail: is it possible to plot the value of the variable of
interest, corresponding to each square of the grid, inside the
respective square?

With warmest regards,

···

--
Dr. Mauro J. Cavalcanti
Ecoinformatics Studio
P.O. Box 46521, CEP 20551-970
Rio de Janeiro, RJ, BRASIL
E-mail: maurobio@...287...
Web: http://studio.infobio.net
Linux Registered User #473524 * Ubuntu User #22717
"Life is complex. It consists of real and imaginary parts."