'Program name: CUES_logger2_20101025.CR3
'CR3000 Series Datalogger
'MMSA Site AFter replacement of CR7's
'
'Updated:  08/25/2010 -- Kerry J. Claffey (CRREL)
'Phone 603-646-4634, e-mail: kclaffey@crrel.usace.army.mil

'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

'Program updated 10/25/2010 -- loaded into Cues #2 on 10/26/10.
'This UPDATE changes the SNOW_Depth data table output ONLY!
'The change increases the SNOW_Depth data table to an output rate of every 5 minutes
'It changes the Snow Pinger insturment output to sampled data instead of averaged data
'and it changed the Snow Pillow output to output both load cell seperatly -- 
'to output the raw mVolt readings on the snow pillow, and to output both averaged and 
'sampled show pillow data.

'						****************	CHANGE OFFSETS FOR SNOW DEPTH SENSORS HERE **********************

'declare offsets for Snow Depth Gauges
Const Plat_depth_offset= 598
Const Plat_Distance_offset=0
Const Remote_boom_offset= 594
Const Windmast_depth_offset= 578
Const Windmast_distance_offset=131.45

'declare SnowPillow offsets
Const SnowPillow_offset(1) = 0
Const SnowPillwo_offset(2) = 0

'Declare Constants for Thermistor calculations for in ground probe.
Const Const1 = 14661 * PWR(10,-7)
Const Const2 = 23857 * PWR(10,-8)
Const Const3 = 10049 * PWR(10,-11)

'Declare Public Variables
Public PTemp, batt_volt
Public Tip(16)
Public SoilMoisture(12)
Public xTerm
Public SoilTemperature(10)
Public SoilTempOut(10)
Public Therm_Ohm(10)
Public PlatRh
Public PlatTemperature
Public Snow_T107_C(10)
Public Plat_depth, plat_distance
Public RemoteBoom_depth
Public WindMast_pingerRAW(2)
Alias  WindMast_pingerRAW(2) = WindMast_SnowQuality
Public WindMast_CORR, WindMast_distance 
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

'Declare Other Variables
Dim I,J
Dim Term2,Term3,RtoC
Dim Dummy(20)

' 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(),IEEE4)
	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")
	Average (6,SoilMoisture(1),IEEE4,0)
	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")
	Average (6,SoilMoisture(7),IEEE4,0)
	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,IEEE4)' RH first then Platform Temperature
	FieldNames("Plat_RH:PCT")
	Sample (1,PlatTemperature,IEEE4) ' Platform Temperature
	FieldNames("Plat_Temp:DegC")
	Sample (8,Snow_T107_C(),IEEE4)
	FieldNames("SnowTemp1:DegC,SnowTemp2:DegC,SnowTemp3:DegC,SnowTemp4:DegC,SnowTemp5:DegC,SnowTemp6:DegC,SnowTemp7:DegC,Snowtemp8")
	Sample (10,SoilTempOut(),IEEE4) ' 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,IEEE4)
	FieldNames("CO2:PPM")
	Sample (1,BunkerTemp,IEEE4)
	FieldNames("BunkerTemp:C")
	Sample (1,Barometer,IEEE4)
	FieldNames("Station_Pressure:mBars")
EndTable

