# How to determine the orientation of coastline from NetCDF file

I have a NetCDF file (gridded data) with a landsea mask. I would like to determine, for the coastal grid-cells, what the orientation of the coastline is. I need to do this in order to calculate the angle between incident wind direction and the coastline at each coastal grid cell.

For example, if at a certain location the coastline is zonal (east to west) and the land is to the south, and the sea to the north, the orientation would be 0°:

If the coast is zonal but the position of land and ocean are inverted, the orientation of the coast would be 180°.

If the coast is 45° with the land to the south, the angle is 45°. With the land to the north, the orientation is 135°... you get the idea.

Any suggestions on how to get this information from the landsea mask grid?

• Gridded data is orthogonal by definition, so you either have 0/180° or 90/270°.
– Erik
Jul 12 '19 at 7:35
• There are sometimes curvilinear grids, like those with 3 poles or 2 poles and one over Greenland. May 14 '20 at 7:13
• Perhaps it may be best to take like a regional linear average of the coast and then calculate as Baroclinic suggested? Perhaps use a running mean over a handful of data points. But of course that means transferring to something "vector" instead of "raster"? I don't know, thinking out loud, I was considering this challenge a bit years ago for hurricanes, but never got to it. Jun 9 at 4:08

Well, one way is using mathematics. Let's call the boolean landmask variable $$L$$, where $$L=\{^{0 \text{ if land}}_{1\text{ if water}}$$ Then the gradient is $$\nabla L =\{^{<\frac{\delta L}{\delta x},\frac{\delta L}{\delta y}> \text{ at the coastline}}_{\vec{0} \text{ not at the coastline}}$$ Let's look at just the coastline. The angle is $$\arctan2(\frac{\delta L}{\delta x},\frac{\delta L}{\delta y})$$.
Now if you want to find out the angle difference between the coast and the wind, then you can use the properties of the dot product to find the angle difference. That is $$\delta \phi=\arccos\left(\frac{\nabla L \cdot \vec{v}}{|\nabla L||\vec{v}|}\right)=\arccos\left(\frac{u\frac{\delta L}{\delta x}+v\frac{\delta L}{\delta y}}{\sqrt{u^2+v^2}\sqrt{\left(\frac{\delta L}{\delta x}\right)^2+\left(\frac{\delta L}{\delta y}\right)^2}}\right)$$