FUNCTION_BLOCK InsertCalibrationPoint
shiftStartIndex := 0;
success := FALSE;

COP_LEVEL(TRUE, inLevelArr, 0, outLevelArr, 0, 0, FALSE);
COP_VOLUME(TRUE, inVolumeArr, 0, outVolumeArr, 0, 0, FALSE);


calMaxIndex := FindNumberOfCalPoints(inLevelArr);
IF calMaxIndex = 0 THEN
	(* theres no existing calibration data *)
	outLevelArr[1] := level;
	outVolumeArr[1] := volume;
	success := TRUE;
	
ELSIF level < outLevelArr[1] THEN
	(* less than the first point *)
	shiftStartIndex := 1;

ELSIF level > outLevelArr[calMaxIndex] THEN
	(* Greater than the last point *)
	IF calMaxIndex < 10 THEN
		outLevelArr[calMaxIndex + 1] := level;
		outVolumeArr[calMaxIndex + 1] := volume;
		success := TRUE;
	END_IF;
	
ELSE
	(* Somewhere in the middle *)
	FOR calIndex := 1 TO calMaxIndex - 1 DO
		IF outLevelArr[calIndex] < level AND outLevelArr[calIndex + 1] > level THEN
			shiftStartIndex := calIndex + 1;
		END_IF;
	END_FOR;
END_IF;

IF shiftStartIndex > 0 THEN 
	FOR shiftIndex := 9 TO shiftStartIndex BY -1 DO
		outLevelArr[shiftIndex + 1] := outLevelArr[shiftIndex];
		outVolumeArr[shiftIndex + 1] := outVolumeArr[shiftIndex];
	END_FOR;
	
	outLevelArr[shiftStartIndex] := level;
	outVolumeArr[shiftStartIndex] := volume;
	success := TRUE;
END_IF;
END_FUNCTION_BLOCK