From 9bbad394700a60e5ed7f1ed67e48e884c8485fc1 Mon Sep 17 00:00:00 2001 From: Xylar Asay-Davis Date: Wed, 6 May 2020 07:52:12 +0200 Subject: [PATCH] Add antarctic_ice_shelves_polys to download (#7) * Bump the version number to 0.2.3 * Add antarctic_ice_shelves_polys to download This merge switches to using a python script to do the downloading. This let us get the version number more easily from cartopy, as well as add features that are not downloaded by default. I have kind of cheated by downloading the python file we want and then importing it as a module. If a better way exists, I'm eager to hear it! --- bin/download_cartopy_data.py | 53 ++++++++++++++++++++++++++++++++++++ bin/download_cartopy_data.sh | 30 -------------------- setup.py | 4 +-- 3 files changed, 55 insertions(+), 32 deletions(-) create mode 100755 bin/download_cartopy_data.py delete mode 100644 bin/download_cartopy_data.sh diff --git a/bin/download_cartopy_data.py b/bin/download_cartopy_data.py new file mode 100755 index 0000000..e60e5f4 --- /dev/null +++ b/bin/download_cartopy_data.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python +import argparse +import os +import urllib.request +import tempfile +import cartopy +from cartopy import config +import sys + + +def main(target_dir): + try: + os.makedirs(target_dir) + except OSError: + pass + + version = cartopy.__version__ + + with tempfile.TemporaryDirectory() as tmpdirname: + target_dir = os.path.abspath(target_dir) + owd = os.getcwd() + os.chdir(tmpdirname) + urllib.request.urlretrieve( + 'https://raw.githubusercontent.com/SciTools/cartopy/v{}/tools/feature_download.py'.format(version), + 'feature_download.py') + + file = open('__init__.py', 'w') + file.close() + + sys.path.append(tmpdirname) + + from feature_download import FEATURE_DEFN_GROUPS, download_features + + # add Antarctic ice shelves + FEATURE_DEFN_GROUPS['physical'] = \ + FEATURE_DEFN_GROUPS['physical'] + \ + (('physical', 'antarctic_ice_shelves_polys', ('50m', '10m')),) + + config['pre_existing_data_dir'] = target_dir + config['data_dir'] = target_dir + config['repo_data_dir'] = target_dir + download_features( + ['cultural-extra', 'cultural', 'gshhs', 'physical'], dry_run=False) + os.chdir(owd) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description='Download cartopy data for caching.') + parser.add_argument('--output', '-o', required=True, + help='save datasets in the specified directory') + args = parser.parse_args() + main(args.output) diff --git a/bin/download_cartopy_data.sh b/bin/download_cartopy_data.sh deleted file mode 100644 index efbaa68..0000000 --- a/bin/download_cartopy_data.sh +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env bash - -# how to use? -echo "Usage:" $0 "" -echo "" -echo " : Full path to a target dir. Should be different from the" -echo " conda installation." -echo "" -[ $# -ne 1 ] && exit - -# read target dir -target_dir=$1 -mkdir -p ${target_dir} - -# create tmp dir -[[ -n ${TMPDIR} ]] || TMPDIR=/tmp -tmp_dir=${TMPDIR}/`date +%s%N`_get_full_cartopy -mkdir ${tmp_dir} - -# cd to tmp dir, get cartopy source, download data to repo data path -( - cd ${tmp_dir} - wget https://raw.githubusercontent.com/SciTools/cartopy/v0.17.0/tools/feature_download.py - python feature_download.py \ - --output ${target_dir} cultural-extra cultural gshhs physical \ - --ignore-repo-data -) - -# clean up -rm -rf ${tmp_dir} diff --git a/setup.py b/setup.py index 58d4fa9..cb6d468 100644 --- a/setup.py +++ b/setup.py @@ -1,12 +1,12 @@ from setuptools import setup setup(name='cartopy_offlinedata', - version='0.2.2', + version='0.2.3', description='Add shared data path to cartopy config', url='https://github.com/willirath/cartopy_offlinedata', author='Willi Rath', author_email='wrath@geomar.de', license='MIT', packages=['cartopy_userconfig'], - scripts=['bin/download_cartopy_data.sh'], + scripts=['bin/download_cartopy_data.py'], zip_safe=False)