Create a Geotiff file from scattered data in python - python

I have a dataset of points (latitude, longitude, value) and i want to create a Geotiff file from these points.
latitude,longitude and the value are float32 numbers
I want to generate a custom raster that interpolate over those points.
Link
I want to obtain a grid as in the link above and for every point in the grid have a value (computed from original points with some kind of interpolation i guess)

The tutorial you pointed in your question is the theory behind gdal_grid utility:
https://www.gdal.org/gdal_grid.html
Check the bottom of the page for examples of how to interpolate a geotiff from scattered data.
For how to use in python:
https://gis.stackexchange.com/questions/254330/python-gdal-grid-correct-use

Related

Plotting a coastline on cartesian grid in matplotlib

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.

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.

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.

Using python Basemap.pcolormesh with non-monotonic longitude jumps

I have satellite sweep data that I am attempting to plot on a basemap using pcolormesh.
The data is organized as a 2d matrix, (bTemp), with two corresponding 2D arrays lat and lon, that give the corresponding latitude and longitude at each point. So my plotting code looks like
m = Basemap()
m.drawcoastlines()
m.pcolormesh(lon, lat, bTemp)
However, this doesn't quite give me the result I am looking for. A large stripe runs across the map.
I think the cause of this is that my longitude increases non-monotonically along a given ray, at the wrap-around point.
Here is a plot of on ray in my longitude array
plot(lon[100,:])
What would the best way to fix this be, so that the pcolormesh plot just jumps to the other side of the map without connecting the two points with a filled in area?
You could try using numpy.unwrap to unwrap your data. It was designed exactly for this: to unwrap angular jumps (in radians) that are artifacts due to the way how we use single-valued functions.
You need to convert your longitudes back and forth between degrees and radians for this to work, but the gist is this:
import numpy as np
lon_unwrapped = np.rad2deg(np.unwrap(np.deg2rad(lon)))
This will find those jumps in your data that are larger than 180 degrees, and take their 360-degree complement. Example:
np.rad2deg(np.unwrap(np.deg2rad([150,-150])))
# array([ 150., 210.])
Additionally, unwrap accepts ndarray data, and you can tell it along which axes to perform the unravelling. By default this is the last dimension, which seems to be fine for your case, where you want to unwrap lon[i,:].

Interpolation of irregularly spaced data into 3d grid

I'm working with some instrument data that has records the temperature at a specific latitude, longitude, and pressure (height) coordinate. I need to create a 3d grid from this instrument data that I can then use to take a vertical cross sections of the interpolated gridded data. I've looked at pretty much every interpolation function/library I can find and I'm still having trouble just wrapping my head around how to do this.
I'd prefer not to use Mayavi, since it seems to bug out on my school's server and I'd rather not try to deal with fixing it right now.
The data is currently in 4 separate 1d arrays and I used those to mock up some scatter plots of what I'm trying to get.
Here is the structure of my instrument data points:
And here is what I'm trying to create:
Ultimately, I'd like to create some kind of 3d contour from these points that I can take slices of. Each of the plotted points has a corresponding temperature attached to it, which is really what I think is throwing me off in terms of dimensions and whatnot.
There are a few options to go from the unstructured data which you have to a structured dataset.
The simplest option might be to use the scipy interpolate.griddata method which can interpolate unstructured points using, linear or cubic interpolation.
Another option is to define your grid and then average all of the unstructured points which fall into each grid cell, giving you some gridded representation of the data. You could use a tool such as CIS to do this easily (full disclosure, I wrote this package to do exactly this kind of thing).
Or, there are more complicated methods of interpolating the data by trying to determine the most likely value of the grid points based on the unstructured data, for example using kriging with the pyKriging package, though I've never used this.

Categories