Skip to content

Commit

Permalink
Patching dtype issues with numpy 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
MJWeberg committed Jun 20, 2024
1 parent 9a10575 commit 48b6109
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
16 changes: 6 additions & 10 deletions eispac/core/eismap.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import numpy as np
import astropy.units as u
from astropy.io import fits
from astropy.visualization import ImageNormalize, AsinhStretch, LinearStretch
import sunpy.map
from sunpy.map.mapbase import SpatialPair
Expand Down Expand Up @@ -48,16 +49,11 @@ def __init__(self, data, header=None, **kwargs):
# number of values
if isinstance(data, (str, pathlib.Path)):
if pathlib.Path(data).suffix.lower().startswith('.fit'):
import sunpy.io.fits
hdu_list = sunpy.io.fits.read(data)
data = hdu_list[0][0]
header = hdu_list[0][1]
if len(hdu_list) >= 2:
rec_errs = hdu_list[1][0]
ny = rec_errs.shape[0] # y-axis
nx = rec_errs[0][0].shape[0] # x-axis
errs = rec_errs['errors'].view(type=np.ndarray).reshape(ny,nx)
kwargs['uncertainty'] = errs
with fits.open(data, mode='readonly') as hdul:
data = hdul[0].data
header = hdul[0].header
if len(hdul) >= 2:
kwargs['uncertainty'] = hdul[1].data['errors']

super().__init__(data, header, **kwargs)

Expand Down
8 changes: 4 additions & 4 deletions eispac/core/read_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def read_cube(filename=None, window=0, apply_radcal=True, radcal=None,
for key in f_head['index']:
val = np.array(f_head['index/'+key])
if type(val[0]) == np.bytes_:
val = val.astype(np.unicode_) # convert bytes to unicode
val = val.astype(np.str_) # convert bytes to unicode
if val.size == 1:
val = val[0]
index[key] = val
Expand All @@ -153,7 +153,7 @@ def read_cube(filename=None, window=0, apply_radcal=True, radcal=None,
for key in f_head['pointing']:
val = np.array(f_head['pointing/'+key])
if type(val[0]) == np.bytes_:
val = val.astype(np.unicode_) # convert bytes to unicode
val = val.astype(np.str_) # convert bytes to unicode
if val.size == 1:
val = val[0]
pointing[key] = val
Expand All @@ -175,8 +175,8 @@ def read_cube(filename=None, window=0, apply_radcal=True, radcal=None,

# Read time and duration information
try:
meta['date_obs'] = np.array(f_head['times/date_obs']).astype(np.unicode_)
meta['date_obs_format'] = np.array(f_head['times/time_format']).astype(np.unicode_)[0]
meta['date_obs'] = np.array(f_head['times/date_obs']).astype(np.str_)
meta['date_obs_format'] = np.array(f_head['times/time_format']).astype(np.str_)[0]
except KeyError:
print('WARNING: complete date_obs information for each raster step'
+' is missing in the HDF5 header file!')
Expand Down
4 changes: 2 additions & 2 deletions eispac/core/read_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ def walk_and_load(hdf5_file, hdf5_path, verbose=False):
# Convert objects, byte or ascii strings to uncode
try:
# Old method, for consistency
output = output.astype(np.unicode_) # strings to unicode
output = output.astype(np.str_) # strings to unicode
except:
# Better handling of unicode characters
output = np.array(output.item().decode('utf-8'))
output = np.array([ITEM.decode('utf-8') for ITEM in output])
if output.size == 1:
# Extract value from a single-element array (0- or 1-D arrays only)
output = output.item()
Expand Down

0 comments on commit 48b6109

Please sign in to comment.