# Path: src/pys5p/biweight.py
# def biweight(data, axis=None, spread=False):
#     """
#     Calculate Tukey's biweight.
#     Implementation based on Eqn. 7.6 and 7.7 in the SWIR OCAL ATBD.
# 
#     Parameters
#     ----------
#     data   :   array_like
#        input array
#     axis   :   int, optional
#        axis along which the biweight medians are computed.
#         - Note that axis will be ignored when data is a 1-D array.
#     spread :   bool, optional
#        if True, then return also the biweight spread.
# 
#     Returns
#     -------
#     out    :   ndarray or tuple
#        biweight median and biweight spread if parameter "spread" is True
#     """
#     if axis is None or data.ndim == 1:
#         mask = np.isfinite(data)
#         if np.all(~mask):
#             if spread:
#                 return (np.nan, 0.)
#             return np.nan
#         return __biweight(data, spread)
# 
#     # only a single axis!
#     if not isinstance(axis, int):
#         raise TypeError('axis not an integer')
#     if not 0 <= axis < data.ndim:
#         raise ValueError('axis out-of-range')
# 
#     with warnings.catch_warnings():
#         warnings.filterwarnings('ignore', r'All-NaN (slice|axis) encountered')
#         return __biweight_axis(data, axis, spread)
# 
# Path: src/pys5p/swir_texp.py
# def swir_exp_time(int_delay: int, int_hold: int) -> float:
#     """
#     Parameters
#     ----------
#     int_delay :  int
#       parameters int_delay from the instrument_settings
#     int_hold  :  int
#       parameters int_holt from the instrument_settings
#     Returns
#     -------
#     float
#         exact (SWIR) pixel exposure time
#     """
#     return 1.25e-6 * (65540 - int_delay + int_hold)


from datetime import datetime, timedelta
from pathlib import Path, PurePosixPath
from setuptools_scm import get_version
from .biweight import biweight
from .swir_texp import swir_exp_time
import h5py
import numpy as np
        hk_tbl = self.fid['/NOMINAL_HK/TEMPERATURES/instr_temperatures'][:]
        swir_hk['imager_temp'] = hk_tbl['instr_temp_29']
        swir_hk['obm_temp'] = hk_tbl['instr_temp_28']
        swir_hk['calib_unit_temp'] = hk_tbl['instr_temp_25']

        hk_tbl = self.fid['/DETECTOR4/DETECTOR_HK/heater_data'][:]
        swir_hk['detector_heater'] = hk_tbl['det_htr_curr']

        hk_tbl = self.fid['/NOMINAL_HK/HEATERS/heater_data'][:]
        swir_hk['obm_heater'] = hk_tbl['meas_cur_val_htr12']
        swir_hk['obm_heater_cycle'] = hk_tbl['last_pwm_val_htr12']
        swir_hk['fee_box_heater'] = hk_tbl['meas_cur_val_htr13']
        swir_hk['fee_box_heater_cycle'] = hk_tbl['last_pwm_val_htr13']

        # CHECK: works only when all elements of swir_hk are floats
        if fill_as_nan:
            for key in dtype_hk_db.names:
                swir_hk[key][swir_hk[key] == 999.] = np.nan

        if stats is None:
            return swir_hk

        if stats == 'median':
            hk_median = np.empty(1, dtype=dtype_hk_db)
            for key in dtype_hk_db.names:
                if np.all(np.isnan(swir_hk[key])):
                    hk_median[key][0] = np.nan
                elif np.nanmin(swir_hk[key]) == np.nanmax(swir_hk[key]):
                    hk_median[key][0] = swir_hk[key][0]
                else:

# Path: src/pys5p/biweight.py
# def biweight(data, axis=None, spread=False):
#     """
#     Calculate Tukey's biweight.
#     Implementation based on Eqn. 7.6 and 7.7 in the SWIR OCAL ATBD.
# 
#     Parameters
#     ----------
#     data   :   array_like
#        input array
#     axis   :   int, optional
#        axis along which the biweight medians are computed.
#         - Note that axis will be ignored when data is a 1-D array.
#     spread :   bool, optional
#        if True, then return also the biweight spread.
# 
#     Returns
#     -------
#     out    :   ndarray or tuple
#        biweight median and biweight spread if parameter "spread" is True
#     """
#     if axis is None or data.ndim == 1:
#         mask = np.isfinite(data)
#         if np.all(~mask):
#             if spread:
#                 return (np.nan, 0.)
#             return np.nan
#         return __biweight(data, spread)
# 
#     # only a single axis!
#     if not isinstance(axis, int):
#         raise TypeError('axis not an integer')
#     if not 0 <= axis < data.ndim:
#         raise ValueError('axis out-of-range')
# 
#     with warnings.catch_warnings():
#         warnings.filterwarnings('ignore', r'All-NaN (slice|axis) encountered')
#         return __biweight_axis(data, axis, spread)
# 
# Path: src/pys5p/swir_texp.py
# def swir_exp_time(int_delay: int, int_hold: int) -> float:
#     """
#     Parameters
#     ----------
#     int_delay :  int
#       parameters int_delay from the instrument_settings
#     int_hold  :  int
#       parameters int_holt from the instrument_settings
#     Returns
#     -------
#     float
#         exact (SWIR) pixel exposure time
#     """
#     return 1.25e-6 * (65540 - int_delay + int_hold)


# Path: src/pys5p/l1b_io.py
from datetime import datetime, timedelta
from pathlib import Path, PurePosixPath
from setuptools_scm import get_version
from .biweight import biweight
from .swir_texp import swir_exp_time
import h5py
import numpy as np

        hk_tbl = self.fid['/NOMINAL_HK/TEMPERATURES/instr_temperatures'][:]
        swir_hk['imager_temp'] = hk_tbl['instr_temp_29']
        swir_hk['obm_temp'] = hk_tbl['instr_temp_28']
        swir_hk['calib_unit_temp'] = hk_tbl['instr_temp_25']

        hk_tbl = self.fid['/DETECTOR4/DETECTOR_HK/heater_data'][:]
        swir_hk['detector_heater'] = hk_tbl['det_htr_curr']

        hk_tbl = self.fid['/NOMINAL_HK/HEATERS/heater_data'][:]
        swir_hk['obm_heater'] = hk_tbl['meas_cur_val_htr12']
        swir_hk['obm_heater_cycle'] = hk_tbl['last_pwm_val_htr12']
        swir_hk['fee_box_heater'] = hk_tbl['meas_cur_val_htr13']
        swir_hk['fee_box_heater_cycle'] = hk_tbl['last_pwm_val_htr13']

        # CHECK: works only when all elements of swir_hk are floats
        if fill_as_nan:
            for key in dtype_hk_db.names:
                swir_hk[key][swir_hk[key] == 999.] = np.nan

        if stats is None:
            return swir_hk

        if stats == 'median':
            hk_median = np.empty(1, dtype=dtype_hk_db)
            for key in dtype_hk_db.names:
                if np.all(np.isnan(swir_hk[key])):
                    hk_median[key][0] = np.nan
                elif np.nanmin(swir_hk[key]) == np.nanmax(swir_hk[key]):
                    hk_median[key][0] = swir_hk[key][0]
                else:

                    hk_median[key][0] = biweight(swir_hk[key])