pypsa-eur/scripts/build_cutout.py
FabianHofmann eaf30a9b65
Introduce mocksnakemake which acutally parses Snakefile (#107)
* rewrite mocksnakemake for parsing real Snakefile

* continue add function to scripts

* going through all scripts, setting new mocksnakemake

* fix plotting scripts

* fix build_country_flh

* fix build_country_flh II

* adjust config files

* fix make_summary for tutorial network

* create dir also for output

* incorporate suggestions

* consistent import of mocksnakemake

* consistent import of mocksnakemake II

* Update scripts/_helpers.py

Co-Authored-By: euronion <42553970+euronion@users.noreply.github.com>

* Update scripts/_helpers.py

Co-Authored-By: euronion <42553970+euronion@users.noreply.github.com>

* Update scripts/_helpers.py

Co-Authored-By: euronion <42553970+euronion@users.noreply.github.com>

* Update scripts/_helpers.py

Co-Authored-By: euronion <42553970+euronion@users.noreply.github.com>

* Update scripts/plot_network.py

Co-Authored-By: euronion <42553970+euronion@users.noreply.github.com>

* Update scripts/plot_network.py

Co-Authored-By: euronion <42553970+euronion@users.noreply.github.com>

* Update scripts/retrieve_databundle.py

Co-Authored-By: euronion <42553970+euronion@users.noreply.github.com>

* use pathlib for mocksnakemake

* rename mocksnakemake into mock_snakemake

* revert change in data

* Update scripts/_helpers.py

Co-Authored-By: euronion <42553970+euronion@users.noreply.github.com>

* remove setting logfile in mock_snakemake, use Path in configure_logging

* fix fallback path and base_dir
fix return type of make_io_accessable

* reformulate mock_snakemake

* incorporate suggestion, fix typos

* mock_snakemake: apply absolute paths again, add assertion error
*.py: make hard coded io path accessable for mock_snakemake

* retrieve_natura_raster: use snakemake.output for fn_out

* include suggestion

* Apply suggestions from code review

Co-Authored-By: Jonas Hörsch <jonas.hoersch@posteo.de>

* linting, add return ad end of file

* Update scripts/plot_p_nom_max.py

Co-Authored-By: Jonas Hörsch <jonas.hoersch@posteo.de>

* Update scripts/plot_p_nom_max.py

fixes #112

Co-Authored-By: Jonas Hörsch <jonas.hoersch@posteo.de>

* plot_p_nom_max: small correction

* config.tutorial.yaml fix snapshots end

* use techs instead of technology

* revert try out from previous commit, complete replacing

* change clusters -> clusts in plot_p_nom_max due to wildcard constraints of clusters

* change clusters -> clusts in plot_p_nom_max due to wildcard constraints of clusters II
2019-12-09 21:29:15 +01:00

113 lines
5.5 KiB
Python

"""
Create cutouts with `atlite <https://atlite.readthedocs.io/en/latest/>`_.
For this rule to work you must have
- installed the `Copernicus Climate Data Store <https://cds.climate.copernicus.eu>`_ ``cdsapi`` package (`install with `pip``) and
- registered and setup your CDS API key as described `on their website <https://cds.climate.copernicus.eu/api-how-to>`_.
.. seealso::
For details on the weather data read the `atlite documentation <https://atlite.readthedocs.io/en/latest/>`_.
If you need help specifically for creating cutouts `the corresponding section in the atlite documentation <https://atlite.readthedocs.io/en/latest/examples/create_cutout.html>`_ should be helpful.
Relevant Settings
-----------------
.. code:: yaml
atlite:
nprocesses:
cutouts:
{cutout}:
.. seealso::
Documentation of the configuration file ``config.yaml`` at
:ref:`atlite_cf`
Inputs
------
*None*
Outputs
-------
- ``cutouts/{cutout}``: weather data from either the `ERA5 <https://www.ecmwf.int/en/forecasts/datasets/reanalysis-datasets/era5>`_
reanalysis weather dataset or `SARAH-2 <https://wui.cmsaf.eu/safira/action/viewProduktSearch>`_
satellite-based historic weather data with the following structure:
**ERA5 cutout:**
=================== ========== ========== =========================================================
Field Dimensions Unit Description
=================== ========== ========== =========================================================
pressure time, y, x Pa Surface pressure
------------------- ---------- ---------- ---------------------------------------------------------
temperature time, y, x K Air temperature 2 meters above the surface.
------------------- ---------- ---------- ---------------------------------------------------------
soil temperature time, y, x K Soil temperature between 1 meters and 3 meters
depth (layer 4).
------------------- ---------- ---------- ---------------------------------------------------------
influx_toa time, y, x Wm**-2 Top of Earth's atmosphere TOA incident solar radiation
------------------- ---------- ---------- ---------------------------------------------------------
influx_direct time, y, x Wm**-2 Total sky direct solar radiation at surface
------------------- ---------- ---------- ---------------------------------------------------------
runoff time, y, x m `Runoff <https://en.wikipedia.org/wiki/Surface_runoff>`_
(volume per area)
------------------- ---------- ---------- ---------------------------------------------------------
roughness y, x m Forecast surface roughness
(`roughness length <https://en.wikipedia.org/wiki/Roughness_length>`_)
------------------- ---------- ---------- ---------------------------------------------------------
height y, x m Surface elevation above sea level
------------------- ---------- ---------- ---------------------------------------------------------
albedo time, y, x -- `Albedo <https://en.wikipedia.org/wiki/Albedo>`_
measure of diffuse reflection of solar radiation.
Calculated from relation between surface solar radiation
downwards (Jm**-2) and surface net solar radiation
(Jm**-2). Takes values between 0 and 1.
------------------- ---------- ---------- ---------------------------------------------------------
influx_diffuse time, y, x Wm**-2 Diffuse solar radiation at surface.
Surface solar radiation downwards minus
direct solar radiation.
------------------- ---------- ---------- ---------------------------------------------------------
wnd100m time, y, x ms**-1 Wind speeds at 100 meters (regardless of direction)
=================== ========== ========== =========================================================
.. image:: ../img/era5.png
:scale: 40 %
A **SARAH-2 cutout** can be used to amend the fields ``temperature``, ``influx_toa``, ``influx_direct``, ``albedo``,
``influx_diffuse`` of ERA5 using satellite-based radiation observations.
.. image:: ../img/sarah.png
:scale: 40 %
Description
-----------
"""
import logging
logger = logging.getLogger(__name__)
from _helpers import configure_logging
import os
import atlite
if __name__ == "__main__":
if 'snakemake' not in globals():
from _helpers import mock_snakemake
snakemake = mock_snakemake('build_cutout', cutout='europe-2013-era5')
configure_logging(snakemake)
cutout_params = snakemake.config['atlite']['cutouts'][snakemake.wildcards.cutout]
for p in ('xs', 'ys', 'years', 'months'):
if p in cutout_params:
cutout_params[p] = slice(*cutout_params[p])
cutout = atlite.Cutout(snakemake.wildcards.cutout,
cutout_dir=os.path.dirname(snakemake.output[0]),
**cutout_params)
cutout.prepare(nprocesses=snakemake.config['atlite'].get('nprocesses', 4))