Source code for Meteorographica.scripts.fetch_backgrounds

#!/usr/bin env python

# (C) British Crown Copyright 2017, Met Office
# This code is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU Lesser General Public License for more details.

# Utility functions for weather mapping.

import os
import os.path

# Fetch the background data from Natural Earth
[docs]def fetch_backgrounds(): """Fetch plot background data from Natural Earth. `Cartopy <>`_ uses background images from `Natural Earth <>`_ for things like continent outlines on world maps - Meteorographica folows it in this. This function downloads some Natural Earth data (continent backgrounds) and modifies it to make ocean regions transparent for easy use in map plots. You should only have to run this function once as part of the set-up of this module. Requires three utility functions to be available: `wget <>`_, `unzip <>`_, and `imagemagick convert <>`_. It will create map background files in the directory specified by the CARTOPY_USER_BACKGROUNDS environment variable which should have been set during the installation of `Cartopy <>`_. Raises: StandardError: Environment variable 'CARTOPY_USER_BACKGROUNDS' is not set | """ bgdir=os.getenv('CARTOPY_USER_BACKGROUNDS') if bgdir is None: raise Exception("CARTOPY_USER_BACKGROUNDS environment " + "variable is undefined") if not os.path.isdir(bgdir): os.makedirs(bgdir) os.chdir(bgdir) if not os.path.isfile("GRAY_50M_SR_W_tpo.png"): if not os.path.isfile("GRAY_50M_SR_W.tif"): if not os.path.isfile(""): os.system("wget" + "download/50m/raster/") os.system("unzip") os.system("convert GRAY_50M_SR_W/GRAY_50M_SR_W.tif " + "-transparent '#6a6a6a' " + "GRAY_50M_SR_W_tpo.png") if not os.path.isfile("GRAY_LR_SR_W_tpo.png"): if not os.path.isfile("GRAY_LR_SR_W.tif"): if not os.path.isfile(""): os.system("wget" + "download/10m/raster/") os.system("unzip") os.system("convert GRAY_LR_SR_W/GRAY_LR_SR_W.tif " + "-transparent '#6a6a6a' " + "GRAY_LR_SR_W_tpo.png") if not os.path.isfile("GRAY_HR_SR_W_tpo.png"): if not os.path.isfile("GRAY_HR_SR_W.tif"): if not os.path.isfile(""): os.system("wget" + "download/10m/raster/") os.system("unzip") os.system("convert GRAY_HR_SR_W/GRAY_HR_SR_W.tif " + "-transparent '#6a6a6a' " + "GRAY_HR_SR_W_tpo.png") idx_txt='''{"__comment__": "JSON file specifying the image to use for a given type/name and resolution. Read in by cartopy.mpl.geoaxes.read_user_background_images.", "GreyT": { "__comment__": "Grey topography - land only ", "__source__": "", "__projection__": "PlateCarree", "low": "GRAY_50M_SR_W_tpo.png", "50m": "GRAY_50M_SR_W_tpo.png", "med": "GRAY_LR_SR_W_tpo.png", "high": "GRAY_HR_SR_W_tpo.png", "10m": "GRAY_HR_SR_W_tpo.png" } }''' if not os.path.isfile("images.json"): jf = open("images.json","w") jf.write(idx_txt) jf.close
# Actually want to call this directly as a script if __name__ == '__main__': fetch_backgrounds()