4

I am trying to calculate relative vorticity i.e dV/dX - dU/dY and I am currently using the gradient function on numpy. Here is my code below. I would like to know if there is a better way of doing this instead of trying to reshape the array when I want to do dU/dY. Is there a better way of doing differentiation given a Two matrices with numbers only say U and Y and I would like to do differentiate U wrt Y.

import numpy as np
import netCDF4
import matplotlib.pyplot as plt
from numpy import *
import decimal
from netCDF4 import Dataset

ncfile= Dataset('test.nc','r')

#--------------------Reading in Variables---------------------------------#

lon      = ncfile.variables['lon'][:]
lat      = ncfile.variables['lat'][:]
UWind850 = ncfile.variables['U'][:,22,:,:] (time, level,lat,lon)
VWind850 = ncfile.variables['V'][:,22,:,:] (time, level,lat,lon)
time     = ncfile.variables['time'][:] 
MSLP     = ncfile.variables['PSL'][:]

# Variable[time,Longitude,Latitude]
#These values are equivalent to I,J and L in the netCDF file

t = 30  #time
x = 300 #longitude 
y = 240 #latitude


#-----------------------Calculating Vorticity-----------------------------#

dX = np.gradient(lon) #shape 300
dY = np.gradient(lat) #shape 240

#VWind850.shape (30,240,300)
#UWind850.shape (30,240,300)

dV = (np.gradient(VWind850))

#dV.shape(3,30,240,300) --The extra "3" dimension is caused by the gradient because 
#Its creating a Matrix for gradients by (time,latitude,longitude)

Vgradient =  dV[2]/dX



UWindTemp = np.reshape(UWind850,(30,300,240)) # I am reshaping so I can divide by dY



dU = (np.gradient(UWindTemp))
Ugradient =  dU[2]/dY
Ugradient = np.reshape(Ugradient,(30,240,300)) # Taking it back to normal


VORT= Vgradient - Ugradient 

# VORT.shape(time, latitude, longitude)
#-------------------------------------------------------------------------#

0 Answers0