//#############################################################################
// ASL IPS Ice Draft 
//
// (c) CopyRight ASL Environmental Sciences Inc. 1997-2001
//
//	File:			IceDraft_Def.c
//
//	Purpose:		Definitions file required for ice draft software IceDraft.c
//
//	Development:		National Instruments Labwindows/CVI 5.0 and higher
//					
//	Date:			March 2001
//
// 	Programmer:		Dave Billenness
///////////////////////////////////////////////////////////////////////////////
//
// See if already defined
//
#ifndef _ICEDRAFT_DEFS_H
#define _ICEDRAFT_DEFS_H

// =======
// DEFINES
// =======

// GENERAL PROGRAM DEFINITIONS:
#define ICEDRAFT_MAXARRAYSIZE		1000	// Defines the maximum array size of N - the number of 
						// elements in the CTD data
#define BINWIDTH			1 	// When converting the CTD from a time series to a pressure
 						// bin average, this is the size of the bins that the data is placed into
						// for the pressure average in units of decibars - 1 is ~= 1m depth bins


// ERROR CODE DEFINITIONS:
// return of 1 means data has been decoded but not processed (ice draft is not useable)
// return of 0 means data has been processed and ice drafts computed
#define DATA_DECODED					1
#define DATA_PROCESSED					0
#define ERROR_EXCEEDED_MAX_CTD_ARRAYSIZE		-1
#define ERROR_RANGE_EQ_ZERO				-2
#define ERROR_SHORT_IPS_DATA_STRING			-3
#define ERROR_CTD_INVALID_SALINITY			-4
#define	ERROR_CTD_NOT_ENOUGH_DATA			-5
#define ERROR_CTD_INVALID_DENSITY			-6
#define	ERROR_CTD_PST0_EQ0_IN_STATE			-7
#define ERROR_CTD_KSTP_EQ0_IN_STATE			-8
#define ERROR_CTD_ALPHA_EQNEG1_IN_PSS78			-9
#define ERROR_CTD_RT_LESS0_IN_PSS78			-10
#define	ERROR_DENSITY_EQUAL_0				-11



// DEFINE MIN ALLOWABLE IPS DATA OUTPUT STRING SIZE AND 
// START LOCATION OF DATA IN IPS OUTPUT STRING:
#define MIN_IPS_STRINGSIZE			40 		// miniumum length of character array from IPS
#define	DATE_LOCATION 				0 		// location in IPS character array of date
#define	TIME_LOCATION 				6 		// location in IPS character array of time
#define	PRESSURE_LOCATION 			12 		// location in IPS character array of pressure
#define	TEMPERATURE_LOCATION 			18 		// location in IPS character array of temperature
#define	TX_LOCATION 				24 		// location in IPS character array of Tilt-x
#define	TY_LOCATION 				26 		// location in IPS character array of Tilt-y
#define	TRAVELTIME_LOCATION 			28 		// location in IPS character array of Travel time
#define	AMPLITUDE_LOCATION 			32 		// location in IPS character array of amplitude
#define	PERSISTENCE_LOCATION 			34 		// location in IPS character array of persistence
#define	PRESSUREAGE_LOCATION 			36 		// location in IPS character array of pressure age
#define	FLAG_LOCATION 				38 		// location in IPS character array of flag value

// GENERAL CONSTANT DEFINITIONS:
#define	GRAVITY					9.83162717057325// Value of g (m/s^2) for latitude = 85 degrees and depth = 50 m			
#define	TRANSDUCERDISTANCE 			-0.066 		// Offset of pressure transducer from acoustic in metres
#define PI					3.14159265358979

// CONVERT PRESSURE FROM BARS TO DECIBARS USING PRESSURE = PRESSURE*CONVERT_PRESSURE
// THIS CONVERTS BOTH THE ATMOSPHERIC PRESSURE AND THE CTD PRESSURE TO DECIBARS
#define	CONVERT_PRESSURE			10

// ==========
// Structures
// ==========

// Define the structure that is passed out of the function that contains all of the decoded information
// from the IPSString, the computed ice draft and the computed average sound speed and average density.
typedef struct 
{   
	short 					Year;   		// two digit year of IPS data record
	short 					Month;			// two digit month of IPS data record
	short 					Day;			// two digit day of IPS data record
	short 					Hour;			// two digit hour of IPS data record
	short 					Minute;			// two digit minute of IPS data record
	short 					Second;			// two digit second of IPS data record

	double			 		Pressure;		// measured IPS pressure (in decibars)
	double			 		Temperature;		// measured IPS temperature (in degrees C)
	double 					TiltX;			// measured IPS tilt-x componenent (in degrees) 
	double					TiltY;			// measured IPS tilt-y componenent (in degrees) 
	double					TravelTime;		// measured IPS travel time of acoustic ping (in seconds)
	double			 		Range;			// computed IPS range using average sound speed (in metres) 
	unsigned short int			Amplitude;		// IPS echo level (in counts out of 255)
	double 					Persistence;		// persistence of IPS echo (ms)
	unsigned short int			PressureAge;		// age of pressure IPS measurement (in ping intervals). Max is 255.
	signed short int			Flag;			// IPS flag value
	signed short int			Status;			// status of data - see error codes above
	
	double					Draft;			// computed ice draft (in metres)

}icedraft_t;


// ==========
// Prototypes
// ==========
int decodeIPS(icedraft_t *Ice, char IPSString[]);
int calcAvgProp(double Patm, int N, double Conductivity[], double Temperature[], double Pressure[], double *AverageSoundSpeed, double *AverageDensity);
int IceDraft(icedraft_t *Ice, double *CalcWaterlevel, double Patm, double AverageSoundSpeed, double AverageDensity);
int pss78(double *Salinity, double P, double T, double C);
int ips_sound(double *SoundSpeed, double P, double T, double Salinity);
int state(double *Density, int ip, double P, double T, double Salinity);
int bins(double min, double max, double Data[], double A[], double B[], int N, double *AvgA, double *AvgB);
void Paros_Tilt(long int IntTiltX, long int IntTiltY, double *TiltX, double *TiltY);
void Paros_Temperature(double TemperaturePeriod, double *Temperature);
void Paros_Pressure(double TemperaturePeriod, double PressurePeriod, double *Pressure);
void RemoveZeroes(double *DataPtr, double Mean, int Value2Interp, long int N);
#endif
