~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina4/src/tools/nmr-segment/stim_report.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2   stim_report.c
  3 
  4   a.lacey   28.5.98
  5 */
  6 
  7 #include <stdio.h>
  8 #include <math.h>
  9 #include <tina/sys.h>
 10 #include <tina/sysfuncs.h>
 11 #include <tina/math.h>
 12 #include <tina/mathfuncs.h>
 13 #include <tina/vision.h>
 14 #include <tina/visionfuncs.h>
 15 #include <tina/tv.h>
 16 #include <tina/tvfuncs.h>
 17 #include <tina/draw.h>
 18 #include <tina/drawfuncs.h>
 19 #include <tina/seqoral.h>
 20 #include <tina/seqpro.h>
 21 #include <tina/stimdef.h>
 22 
 23 static char  c_convert[256] = {"convert"};
 24 static char  c_latex[256] = {"latex"};
 25 static char  c_dvips[256] = {"dvips"};
 26 static char  c_tidy[256] = {"rm -f"};
 27 
 28 static char     comm[512];
 29 static float    flow, area, pvol, tpvol;
 30 static float    pflow, tpflow, hrate;
 31 static int      pixs;
 32 
 33 
 34 static void  gen_flow_graph(Pl_flow *p, char *report)
 35 {
 36   FILE    *fp;
 37   int      i;
 38   char    *name;
 39 
 40   open_gnuplot_program_link("gnuplot");
 41   name = (char *)get_gnuplot_tname();
 42   send_gnuplot_comm("set nokey\n");
 43   send_gnuplot_comm("set term post\n");
 44   send_gnuplot_comm("set xlabel \"time (secs)\"\n");
 45   send_gnuplot_comm("set ylabel \"Flowrate (ml/sec)\"\n");
 46   sprintf(comm, "set output \"%s_graph.ps\"\n", report);
 47   send_gnuplot_comm(comm);
 48   sprintf(comm, "plot \"%s\" with linespoints\n", name);
 49   
 50   fp = (FILE *)open_gnuplot_data_link(comm);
 51   for (i = p->x_n1; i < p->x_n2; i++)
 52     {
 53       fprintf(fp, "%f %f\n", p->x[i], p->y1[i]);
 54       fflush(fp);
 55     }
 56 
 57   close_gnuplot_link(NULL);
 58 }
 59 
 60 
 61 static Bool gen_flow_images(Pl_flow *p, char *report)
 62 {
 63   List  *store = (List *)get_start_el();
 64   Imrect  *im = NULL, *tim = NULL;
 65 
 66   if ((store == NULL) || (store->to == NULL))
 67     return (false);
 68   im = (Imrect *)(store->to);
 69   
 70   sprintf(comm, "%s_image.pgm", report);  
 71   pgm_write_image(im, comm);
 72   sprintf(comm, "%s %s_image.pgm %s_image.eps\n", c_convert, report, report);
 73   system(comm);
 74   sprintf(comm, "rm %s_image.pgm", report);
 75   system(comm);
 76 
 77   if (p->mask != NULL)
 78     {
 79       sprintf(comm, "%s_mask.pgm", report);
 80       tim = im_times(255.0, p->mask);  
 81       pgm_write_image(tim, comm);
 82       sprintf(comm, "%s %s_mask.pgm %s_mask.eps\n", c_convert, report, 
 83               report);
 84       system(comm);
 85       sprintf(comm, "rm %s_mask.pgm", report);
 86       system(comm);
 87       im_free(tim);
 88     }
 89 
 90   return (true);
 91 }
 92 
 93 
 94 static void  rep_sepline(FILE *fp)
 95 {
 96   fprintf(fp, "\\vspace{4mm}\n\n");
 97 }
 98 
 99 
