r97: Converted Scanner and Projections to C++
[ctsim.git] / cgi-bin / ctsim.cgi.in
1 #!/usr/bin/perl
2 # @configure_input@
3
4 use strict;
5 use CGI;
6 use File::Basename;
7 use Fcntl ':flock';
8
9 require './ctsim.conf';
10
11 my $opt_d = 0;
12 $ENV{'PATH'} = "$::bindir:$::lamrundir";
13
14 my $fromhost = $ENV{'REMOTE_HOST'};
15
16 my $gmt_start = time();
17 my $gmt_end;
18 my $execution_time;
19
20 my %in;
21 CGI::ReadParse(\%in);
22
23 # Incoming variables 
24 #   Phantom_Name, Phantom_Nx, Phantom_Ny, Phantom_NSample
25 #   PJ_NDet, PJ_NRot, PJ_NRay, PJ_RotAngle,
26 #   IR_Nx, IR_Ny, IR_Filter, IR_Filter_Param
27
28 my $error = "";
29
30 my $Phantom_Name = FilterMetaChars($in{'Phantom_Name'});
31 my $Phantom_Nx = FilterToNumber($in{'Phantom_Nx'});
32 my $Phantom_Ny = FilterToNumber($in{'Phantom_Ny'});
33 my $Phantom_NSample = FilterToNumber($in{'Phantom_NSample'});
34 $error .= "Phantom name must not be blank<br>" if ($Phantom_Name eq "");
35 $error .= "Phantom NX and NY must be between 5 and 1024<br>" if ($Phantom_Nx < 5 || $Phantom_Nx > 1024 || $Phantom_Ny < 5 || $Phantom_Ny > 1024);
36 $error .= "Phantom NSample must be between 1 and 10<br>" if ($Phantom_NSample < 1 || $Phantom_NSample > 10);
37
38 my $PJ_NDet = FilterToNumber($in{'PJ_NDet'});
39 my $PJ_NRot = FilterToNumber($in{'PJ_NRot'});
40 my $PJ_NRay = FilterToNumber($in{'PJ_NRay'});
41 my $PJ_RotAngle = FilterToNumber($in{'PJ_RotAngle'});
42 $error .= "Projection NDet must be between 5 and 1800<br>" if ($PJ_NDet < 5 || $PJ_NDet > 1800);
43 $error .= "Projection NRot must be between 5 and 2048<br>" if ($PJ_NRot < 5 || $PJ_NRot > 2048);
44 $error .= "Projection RotAngle must be between 0.1 and 2<br>" if ($PJ_RotAngle < 0.1 || $PJ_RotAngle > 2);
45
46 #my $IR_Nx = FilterToNumber($in{'IR_Nx'});
47 #my $IR_Ny = FilterToNumber($in{'IR_Ny'});
48 my $IR_Nx = $Phantom_Nx;
49 my $IR_Ny = $Phantom_Ny;
50 my $IR_Filter = FilterMetaChars($in{'IR_Filter'});
51 my $IR_Filter_Param = FilterToNumber($in{'IR_Filter_Param'});
52 my $IR_Interp = FilterMetaChars($in{'IR_Interp'});
53 my $IR_Backproj = FilterMetaChars($in{'IR_Backproj'});
54
55 my $Disp_Min = "auto";
56 my $Disp_Max = "auto";
57 $Disp_Min = FilterToNumber($in{'Disp_Min'}) if ($in{'Disp_Min'} ne "auto" && $in{'Disp_Min'} ne "");
58 $Disp_Max = FilterToNumber($in{'Disp_Max'}) if ($in{'Disp_Max'} ne "auto" && $in{'Disp_Max'} ne "");
59 if ($Disp_Min ne 'auto' && ! ($Disp_Min =~ /^[\d\.\-]+$/)) {
60     $error .= "Display Minimum must be 'auto' or numeric (received $Disp_Min) <br>";
61 }
62 if ($Disp_Max ne 'auto' && ! ($Disp_Max =~ /^[\d\.\-]+$/)) {
63     $error .= "Display Maximum must be 'auto' or numeric (received $Disp_Max) <br>";
64 }
65
66 my $MPI_Str = FilterMetaChars($in{'MPI'});
67 my $MPI = 0;
68 $MPI = 1 if ($MPI_Str eq "yes" && $::mpi_enable ne "");
69
70 $error .= "IR Nx and Ny must be between 5 and 1024<br>" if ($IR_Nx < 5 || $IR_Nx > 1024 || $IR_Ny < 5 || $IR_Ny > 1024);
71 $error .= "IR Filter Parameter must be between 0 and 1<br>" if ($IR_Filter_Param < 0 || $IR_Filter_Param > 1);
72
73
74 my $tmpid = $$;
75 my $auto_window_img = "std0.1";
76 my $auto_window_diff = "std1";
77 my $auto_window_pj = "full";
78 my $logfile = "$::jobdir/ctsim.log";
79
80 my $result_fname = "$::datadir/result-$tmpid.html";
81 my $phantom_fname = "$::datadir/phantom-$tmpid.if";
82 my $pj_fname = "$::datadir/pj-$tmpid.pj";
83 my $ir_fname = "$::datadir/ir-$tmpid.if";
84 my $pj_if_fname = "$::datadir/pj-$tmpid.if";
85 my $diff_fname = "$::datadir/diff-$tmpid.if";
86 my $phantom_png = "$::datadir/phantom-$tmpid.png";
87 my $ir_png = "$::datadir/ir-$tmpid.png";
88 my $pj_png = "$::datadir/pj-$tmpid.png";
89 my $diff_png = "$::datadir/diff-$tmpid.png";
90
91 my $result_url = "$::url_datadir/result-$tmpid.html";
92 my $phantom_png_url = "$::url_datadir/phantom-$tmpid.png";
93 my $ir_png_url = "$::url_datadir/ir-$tmpid.png";
94 my $pj_png_url = "$::url_datadir/pj-$tmpid.png";
95 my $diff_png_url = "$::url_datadir/diff-$tmpid.png";
96
97 my $ctrec_ver = "$::bindir/ctrec";
98 my $phm2pj_ver = "$::bindir/phm2pj";
99 my $phm2if_ver = "$::bindir/phm2if";
100 my $diff_ver = "$::bindir/if-2";
101 $ctrec_ver = "/opt/lam/bin/mpirun N N $::lamrundir/ctrec-lam" if $MPI;
102 $phm2pj_ver = "/opt/lam/bin/mpirun N N $::lamrundir/phm2pj-lam" if $MPI;
103 $phm2if_ver = "/opt/lam/bin/mpirun N N $::lamrundir/phm2if-lam" if $MPI;
104
105 my $gp_cmd = "$phm2if_ver $phantom_fname $Phantom_Nx $Phantom_Ny --phantom $Phantom_Name --nsample $Phantom_NSample";
106 my $pj_cmd = "$phm2pj_ver $pj_fname $PJ_NDet $PJ_NRot --phantom $Phantom_Name --nray $PJ_NRay --rotangle $PJ_RotAngle";
107 my $pj_if_cmd = "$::bindir/pj2if $pj_fname $pj_if_fname";
108 my $ir_cmd = "$ctrec_ver $pj_fname $ir_fname $IR_Nx $IR_Ny --filter $IR_Filter --filter-param $IR_Filter_Param --interp $IR_Interp --backproj $IR_Backproj";
109 my $diff_cmd = "$diff_ver $phantom_fname $ir_fname $diff_fname --comp";
110
111 my $window_options = "--auto $auto_window_img";
112 if ($Disp_Min ne 'auto') {
113     $window_options .= " --min $Disp_Min";
114 }
115 if ($Disp_Max ne 'auto') {
116     $window_options .= " --max $Disp_Max";
117 }
118
119 my $png1_cmd = "$::bindir/if2img $phantom_fname $phantom_png $window_options --stats --format png";
120 my $png2_cmd = "$::bindir/if2img $ir_fname $ir_png $window_options --stats --format png";
121 my $png3_cmd = "$::bindir/if2img $pj_if_fname $pj_png --auto $auto_window_pj --stats --format png";
122 my $png4_cmd = "$::bindir/if2img $diff_fname $diff_png --auto $auto_window_diff --stats --format png";
123
124 my $title = "CT Simulation Results";
125
126 my $out = head();
127 $out .= "<HTML> <HEAD><TITLE> $title </TITLE></HEAD>\n";
128 $out .= "<BODY  BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\"  VLINK=\"#800020\" LINK=\"#0000FF\">\n";
129 $out .= "<H1>$title</H1><HR>\n";
130
131 if ($opt_d) {
132     $out .= "<H2>Commands</H2>\n";
133     $out .= "$gp_cmd<br>\n$pj_cmd<br>\n$pj_if_cmd<br>\n$ir_cmd<br>\n$diff_cmd<br>\n$png1_cmd<br>\n$png2_cmd<br>\n" .
134             "$png3_cmd<br>\n$png4_cmd<br>\n";
135 }
136
137 my $cmdout = "";
138 if ($error ne "") {
139     $out .= "<FONT COLOR=\"#FF0000\">The following errors were present in your request.<br>\n";
140     $out .= "Please correct them and try submitting your request again.</FONT><br>\n";
141     $out .= "<i>$error</i>";
142 } else {
143   my $gp_out;
144   my $pj_out;
145   my $pj_if_out;
146   my $ir_out;
147   my $diff_out;
148   my $png_gp_out;
149   my $png_ir_out;
150   my $png_pj_out;
151   my $png_diff_out;
152   $gp_out = `$gp_cmd`;
153   if (-s $phantom_fname) {
154     $pj_out .= `$pj_cmd`;
155     $png_gp_out .= `$png1_cmd`;
156     if (-s $pj_fname) {
157       $pj_if_out .= `$pj_if_cmd`;
158       $png_pj_out .= `$png3_cmd`;
159       $ir_out .= `$ir_cmd`;
160       if (-s $ir_fname) {
161         $png_ir_out .= `$png2_cmd`;
162         $diff_out .= `$diff_cmd`;
163         $png_diff_out .= `$png4_cmd`;
164       }
165     }
166   }
167
168   $cmdout = "$gp_cmd\n $gp_out $pj_cmd\n $pj_out $pj_if_cmd\n $pj_if_out $ir_cmd\n $ir_out $diff_cmd\n $diff_out $png1_cmd\n $png_gp_out $png2_cmd\n $png_ir_out $png3_cmd\n $png_pj_out $png4_cmd\n $png_diff_out";
169   if (open(LOGFILE,">> $logfile")) {
170     flock(LOGFILE,LOCK_EX);
171     seek(LOGFILE, 0, 2);
172     print LOGFILE "Job $tmpid\n";
173     print LOGFILE $cmdout;
174     print LOGFILE "----------------------------------------------------\n";
175     flock(LOGFILE,LOCK_UN);
176     close(LOGFILE);
177   }
178   $gmt_end = time();
179   $execution_time = $gmt_end - $gmt_start;
180   if ($opt_d) {
181     $out .= "<H3>Command Output</H3>$cmdout<HR>\n";
182   }
183   my $png_gp_out_html = $png_gp_out;
184   my $png_ir_out_html = $png_ir_out;
185   my $png_pj_out_html = $png_pj_out;
186   my $png_diff_out_html = $png_diff_out;
187   $png_gp_out_html =~ s/\n/<br>/gms;
188   $png_ir_out_html =~ s/\n/<br>/gms;
189   $png_pj_out_html =~ s/\n/<br>/gms;
190   $png_diff_out_html =~ s/\n/<br>/gms;
191   $out .= "<TABLE><TR><TD>Phantom Image</TD><TD>Reconstructed Image</TD></TR>\n";
192   $out .= "<TR><TD><IMG SRC=\"$phantom_png_url\"><br><FONT SIZE=1>$png_gp_out</FONT></TD>\n";
193   $out .= "<TD><IMG SRC=\"$ir_png_url\"><br><FONT SIZE=1>$png_ir_out</FONT></TD></TR>\n";
194   $out .= "<TR><TD>Projection Sinusoid</TD><TD>Phantom/Reconst Error</TD></TR>\n";
195   $out .= "<TR><TD><IMG SRC=\"$pj_png_url\"><br><FONT SIZE=1>$png_pj_out</FONT></TD>\n";
196   $out .= "<TD><IMG SRC=\"$diff_png_url\"><br><FONT SIZE=2>$diff_out</FONT><br><FONT SIZE=1>$png_diff_out</FONT></TD></TR>\n";
197   $out .= "</TABLE>";
198   $out .= "Execution time: $execution_time seconds\n";
199 }
200
201 $out .= "<HR>\n";
202 $out .= "Return to <A HREF=\"/ctsim/index.html\">CT Simulation Designer</a>.<br>\n";
203 $out .= "Return to <A HREF=\"www.med-info.com\">Med-Info</a>.\n";
204 $out .= "</BODY> </HTML>";
205 $out .= "\n";
206
207 if (open(OUTFILE,"> $result_fname"))
208 {
209     flock(OUTFILE,LOCK_EX);
210     print OUTFILE $out;
211     flock(OUTFILE,LOCK_UN);
212     close OUTFILE;
213     print "Location: $result_url\n\n";
214 }
215 else
216 {
217     print "Content-type: text/plain\n\n";
218     print "The simulator was unable to create an result file.\n";
219 }
220 if (open(JOBFILES,"> $::jobdir/$tmpid"))
221 {
222     flock(JOBFILES,LOCK_EX);
223     print JOBFILES "gmt_start=$gmt_start\n";
224     print JOBFILES "gmt_end=$gmt_end\n";
225     print JOBFILES "execution_time=$execution_time\n";
226     print JOBFILES "Phantom_Name=$Phantom_Name\n";
227     print JOBFILES "Phantom_Nx=$Phantom_Nx\n";
228     print JOBFILES "Phantom_Ny=$Phantom_Nx\n";
229     print JOBFILES "Phantom_NSample=$Phantom_NSample\n";
230     print JOBFILES "PJ_NDet=$PJ_NDet\n";
231     print JOBFILES "PJ_NRot=$PJ_NRot\n";
232     print JOBFILES "PJ_NRay=$PJ_NRay\n";
233     print JOBFILES "PJ_RotAngle=$PJ_RotAngle\n";
234     print JOBFILES "IR_Nx=$IR_Nx\n";
235     print JOBFILES "IR_Ny=$IR_Ny\n";
236     print JOBFILES "IR_Interp=$IR_Interp\n";
237     print JOBFILES "IR_Filter=$IR_Filter\n";
238     print JOBFILES "IR_Filter_Param=$IR_Filter_Param\n";
239     print JOBFILES "IR_Backproj=$IR_Backproj\n";
240     print JOBFILES "Disp_Min=$Disp_Min\n";
241     print JOBFILES "Disp_Max=$Disp_Max\n";
242     print JOBFILES "MPI=$MPI\n";
243     print JOBFILES "Files=$result_fname,$phantom_fname,$pj_fname,$ir_fname,$phantom_png,$ir_png,$pj_if_fname,$pj_png\n" if ($error eq "");
244     printf JOBFILES "cmdout=$cmdout\n";
245     flock(JOBFILES,LOCK_UN);
246     close JOBFILES;
247 }
248
249
250 exit;
251
252
253 sub internal_error
254 {
255   my $out = head();
256   $out .= "<H1>Internal error</H1>
257   Please notify <A HREF=mailto:webmaster\@med-info.com>webmaster\@med-info.com</A>
258   </BODY>";
259   print $out;
260   exit;
261 }
262
263 sub head
264 {
265 #  "Content: text/html\n\n";
266 }
267
268
269 sub FilterMetaChars
270 {
271    my $var = pop(@_);
272    $var =~ /^(\w+)$/;  
273    $1;
274 }
275
276 sub FilterToNumber
277 {
278    my $var = pop(@_);
279    $var =~ /^(\-*\d*\.*\d*)$/;  
280    $1;
281 }