# 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?

Thank you in advance.

• 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

## 1 Answer

It should be noted that NetCDF data just describes the format that the data is in. One dataset that I know of (IBTrACS) contains data that is not gridded data, and also contains a landmask. But this is besides your question.

To rephrase your question: How can I determine the orientation of the coastline in a gridded dataset?

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)$$

• Thank you for your answer. I really like your idea of doing the gradient of L. I must say though, that I find it awkward the part of your answer that reads "Well, one way is using mathematics." -- was this really necessary? It reads to me a bit like "Well, use your brain". As if "using mathematics" had not occurred to me. This is not encouraging for people to ask further questions. But thank you for the good answer again. Jun 9 at 6:43
• @ouranos I am 100 % sure that BaroclinicCplusplus meant nothing of the sort. He was not being condescending at all. "Well one way is using mathematics" is just an expression that says I am going to be using some Math to explain my answer. That's it. Jun 9 at 9:21
• Thanks @gansub. Yes, I initially was going to say 'vector calculus', but at some point changed it to just 'mathematics' since there isn't actual calculus (the use of infinitesimals) involved. I'm fairly certain there are other ways to do it, not using strict mathematics. GIS may have one solution, for example. Jun 9 at 15:50