#!/usr/bin/perl -w

use strict;

my %coeff = ( # [ A B C ]
    # from https://us.flukecal.com/pt100-calculator
    "PT-392" => [ 3.9827e-3, -5.875e-7, -4.171e-12, ],
    "PT-385" => [ 3.9083e-3, -5.775e-7, -4.183e-12, ],

    # http://www.code10.info/index.php%3Foption%3Dcom_content%26view%3Darticle%26id%3D82:measuring-temperature-platinum-resistance-thermometers%26catid%3D60:temperature%26Itemid%3D83
    "IPTS-68" => [ 3.90802e-03, -5.80195e-07, -4.27350e-12, ],
    "ITS-90"  => [ 3.9083e-03 , -5.7750e-07 , -4.1830e-12 , ],
);

my $R0 = 100;
my ($A, $B, $C);

sub negt($) {
    my $t = shift;
    my $R = 1.0 + $A*$t + $B * $t*$t + $C*($t - 100)*$t*$t*$t;
    $R *= $R0;
}

sub post($) {
    my $t = shift;
    my $R = 1.0 + $A*$t + $B * $t*$t;
    $R *= $R0;
}

sub main() {
    if (@ARGV < 1) {
	print("choose one of: ", join(" ", keys %coeff), "\n");
	return;
    }
    ($A, $B, $C) = @{$coeff{$ARGV[0]}};
    printf "%.4e %.4e %.4e\n", $A, $B, $C;
    #return;
    {
	my $t = -200;
	my $R = negt($t);
	printf "%4d %8.4f\n", $t, $R;
    }
    for (my $T = -190; $T <= 0; $T += 10) {
	printf "%4d", $T;
	for (my $d = 0; $d > -10; $d--) {
	    my $t = $T + $d;
	    my $R = post($t);
	    printf " %8.4f", $R;
	}
	print "\n";
    }

    for (my $T = 0; $T < 850; $T += 10) {
	printf "%4d", $T;
	for (my $d = 0; $d < 10; $d++) {
	    my $t = $T + $d;
	    my $R = post($t);
	    printf " %8.4f", $R;
	}
	print "\n";
    }
    {
	my $t = 850;
	my $R = post($t);
	printf "%4d %8.4f\n", $t, $R;
    }
}

main();
