Skip to content

Commit

Permalink
Made pyproj an optional dependency. Coordinate conversion is disabled…
Browse files Browse the repository at this point in the history
… if not present.
  • Loading branch information
Bas Hoonhout committed Feb 2, 2018
1 parent 804d4a7 commit 8203caf
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
4 changes: 4 additions & 0 deletions docs/whatsnew.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Improvements
* Also read units from quantities other than VarDens (e.g. EnDens and
AcDens)

* Package `pyproj` is not an optional dependency. Coordinate
conversion is disabled if `pyproj` is not installed. Instead a
warning is given in that situation.

New functions/methods
^^^^^^^^^^^^^^^^^^^^^

Expand Down
30 changes: 18 additions & 12 deletions oceanwaves/oceanwaves.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from __future__ import absolute_import

import copy
import pyproj
import logging
import numpy as np
import xarray as xr
import scipy.integrate
import scipy.interpolate
from collections import OrderedDict

from xarray.core.coordinates import DatasetCoordinates
from xarray.core.dataset import DataVariables
try:
import pyproj
HAS_PYPROJ = True
except ImportError:
HAS_PYPROJ = False

from oceanwaves.utils import *
from oceanwaves.units import simplify
Expand Down Expand Up @@ -1088,15 +1090,19 @@ def convert_coordinates(self, crs):
if self.has_dimension('location'):

if crs is not None:
k = self._key_lookup('_location')
p1 = pyproj.Proj(init=crs)
p2 = pyproj.Proj(proj='latlong', datum='WGS84')
x = self._variables['%s_x' % k].values
y = self._variables['%s_y' % k].values
lat, lon = pyproj.transform(p1, p2, x, y)
self.variables['%s_lat' % k].values = lat
self.variables['%s_lon' % k].values = lon
self.attrs['_crs'] = crs
if not HAS_PYPROJ:
logger.warn('Package "pyproj" is not installed, cannot '
'apply coordinate reference system.')
else:
k = self._key_lookup('_location')
p1 = pyproj.Proj(init=crs)
p2 = pyproj.Proj(proj='latlong', datum='WGS84')
x = self._variables['%s_x' % k].values
y = self._variables['%s_y' % k].values
lat, lon = pyproj.transform(p1, p2, x, y)
self.variables['%s_lat' % k].values = lat
self.variables['%s_lon' % k].values = lon
self.attrs['_crs'] = crs


def __getitem__(self, key):
Expand Down
13 changes: 11 additions & 2 deletions oceanwaves/swan.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@
import re
import glob
import json
import pyproj
import logging
import xarray as xr
import numpy as np
import pandas as pd
from datetime import datetime
from collections import OrderedDict

try:
import pyproj
HAS_PYPROJ = True
except ImportError:
HAS_PYPROJ = False

import oceanwaves.oceanwaves


Expand Down Expand Up @@ -412,7 +417,11 @@ def write_locations(self, latlon=False):

crs = self._get_attr('_crs')
if crs is not None:
latlon = pyproj.Proj(init=crs).is_latlong()
if not HAS_PYPROJ:
logger.warn('Package "pyproj" is not installed, cannot '
'apply coordinate reference system.')
else:
latlon = pyproj.Proj(init=crs).is_latlong()

if latlon:
self.fp.write('LONLAT\n')
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
python_requires='>=2.7, <4',
install_requires=[
'docopt',
'pyproj',
'xarray',
'scipy',
'numpy',
Expand Down

0 comments on commit 8203caf

Please sign in to comment.