'CR3000 Series Datalogger
'MMSA Site AFter replacement of CR7's
'
'program author: Jesse Stanley USA CRREL AUG 2007.
' ph. 603-646-4263
' Modified 07FEB2008 to incorporate Barometer and CO2
' Modified by JMS to change millvolts to millibars corrected for our elevation
' on Aug 25 2008
'						****************	CHANGE OFFSETS FOR SNOW DEPTH SENSORS HERE **********************
Const Plat_depth_offset=610.35 'CM
Const Remote_boom_offset=590.78 'CM
Const Windmast_offset=-275.46 'CM

'Declare Public Variables
Public PTemp, batt_volt
Public Tip(16)
Public SoilMoisture(12)
Public SoilTemperature(16)
Public SoilTempOut(16)
Public PlatRh
Public PlatTemperature
Public Snow_T107_C(8)
Public Plat_depth
Public RemoteBoom_depth
Public WindMast_depth
Public Plat_SnowPillow(2)
Public Plat_Snow_Pillow_total
Public SnowPillow2  ' On the ground snow pillow
Public Tip_eng(16)
Public CoTwo, BunkerTemp
Public Barometer
Public Forest_Sno_Pillow

'Declare Other Variables
Dim I,J
Dim Term2,Term3,RtoC
Dim Dummy(20)

'Declare Constants for Thermistor calculations for in ground probe.
'CONST PI = 3.141592654

Const Cube = 3
Const	Const1 = 14661 * PWR(10,-7)
Const Const2 = 23857 * PWR(10,-8)
Const Const3 = 10049 * PWR(10,-11)

' Data Table Melt gives tipping buckets in order 1 - 9
' then 12 Soil Moisture probes first 6 are south
' second 6 are North
' #1 is top of South and #7 is top of North
' Sensor Depths are 10,20,30,40,60,and 100 cm
DataTable (MELT,1,-1)
CardOut(0,-1)
	DataInterval (0,15,Min,0)
	Sample (9,Tip_eng(),FP2)
	FieldNames("T_Bucket1:L,T_Bucket2:L,T_Bucket3:L,T_Bucket4:L,T_Bucket5:L,T_Bucket6:L,T_Bucket7:L,T_Bucket8:L,T_Bucket9:L")
	Sample (6,SoilMoisture(1),FP2)
	FieldNames("S_Soil_M10cm:Volts,S_Soil_M20cm:Volts,S_Soil_M30cm:Volts,S_Soil_M40cm:Volts,S_Soil_M60cm:Volts,S_Soil_M100cm:Volts")
	Sample (6,SoilMoisture(7),FP2)
	FieldNames("N_Soil_M10cm:Volts,N_Soil_M20cm:Volts,N_Soil_M30cm:Volts,N_Soil_M40cm:Volts,N_Soil_M60cm:Volts,N_Soil_M100cm:Volts")
EndTable

'Data Table RH_Temp gives Platform RH, temperature, snow temperature 107 probes,
'and soil temperature where soil temp are 1-10 and 0 to -90 cm in 10cm decrements
DataTable (RH_TEMPERATURES,1,-1)
CardOut(0,-1)
	DataInterval(0,1,min,0)
	Sample (1,PlatRh,FP2)' RH first then Platform Temperature
	FieldNames("Plat_RH:PCT")
	Sample (1,PlatTemperature,FP2) ' Platform Temperature
	FieldNames("Plat_Temp:DegC")
	Sample (8,Snow_T107_C(),FP2)
	FieldNames("SnowTemp1:DegC,SnowTemp2:DegC,SnowTemp3:DegC,SnowTemp4:DegC,SnowTemp5:DegC,SnowTemp6:DegC,SnowTemp7:DegC,Snowtemp8")
	Sample (10,SoilTempOut(),FP2) ' 0 to -90 CM at -10cm depths
	FieldNames("Soil_temp_0cm:DegC,Soil_temp_M10cm:DegC,Soil_temp_M20cm:DegC,Soil_temp_M30cm:DegC,Soil_temp_M40cm:DegC,Soil_temp_M50cm:DegC,Soil_temp_M60cm:DegC,Soil_temp_M70cm:DegC,Soil_temp_M80cm:DegC,Soil_temp_M90cm:DegC")
	Sample (1,CoTwo,FP2)
	FieldNames("CO2:PPM")
	Sample (1,BunkerTemp,FP2)
	FieldNames("BunkerTemp:C")
	Sample (1,Barometer,IEEE4)
	FieldNames("ATMP:mBars")
EndTable

DataTable (SNOW_DEPTH,1,-1)
CardOut(0,-1)
	DataInterval(0,15,Min,0)
		Average (1,Plat_depth,FP2,0) ' Platform boom snow depth
		FieldNames("Plat_sno_depth:cm")
		Average (1,RemoteBoom_depth,FP2,0)' Boom on beam south of Bunker
		FieldNames("RemoteBoom_depth:cm") 
		Average (1,WindMast_depth,FP2,0)' Wind Mast depth sensor
		FieldNames("WindMast_depth:cm")
		Average (1,Plat_Snow_Pillow_total ,FP2,0)
		FieldNames("SnowPillow_Platform: mm of Water ")
		Average(1,SnowPillow2,FP2,0)
		FieldNames("SnowPillowOnGround: mm of water ")
		Average(1,Forest_Sno_Pillow,FP2,0)
		FieldNames("SnowPillow_ForestService: milliVolts ")
EndTable

