def get_cal_coefficients(data):
"""The calibration coefficients for the AL5002 instrument drift
inbetween calibrations. It is assumed that the drifting is linear
and too take account of this new coefficients are calculated for
each data point, which are then used to recalculate the CO concentrations.
"""
sens = data['AL52CO_sens'].ravel()
zero = data['AL52CO_zero'].ravel()
utc_time = [time.mktime(num2date(i).timetuple()) for \
i in data['mpl_timestamp'][:,0]]
# create copies of sens and zero calibration coefficients
sens_new, zero_new = sens[:], zero[:]
# get calibration periods
ix=np.where(sens[1:]-sens[:-1] != 0)[0]
# remove nan values
ix=ix[~np.isnan((sens[1:]-sens[:-1])[ix])]
# ignore the first 100 data points
ix=ix[ix>100]
# the +2 is a dodgy way to make sure that the values have changed.
# Apparently the zero and sens parameters do not change at
# exactly the same time in the data stream
ix=[10]+list(ix+2)+[sens.size-2]
# loop over all calibration periods
table=[]
for i in range(len(ix)-1):
ix1=ix[i]
ix2=ix[i+1]
sens_new[ix1:ix2]=np.interp(utc_time[ix1:ix2], np.float32([utc_time[ix1], utc_time[ix2]]), [sens[ix1], sens[ix2]])
zero_new[ix1:ix2]=np.interp(utc_time[ix1:ix2], np.float32([utc_time[ix1], utc_time[ix2]]), [zero[ix1], zero[ix2]])
# write calibration information to stdout
timestamp_start=datetime.datetime.utcfromtimestamp(utc_time[ix1]).strftime('%Y-%m-%d %H:%M:%S')
timestamp_end=datetime.datetime.utcfromtimestamp(utc_time[ix2]).strftime('%Y-%m-%d %H:%M:%S')
if np.isnan(sens[ix1]):
sens_string=' nan'
else:
sens_string='%6.2f' % (sens[ix1],)
if np.isnan(zero[ix1]):
zero_string=' nan'
else:
zero_string='%6i' % (zero[ix1],)
table.append([timestamp_start, timestamp_end, sens_string, zero_string])
return table
评论列表
文章目录