Skip to content

Commit

Permalink
Endianness now handled in pythong, instead of the C-extension.
Browse files Browse the repository at this point in the history
  • Loading branch information
kmacdonald-stsci committed Dec 28, 2023
1 parent 288a69e commit 777089d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
72 changes: 69 additions & 3 deletions src/stcal/ramp_fitting/ols_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import warnings
from multiprocessing import cpu_count
from multiprocessing.pool import Pool
import sys

import numpy as np

Expand Down Expand Up @@ -657,14 +658,15 @@ def ols_ramp_fit_single(ramp_data, buffsize, save_opt, readnoise_2d, gain_2d, we
opt_info : tuple
The tuple of computed optional results arrays for fitting.
"""

use_c = False
# use_c = True
# use_c = False
use_c = True
if use_c:
print(" ")
print("=" * 80)
c_start = time.time()

ramp_data, gain_2d, readnoise_2d = endianness_handler(ramp_data, gain_2d, readnoise_2d)

if ramp_data.drop_frames1 is None:
ramp_data.drop_frames1 = 0
print(" ---------------- Entering C Code ----------------")
Expand Down Expand Up @@ -692,6 +694,70 @@ def ols_ramp_fit_single(ramp_data, buffsize, save_opt, readnoise_2d, gain_2d, we
c_python_time_comparison(c_start, c_end, p_start, p_end)

Check warning on line 694 in src/stcal/ramp_fitting/ols_fit.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/ramp_fitting/ols_fit.py#L692-L694

Added lines #L692 - L694 were not covered by tests

return image_info, integ_info, opt_info

Check warning on line 696 in src/stcal/ramp_fitting/ols_fit.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/ramp_fitting/ols_fit.py#L696

Added line #L696 was not covered by tests


def handle_array_endianness(arr, sys_order):
"""
Determines if the array byte order is the same as the system byte order. If
it is not, then byteswap the array.
Parameters
----------
arr : ndarray
The array whose endianness to check against the system endianness.
sys_order : str
The system order ("<" is little endian, while ">" is big endian).
Return
------
arr : ndarray
The ndarray in the correct byte order
"""
arr_order = arr.dtype.byteorder
if (arr_order == ">" and sys_order == "<") or (arr_order == "<" and sys_order == ">"):
arr.newbyteorder().byteswap(inplace=True)

Check warning on line 719 in src/stcal/ramp_fitting/ols_fit.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/ramp_fitting/ols_fit.py#L719

Added line #L719 was not covered by tests
return arr


def endianness_handler(ramp_data, gain_2d, readnoise_2d):
"""
Check all arrays for endianness against the system endianness, so when used by the C
extension, the endianness is correct.
Parameters
----------
ramp_data : RampData
Carries ndarrays needing checked and possibly byte swapped.
gain_2d : ndarray
An ndarray needing checked and possibly byte swapped.
readnoise_2d : ndarray
An ndarray needing checked and possibly byte swapped.
Return
------
ramp_data : RampData
Carries ndarrays checked and possibly byte swapped.
gain_2d : ndarray
An ndarray checked and possibly byte swapped.
readnoise_2d : ndarray
An ndarray checked and possibly byte swapped.
"""
sys_order = "<" if sys.byteorder=="little" else ">"

gain_2d = handle_array_endianness(gain_2d, sys_order)
readnoise_2d = handle_array_endianness(readnoise_2d, sys_order)

ramp_data.data = handle_array_endianness(ramp_data.data, sys_order)
ramp_data.err = handle_array_endianness(ramp_data.err, sys_order)
ramp_data.groupdq = handle_array_endianness(ramp_data.groupdq, sys_order)
ramp_data.pixeldq = handle_array_endianness(ramp_data.pixeldq, sys_order)

return ramp_data, gain_2d, readnoise_2d



Expand Down
1 change: 1 addition & 0 deletions src/stcal/ramp_fitting/src/slope_fitter.c
Original file line number Diff line number Diff line change
Expand Up @@ -1785,6 +1785,7 @@ get_ramp_data_endianness(
rd->get_zframe = get_float3;

# if 0
/* Byte swapping now handled in python code. */
rd->get_data = (PyArray_ISBYTESWAPPED(rd->data)) ? get_float4_swp : get_float4;
rd->get_err = (PyArray_ISBYTESWAPPED(rd->err)) ? get_float4_swp : get_float4;

Expand Down

0 comments on commit 777089d

Please sign in to comment.