From c24d764d4eef8458244d205dee74c335db74390c Mon Sep 17 00:00:00 2001 From: Tyler Sutterley Date: Fri, 9 Dec 2022 13:49:52 -0800 Subject: [PATCH] docs: slimmer build to prevent overutilization (#24) --- geoid_toolkit/spatial.py | 14 ++++++++++---- requirements.txt | 3 --- scripts/read_EGM2008_geoid_grids.py | 13 ++++++++++++- scripts/read_ICGEM_geoid_grids.py | 13 ++++++++++++- setup.py | 8 ++++---- 5 files changed, 38 insertions(+), 13 deletions(-) diff --git a/geoid_toolkit/spatial.py b/geoid_toolkit/spatial.py index c46c776..8399347 100644 --- a/geoid_toolkit/spatial.py +++ b/geoid_toolkit/spatial.py @@ -1,7 +1,7 @@ #!/usr/bin/env python u""" spatial.py -Written by Tyler Sutterley (10/2022) +Written by Tyler Sutterley (12/2022) Utilities for reading, writing and operating on spatial data @@ -20,6 +20,7 @@ UPDATE HISTORY: Updated 12/2022: add software information to output HDF5 and netCDF4 + place netCDF4 import within try/except statements Updated 10/2022: added datetime parser for ascii time columns Updated 06/2022: added field_mapping options to netCDF4 and HDF5 reads added from_file wrapper function to read from particular formats @@ -53,7 +54,6 @@ import uuid import yaml import logging -import netCDF4 import datetime import warnings import numpy as np @@ -61,6 +61,12 @@ import geoid_toolkit.version from geoid_toolkit.utilities import get_git_revision_hash # attempt imports +try: + import h5py +except (ImportError, ModuleNotFoundError) as e: + warnings.filterwarnings("always") + warnings.warn("h5py not available") + warnings.warn("Some functions will throw an exception if called") try: import osgeo.gdal, osgeo.osr, osgeo.gdalconst except (ImportError, ModuleNotFoundError) as e: @@ -68,10 +74,10 @@ warnings.warn("GDAL not available") warnings.warn("Some functions will throw an exception if called") try: - import h5py + import netCDF4 except (ImportError, ModuleNotFoundError) as e: warnings.filterwarnings("always") - warnings.warn("h5py not available") + warnings.warn("netCDF4 not available") warnings.warn("Some functions will throw an exception if called") # ignore warnings warnings.filterwarnings("ignore") diff --git a/requirements.txt b/requirements.txt index 4b9385f..bc25372 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,8 +1,5 @@ -gdal -h5py lxml matplotlib -netCDF4 numpy pyproj python-dateutil diff --git a/scripts/read_EGM2008_geoid_grids.py b/scripts/read_EGM2008_geoid_grids.py index 0396191..d49d5ea 100644 --- a/scripts/read_EGM2008_geoid_grids.py +++ b/scripts/read_EGM2008_geoid_grids.py @@ -25,18 +25,29 @@ UPDATE HISTORY: Updated 12/2022: single implicit import of geoid toolkit + place netCDF4 import within try/except statements Written 06/2022 """ from __future__ import print_function import os import logging -import netCDF4 import argparse import datetime +import warnings import numpy as np import geoid_toolkit as geoidtk +# attempt imports +try: + import netCDF4 +except (ImportError, ModuleNotFoundError) as e: + warnings.filterwarnings("always") + warnings.warn("netCDF4 not available") + warnings.warn("Some functions will throw an exception if called") +# ignore warnings +warnings.filterwarnings("ignore") + def read_EGM2008_geoid_grids(FILE, FILENAME=None, LOVE=0.3, VERBOSE=False, MODE=0o775): diff --git a/scripts/read_ICGEM_geoid_grids.py b/scripts/read_ICGEM_geoid_grids.py index 4363195..eba9dd4 100644 --- a/scripts/read_ICGEM_geoid_grids.py +++ b/scripts/read_ICGEM_geoid_grids.py @@ -24,6 +24,7 @@ UPDATE HISTORY: Updated 12/2022: single implicit import of geoid toolkit + place netCDF4 import within try/except statements Updated 05/2022: use argparse descriptions within sphinx documentation Updated 04/2022: include utf-8 encoding in reads to be windows compliant check if gravity field data file is present in file-system @@ -43,12 +44,22 @@ import os import re import logging -import netCDF4 import argparse import datetime +import warnings import numpy as np import geoid_toolkit as geoidtk +# attempt imports +try: + import netCDF4 +except (ImportError, ModuleNotFoundError) as e: + warnings.filterwarnings("always") + warnings.warn("netCDF4 not available") + warnings.warn("Some functions will throw an exception if called") +# ignore warnings +warnings.filterwarnings("ignore") + # PURPOSE: Reads .gdf grids from the GFZ calculation service def read_ICGEM_geoid_grids(FILE, FILENAME=None, MARKER='', SPACING=None, VERBOSE=False, MODE=0o775): diff --git a/setup.py b/setup.py index 3774f2c..67be4b3 100644 --- a/setup.py +++ b/setup.py @@ -43,13 +43,13 @@ def check_output(cmd): except Exception as e: log.warning('Failed to get options via gdal-config') else: - log.info(f"GDAL version from via gdal-config: {0}gdal_output[3]") + log.info(f"GDAL version from via gdal-config: {gdal_output[3]}") # if setting GDAL version from via gdal-config -if gdal_output[3]: +if gdal_output[3] and ('gdal' in install_requires): # add version information to gdal in install_requires gdal_index = install_requires.index('gdal') install_requires[gdal_index] = f'gdal=={gdal_output[3]}' -elif any(install_requires): +elif any(install_requires) and ('gdal' in install_requires): # gdal version not found gdal_index = install_requires.index('gdal') install_requires.pop(gdal_index) @@ -66,7 +66,7 @@ def check_output(cmd): else: log.info(f"HDF5 version from via h5dump: {hdf5_version}") # if the HDF5 version not found -if not any(hdf5_output) and any(install_requires): +if not any(hdf5_output) and ('h5py' in install_requires): hdf5_index = install_requires.index('h5py') install_requires.pop(hdf5_index)