DataTable (SNOW_DEPTH,1,-1)
CardOut(0,-1)
	DataInterval(0,5,Min,0)
		Sample (1,RemoteBoom_depth,IEEE4)
		'Average (1,RemoteBoom_depth,IEEE4,0)' Boom on beam south of Bunker
		FieldNames("RemoteBoom_depth:cm") 

		Sample (1,Plat_depth,IEEE4)
		'Average (1,Plat_depth,IEEE4,0) ' Platform boom snow depth
		FieldNames("Plat_sno_depth:cm")
		Sample (1,plat_distance,IEEE4)
		'Average (1,Plat_distance,IEEE4,0) ' Platform boom snow depth
		FieldNames("Distance_Snow_to_Plat_Boom:cm")
    
    Sample (1,WindMast_depth,IEEE4)
		'Average (1,WindMast_depth,IEEE4,0)' Wind Mast depth sensor
		FieldNames("WindMast_depth:cm")
		Sample (1,WindMast_distance,IEEE4)
		'Average (1,WindMast_distance,IEEE4,0)' Wind Mast depth sensor
		FieldNames("WindMast_distance_to_SonicCenter:cm")
		'Average (1,WindMast_SnowQuality,IEEE4,0) 'Quality of Windmast Snow Reading Numbers between 162 and 210 are best
		sample (1,WindMast_SnowQuality,IEEE4)
		FieldNames("WindMast_Snow_Quality_number:# between 162 & 210 best")
		Sample (1,Plat_SnowPillow(1),IEEE4)
		FieldNames("SnowPillow_1_Sample: mVolts ")
		Sample (1,Plat_SnowPillow(2),IEEE4)
		FieldNames("SnowPillow_2_Sample: mVolts ")
		Average (1,Plat_SnowPillow(1),IEEE4,0)
		FieldNames("SnowPillow_1_Average: mVolts ")
		Average (1,Plat_SnowPillow(2) ,IEEE4,0)
		FieldNames("SnowPillow_2_Average: mVolts ")
		'Average(1,SnowPillow2,IEEE4,0)
		'FieldNames("SnowPillowOnGround: mm of water ")
EndTable

' 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,10)
			PulsePort(3,10000)
			BrFull(xTerm,1,mV200,4,2,1,200,1,1,5000,_60Hz,1,0)
			xTerm = (-xTerm/1000) + (10/(10+10))
			therm_ohm(I) = 10*(xTerm/(1-xTerm))*1000' BR Transform Rf[X/1-X]
			SoilTempOut(I) = (1/(const1+const2*LN(therm_ohm(I))+const3*(LN(therm_ohm(I)))^3))-273.15
		'LW_Temp_K = 1/(con1+con2*LN(therm_ohm)+con3*(LN(therm_ohm))^3)
			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)
        'Measure soil moisture gauges -- output in mVolts
			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)
		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)
		
		' Read Depth Sensor from Platform Boom
		VoltDiff(Plat_depth,1,mV5000,7,0,3000,_60Hz,-.25,Plat_depth_offset)
		Plat_distance = Plat_depth_offset - Plat_depth + Plat_distance_offset
		' Read Depth Sensor from Platform Boom
		VoltDiff(RemoteBoom_depth,1,mV5000,8,0,3000,_60Hz,-.25,Remote_boom_offset)
		' Read Depth Sensor from Wind Mast
		'VoltDiff(WindMast_depth,1,mV5000,9,0,3000,_60Hz,.5,Windmast_offset)
		' Wind Mast snow Pinger is a Campbell SR50 Snow Depth Gauge
        '** read snow Sounder
        SDI12Recorder(WindMast_pingerRAW(),7, 0, "M1!", 100,0)
        WindMast_SnowQuality = WindMast_SnowQuality/100
        WindMast_CORR=(WindMast_PingerRaw(1)*(SQR((PlatTemperature+273.15)/273.15)))
        
        WindMast_depth =  -(Windmast_depth_offset - Windmast_CORR)
        WindMast_distance = WindMast_corr + Windmast_distance_offset 
       				
		  	WindMast_depth = WindMast_depth * -1
		
		VoltDiff(Barometer,1,mV5000,12,0,3000,_60Hz,0.06,600)
		' NOT Corrected for site elevation of 9631 feet. 
		' Offset of 600 set for transducer Range at transducer 600 - 900 millibars
		
		' Start of Snow Job
		'Generic Full Bridge measurements for Snow Pillow Diff channels 10 & 11
		BrFull(Plat_SnowPillow(1),1,AutoRange,10,1,1,5000,True,True,3000,_60Hz,1,0)
		BrFull(Plat_SnowPillow(2),1,AutoRange,11,1,1,5000,True,True,3000,_60Hz,1,0)
	  '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

