0

I'm trying to calculate the 3D divergence field on a layer of the atmosphere (500mb) for wind. The data I have is the U, V and W wind vectors from a grib file, as follows:

DataA = source.select(name='U component of wind',typeOfLevel='isobaricInhPa',level=250)[0]
DataA = DataA.values
DataB = source.select(name='V component of wind',typeOfLevel='isobaricInhPa',level=250)[0]
DataB = DataB.values
DataC = source.select(name='Vertical velocity',typeOfLevel='isobaricInhPa',level=250)[0]
DataC = DataC.values

What I am wondering is how to combine this into one array so that I can calculate the divergence (presumably from np.grad). E.g. I tried:

Data  = np.zeros((3,721,1440))
Data[0:,:] = DataA
Data[1:,:] = DataB
Data[2:,:] = DataC

Plot = np.gradient(Data,axis=0)

But this produces a (3,1,1440) array (which is wrong as the output should be the same dimension as the input data, i.e. a 721 (lat) by 1440 (lon) grid..

J W
  • 617
  • 1
  • 9
  • 28
  • Possible duplicate of [Compute divergence of vector field using python](http://stackoverflow.com/questions/11435809/compute-divergence-of-vector-field-using-python) – Nico Schertler Jul 25 '16 at 13:14
  • I have seen this, but it was for a 2D case and I wasn't sure how to apply it to this data. (Hence I feel this is worthy of a new question) – J W Jul 25 '16 at 13:35
  • The crucial part is the reduction with `np.add`. The gradient alone will not give you the divergence. You will need the sum of the gradient's entries. – Nico Schertler Jul 25 '16 at 14:00
  • Either combination of axis numbers of `Plot = np.sum(np.gradient(Data),axis=0)` produces a (3,#,1440) array. – J W Jul 25 '16 at 15:18

0 Answers0