diff --git a/src/stcal/ramp_fitting/ols_fit.py b/src/stcal/ramp_fitting/ols_fit.py index 5baf8c216..763ed4844 100644 --- a/src/stcal/ramp_fitting/ols_fit.py +++ b/src/stcal/ramp_fitting/ols_fit.py @@ -5,6 +5,7 @@ import warnings from multiprocessing import cpu_count from multiprocessing.pool import Pool +import sys import numpy as np @@ -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 ----------------") @@ -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) return image_info, integ_info, opt_info + + +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) + 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 diff --git a/src/stcal/ramp_fitting/src/slope_fitter.c b/src/stcal/ramp_fitting/src/slope_fitter.c index a6d69f2aa..bb0ccf72d 100644 --- a/src/stcal/ramp_fitting/src/slope_fitter.c +++ b/src/stcal/ramp_fitting/src/slope_fitter.c @@ -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;