-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Docs improvements and minor bug fixes (#22)
- Loading branch information
Showing
22 changed files
with
371 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.. _recipes: | ||
|
||
Recipes | ||
======= | ||
|
||
Plotting | ||
-------- | ||
|
||
How to make nice looking plots. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
=========================== | ||
Plot on a google static map | ||
=========================== | ||
In this script, we use `motionless <https://github.com/ryancox/motionless>`_ | ||
to download an image from the `google static map API <http://code.google.com/apis/maps/documentation/staticmaps/>`_ | ||
and plot it on a :py:class:`~salem.Map`. We then add information to the map | ||
such as a glacier outline (from the `RGI <http://www.glims.org/RGI/>`_), | ||
and ground penetrating radar measurements (GPR, from the | ||
`GlaThiDa <http://www.gtn-g.ch/data_catalogue_glathida/>`_ database). | ||
The GPR measurements were realized in 1997, the glacier outlines are from | ||
2003, and the map background is from 2016. This illustrates the retreat of | ||
the Kesselwandferner glacier. | ||
""" | ||
|
||
import numpy as np | ||
import pandas as pd | ||
import salem | ||
from salem import get_demo_file, DataLevels, GoogleVisibleMap, Map | ||
import matplotlib.pyplot as plt | ||
|
||
# prepare the figure | ||
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5)) | ||
|
||
# read the shapefile and use its extent to define a ideally sized map | ||
shp = salem.read_shapefile(get_demo_file('rgi_kesselwand.shp')) | ||
g = GoogleVisibleMap(x=[shp.min_x, shp.max_x], y=[shp.min_y, shp.max_y], | ||
maptype='satellite') # try out: 'terrain' | ||
|
||
# the google static image is a standard rgb image | ||
ggl_img = g.get_vardata() | ||
ax1.imshow(ggl_img) | ||
ax1.set_title('Google static map') | ||
|
||
# make a map of the same size as the image | ||
sm = Map(g.grid, factor=1) | ||
sm.set_shapefile(shp) # add the glacier outlines | ||
sm.set_rgb(ggl_img) # add the background rgb image | ||
sm.visualize(ax=ax2) # plot it | ||
ax2.set_title('GPR measurements') | ||
|
||
# read the point GPR data and add them to the plot | ||
df = pd.read_csv(get_demo_file('gtd_ttt_kesselwand.csv')) | ||
dl = DataLevels(df.THICKNESS, levels=np.arange(10, 201, 10), extend='both') | ||
x, y = sm.grid.transform(df.POINT_LON.values, df.POINT_LAT.values) | ||
ax2.scatter(x, y, color=dl.to_rgb(), s=50, edgecolors='k', linewidths=1) | ||
dl.append_colorbar(ax2, label='Ice thickness (m)') | ||
|
||
# make it nice | ||
plt.tight_layout() | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
=================== | ||
Topographic shading | ||
=================== | ||
How to add topographic shading to a plot. | ||
""" | ||
|
||
from salem import mercator_grid, Map, get_demo_file | ||
import matplotlib.pyplot as plt | ||
|
||
# prepare the figure | ||
|
||
f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(8, 7)) | ||
|
||
# map extent | ||
grid = mercator_grid(center_ll=(10.76, 46.79), extent=(18000, 14000)) | ||
sm = Map(grid, countries=False) | ||
sm.set_lonlat_contours(interval=0) | ||
|
||
# add topography | ||
fpath = get_demo_file('hef_srtm.tif') | ||
sm.set_topography(fpath) | ||
sm.visualize(ax=ax1, addcbar=False, title='relief_factor=0.7 (default)') | ||
|
||
# stronger shading | ||
sm.set_topography(fpath, relief_factor=1.4) | ||
sm.visualize(ax=ax2, addcbar=False, title='relief_factor=1.4') | ||
|
||
# add color shading | ||
z = sm.set_topography(fpath) | ||
sm.set_data(z) | ||
sm.set_cmap('topo') | ||
sm.visualize(ax=ax3, title='Color with shading', addcbar=False) | ||
|
||
# color without topo shading | ||
sm.set_topography() | ||
sm.visualize(ax=ax4, title='Color without shading', addcbar=False) | ||
|
||
# make it nice | ||
plt.tight_layout() | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
.. _xarray: | ||
|
||
xarray accessors | ||
================ | ||
|
||
One of the main purposes of Salem is to add georeferencing tools to | ||
`xarray`_ 's data structures. These tools can be accessed via a `special`_ | ||
``.salem`` attribute, available for both `xarray.DataArray`_ and | ||
`xarray.Dataset`_ objects after a simple ``import salem`` in your code. | ||
|
||
.. _xarray: http://xarray.pydata.org/ | ||
.. _special: http://xarray.pydata.org/en/stable/internals.html#extending-xarray | ||
.. _xarray.DataArray: http://xarray.pydata.org/en/stable/data-structures.html#dataarray | ||
.. _xarray.Dataset: http://xarray.pydata.org/en/stable/data-structures.html#dataset | ||
|
||
|
||
Initializing the accessor | ||
------------------------- | ||
|
||
Automated projection parsing | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Salem will try to understand in which projection the Dataset or DataArray is | ||
defined. For example, with a platte carree (or lon/lat) projection, Salem will | ||
know what to do based on the coordinates' names: | ||
|
||
.. ipython:: python | ||
import numpy as np | ||
import xarray as xr | ||
import salem | ||
da = xr.DataArray(np.arange(20).reshape(4, 5), dims=['lat', 'lon'], | ||
coords={'lat':np.linspace(0, 30, 4), 'lon':np.linspace(-20, 20, 5)}) | ||
da.salem | ||
@savefig plot_xarray_simple.png width=80% | ||
da.salem.quick_map(); | ||
While the above should work with many (most?) climate datasets (such as | ||
atmospheric reanalyses or GCM output), certain NetCDF files will have a less | ||
standard map projection requiring special parsing. There are `conventions`_ to | ||
formalise these things in the NetCDF model, but Salem doesn't understand them | ||
yet. Currently, Salem can parse: | ||
- platte carree (or lon/lat) projections | ||
- WRF projections (see :ref:`wrf`) | ||
- virually any projection explicitly provided by the user | ||
- for geotiff files only: any projection that `rasterio`_ can understand | ||
|
||
.. _conventions: http://cfconventions.org/Data/cf-conventions/cf-conventions-1.7/build/ch05s06.html | ||
.. _rasterio: https://mapbox.github.io/rasterio/ | ||
|
||
|
||
From geotiffs | ||
~~~~~~~~~~~~~ | ||
|
||
Salem uses `rasterio`_ to open and parse geotiff files: | ||
|
||
.. ipython:: python | ||
fpath = salem.get_demo_file('himalaya.tif') | ||
ds = salem.open_xr_dataset(fpath) | ||
hmap = ds.salem.get_map(cmap='topo') | ||
hmap.set_data(ds['data']) | ||
@savefig plot_xarray_geotiff.png width=80% | ||
hmap.visualize(); | ||
Custom projections | ||
~~~~~~~~~~~~~~~~~~ | ||
|
||
Alternatively, Salem will understand any projection supported by `pyproj`_. | ||
The proj info has to be provided as attribute: | ||
|
||
.. _pyproj: https://jswhit.github.io/pyproj/ | ||
|
||
.. ipython:: python | ||
dutm = xr.DataArray(np.arange(20).reshape(4, 5), dims=['y', 'x'], | ||
coords={'y': np.arange(3, 7)*2e5, | ||
'x': np.arange(1, 6)*2e5}) | ||
psrs = 'epsg:32630' # http://spatialreference.org/ref/epsg/wgs-84-utm-zone-30n/ | ||
dutm.attrs['pyproj_srs'] = psrs | ||
@savefig plot_xarray_utm.png width=80% | ||
dutm.salem.quick_map(interp='linear'); |
Oops, something went wrong.