100 static void  rep_end_doc(FILE *fp)
101 {
102   fprintf(fp, "\\end{document}");
103 }
104 
105 static void  rep_latex_preamble(FILE *fp)
106 {
107   fprintf(fp, "%%\n%% TINA flow report\n%% Autogenerated - a.lacey@server1.smb.man.ac.uk\n%\n");
108   fprintf(fp, "\n\\documentclass{tina_rep}\n\\usepackage[dvips]{graphicx}\n");
109   fprintf(fp, "\\usepackage{subfigure}\n");
110   fprintf(fp, "\\begin{document}\n\n");
111   fprintf(fp, "\\centering{\\bf {\\Large Flow Report}}\n");
112 
113 }
114 
115 
116 static void rep_text_data(FILE *fp, char *patient, char *notes)
117 {
118   List  *store = (List *)get_start_el();
119   Imrect  *im = NULL;
120         char    *date = NULL;
121 
122   if ((store == NULL) || (store->to == NULL))
123     return;
124   im = (Imrect *)(store->to);
125   
126   if (im != NULL)
127     date = (char *)prop_get(im->props, PAT_DATA);
128  
129   fprintf(fp, "%%\n%% Text data\n%\n\n");
130         fprintf(fp, "\\begin{center}\n");
131   fprintf(fp, "\\begin{tabular}{|ll|}\n");
132   fprintf(fp, "\\hline\n");
133   fprintf(fp, "{\\bf Patient:} & %s \\\\ \n", patient);
134         if (date != NULL)
135                 fprintf(fp, "{\\bf Study Date:} & %s \\\\ \n", date);
136   fprintf(fp, "{\\bf Notes:} & %s \\\\ \n", notes);
137   fprintf(fp, "\\hline\n");
138   fprintf(fp, "\\end{tabular}\n");
139   fprintf(fp, "\\end{center}\n\n");
140 }
141 
142 
143 static void rep_table_data(FILE *fp, Pl_flow *p)
144 {
145   int      calc_mask_size(Imrect *im);
146   Imrect  *im = NULL;
147   List  *store = (List *)get_start_el();
148   Vec3    *scale = NULL;
149  
150   if (store != NULL)
151     im = (Imrect *)(store->to);
152   if (im != NULL)
153     scale = (Vec3 *)prop_get(im->props, VOXELS);
154 
155   pixs = calc_mask_size(p->mask);
156   area = (scale->el[0])*(scale->el[1])*(double)pixs;
157   hrate = 60.0/(p->x[(p->x_n2-1)]-p->x[(p->x_n1)]);
158   flow = p->y1_area*hrate;
159   pflow = p->y1_diff_max;
160   pvol = p->y1_max;
161   tpvol = p->y1_max_t*(p->x[p->x_n1+1]-p->x[p->x_n1]);
162   tpflow = p->y1_diff_max_t*(p->x[p->x_n1+1]-p->x[p->x_n1]);
163  
164   fprintf(fp, "%%\n%% Table data\n%%\n\n");
165         fprintf(fp, "\\begin{center}\n");
166   fprintf(fp, "\\begin{tabular}{|l|l|l|}\n");
167   fprintf(fp, "\\hline\n");
168   fprintf(fp, "{\\bf C.S. Area} & %4.2f $mm^2$ & %d pixels\\\\ \n", area, pixs);  
169   fprintf(fp, "\\hline\n");
170   fprintf(fp, "{\\bf Total Flow } & %4.2f $ml$ & in 1 minute\\\\ \n", flow);  
171   fprintf(fp, "\\hline\n");
172   fprintf(fp, "{\\bf Peak Flowrate} & %4.2f $ml/sec$ & @%4.4f $secs$ \\\\ \n", 
173           pvol, tpvol);  
174   fprintf(fp, "\\hline\n");
175   fprintf(fp, "{\\bf Heart Rate} & %4.2f $bpm$ & - \\\\ \n", hrate);
176   fprintf(fp, "\\hline\n");
177   fprintf(fp, "\\end{tabular}\n");
178   fprintf(fp, "\\end{center}\n\n");
179 }
180 
181 
182 static void  rep_image_data(FILE *fp, Pl_flow *p, char *report, Bool pim)
183 {
184   fprintf(fp, "%%\n%% Table & image\n%%\n\n");
185   fprintf(fp, "\\begin{figure}[ht]\n");
186   if (pim && (p->mask != NULL))
187     {
188       fprintf(fp, "\\centering\n");
189       fprintf(fp, "\\begin{minipage}[c]{0.4\\textwidth}\n");
190       fprintf(fp, "\\centering\n");
191       fprintf(fp, "\\includegraphics[width=2in]{%s_image.eps}\n",report);
192       fprintf(fp, "\\caption{1st image in sequence}\n");  
193       fprintf(fp, "\\end{minipage}\n");
194 
195       fprintf(fp, "\\begin{minipage}[c]{0.4\\textwidth}\n");
196       fprintf(fp, "\\centering\n");
197       fprintf(fp, "\\includegraphics[width=2in]{%s_mask.eps}\n",report);
198       fprintf(fp, "\\caption{Image mask}\n");  
199       fprintf(fp, "\\end{minipage}\n");
200     }
201   else if (pim && (p->mask == NULL))
202     {
203       fprintf(fp, "\\centering{\\includegraphics[width=4in]{%s_image.eps}}\n", 
204               report);
205       fprintf(fp, "\\caption{Image}\n");
206     }
207   else   
208     fprintf(fp, "\\centering{No Image}\n");
209   fprintf(fp, "\\end{figure}\n\n");
210 }
211 
212 
213 
214 static void  rep_graph(FILE *fp, char *report)
215 {
216   fprintf(fp, "%%\n%%Graph\n%%\n\n");
217   fprintf(fp, "\\begin{figure}[ht]\n");
218   fprintf(fp, "\\centering{\\includegraphics[height=4.5in,angle=-90]{%s_graph.ps}}\n", report);
219   fprintf(fp, "\\caption{Flowrate graph}\n");
220   fprintf(fp, "\\end{figure}\n\n");
221 }
222 
223 
224 static void  build_postscript(char *report)
225 {
226   sprintf(comm, "%s %s_report > /dev/null", c_latex, report);
227   system(comm);
228   system(comm);
229   sprintf(comm, "%s -o%s.ps %s_report.dvi > /dev/null", c_dvips, report, report);
230   system(comm);
231 }
232 
233 
234 static void  tidy_files(char *report)
235 {
236   sprintf(comm, "%s %s_report.dvi > /dev/null", c_tidy, report);
237   system(comm);
238   sprintf(comm, "%s %s_report.tex > /dev/null", c_tidy, report);
239   system(comm);
240   sprintf(comm, "%s %s_report.aux > /dev/null", c_tidy, report);
241   system(comm);
242   sprintf(comm, "%s %s_report.log > /dev/null", c_tidy, report);
243   system(comm);
244   sprintf(comm, "%s %s_graph.ps > /dev/null", c_tidy, report);
245   system(comm);
246   sprintf(comm, "%s %s_image.eps > /dev/null", c_tidy, report);
247   system(comm);
248   sprintf(comm, "%s %s_mask.eps > /dev/null", c_tidy, report);
249   system(comm);
250 }
251 
252 
253 void   flow_report(char *report, char *patient, char *notes)
254 {
255   Sequence *seq = (Sequence *)get_seq_ptr();
256   Pl_flow *p = get_pl_flow();
257   FILE    *fp;
258 
259   gen_flow_graph(p, report);
260 
261   sprintf(comm, "%s_report.tex", report);
262   if ((fp = fopen(comm, "w")) == NULL)
263     return;
264 
265   rep_latex_preamble(fp);
266   rep_sepline(fp);
267 
268   rep_text_data(fp, patient, notes);
269   rep_sepline(fp);
270 
271   rep_image_data(fp, p, report, gen_flow_images(p, report));
272   rep_sepline(fp);
273 
274   rep_table_data(fp, p);
275   rep_sepline(fp);
276 
277   rep_graph(fp, report);
278   
279   rep_end_doc(fp);
280 
281   fclose(fp);
282 
283   build_postscript(report);
284  
285   tidy_files(report);
286 }
287 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.