<p>The easy answer is to use finite difference. Which one is entirely your choice. For example, I'll choose a simple centered difference scheme.</p>

<p>You say you have a grid, and your point ($x$,$y$) is at the ith and jth gridpoint, where i indicates the left-right index on the grid and j indicates the north-south index on the grid. Therefore $u$ and $v$ can be expressed as $u(x_{i,j},y_{i,j})$ and $v(x_{i,j},y_{i,j})$.</p>

<p>$$\frac{du}{dx}\approx\frac{\Delta u}{\Delta x}=\frac{u(x+\Delta x,y)-u(x-\Delta x,y)}{\Delta x}=\frac{u(x_{i+1,j},y)-u(x_{i-1,j},y)}{x_{i+1,j}-x_{i-1,j}}=\frac{u_{i+1,j}-u_{i-1,j}}{x_{i+1,j}-x_{i-1,j}}$$</p>

<p>A similar process can be applied to the v component, just change the index, and the variables</p>

CC BY-SA 3.0
2017-06-28T14:44:36.230Z
2017-06-28T14:44:36.230Z
BarocliniCplusplus
<p>I imagine you have a horizontal grid (x,y) and wind components u(x,y) and v(x,y).</p>

<p>Normally we do 1-degree of latitude constant (y-dimension) and equal to 110000 m. So your <code>dy</code> depends on how many degrees per grid point you have (yresolution). However, <code>dx</code> will vary according to the latitude.</p>

<p><code>dy=110000*yesolution;</code></p>

<p>Now, we may use <strong>centered finite differences</strong> to compute what you want. (Below is a Matlab code, but I believe generic enough to be reproducible in other languages).</p>

<p><code>for y=2:length(lat)-1
     dx=abs(110000*cos(latx(y)*(2*pi/360))*xresolution);
     for x=2:length(lon)-1
          div(x,y,1)= (u(x+1,y,1)-u(x-1,y,1))/(2*dx) + (v(x,y+1,1)-v(x,y-1,1)/(2*dy);
     end
 end</code></p>

<p>Note that you will have an empty frame around your divergence field when x=1, x=max(x), y=1 and y=max(y), once they do not have two neighboors to compute the differences. The same is observed when you do this with <code>hdivg</code> function in Grads, for example. </p>

<p>By the way, this is based on and yields the same result as Grads (I have checked!) using <a href="http://cola.gmu.edu/grads/gadoc/gradfunccdiff.html" rel="nofollow noreferrer">cdiff</a> to reproduce <code>hdivg</code>.</p>

<p>Also note that the cosine function here works with radians, so if you compute cosines in degrees directly (i.e. <code>cosd</code> function in Matlab), you should omit the term scaling by <code>(2*pi/360)</code>, which is just a conversion.</p>

<p>Hope it helps!</p>

CC BY-SA 4.0
2019-03-22T10:55:10.613Z
Checking this answer back a while later, you are correct @gansub about the boudaries. Computing divergence and vorticity with GrADS, using `hdivg` and `hcurl`, also return missing values at the boundary grid cells. I guess it's fine for most applications.
ouranos
CC BY-SA 3.0
2017-06-22T20:09:28.207Z
2017-07-02T22:42:39.697Z
2017-07-02T22:42:39.697Z
ouranos
<p>I have $u$-wind, $v$-wind and specific humidity. I would like to compute moisture flux convergence at a grid point. So I need to compute the value of $q(\frac{du}{dx} + \frac{dv}{dy})$</p>

<p>My question is how to compute $\frac{du}{dx}$ and $\frac{dv}{dy}$ from $u$-wind and $v$-wind at gird point $(x,y)$? </p>

CC BY-SA 3.0
2017-02-09T14:07:50.953Z
I did not do that. Moreover, that was with NCL. I would like to do it using a program like FORTRAN code or shell script.
Kay
CC BY-SA 3.0
2017-02-09T16:21:35.607Z
For this calculation you probably need the _u_-wind and _v_-wind of the neighboring grid cells as well as the distances to the neighboring grid cells.
FuzzyLeapfrog
CC BY-SA 3.0
2017-02-09T21:11:00.930Z
See item 3 under http://earthscience.stackexchange.com/questions/19/how-do-weather-models-work/666#666
milancurcic
CC BY-SA 3.0
2017-02-10T00:46:48.433Z
@milancurcic : Thank you very much. That is exactly what I was looking for.
Kay
CC BY-SA 3.0
2017-02-10T00:52:38.260Z
@FuzzyLeapfrog : Thanks a lot. Yes, you are right, I need to consider the neighboring grid cells also.
Kay
CC BY-SA 3.0
2017-02-10T00:58:27.133Z
@gansub : Thank you very much for your comment. I had searched a lot in google. But my doubt got cleared here by FuzzyLeapfrog and milancurcic.
Kay
CC BY-SA 4.0
2019-01-24T14:10:06.903Z
@Kay, are any of the answers provided fitting your question? Please consider accepting them :)
ouranos
<meteorology><atmosphere><atmosphere-modelling><moisture-flux>
How to compute du/dx and dv/dy in moisture flux convergence?
Kay
# How to compute du/dx and dv/dy in moisture flux convergence?

