#!/usr/bin/perl -w
# SCMD/cgi-bin/CMD: program to answer *and generate* stars
# number of stars form (version 2)
use strict; # enforce variable declarations and quoting
use CGI qw(:standard);


sub bail{
    my $error ="@_";
    print h1("Unexpected Error"), p($error), end_html;
    die $error;
}

my $dataurl="http://cascade.yonsei.ac.kr/~yckim/cgi-bin/YY/table/SCMD.dat";
my $dataurl2="http://cascade.yonsei.ac.kr/~yckim/cgi-bin/YY/params/YYISO.IN";
my $BVurl="http://cascade.yonsei.ac.kr/~yckim/cgi-bin/YY/table/SCMD_BV.ps";
my $BVurl2="http://cascade.yonsei.ac.kr/~yckim/cgi-bin/YY/table/SCMD_BV.gif";
my $VIurl="http://cascade.yonsei.ac.kr/~yckim/cgi-bin/YY/table/SCMD_VI.ps";
my $VIurl2="http://cascade.yonsei.ac.kr/~yckim/cgi-bin/YY/table/SCMD_VI.gif";
my $defurl="http://cascade.yonsei.ac.kr/~yckim/cgi-bin/YY/params/def.html";

print header, start_html("Synthetic CMD"), h1("Synthetic CMD v2.0");
print hr();
print p("click here for : <a href=\"$defurl\"> parameter definitions</a>");

if (param()){ # the form has already been filled out
    my $alpha = param("Alpha enrichment");
    my $Z = param("Metallicity");
    my $Age = param("Age");
    my $N = param("Number");
    my $IMF_s = param("IMF slope");
    my $IMF_u = param("IMF upper limit");
    my $IMF_l = param("IMF lower limit");
    my $ISEED = param("ISEED");
    my $BF = param("Binary Fraction");
    my $q = param("qmass");
    
    if ($N <= 0 || $N >100000)
    {bail("Number of stars must be greater than 0 and less a maximum of 100000. $!");}

    #remove older temporary files 
    system("\\rm params/SCMD.IN");
    system("\\rm params/YYISO.IN");
    system("\\rm code/junk*");
    system("\\rm code/logfile");
    
    open(PARAMS,">params/SCMD.IN") || bail("Cannot open 'SCMD.IN' for reading $!");
    print PARAMS "$N !nstar\n$IMF_s\d0 !IMF slope\n$IMF_u\d0 !IMF upper limit\n$IMF_l\d0 !IMF lower limit\n$ISEED !RN ISEED\n$BF !binary fraction\n$q\d0 !qmass\n";
    close PARAMS or warn $!;
    
    #System calls to update the YY.nml file for interpolation routine
    system("sed 's/AFe_INIT/$alpha/g' code/YY.nml_INIT > code/junk1.nml");
    system("sed 's/Z_INIT/$Z/g' code/junk1.nml > code/junk2.nml");
    system("sed 's/Age_INIT/$Age/g' code/junk2.nml > code/junk3.nml");
    system("mv code/junk3.nml code/YY.nml");
    
    print p("Your input paramaters were:");
    print("alpha=$alpha,  Z=$Z,  Age=$Age,  N=$N,  IMF=$IMF_l - $IMF_u with slope $IMF_s,  ISEED=$ISEED,  Binary Fraction=$BF,  q=$q");
    
    print p("Please wait a moment while your syntetic CMD is created.");
    
    #System calls to remove older plots and data tables
    system("\\rm table/SCMD.dat");
    system("\\rm table/SCMD_BV*");
    system("\\rm table/SCMD_VI*");
    
    #System calls to Fortran codes and plotting code
    system("code/YYmix2 > code/logfile");
    system("code/SCMDv2");
    system("gnuplot code/SCMD.gnu");

    #reset bounding box of postscript file
    system("sed '6,6s/50/30/' table/SCMD_BV.ps > table/SCMD_BV1.ps");
    system("sed '6,6s/50/30/' table/SCMD_VI.ps > table/SCMD_VI1.ps");

    #conversion from PS to gif
    system("/usr/X11R6/bin/convert table/SCMD_BV1.ps table/SCMD_BV.gif >> code/logfile");
    system("/usr/X11R6/bin/convert table/SCMD_VI1.ps table/SCMD_VI.gif >> code/logfile");
#   system("table/ps2gif >> code/logfile");
    
    print p("click here to : <a href=\"$dataurl2\"> get your Isochrone data</a>");

    print p("click here to : <a href=\"$dataurl\"> get your Synthesic CMD data</a>");
    print p("click here to get a plot of your: <a href=\"$BVurl\">  (B-V) SCMD</a> or <a href=\"$VIurl\"> (V-I) SCMD</a> in postscript");
    print p("<table align=\"middle\">");
    print p("<tr>");
    print p("<td><img height=500 src=\"$BVurl2\"></td>");
    print p("<td><img height=500 src=\"$VIurl2\"></td>");
    print p("</tr>");
    print p("</table>");
    
    } 
else { # first time through, so present clear form
    print hr(); # hr() emits html horizontal rule: <HR>
    print start_form();
    print p("Alpha [alpha/Fe] (0.0-0.6): ", textfield("Alpha enrichment"));
    #print p("Alpha [alpha/Fe]: ", popup_menu("Alpha enrichment", [0.0,0.3,0.6]));
    print p("Metallicity (Z < 0.08): ", textfield("Metallicity"));
    #print p("Metallicity (8 for solar): ", popup_menu("Metallicity", [1..11]));
    print p("Age (Gyrs): ", textfield("Age"));
    #print p("Age (Gyrs): ", popup_menu("Age", [1..20]));
    print p("Initial Number of Stars (max=100000): ", textfield("Number"));
    print p("IMF slope (1.35 for Salpeter) : ", textfield("IMF slope"));
    print p("IMF (upper limit) in Solar Masses: ", textfield("IMF upper limit"));
    print p("IMF (lower limit) in Solar Masses: ", textfield("IMF lower limit"));
    print p("ISEED (integer): ", textfield("ISEED"));
    print p("Binary Fraction (0.0-1.0): ", textfield("Binary Fraction"));
    print p("qmass (0.0-1.0): ", textfield("qmass"));
    print p(submit("submit"), reset("clear"));
    print end_form, hr();
}
print end_html;





