Skip to content

Commit

Permalink
Merge pull request #233 from djhoese/bugfix-trapz
Browse files Browse the repository at this point in the history
Replace deprecated numpy trapezoid/trapz usage with scipy trapezoid
  • Loading branch information
djhoese authored Aug 14, 2024
2 parents 101113b + af71f1e commit de18f70
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 38 deletions.
19 changes: 0 additions & 19 deletions pyspectral/_compat.py

This file was deleted.

9 changes: 2 additions & 7 deletions pyspectral/radiance_tb_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,12 @@
from numbers import Number

import numpy as np
from scipy.integrate import trapezoid

from pyspectral._compat import np_trapezoid
from pyspectral.blackbody import C_SPEED, H_PLANCK, K_BOLTZMANN, blackbody, blackbody_wn
from pyspectral.rsr_reader import RelativeSpectralResponse
from pyspectral.utils import BANDNAMES, WAVE_LENGTH, WAVE_NUMBER, convert2wavenumber, get_bandname_from_wavelength

try:
from scipy.integrate import trapezoid
except ImportError:
from scipy.integrate import trapz as trapezoid

LOG = logging.getLogger(__name__)

BLACKBODY_FUNC = {WAVE_LENGTH: blackbody,
Expand Down Expand Up @@ -160,7 +155,7 @@ def _get_rsr(self):
self._wave_si_scale)
self.response = self.rsr[self.bandname][self.detector]['response']
# Get the integral of the spectral response curve:
self.rsr_integral = np_trapezoid(self.response, self.wavelength_or_wavenumber)
self.rsr_integral = trapezoid(self.response, self.wavelength_or_wavenumber)

def _getsatname(self):
"""Get the satellite name used in the rsr-reader, from the platform and number."""
Expand Down
5 changes: 3 additions & 2 deletions pyspectral/rsr_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
from glob import glob
from os.path import expanduser

from pyspectral._compat import np_trapezoid
from scipy.integrate import trapezoid

from pyspectral.bandnames import BANDNAMES
from pyspectral.config import get_config
from pyspectral.utils import (
Expand Down Expand Up @@ -213,7 +214,7 @@ def integral(self, bandname):
for det in self.rsr[bandname].keys():
wvl = self.rsr[bandname][det]['wavelength']
resp = self.rsr[bandname][det]['response']
intg[det] = np_trapezoid(resp, wvl)
intg[det] = trapezoid(resp, wvl)
return intg

def convert(self):
Expand Down
11 changes: 5 additions & 6 deletions pyspectral/solar.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
from pathlib import Path

import numpy as np

from pyspectral._compat import np_trapezoid
from scipy.integrate import trapezoid

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -123,9 +122,9 @@ def _load(self):
def solar_constant(self):
"""Calculate the solar constant."""
if self.wavenumber is not None:
return np_trapezoid(self.irradiance, self.wavenumber)
return trapezoid(self.irradiance, self.wavenumber)
if self.wavelength is not None:
return np_trapezoid(self.irradiance, self.wavelength)
return trapezoid(self.irradiance, self.wavelength)

raise TypeError('Neither wavelengths nor wavenumbers available!')

Expand Down Expand Up @@ -206,10 +205,10 @@ def _band_calculations(self, rsr, flux, scale, **options):

# Calculate the solar-flux: (w/m2)
if flux:
return np_trapezoid(irr * resp_ipol, wvl)
return trapezoid(irr * resp_ipol, wvl)

# Divide by the equivalent band width:
return np_trapezoid(irr * resp_ipol, wvl) / np_trapezoid(resp_ipol, wvl)
return trapezoid(irr * resp_ipol, wvl) / trapezoid(resp_ipol, wvl)

def interpolate(self, **options):
"""Interpolate Irradiance to a specified evenly spaced resolution/grid.
Expand Down
4 changes: 2 additions & 2 deletions pyspectral/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@

import numpy as np
import requests
from scipy.integrate import trapezoid

from pyspectral._compat import np_trapezoid
from pyspectral.bandnames import BANDNAMES
from pyspectral.config import get_config

Expand Down Expand Up @@ -226,7 +226,7 @@ def get_central_wave(wav, resp, weight=1.0):
# if info['unit'].find('-1') > 0:
# Wavenumber:
# res *=
return np_trapezoid(resp * wav * weight, wav) / np_trapezoid(resp * weight, wav)
return trapezoid(resp * wav * weight, wav) / trapezoid(resp * weight, wav)


def get_bandname_from_wavelength(sensor, wavelength, rsr, epsilon=0.1, multiple_bands=False):
Expand Down
3 changes: 2 additions & 1 deletion rsr_convert_scripts/seviri_rsr.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import numpy as np
import pkg_resources
from scipy.integrate import trapezoid
from xlrd import open_workbook

from pyspectral.config import get_config
Expand Down Expand Up @@ -210,7 +211,7 @@ def get_centrals(self):

def get_central_wave(wavl, resp):
"""Calculate the central wavelength (or the central wavenumber if inputs are wave numbers)."""
return np.trapz(resp * wavl, wavl) / np.trapz(resp, wavl)
return trapezoid(resp * wavl, wavl) / trapezoid(resp, wavl)


def generate_seviri_file(seviri, platform_name):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
with open('./README.md', 'r') as fd:
long_description = fd.read()

requires = ['numpy', 'scipy', 'python-geotiepoints>=1.1.1',
requires = ['numpy', 'scipy>=1.6.0', 'python-geotiepoints>=1.1.1',
'h5py>=2.5', 'requests', 'pyyaml', 'platformdirs']

dask_extra = ['dask[array]']
Expand Down

0 comments on commit de18f70

Please sign in to comment.