#!/usr/bin/perl -w
# $Id$
# Dieses Script (delta_t_m) wurde von Rdiger Beck erstellt
# Es ist freie Software
# Bei Fehlern wenden Sie sich bitte an mich.
# jeffbeck@web.de  oder  jeffbeck@gmx.de


# Bibliotheken
use strict;
use Getopt::Long;
Getopt::Long::Configure ("bundling");
use coldformix;

# ==========================================================================

# Choose the Language: de, en
my $lang="de";

# Choose the Unit-System: si_symbol,
my $unit_system = "si_symbol";


# ===========================================================================
# Optionen verarbeiten
# ==========================================================================
# Parsen der Optionen
my $testopt=GetOptions(
           "lang|l=s" => \$lang,
           "unit_system|u=s" => \$unit_system
          );


print "Language: $lang \n";
print "Unit_System: $unit_system \n";

# Prfen, ob Optionen erkannt wurden, sonst Abbruch
&coldformix::check_options($testopt);

print "Starting\n";

open(DTMDATA,">../coldformix-generated/$lang/dtm-data");




# ===========================================================================
# Create Data
# ==========================================================================


# start heights in mm
my $start_low=0;
my $start_height=250;

# start_values in K
my $start_low_value=1;
my $start_high_value=100;

my $exactness=0.001;



for (my $approach = 1; $approach <= 10; $approach=$approach+0.2) { 
    print "Looking for height of $approach\n";
    my $height=$start_height;
    my $value=$start_high_value;

    my $height_over=$start_height;
    my $value_over=$start_high_value;
    my $height_under=$start_low;
    my $value_under=$start_low_value;

    my $close_enough=0;

    while ($close_enough==0){  
        print "      Ober-Wert ist  $value_over (at $height_over mm)\n";  
        print "      Unter-Wert ist $value_under (at $height_under mm)\n";  

        # calculating middle value
        ($height,$value) = &delta_t_m($height_under,
                                      $height_over,
                                      $value_under,
		   	              $value_over);
    
        # replace either over or under value with new value
        if ($value<=$approach){
            $height_under = $height;
            $value_under = $value;
        } else {
            # save this pair 
            $height_over = $height;
            $value_over = $value;
        }

        # check if result $value is close enough at $approach
        if ($value_over < $approach+$exactness and
            $value_under > $approach-$exactness){
            print "Close enough\n";
            # round height
            my $height_round=int($height*100+0.5)/100;
            print DTMDATA "$approach"."::"."$height_round"."::".
                          "$value"."::"."$height"."::\n";
            $close_enough=1;
        }
    }

}


for (my $approach = 10; $approach <= 100; $approach=$approach+1) { 
    print "Looking for height of $approach\n";
    my $height=$start_height;
    my $value=$start_high_value;

    my $height_over=$start_height;
    my $value_over=$start_high_value;
    my $height_under=$start_low;
    my $value_under=$start_low_value;

    my $close_enough=0;

    while ($close_enough==0){  
        print "      Ober-Wert ist  $value_over (at $height_over mm)\n";  
        print "      Unter-Wert ist $value_under (at $height_under mm)\n";  

        # calculating middle value
        ($height,$value) = &delta_t_m($height_under,
                                      $height_over,
                                      $value_under,
		   	              $value_over);
    
        # replace either over or under value with new value
        if ($value<=$approach){
            $height_under = $height;
            $value_under = $value;
        } else {
            # save this pair 
            $height_over = $height;
            $value_over = $value;
        }

        # check if result $value is close enough at $approach
        if ($value_over < $approach+$exactness and
            $value_under > $approach-$exactness){
            print "Close enough\n";
            # round height
            my $height_round=int($height*100+0.5)/100;
            print DTMDATA "$approach"."::"."$height_round"."::".
                          "$value"."::"."$height"."::\n";
            $close_enough=1;
        }
    }

}


close(DTMDATA);


# ===========================================================================
# Create *.tex.file
# ==========================================================================


open(TEX,">../coldformix-generated/$lang/latex/dtm-data.tex");

print TEX '%\documentclass{article}
%\begin{document}
%\setlength{\topmargin}{-20mm}
%\setlength{\textheight}{252mm}
\setlength{\unitlength}{1mm}
\begin{center}
\begin{picture}(100,260)
   \thicklines
   % 3 Axes:
   \put(0,0){\line(0,1){250}}
   \put(50,0){\line(0,1){250}}
   \put(100,0){\line(0,1){250}}
   % name the scales
   \put(0,-5){\makebox(0,0)[c]{$\Delta T_{min}$}}
   \put(50,-5){\makebox(0,0)[c]{$\Delta T_{m}$}}
   \put(100,-5){\makebox(0,0)[c]{$\Delta T_{max}$}}
   % small lines and text on the axes
';

open(DATA,"<../coldformix-generated/$lang/dtm-data") || die "Fehler";

while (<DATA>){
    chomp();
    my ($value,$height,$value_ex,$height_ex) = split(/::/);
    my $length;
    my $start_1;
    my $start_2;
    my $start_3;
    my $print_num=0;
    if ($value < 10){
        if ($value==int($value)){
	   $length=5;
           $print_num=1;
           $start_1=2.5;
           $start_2=52.5;
           $start_3=102.5;
        } else {
	   $length=3;
           $print_num=0;
           $start_1=1.5;
           $start_2=51.5;
           $start_3=101.5;
        }
    } elsif ($value >= 10){
        if ($value/5==int($value/5)){
	   $length=5;
           $print_num=1;
           $start_1=2.5;
           $start_2=52.5;
           $start_3=102.5;
        } else {
	   $length=3;
           $print_num=0;
           $start_1=1.5;
           $start_2=51.5;
           $start_3=101.5;
        }
    }

    # print lines and text
    print TEX "   \\put($start_1,$height){\\line(-1,0){$length}}\n";
    if ($print_num==1){
	my $start=$start_1-$length-10.8;
        my $height_num=$height-2;
       print TEX "   \\put($start,$height_num){\\makebox(10,4)[r]{$value}}\n";
    }
    print TEX "   \\put($start_2,$height){\\line(-1,0){$length}}\n";
    if ($print_num==1){
	my $start=$start_2-$length-10.8;
        my $height_num=$height-2;
       print TEX "   \\put($start,$height_num){\\makebox(10,4)[r]{$value}}\n";
    }
    print TEX "   \\put($start_3,$height){\\line(-1,0){$length}}\n";
    if ($print_num==1){
	my $start=$start_3-$length-10.8;
        my $height_num=$height-2;
       print TEX "   \\put($start,$height_num){\\makebox(10,4)[r]{$value}}\n";
    }
}

print TEX '\end{picture}
\end{center}
%\end{document}
';

close(TEX);

#system("cd /home/bz/cvs/coldformix/coldformix-generated/de; latex dtm-data.tex");
#system("cd /home/bz/cvs/coldformix/coldformix-generated/de; dvips dtm-data.dvi");
#system("cd /home/bz/cvs/coldformix/coldformix-generated/de; kghostview dtm-data.ps");



# ===========================================================================
# sub
# ==========================================================================

sub delta_t_m {
    my ($low,$high,$low_value,$high_value) = @_;
    #print "   Delta T_max= $high_value (at $high mm)\n";
    #print "   Delta T_min= $low_value (at $low mm)\n";
    my $d_t_m=($high+$low)/2;
    print "$high --- $low \n";
    my $d_t_m_value=($high_value-$low_value)/log($high_value/$low_value);

    print "===> Delta T_m= $d_t_m_value (at $d_t_m mm)\n";
    return ($d_t_m,$d_t_m_value);
}
