Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace deprecated numpy trapezoid/trapz usage with scipy trapezoid #233

Merged
merged 2 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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)

Check warning on line 125 in pyspectral/solar.py

View check run for this annotation

Codecov / codecov/patch

pyspectral/solar.py#L125

Added line #L125 was not covered by tests
if self.wavelength is not None:
return np_trapezoid(self.irradiance, self.wavelength)
return trapezoid(self.irradiance, self.wavelength)

Check warning on line 127 in pyspectral/solar.py

View check run for this annotation

Codecov / codecov/patch

pyspectral/solar.py#L127

Added line #L127 was not covered by tests

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

Expand Down Expand Up @@ -206,10 +205,10 @@

# 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)

Check warning on line 211 in pyspectral/solar.py

View check run for this annotation

Codecov / codecov/patch

pyspectral/solar.py#L211

Added line #L211 was not covered by tests

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