/*
 * apf_math.h
 *
 *  Created on: Sep 1, 2024
 *      Author: tm
 */

#ifndef APF_MATH_H_
#define APF_MATH_H_

#define ushort uint16_t

#define TABS     (-273.15)


// ----------- from lsqfit.h -----------------------------------------
/* Linear and NonLinear Model Based Least Squares Curve Fitting (of absorbance spectra) - function prototypes */

//defd in apf.h
#ifdef OLDCODE
#define FITVALUES   10              // Use to dimension an array of type double
#define SALINITY    0               // Returned Salinity  Value from Fitting
#define NITRATE     1               // Returned Nitrate   Value from Fitting
#define BISULFIDE   2               // Returned BiSulfide Value from Fitting
#define BASELINE_A  3               // Returned Baseline a Coef from Fitting
#define BASELINE_B  4               // Returned Baseline b Coef from Fitting
#define BASELINE_C  5               // Returned Baseline c Coef from Fitting
#define FIT_ERROR   6               // Returned RMS ABS Err Val from Fitting
#define FIT_CHISQ   7               // Returned CHISQ Error Val from Fitting
#define FIT_GAMMQ   8               // Returned Fit Quality Val from Fitting
#endif

void InitLambdaArray(void);
unsigned short calcCRC(unsigned char val, unsigned short crc);
int MatchPixelsToLambda(double *Lambda, uint32_t lambda_beg, uint32_t lambda_end, uint32_t *pixel_beg, uint32_t *pixel_end);
double CellNumToLambda(uint32_t cell);    //  double CellNumToLambda(int cell);   //double CellNumToLambda(int cell, double *ZeissCoefs);
void FloatVectorMinMax(unsigned short end, float data[], float *min, float *max);
void FloatVectorMeanStdDev(unsigned short end, float data[], float *mean, float *stdev);
void ShortVectorMinMax(unsigned short end, unsigned short data[], unsigned short *min, unsigned short *max);
void ShortVectorMeanStdDev(unsigned short end, unsigned short data[], float *mean, float *stdev);
float CtoF(float degC);
double poly(double x, int degree, double p[]);
double rtot(double r, double a[]);
double ttor(double t, double a[]);
unsigned short fCrc16Bit(const unsigned char *msg);
int LinFit(double **CoefVals, double *LambdaVals, double *AbsVals, double *AbsFitVals, double *ResVals, double *FitVals, ushort fit_pixels, ushort fit_concs, ushort bsl_model);

int    _lfit(double x[], double y[], double yfit[], double sig[], double **Ecoefs, double a[],
        int ia[], int ma, double **covar, double *chisq, ushort fitpixels, ushort fitconcs, ushort bslmodel,
        void (*funcs)(double [], double [], double **, int, ushort, ushort));

#endif /* APF_MATH_H_ */