'Defined Subroutine for Soil Temperature Thermistor Calculations
Sub SOILSUB
	For J= 1 To 10 Step 1
		RtoC= 10*(SoilTemperature(J)/(1-SoilTemperature(J)))' BR Transform Rf[X/1-X]
		RtoC = RtoC * 1000
		RtoC = LN(RtoC)
		Term2 = Const2 * RtoC
		Term3 = PWR(RtoC,Cube)
		Term3 = Term3 * Const3
		RtoC = Const1 + Term2 + Term3
		RtoC = (1/RtoC) - 273.15
		SoilTempOut(J) = RtoC
	Next J

EndSub
' Subroutine to do Polynomial fit on Soil Moisture probe data
' Equation is: y = 1740.1x^5 - 5023.9x^4 + 5658.9x^3 - 3045.4x^2 + 816.51x - 84.835
' when Y = Percent Volume and X = Volts

' Main Program
BeginProg

	'SequentialMode 
	
	Scan (60,Sec,0,0)
		
		' MUX 3 Air/Snow Temperatures
		PortSet(5,1) 'SET Mux3
		I=1
		SubScan(0,mSec,8)
			'Switch to next AM416 Multiplexer channel
			PulsePort(6,10000)
			'107 Temperature Probe (4-wire) measurements T107_C on the AM416 Multiplexer:
			Therm107(Snow_T107_C(I),1,1,3,3000,_60Hz,1.0,0)
			I=I+1
		NextSubScan
		PortSet(5,0) ' Reset MUX3

' Set MUX2 Soil Temperatures
		I=1 ' reset I to initial value
		PortSet(4,1)' Set MUX2
		SubScan(0,mSec,16)
			PulsePort(3,10000)
			BrFull(SoilTemperature(I),1,mV200,4,2,1,200,1,1,3000,_60Hz,.001,0)
			I=I+1
		NextSubScan
		PortSet(4,0)' RESET MUX2

		I=1
		PortSet(2,1)' Set MUX1 Soil Moisture gauges
		SubScan(0,mSec,12)
			PulsePort(1,10000)
			VoltDiff(SoilMoisture(I),1,mV5000,6,0,3000,_60Hz,1.0,0)
			I=I+1
		NextSubScan

		PortSet(2,0)' RESET MUX1
		
		' Read in Snow pillow #2 ) IN the ground
		BrFull(SnowPillow2,1,mV1000,3,4,1,5000,1,1,3000,_60Hz,-635.0,-152.4)
		
		Battery (batt_volt)
		
		' Read Depth Sensor from Platform Boom
		VoltDiff(Plat_depth,1,mV5000,7,0,3000,_60Hz,-.5,Plat_depth_offset)
		' Read Depth Sensor from Platform Boom
		VoltDiff(RemoteBoom_depth,1,mV5000,8,0,3000,_60Hz,-.5,Remote_boom_offset)
		' Read Depth Sensor from Wind Mast
		VoltDiff(WindMast_depth,1,mV5000,9,0,3000,_60Hz,.5,Windmast_offset)
		' Measure Platform RH and Temperature
		WindMast_depth = WindMast_depth * -1
		
		' Snow Pillow from James Howle Forest Service
		VoltDiff(Forest_Sno_Pillow,1,mV5000,14,0,3000,_60Hz,1.0,0)
		VoltDiff(Barometer,1,mV5000,12,0,3000,_60Hz,0.06,906.59)
		' Corrected for site elevation of 9631 feet. 
		' Range at transducer 600 - 900 millibars
				
		VoltSe(PlatTemperature,1,mV1000,9,0,3000,_60Hz,.1,-40)
		VoltSe(PlatRh,1,mV1000,10,0,3000,_60Hz,.1,0)
		VoltSe(CoTwo,1,mV5000,25,0,3000,_60Hz,1.0,0)
		VoltSe(BunkerTemp,1,mV5000,26,0,3000,_60Hz,.01,0)
		
		' Start of Snow Job
		'Generic Full Bridge measurements for Snow Pillow Diff channels 10 & 11
		BrFull(Plat_SnowPillow(1),1,mV20,10,1,1,5000,True,True,3000,_60Hz,-1.0,-.013)
		BrFull(Plat_SnowPillow(2),1,mV20,11,1,1,5000,True,True,3000,_60Hz,-1.0,-.012)
	  Plat_Snow_Pillow_total = Plat_SnowPillow(1)+Plat_SnowPillow(2)-.0018
	  Plat_Snow_Pillow_total = Plat_Snow_Pillow_total * 1241.447
		Plat_Snow_Pillow_total = Plat_Snow_Pillow_total - 5.36
		' End of snow job
		
		

' Call to calculate temperatures from voltage measurements in SoilTemperatue()
		Call SOILSUB
		
	
		' IMPORTANT This seems to need to be last! (No endif?) wouldn't compile with it.
		 
		If TimeIntoInterval(0,15,Min) Then
				Delay (2,5,Sec)' Option 2 (The first Number), 5 sec is time to delay, is specific to successive calls to SDM devices		
				SDMSW8A (Tip(),16,1,2,1,1,0)
				' Calculate Volume from first 8 tipping buckets  The Number 9 after for loop
				For J=1 To 8 Step 1
					Tip_eng(J) = Tip(J) * .08
				Next J
				' Big tipping bucket (#9) is 1.5 Liters per tip
				Tip_eng(9) = Tip(9) * 1.5
				
		EndIf
	
	' Call output Tables
		CallTable MELT
		CallTable RH_TEMPERATURES
		CallTable SNOW_DEPTH
		
		
	
	NextScan



EndProg

