Plotting a coastline on cartesian grid in matplotlib - python

I am making maps of meteorological data (x,y-coordinates in m) using matplotlib.pyplot.contourf(). I want to plot a coastline, but all the examples I find on internet use lat-lon data (with cartopy or basemap).
Is there a way (without transforming the data to a lat-lon grid) to plot a coastline on my cartesian grid? I know size of the grid, and its center's lat-lon coordinates.
I haven't tried anything but look for similar examples, which I could not find.

The solution is to use cartopy's gnomonic projection: https://scitools.org.uk/cartopy/docs/v0.15/crs/projections.html#gnomonic , e.g.
proj =ccrs.Gnomonic(central_latitude=0, central_longitude= 0)
The origin of the data need to be specified (in lat-lon), and it expects the data coordinates to be distance in meters from that origin. Then, the normal cartopy features (like coastlines) work as usual.

Related

How to create a curvilinear coordinate grid from scrach with python?

I have a NetCDF obtained from a WRF simulation with a curvilinear grid with the following attributes:
points=48000 (250x192)
XLONG : -46.01144 to 42.05725 degree_east
XLAT : 24.87103 to 63.47381 degree_north
20km of horizontal resolution
I want to create new coordinates for the same domain, but with a horizontal resolution of 5km. I would like to do it with xarray if it's possible.
At present it seems difficult to deal with curvilinear grids with xarray (https://github.com/pydata/xarray/issues/2281)
But you can use cdo to transform your grid to a regular grid using cdo (http://gradsusr.org/pipermail/gradsusr/2014-February/036493.html)
And then you can actually regrid to any resolution you may want.
You can follow the example at http://xarray.pydata.org/en/stable/interpolation.html#example to interpolate to a higher resolution.

Is it an orthogonal projection in the Nearside Perspective?

I'm currently working on world map visualizations. For now, I can use a home-made software for visualizations and point projections (Java), but I would like to upgrade the soft to be able to use a similar tool in Python.
Thus, I wanted to use cartopy with the module PROJ4, not to re-code everything, and use the wonderfull existing libraries.
It perfectly works for the PlateCarree projection, but when I want to use the Nearside Perspective, I observe a small difference between the two methods.
The two following pictures are extracted from the Java software (1) and the cartopy plot (2).
Cartopy (0.17) is used with matplotlib (3.0.2) and proj4 (4.9.1). In both pictures, we are observing at lon=lat=0° and at 400 km.
Here is the first image (Java):
Java visualization
Here is the second one (Cartopy):
Cartopy representation
As one can observe, lands are over-represented in the cartopy plot. Asuming that I want to get exactly the same projection as the one in my Java software (same representation as the "TrueView angles" in Telecom fields), I discovered in the cartopy crs module:
class NearsidePerspective(_Satellite):
"""
Perspective view looking directly down from above a point on the globe.
In this projection, the projected coordinates are x and y measured from
the origin of a plane tangent to the Earth directly below the perspective
point (e.g. a satellite).
"""
So I got this question: which projection is this about? Are the angles kept, which would means that I have an undetected problem? Or is it an orthogonal projection on the tangent plane? In this case, angles are not conserved, and I would need a solution to apply another projection (the correct one in my case). I might use the wrong one...
Thanks for your time,
Lou
I'm not sure if it's an orthogonal projection, but what CartoPy is using is directly from Proj4:
https://proj4.org/operations/projections/nsper.html
I think coordinates in this Nearside Perspective coordinates are Cartesian distances (distances from the origin on a plane), not angles. It sounds like angles are what's being used for your projection. Have you looked at using the Geostationary projection, but with a different satellite height?
https://scitools.org.uk/cartopy/docs/latest/crs/projections.html#geostationary
I can say that in this projection, the coordinates are angles (multiplied by the satellite height). Might be what you're looking for.

Seam where longitude wraps around in matplotlib/cartopy Mollweide projection

I am plotting some NETCDF data, handled by xarray in Matplotlib using a Cartopy wrapper for the map projections. When I produce a filled contour plot (contourf), I end up with a 'seam' at longitude zero (where my data longitudes begin and end). Is there an in-line way to interpolate between longitude[-1] and longitude[0], perhaps by treating the data as periodic or something?
Please see the attached graphic for an example:
You should add a cyclic point to your data array and its longitude coordinate using the add_cyclic_point function: http://scitools.org.uk/cartopy/docs/v0.16/cartopy/util/util.html#cartopy.util.add_cyclic_point.

How to slice 2D grid from 3D irrigular data?

I have some data of soil's moisture content (Theta) in the form of 3D-domain points (CSV file of the columns x, y, z, Theta). I want to take cross sections from the 3D domain in some specific positions (section ABCD in the figure). I want to calculate the value of Theta in a 5*5 grid in the cross-section, but the points around each node of the grid are not coplanar with the unknown point. I did this before for 2D domains in python, but the 3D domains seem more complicated for me. I found that plotly can make something like that in its virtual environment but I want this to output a numpy array or pandas DataFrame to draw it as a contour in the jupyter notebook.
I know that finding the grid involves finding the value of each point like P0 in the figure by interpolation or gridding from its neighbors, then to draw the cross section using matplotlib, but I don' know how to do it.
Related question, Is slicing 2D grids from 3D grids available in matplotlib or similar libraries?
Thanks for all help.
The underlying problem is 3D interpolation. There are numerous packages which can do this type of thing, or you can write your own (using, e.g. KDE, which is basically just a type of smoothing/binning). There is a lot of material on the topic, like
This answer https://stackoverflow.com/a/15753011/230468
The scipy docs
This extensive set of option on scicomp.stack
And this blog post (with some good examples)
Have you tried playing with pyugrid? It's a library specifically for manipulating unstructured grids, so it sounds like it might be of some use to you. Check out these example notebooks.

How interpolate 3D coordinates

I have data points in x,y,z format. They form a point cloud of a closed manifold. How can I interpolate them using R-Project or Python? (Like polynomial splines)
It depends on what the points originally represented. Just having an array of points is generally not enough to derive the original manifold from. You need to know which points go together.
The most common low-level boundary representation ("brep") is a bunch of triangles. This is e.g. what OpenGL and Directx get as input. I've written a Python software that can convert triangular meshes in STL format to e.g. a PDF image. Maybe you can adapt that to for your purpose. Interpolating a triangle is usually not necessary, but rather trivail to do. Create three new points each halfway between two original point. These three points form an inner triangle, and the rest of the surface forms three triangles. So with this you have transformed one triangle into four triangles.
If the points are control points for spline surface patches (like NURBS, or Bézier surfaces), you have to know which points together form a patch. Since these are parametric surfaces, once you know the control points, all the points on the surface can be determined. Below is the function for a Bézier surface. The parameters u and v are the the parametric coordinates of the surface. They run from 0 to 1 along two adjecent edges of the patch. The control points are k_ij.
The B functions are weight functions for each control point;
Suppose you want to approximate a Bézier surface by a grid of 10x10 points. To do that you have to evaluate the function p for u and v running from 0 to 1 in 10 steps (generating the steps is easily done with numpy.linspace).
For each (u,v) pair, p returns a 3D point.
If you want to visualise these points, you could use mplot3d from matplotlib.
By "compact manifold" do you mean a lower dimensional function like a trajectory or a surface that is embedded in 3d? You have several alternatives for the surface-problem in R depending on how "parametric" or "non-parametric" you want to be. Regression splines of various sorts could be applied within the framework of estimating mean f(x,y) and if these values were "tightly" spaced you may get a relatively accurate and simple summary estimate. There are several non-parametric methods such as found in packages 'locfit', 'akima' and 'mgcv'. (I'm not really sure how I would go about statistically estimating a 1-d manifold in 3-space.)
Edit: But if I did want to see a 3D distribution and get an idea of whether is was a parametric curve or trajectory, I would reach for package:rgl and just plot it in a rotatable 3D frame.
If you are instead trying to form the convex hull (for which the word interpolate is probably the wrong choice), then I know there are 2-d solutions and suspect that searching would find 3-d solutions as well. Constructing the right search strategy will depend on specifics whose absence the 2 comments so far reflects. I'm speculating that attempting to model lower and higher order statistics like the 1st and 99th percentile as a function of (x,y) could be attempted if you wanted to use a regression effort to create boundaries. There is a quantile regression package, 'rq' by Roger Koenker that is well supported.

Categories