svgspnr svgspnr - 1 year ago 120
Python Question

How to concatenate data from multiple netCDF files with Python

I have some netCDF files, 24 for each of the directions (

) and 24 with values for different times. At the final point I have to plot the data for all time steps.

For the plotting I need to interpolate at specific point so I have to knew the nearest neighbor. My plan is to divide the data in to 3D cells so I don't have to search the nearest neighbor in the whole dataset.

So in my first step I read in my data files and create an array witch contains
the coordinates of each point and the value for each time.

After that I calculate for each point the cell it belongs to and append it to a Array of 4 dimensions:

for vec in vecs:
x_ind = int((vec[0]-xmin) / stepWidthX)
y_ind = int((vec[1]-ymin) / stepWidthY)
z_ind = int((vec[2]-zmin) / stepWidthZ)

if x_ind==gridPointsInXdirection:
x_ind = x_ind-1
if y_ind==gridPointsInYdirection:
y_ind = y_ind-1
if z_ind==gridPointsInZdirection:
z_ind = z_ind-1
#print z_ind, y_ind,x_ind

XGridPoints[z_ind, y_ind, x_ind] = np.append(XGridPoints[z_ind, y_ind, x_ind], vec[0])
YGridPoints[z_ind, y_ind, x_ind] = np.append(YGridPoints[z_ind, y_ind, x_ind], vec[1])
ZGridPoints[z_ind, y_ind, x_ind] = np.append(ZGridPoints[z_ind, y_ind, x_ind], vec[2])
VGridPoints[z_ind, y_ind, x_ind] = np.append(VGridPoints[z_ind, y_ind, x_ind], vec[3])

is the array with all data points. So far it's working but my problem now is in
: I have a long list of values and not a list of arrays. Is there a solution to append an array to an array element so that I can access it later something like:

x = XGridPoints[2,3,4][2]
y = YGridPoints[2,3,4][2]
z = ZGridPoints[2,3,4][2]
v[:] = VGridPoints[2,3,4][2]

When I take only one time step it's working but I have a large overdrive if I recalculate the cells and the nearest neighbour for each time step and they do not change the location over time.

Answer Source

Numpy is generally much more convenient if you know a priori the shape of the arrays you'll be working with. Things like appending to arrays suffer a performance penalty. I agree with Sebastian, that the easiest way (if possible) is to create an array large enough to hold everything (worst case scenario). If this is not possible then perhaps you can try with object arrays. For example, create an object array with the 3 spatial dimensions:

import numpy as N
XGridPoints = N.empty((nx, ny, nz), dtype='object')

(and the same for YGridPoints, ZGridPoints, VGridPoints.) And then you can set XGridPoints[z_ind, y_ind, x_ind] to a numpy array and append to that array as you need.