Hi,
I'm building a program to log data from an SDI-12 sensor (acustic doppler). I expect that the sensor will return NaN values occacionally, and I would like these excluded from the average values. The behavior when just calling Average() with NaN values present is to return NaN. I tried a workaround where I only call the table when the value is not NaN. This can, however, result in the table not being called at the end of the DataInterval resulting in no values being written at all for that timestep. I would also like to report the number of values used in the average.
I've included my program below. Table1 is the default datatable. Here NaN's are written if one or more scans results in NaN values. Table3 is only called if the values are not NaN, and gives a usable average and a count of values used in the average. However, as the call is skipped if NaN is occuring at the end of the DataInterval then nothing is written to the table.
Is there a way to exclude NaN values from average and get a count of values used to construct the average?
In a case where all scans in a DataInterval return NaN I would also like a row to be written in Table3, can this be achieved?
'CR800 Series 'Created by Short Cut (4.0) 'Declare Variables and Units Public BattV Public PTemp_C Public SDI12(4) Public Counter_avg Alias SDI12(1)=Depth Alias SDI12(2)=Velocity Alias SDI12(3)=Temperature Alias SDI12(4)=Battery Units BattV=Volts Units PTemp_C=Deg C Units Depth=mm Units Velocity=mm/s Units Temperature=Deg C Units Battery=V Units Counter_avg=Count 'Define Data Tables DataTable(Table1,True,-1) DataInterval(0,5,Min,10) Average(1,Depth,FP2,False) Average(1,Velocity,FP2,False) Average(1,Temperature,FP2,False) Sample(1,Battery,FP2) EndTable DataTable(Table2,True,-1) DataInterval(0,60,Min,10) Minimum(1,BattV,FP2,False,False) Sample(1,PTemp_C,FP2) EndTable DataTable(Table3,True,-1) DataInterval(0,5,Min,10) Average(1,Depth,FP2,False) Average(1,Velocity,FP2,False) Average(1,Temperature,FP2,False) Sample(1,Battery,FP2) Totalize(1, Counter_avg, FP2, False) EndTable 'Main Program BeginProg Counter_avg = 1 'Main Scan Scan(10,Sec,1,0) 'Default CR800 Datalogger Battery Voltage measurement 'BattV' Battery(BattV) 'Default CR800 Datalogger Wiring Panel Temperature measurement 'PTemp_C' PanelTemp(PTemp_C,_60Hz) 'Generic SDI-12 Sensor measurements 'Depth', 'Velocity', 'Temperature', and 'Battery' SDI12Recorder(SDI12(),1,"0","M!",1,0) 'Call Data Tables and Store Data CallTable Table1 CallTable Table2 ' Only call Table3 if Depth and Velocity are not NaN If Depth <> NaN AND Velocity <> NaN then CallTable Table3 EndIf NextScan EndProg
Cheers
Take a look in the CRBasic help for the Average() output processing instruction. The last parameter for this instruction is referred to as a disableVar and it serves the purpose of allowing an expression to control whether the current value should be stored. It could be something as simple as:
Average(1, Temperature, FP2, Temperature = NaN)