//#############################################################################
// ASL IPS Compute Ice Draft Routines
//
// (c) CopyRight ASL Environmental Sciences Inc. 1997-2001
//
//	File:			IceDraft.c
//
//	Purpose:		Main routine for Ice Draft Calculation
//
//	Development:		National Instruments Labwindows/CVI 5.0 and higher
//					
//	Date:			March 20, 2001
//
// 	Programmer:		Dave Billenness
//
//  	Modified 		April 11, 2001 - Changed definitions of date to short ints
//				Changed to only one include file "IceDraft.h" "IceDraft_def.h
//				removed.
//				April 27, 2001 - Function to decode separated from main
//				function IceDraft.c
//******************************************************************************
//#include <ansi_c.h>
#include <stdio.h>
#include <math.h>
#include "IceDraft.h"


//#############################################################################
// Section:  Ice Draft Programs
//
// Function: int IceDraft(icedraft_t *Ice, char IPSString, double Patm, int N, 
//				double Conductivity[], double Temperature[], double Pressure[], int Flag, 
//				double SoundSpeed, double Density)
//
// Purpose:  Compute ice draft given the inputs:
//			Ice - structure containing the decoded IPS values, ice draft, and the average sound speed and density
//			IPSString - string of ASCII characters containing raw IPS output string array
//			Patm - value for atmospheric pressure (in bars)
//			N - number of elements in CTD array
//			C - pointer to first element of conductivity array (C(S,T,0)/C(35,T,0))
//			T - pointer to first element of temperature array (degress Celsius)
//			P - pointer to first element of pressure array (bars)
//			Flag - use input CTD profile to compute soundspeed and density (Flag = 1), or use
//					input values of SoundSpeed and Density (Flag = 0)
//			AvgSoundSpeed - if Flag = 0, use this value instead of the CTD profile (in m/sec)
//			AvgDensity - if Flag = 0, use this value instead of the CTD profile (in kg/m^3)
//
// returns: an int ErrorCode as defined in IceDraft_Def.h
//
//#############################################################################
int IceDraft(icedraft_t *Ice, double Patm, double AverageSoundSpeed, double AverageDensity)
{
		double	Tilt, WaterLevel;

		// Compute the acoustic range from the travel time
		Ice->Range = Ice->TravelTime/2. * AverageSoundSpeed;
		
		// Compute the Tilt Magnitude
		Tilt = sqrt(Ice->TiltX*Ice->TiltX + Ice->TiltY*Ice->TiltY);
		
		// If Range=0 then return with an Error -2
		if(Ice->Range == 0.)
		{
			Ice->Status = ERROR_RANGE_EQ_ZERO;
			return ERROR_RANGE_EQ_ZERO;
		} //if(Ice->Range == 0.)
		
		// Calculate a water level from the IPSPressure and Patm pressure data
		WaterLevel = ((Ice->Pressure - Patm*CONVERT_PRESSURE) * 10000.)/(AverageDensity * GRAVITY) + TRANSDUCERDISTANCE;
		
		// Calculate ice draft
		Ice->Draft = WaterLevel - Ice->Range * cos(Tilt * (PI/180.));
		
		// Change Ice->Status to 0 to indicate that the IPS data has
		// been processed (ie. ice drafts calculated)
		Ice->Status = DATA_PROCESSED;
		
		return DATA_PROCESSED;
}		



