r627: no message
[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_FilterMethod = FilterMetaChars($in{'IR_FilterMethod'});
51 my $IR_Zeropad = FilterToNumber($in{'IR_Zeropad'});
52 my $IR_Filter = FilterMetaChars($in{'IR_Filter'});
53 my $IR_Filter_Param = FilterToNumber($in{'IR_Filter_Param'});
54 my $IR_Interp = FilterMetaChars($in{'IR_Interp'});
55 my $IR_Backproj = FilterMetaChars($in{'IR_Backproj'});
56
57 my $Disp_Min = "auto";
58 my $Disp_Max = "auto";
59 $Disp_Min = FilterToNumber($in{'Disp_Min'}) if ($in{'Disp_Min'} ne "auto" && $in{'Disp_Min'} ne "");
60 $Disp_Max = FilterToNumber($in{'Disp_Max'}) if ($in{'Disp_Max'} ne "auto" && $in{'Disp_Max'} ne "");
61 if ($Disp_Min ne 'auto' && ! ($Disp_Min =~ /^[\d\.\-]+$/)) {
62     $error .= "Display Minimum must be 'auto' or numeric (received $Disp_Min) <br>";
63 }
64 if ($Disp_Max ne 'auto' && ! ($Disp_Max =~ /^[\d\.\-]+$/)) {
65     $error .= "Display Maximum must be 'auto' or numeric (received $Disp_Max) <br>";
66 }
67
68 my $MPI_Str = FilterMetaChars($in{'MPI'});
69 my $MPI = 0;
70 $MPI = 1 if ($MPI_Str eq "yes" && $::mpi_enable ne "");
71
72 $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);
73 $error .= "IR Filter Parameter must be between 0 and 1<br>" if ($IR_Filter_Param < 0 || $IR_Filter_Param > 1);
74
75
76 my $tmpid = $$;
77 my $auto_window_img = "std0.1";
78 my $auto_window_diff = "std1";
79 my $auto_window_pj = "full";
80 my $logfile = "$::jobdir/ctsim.log";
81
82 my $result_fname = "$::datadir/result-$tmpid.html";
83 my $phantom_fname = "$::datadir/phantom-$tmpid.if";
84 my $pj_fname = "$::datadir/pj-$tmpid.pj";
85 my $ir_fname = "$::datadir/ir-$tmpid.if";
86 my $pj_if_fname = "$::datadir/pj-$tmpid.if";
87 my $sub_fname = "$::datadir/sub-$tmpid.if";
88 my $phantom_png = "$::datadir/phantom-$tmpid.png";
89 my $ir_png = "$::datadir/ir-$tmpid.png";
90 my $pj_png = "$::datadir/pj-$tmpid.png";
91 my $sub_png = "$::datadir/sub-$tmpid.png";
92
93 my $result_url = "$::url_datadir/result-$tmpid.html";
94 my $phantom_png_url = "$::url_datadir/phantom-$tmpid.png";
95 my $ir_png_url = "$::url_datadir/ir-$tmpid.png";
96 my $pj_png_url = "$::url_datadir/pj-$tmpid.png";
97 my $sub_png_url = "$::url_datadir/sub-$tmpid.png";
98
99 my $pjrec_ver = "$::bindir/pjrec";
100 my $phm2pj_ver = "$::bindir/phm2pj";
101 my $phm2if_ver = "$::bindir/phm2if";
102 my $diff_ver = "$::bindir/if-2";
103
104 $pjrec_ver = "/opt/lam/bin/mpirun N N $::lamrundir/pjrec-lam" if $MPI;
105 $phm2pj_ver = "/opt/lam/bin/mpirun N N $::lamrundir/phm2pj-lam" if $MPI;
106 $phm2if_ver = "/opt/lam/bin/mpirun N N $::lamrundir/phm2if-lam" if $MPI;
107
108 my $gp_cmd = "$phm2if_ver $phantom_fname $Phantom_Nx $Phantom_Ny --phantom $Phantom_Name --nsample $Phantom_NSample";
109 my $pj_cmd = "$phm2pj_ver $pj_fname $PJ_NDet $PJ_NRot --phantom $Phantom_Name --nray $PJ_NRay --rotangle $PJ_RotAngle";
110 my $pj_if_cmd = "$::bindir/pj2if $pj_fname $pj_if_fname";
111 my $pjrec_cmd = "$pjrec_ver $pj_fname $ir_fname $IR_Nx $IR_Ny --filter $IR_Filter --filter-param $IR_Filter_Param --interp $IR_Interp --backproj $IR_Backproj --filter-method $IR_FilterMethod --zeropad $IR_Zeropad";
112 my $sub_cmd = "$diff_ver $phantom_fname $ir_fname $sub_fname --sub";
113 my $diff_cmd = "$diff_ver $phantom_fname $ir_fname --comp";
114
115 my $window_options = "--auto $auto_window_img";
116 if ($Disp_Min ne 'auto') {
117     $window_options .= " --min $Disp_Min";
118 }
119 if ($Disp_Max ne 'auto') {
120     $window_options .= " --max $Disp_Max";
121 }
122
123 my $png1_cmd = "$::bindir/ifexport $phantom_fname $phantom_png $window_options --stats --format png";
124 my $png2_cmd = "$::bindir/ifexport $ir_fname $ir_png $window_options --stats --format png";
125 my $png3_cmd = "$::bindir/ifexport $pj_if_fname $pj_png --auto $auto_window_pj --stats --format png";
126 my $png4_cmd = "$::bindir/ifexport $sub_fname $sub_png --auto $auto_window_diff --stats --format png";
127
128 my $title = "CT Simulation Results";
129
130 my $out = head();
131 $out .= "<HTML> <HEAD><TITLE> $title </TITLE></HEAD>\n";
132 $out .= "<BODY  BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\"  VLINK=\"#800020\" LINK=\"#0000FF\">\n";
133 $out .= "<H1>$title</H1><HR>\n";
134
135 if ($opt_d) {
136     $out .= "<H2>Commands</H2>\n";
137     $out .= "$gp_cmd<br>\n$pj_cmd<br>\n$pj_if_cmd<br>\n$pjrec_cmd<br>\n$diff_cmd<br>\n$png1_cmd<br>\n$png2_cmd<br>\n" .
138             "$png3_cmd<br>\n$png4_cmd<br>\n";
139 }
140
141 my $cmdout = "";
142 if ($error ne "") {
143     $out .= "<FONT COLOR=\"#FF0000\">The following errors were present in your request.<br>\n";
144     $out .= "Please correct them and try submitting your request again.</FONT><br>\n";
145     $out .= "<i>$error</i>";
146 } else {
147   my $gp_out;
148   my $pj_out;
149   my $pj_if_out;
150   my $pjrec_out;
151   my $sub_out;
152   my $diff_out;
153   my $png_gp_out;
154   my $png_pjrec_out;
155   my $png_pj_out;
156   my $png_sub_out;
157   $gp_out = `$gp_cmd`;
158   if (-s $phantom_fname) {
159     $pj_out .= `$pj_cmd`;
160     $png_gp_out .= `$png1_cmd`;
161     if (-s $pj_fname) {
162       $pj_if_out .= `$pj_if_cmd`;
163       $png_pj_out .= `$png3_cmd`;
164       $pjrec_out .= `$pjrec_cmd`;
165       if (-s $ir_fname) {
166         $png_pjrec_out .= `$png2_cmd`;
167         $sub_out .= `$sub_cmd`;
168         $diff_out .= `$diff_cmd`;
169         $png_sub_out .= `$png4_cmd`;
170       }
171     }
172   }
173
174   $cmdout = "$gp_cmd\n $gp_out $pj_cmd\n $pj_out $pj_if_cmd\n $pj_if_out $pjrec_cmd\n $pjrec_out $diff_cmd\n $diff_out $png1_cmd\n $png_gp_out $png2_cmd\n $png_pjrec_out $png3_cmd\n $png_pj_out $png4_cmd\n $png_sub_out";
175   if (open(LOGFILE,">> $logfile")) {
176     flock(LOGFILE,LOCK_EX);
177     seek(LOGFILE, 0, 2);
178     print LOGFILE "Job $tmpid\n";
179     print LOGFILE $cmdout;
180     print LOGFILE "----------------------------------------------------\n";
181     flock(LOGFILE,LOCK_UN);
182     close(LOGFILE);
183   }
184   $gmt_end = time();
185   $execution_time = $gmt_end - $gmt_start;
186   if ($opt_d) {
187     $out .= "<H3>Command Output</H3>$cmdout<HR>\n";
188   }
189   my $png_gp_out_html = $png_gp_out;
190   my $png_pjrec_out_html = $png_pjrec_out;
191   my $png_pj_out_html = $png_pj_out;
192   my $png_sub_out_html = $png_sub_out;
193   $png_gp_out_html =~ s/\n/<br>/gms;
194   $png_pjrec_out_html =~ s/\n/<br>/gms;
195   $png_pj_out_html =~ s/\n/<br>/gms;
196   $png_sub_out_html =~ s/\n/<br>/gms;
197   $out .= "<TABLE><TR><TD>Phantom Image</TD><TD>Reconstructed Image</TD></TR>\n";
198   $out .= "<TR><TD><IMG SRC=\"$phantom_png_url\"><br><FONT SIZE=1>$png_gp_out</FONT></TD>\n";
199   $out .= "<TD><IMG SRC=\"$ir_png_url\"><br><FONT SIZE=1>$png_pjrec_out</FONT></TD></TR>\n";
200   $out .= "<TR><TD>Projection Sinusoid</TD><TD>Phantom/Reconst Error</TD></TR>\n";
201   $out .= "<TR><TD><IMG SRC=\"$pj_png_url\"><br><FONT SIZE=1>$png_pj_out</FONT></TD>\n";
202   $out .= "<TD><IMG SRC=\"$sub_png_url\"><br><FONT SIZE=2>$sub_out</FONT><br><FONT SIZE=1>$png_sub_out</FONT></TD></TR>\n";
203   $out .= "</TABLE>";
204   $out .= "Execution time: $execution_time seconds\n";
205   $out .= "<p>\nStatistics<br>";
206   $out .= "$diff_out";
207 }
208
209 $out .= "<HR>\n";
210 $out .= "Return to <A HREF=\"/ctsim/index.html\">CT Simulation Designer</a>.<br>\n";
211 $out .= "Return to <A HREF=\"www.med-info.com\">Med-Info</a>.\n";
212 $out .= "</BODY> </HTML>";
213 $out .= "\n";
214
215 if (open(OUTFILE,"> $result_fname"))
216 {
217     flock(OUTFILE,LOCK_EX);
218     print OUTFILE $out;
219     flock(OUTFILE,LOCK_UN);
220     close OUTFILE;
221     print "Location: $result_url\n\n";
222 }
223 else
224 {
225     print "Content-type: text/plain\n\n";
226     print "The simulator was unable to create an result file.\n";
227 }
228 if (open(JOBFILES,"> $::jobdir/$tmpid"))
229 {
230     flock(JOBFILES,LOCK_EX);
231     print JOBFILES "gmt_start=$gmt_start\n";
232     print JOBFILES "gmt_end=$gmt_end\n";
233     print JOBFILES "execution_time=$execution_time\n";
234     print JOBFILES "Phantom_Name=$Phantom_Name\n";
235     print JOBFILES "Phantom_Nx=$Phantom_Nx\n";
236     print JOBFILES "Phantom_Ny=$Phantom_Nx\n";
237     print JOBFILES "Phantom_NSample=$Phantom_NSample\n";
238     print JOBFILES "PJ_NDet=$PJ_NDet\n";
239     print JOBFILES "PJ_NRot=$PJ_NRot\n";
240     print JOBFILES "PJ_NRay=$PJ_NRay\n";
241     print JOBFILES "PJ_RotAngle=$PJ_RotAngle\n";
242     print JOBFILES "IR_Nx=$IR_Nx\n";
243     print JOBFILES "IR_Ny=$IR_Ny\n";
244     print JOBFILES "IR_Interp=$IR_Interp\n";
245     print JOBFILES "IR_Filter=$IR_Filter\n";
246     print JOBFILES "IR_Filter_Param=$IR_Filter_Param\n";
247     print JOBFILES "IR_Backproj=$IR_Backproj\n";
248     print JOBFILES "Disp_Min=$Disp_Min\n";
249     print JOBFILES "Disp_Max=$Disp_Max\n";
250     print JOBFILES "MPI=$MPI\n";
251     print JOBFILES "Files=$result_fname,$phantom_fname,$pj_fname,$ir_fname,$phantom_png,$ir_png,$pj_if_fname,$pj_png\n" if ($error eq "");
252     printf JOBFILES "cmdout=$cmdout\n";
253     flock(JOBFILES,LOCK_UN);
254     close JOBFILES;
255 }
256
257
258 exit;
259
260
261 sub internal_error
262 {
263   my $out = head();
264   $out .= "<H1>Internal error</H1>
265   Please notify <A HREF=mailto:webmaster\@med-info.com>webmaster\@med-info.com</A>
266   </BODY>";
267   print $out;
268   exit;
269 }
270
271 sub head
272 {
273 #  "Content: text/html\n\n";
274 }
275
276
277 sub FilterMetaChars
278 {
279    my $var = pop(@_);
280    $var =~ /^(\w+)$/;  
281    $1;
282 }
283
284 sub FilterToNumber
285 {
286    my $var = pop(@_);
287    $var =~ /^(\-*\d*\.*\d*)$/;  
288    $1;
289 }