unit linmod; interface function linearize(count:real;compensation:integer;curve:char;units,conv_gain:integer):real; implementation {$i typee.h} {$i typej.h} {$i typek.h} {$i typet.h} {$i typea.h} {$i typeu.h} const DEGREES_C=0; DEGREES_F=1; type aint=array[0..9999] of integer; function linearize(count:real;compensation:integer;curve:char;units,conv_gain:integer):real; var done, gain, lower, upper, offset, index:integer; result:real; int_count,numerator,denominator:longint; table:^aint; begin count := count * 16.0; case (conv_gain) of 0:conv_gain:=1; 1:conv_gain:=2; 2:conv_gain:=10; 3:conv_gain:=50; 4:conv_gain:=100; 5:conv_gain:=200; 6:conv_gain:=400; 7:conv_gain:=1000; end;{case} case (curve) of 'e':begin upper:=E_UPPER; table:=@e_array[0]; gain:=E_GAIN; offset:=E_OFFSET; end; 'j':begin upper:=J_UPPER; table:=@j_array[0]; gain:=J_GAIN; offset:=J_OFFSET; end; 'k':begin upper:=K_UPPER; table:=@k_array[0]; gain:=K_GAIN; offset:=K_OFFSET; end; 't':begin upper:=T_UPPER; table:=@t_array[0]; gain:=T_GAIN; offset:=T_OFFSET; end; 'a':begin upper:=A_UPPER; table:=@a_array[0]; gain:=A_GAIN; offset:=A_OFFSET; end; 'u':begin upper:=U_UPPER; table:=@u_array[0]; gain:=U_GAIN; offset:=U_OFFSET; end; end;{case} count :=count * gain / conv_gain; int_count:= trunc(count)+ compensation; lower:=0; if (int_count < table^[0]) or (int_count > table^[upper]) then begin linearize:= -9999.99;exit; end; done:=0; repeat index:=(upper + lower) div 2; if ((int_count = table^[index]) or (int_count > table^[index]) and (int_count < table^[index + 1])) then done:=1 else if (int_count < table^[index]) then upper:=index else lower:=index; until done=1; result:= (index * 10 + offset); numerator:=(int_count - table^[index]) * 10; denominator:=table^[index + 1] - table^[index]; result :=result + numerator / denominator; if (units = DEGREES_C) and (curve <> 'a') then begin result :=result - 32.0; result :=result * 0.55555; end; if (units = DEGREES_F) and (curve = 'a') then begin result :=result * 1.8; result :=result + 32.0; end; linearize:= result; end;{proc linearize} type along=array[0..999] of longint; function reverse_linearize(temperature:real; curve:char; units:integer):integer; var int_temperature,counts,residual,upper,offset,index:integer; table:^along; begin if (units = DEGREES_C) then begin temperature :=temperature * 1.8; temperature := temperature +320.0; int_temperature:=trunc(temperature); end; case (curve) of 'e':begin upper:=E_UPPER; table:=@e_array[0]; offset:=E_OFFSET; end; 'j':begin upper:=J_UPPER; table:=@j_array[0]; offset:=J_OFFSET; end; 'k':begin upper:=K_UPPER; table:=@k_array[0]; offset:=K_OFFSET; end; 't':begin upper:=T_UPPER; table:=@t_array[0]; offset:=T_OFFSET; end; 'a': reverse_linearize:=0; 'u': reverse_linearize:= 0; else begin reverse_linearize:= -9999;exit;end; end; {case} index:=(int_temperature div 100) - (offset div 10); if (index >= upper) then begin reverse_linearize:= -9999;exit;end; residual:=int_temperature mod 100; temperature:=(table^[index + 1] - table^[index]) / 10.0; temperature := temperature *residual / 10.0; counts:=trunc( table^[index] + trunc( temperature)); reverse_linearize:= counts; end; begin end.