(Asked by: username_0 on 2017-02-09)

I have $u$-wind, $v$-wind and specific humidity. I would like to compute moisture flux convergence at a grid point. So I need to compute the value of $q(\frac{du}{dx} + \frac{dv}{dy})$

My question is how to compute $\frac{du}{dx}$ and $\frac{dv}{dy}$ from $u$-wind and $v$-wind at gird point $(x,y)$?

**Tags:** meteorology, atmosphere, atmosphere-modelling, moisture-flux

> **Comments:**
> **username_0**: I did not do that. Moreover, that was with NCL. I would like to do it using a program like FORTRAN code or shell script.  
> **username_1**: For this calculation you probably need the _u_-wind and _v_-wind of the neighboring grid cells as well as the distances to the neighboring grid cells.  
> **username_2**: See item 3 under http://earthscience.stackexchange.com/questions/19/how-do-weather-models-work/666#666  
> **username_0**: @username_2 : Thank you very much. That is exactly what I was looking for.  
> **username_0**: @username_1 : Thanks a lot. Yes, you are right, I need to consider the neighboring grid cells also.  
> **username_0**: @gansub : Thank you very much for your comment. I had searched a lot in google. But my doubt got cleared here by username_1 and username_2.  
> **username_3**: @username_0, are any of the answers provided fitting your question? Please consider accepting them :)  

---

## 2 answers

### Answer by username_3 on 2017-06-22. Score: 5

I imagine you have a horizontal grid (x,y) and wind components u(x,y) and v(x,y).

Normally we do 1-degree of latitude constant (y-dimension) and equal to 110000 m. So your `dy` depends on how many degrees per grid point you have (yresolution). However, `dx` will vary according to the latitude.

`dy=110000*yesolution;`

Now, we may use **centered finite differences** to compute what you want. (Below is a Matlab code, but I believe generic enough to be reproducible in other languages).

`for y=2:length(lat)-1
dx=abs(110000*cos(latx(y)*(2*pi/360))*xresolution);
for x=2:length(lon)-1
div(x,y,1)= (u(x+1,y,1)-u(x-1,y,1))/(2*dx) + (v(x,y+1,1)-v(x,y-1,1)/(2*dy);
end
end`

Note that you will have an empty frame around your divergence field when x=1, x=max(x), y=1 and y=max(y), once they do not have two neighboors to compute the differences. The same is observed when you do this with `hdivg` function in Grads, for example.

By the way, this is based on and yields the same result as Grads (I have checked!) using [cdiff](http://cola.gmu.edu/grads/gadoc/gradfunccdiff.html) to reproduce `hdivg`.

Also note that the cosine function here works with radians, so if you compute cosines in degrees directly (i.e. `cosd` function in Matlab), you should omit the term scaling by `(2*pi/360)`, which is just a conversion.

Hope it helps!

> **Comments:**
> **username_3**: Checking this answer back a while later, you are correct @gansub about the boudaries. Computing divergence and vorticity with GrADS, using `hdivg` and `hcurl`, also return missing values at the boundary grid cells. I guess it's fine for most applications.  

---

### Answer by username_4 on 2017-06-28. Score: 3

The easy answer is to use finite difference. Which one is entirely your choice. For example, I'll choose a simple centered difference scheme.

You say you have a grid, and your point ($x$,$y$) is at the ith and jth gridpoint, where i indicates the left-right index on the grid and j indicates the north-south index on the grid. Therefore $u$ and $v$ can be expressed as $u(x\_{i,j},y\_{i,j})$ and $v(x\_{i,j},y\_{i,j})$.

$$\frac{du}{dx}\approx\frac{\Delta u}{\Delta x}=\frac{u(x+\Delta x,y)-u(x-\Delta x,y)}{\Delta x}=\frac{u(x\_{i+1,j},y)-u(x\_{i-1,j},y)}{x\_{i+1,j}-x\_{i-1,j}}=\frac{u\_{i+1,j}-u\_{i-1,j}}{x\_{i+1,j}-x\_{i-1,j}}$$

A similar process can be applied to the v component, just change the index, and the variables


---

