Skip to content

Commit

Permalink
Fix various numpy and python deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
djhoese committed Jul 19, 2024
1 parent 28dceac commit 2a5e5aa
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
5 changes: 5 additions & 0 deletions pyspectral/_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Various helpers for backwards and forwards compatibility."""

import numpy as np

np_trapezoid = np.trapezoid if hasattr(np, "trapezoid") else np.trapz
3 changes: 2 additions & 1 deletion pyspectral/radiance_tb_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import numpy as np

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
Expand Down Expand Up @@ -159,7 +160,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.trapz(self.response, self.wavelength_or_wavenumber)
self.rsr_integral = np_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: 2 additions & 3 deletions pyspectral/rsr_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
from glob import glob
from os.path import expanduser

import numpy as np

from pyspectral._compat import np_trapezoid
from pyspectral.bandnames import BANDNAMES
from pyspectral.config import get_config
from pyspectral.utils import (
Expand Down Expand Up @@ -214,7 +213,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.trapz(resp, wvl)
intg[det] = np_trapezoid(resp, wvl)
return intg

def convert(self):
Expand Down
10 changes: 6 additions & 4 deletions pyspectral/solar.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

import numpy as np

from pyspectral._compat import np_trapezoid

LOG = logging.getLogger(__name__)

# STANDARD SPECTRA from Air Mass Zero: http://rredc.nrel.gov/solar/spectra/am0/
Expand Down Expand Up @@ -121,9 +123,9 @@ def _load(self):
def solar_constant(self):
"""Calculate the solar constant."""
if self.wavenumber is not None:
return np.trapz(self.irradiance, self.wavenumber)
return np_trapezoid(self.irradiance, self.wavenumber)
if self.wavelength is not None:
return np.trapz(self.irradiance, self.wavelength)
return np_trapezoid(self.irradiance, self.wavelength)

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

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

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

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

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

import logging
import os
import sys
import tarfile
import warnings
from functools import wraps
Expand All @@ -29,6 +30,7 @@
import numpy as np
import requests

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

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


def get_bandname_from_wavelength(sensor, wavelength, rsr, epsilon=0.1, multiple_bands=False):
Expand Down Expand Up @@ -413,7 +415,8 @@ def _download_tarball_and_extract(tarball_url, local_pathname, extract_dir):
handle.write(data)

tar = tarfile.open(local_pathname)
tar.extractall(extract_dir)
tar_kwargs = {} if sys.version_info < (3, 12) else {"filter": "data"}
tar.extractall(extract_dir, **tar_kwargs)
tar.close()
os.remove(local_pathname)

Expand Down

0 comments on commit 2a5e5aa

Please sign in to comment.