[PYTHON MATRIX-SIG] Creating grid object
David Ascher
da@maigret.cog.brown.edu
Thu, 10 Oct 1996 15:57:43 -0400 (EDT)
> Hello, I want to use Numpy (is this name "official"?) to build an
> analysis package for a numerical model. At the moment I implement some
> simple functions, which are working on array-arguments. Now I get the
> idea, that it would nice to have an array-object of a variable say
> salinity, with an attribut about the edges of the grid the variable is
> defined on, so that I can work on arbitray blocks and can get the
> information where this block is later.
I didn't see any response to this message, so I'll give it a shot.
I'm not sure I entirely understand your requirements, but here's what I
think I read in your message.
You want to define a new object type (call it a class) which
has some of the attributes of a python multiarray, and some
attributes which are specific to your application (salinity, etc.).
This I would do by subclassing the UserArray class which is part of the
Lib directory of NumPy. It is basically a class wrapper around the
builtin python type.
> I want to define the hole grid as a global variable and let the edges
> in the new object point at a subregion of the grid. I'm new to python
> and object-oriented programming, but I think that I must define my own
> class. My problem now is, that I want to do normal math directly with
> this object, like
>
> sal_anomaly_on_grid = sal_on_grid - mean_sal_in_depth_on_this_grid
>
> ^new object ^new object ^normal 1d array
>
> At the moment I know only to do something like
>
> sal_anomaly_on_grid = sal_on_grid.data - mean_sal_in_depth_on_this_grid
>
> ^normal array ^new object ^normal array
>
> My grid is defined in two 2d-arrays x_loc,y_loc and one 3d-array z with
> depth of x,y in n layers. My first idea is to have an attribut in the new
> object, wich is a list of 16 points
>
> x_loc(index_of_x_starting_location)
> x_loc(index_of_y_starting_location)
> x_loc(index_of_x_stop_location)
> x_loc(index_of_y_stop_location)
> 4 y_loc(...)
> 8 z
>
> But it would be better, to have references to each gridpoint, to allow
> more flexibel grids, but this is perhaps memory-consuming.
Ah, I think I understand better now. You want to have a big array which
stores all your data, and then have other objects which are in some ways
like 'enhanced slices'. In other words, they point to a subset of the
big array, but they are not just slices because they have other
properties. In other words, you want a UserSlice object, I think. That
doesn't exist to my knowledge, but it certainly seems a worthy project.
Am I paraphrasing your needs correctly? I suspect that the key to doing
this is understanding UserArray.py and slice() well. Remember that the
slice() operator returns an 'array by reference':
>>> a = arange(10)
>>> a
0 1 2 3 4 5 6 7 8 9
>>> b = a[3:5]
>>> b
3 4
>>> a[3] = 0
>>> a
0 1 2 0 4 5 6 7 8 9
>>> b
0 4
[Jim: is UserArray up to date?]
> Is it
> possible to build something like this? Is the definition of such
> classes covered in one of the new python-books? Or must I program
> something in C (I hope not).
I suspect that Mark and Aaron's books cover class creation, maybe
'class-around-type' creation, but probably not the specific numeric
array object. I don't think you need to do any C for this.
> Last but not least, is this group the right place to ask? I think I
> will have more questions in the future.
Yes.
=================
MATRIX-SIG - SIG on Matrix Math for Python
send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
=================