ERIS Pipeline Reference Manual 1.9.2
sc_plot.c
Go to the documentation of this file.
1/*
2 * This file is part of the SKYCORR software package.
3 * Copyright (C) 2009-2013 European Southern Observatory
4 *
5 * This programme is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This programme is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this programme. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19
40/*****************************************************************************
41 * INCLUDES *
42 ****************************************************************************/
43
44#include <sc_plot.h>
45
46/*****************************************************************************
47 * DEFINES *
48 ****************************************************************************/
49
50#define EXIST W_OK /* check for file existence, unistd.h, access() */
51
52/*****************************************************************************
53 * FUNCTIONS *
54 ****************************************************************************/
55
56/*****************************************************************************
57 * CODE *
58 ****************************************************************************/
59
60/*****************************************************************************
61 * *
62 * Routine to plot a single spectrum *
63 * *
64 ****************************************************************************/
65
66void sc_setplottags_single_spec(cpl_parameterlist *plottags,
67 const char *x_column,
68 const char *y_column,
69 const char *title,
70 const char *x_label,
71 const char *y_label,
72 const cpl_parameterlist *parlist)
73{
98 cpl_parameter *p;
99 const cpl_parameter *cp;
100 char terminal[SC_MAXLEN], basedir[SC_MAXLEN];
101 char outdir[SC_MAXLEN], outname[SC_MAXLEN];
102
103 p = cpl_parameter_new_value("plot_title",CPL_TYPE_STRING,"","",title);
104 cpl_parameterlist_append(plottags, p);
105 p = cpl_parameter_new_value("xlabel",CPL_TYPE_STRING,"","",x_label);
106 cpl_parameterlist_append(plottags, p);
107 p = cpl_parameter_new_value("ylabel",CPL_TYPE_STRING,"","",y_label);
108 cpl_parameterlist_append(plottags, p);
109 p = cpl_parameter_new_value("xcol",CPL_TYPE_STRING,"","",x_column);
110 cpl_parameterlist_append(plottags, p);
111 p = cpl_parameter_new_value("ycol",CPL_TYPE_STRING,"","",y_column);
112 cpl_parameterlist_append(plottags, p);
113
114 cp = cpl_parameterlist_find_const(parlist,"plot_type");
115 sprintf(terminal,"%s",cpl_parameter_get_string(cp));
116 p = cpl_parameter_new_value("terminal",CPL_TYPE_STRING,"","",terminal);
117 cpl_parameterlist_append(plottags, p);
118
119 cp = cpl_parameterlist_find_const(parlist, "inst_dir");
120 sprintf(basedir,"%s",cpl_parameter_get_string(cp));
121 p = cpl_parameter_new_value("inst_dir",CPL_TYPE_STRING,"","",basedir);
122 cpl_parameterlist_append(plottags, p);
123
124 cp = cpl_parameterlist_find_const(parlist, "output_dir");
125 sprintf(outdir,"%s",cpl_parameter_get_string(cp));
126 p = cpl_parameter_new_value("output_dir",CPL_TYPE_STRING,"","",outdir);
127 cpl_parameterlist_append(plottags, p);
128
129 cp = cpl_parameterlist_find_const(parlist, "output_name");
130 sprintf(outname,"%s",cpl_parameter_get_string(cp));
131 p = cpl_parameter_new_value("output_name",CPL_TYPE_STRING,"","",outname);
132 cpl_parameterlist_append(plottags, p);
133
134}
135
136/****************************************************************************/
137/****************************************************************************/
138
139cpl_error_code sc_plot_single_spec(const cpl_table *spec,
140 cpl_parameterlist *plottags)
141{
168 FILE *specfile; /* output ASCII spec file ptr */
169 FILE *gnufile; /* gnuplot driverfile */
170 char filename[SC_MAXLEN]="sci_single_spec.dat"; /* filename */
171 char gnuname1[SC_MAXLEN]="single_gnufile_wxt.gnu";
172 char gnuname2[SC_MAXLEN]="single_gnufile_ps.gnu";
173 char gnuname3[SC_MAXLEN]="single_gnufile_x11.gnu";
174 char ps_filename[SC_MAXLEN];
175
176 char tmpdir[SC_MAXLEN];
177 char tmpfilename[SC_MAXLEN];
178// char spectype[SC_MAXLEN];
179 char plot_type[SC_MAXLEN]; /* plot type selection (plot_creation) */
180
181 char system_call[SC_MAXLEN]; /* system call */
182
183 int len=0; /* table length */
184 int run=0; /* runnning variable */
185 //int ncol=0; /* number of columns in inputspectable */
186 //int dummy=0; /* dummy return value for system calls */
187 double lambda=0; /* wavelength of spectrum */
188 double y_value=0; /* y-value of plot (RAD/TRA) */
189 int dir_exist_flag=0; /* Checking for existence of tmp dir */
190 /* Plot limits */
191 double plot_xmin=0., plot_xmax=0.;
192
193 /* plot tags */
194 char x_label[SC_MAXLEN], y_label[SC_MAXLEN]; /* labels */
195 char x_col[SC_MAXLEN], y_col[SC_MAXLEN];
196 char title[SC_MAXLEN]; /* title */
197
198 /* directory + filename for output ps file */
199 cpl_parameter *basedirpar, *outdirpar, *filenamepar, *par;
200
201/*--------------------------------------------------------------------------*/
202/* ------------------------------ INITIALISING -----------------------------*/
203/*--------------------------------------------------------------------------*/
204
205 /* Reading plot tags */
206 par=cpl_parameterlist_find(plottags,"plot_title");
207 sprintf(title,"%s",cpl_parameter_get_string(par));
208 par=cpl_parameterlist_find(plottags,"xlabel");
209 sprintf(x_label,"%s",cpl_parameter_get_string(par));
210 par=cpl_parameterlist_find(plottags,"ylabel");
211 sprintf(y_label,"%s",cpl_parameter_get_string(par));
212
213 par=cpl_parameterlist_find(plottags,"xcol");
214 sprintf(x_col,"%s",cpl_parameter_get_string(par));
215 par=cpl_parameterlist_find(plottags,"ycol");
216 sprintf(y_col,"%s",cpl_parameter_get_string(par));
217
218 par=cpl_parameterlist_find(plottags,"terminal");
219 sprintf(plot_type,"%s",cpl_parameter_get_string(par));
220
221 basedirpar = cpl_parameterlist_find(plottags, "inst_dir");
222 outdirpar = cpl_parameterlist_find(plottags, "output_dir");
223 filenamepar = cpl_parameterlist_find(plottags, "output_name");
224
225 /* Checking table (=input spectra) properties */
226 len=cpl_table_get_nrow(spec); /* length of spectrum */
227 //ncol=cpl_table_get_ncol(spec); /* # of columns */
228 plot_xmin=cpl_table_get_double(spec,x_col,0,NULL);
229 plot_xmax=cpl_table_get_double(spec,x_col,len-1,NULL);
230
231 /* Creating tmp-directory */
232 sprintf(tmpdir,"__tmpDIRtmp__");
233 dir_exist_flag=access(tmpdir,EXIST);
234 if (dir_exist_flag == 0){
235 cpl_msg_warning(cpl_func,"Directory %s already exists!",tmpdir);
236 }
237 else
238 {
239 if (mkdir(tmpdir,0777)) {};
240 }
241
242 /* writing .dat file containing spectrum information */
243 sprintf(tmpfilename,"%s/%s",tmpdir,filename);
244 specfile = fopen(tmpfilename,"w");
245 for (run=0;run<len;run++)
246 {
247 lambda=cpl_table_get_double(spec,x_col,run,NULL);
248 y_value=cpl_table_get_double(spec,y_col,run,NULL);
249 fprintf(specfile,"%5.6g\t%5.6g\n",lambda,y_value);
250 }
251 fclose(specfile);
252
253 /* Creating wxt terminal gnuplot driver file */
254 if ( (strcmp(plot_type,"W") == 0) || (strcmp(plot_type,"WX") == 0) ||
255 (strcmp(plot_type,"XW") == 0) )
256 {
257 sc_basic_initstring(tmpfilename,SC_MAXLEN);
258 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname1);
259 gnufile = fopen(tmpfilename,"w");
260 fprintf(gnufile,"set term wxt\n");
261 fprintf(gnufile,"set termoption enhanced \n");
262 fprintf(gnufile,"set nokey\n");
263 fprintf(gnufile,"set tmargin 2\n");
264 fprintf(gnufile,"set bmargin 5\n");
265 fprintf(gnufile,"set lmargin 13\n");
266 fprintf(gnufile,"set rmargin 3\n");
267 fprintf(gnufile,"set xrange [ %g : %g ]\n",plot_xmin,plot_xmax);
268 fprintf(gnufile,"set title \"%s\"\n",title);
269 fprintf(gnufile,"set xlabel \"%s\"\n",x_label);
270 fprintf(gnufile,"set ylabel \"%s\"\n",y_label);
271 fprintf(gnufile,"plot '%s/%s' using 1:2 with lines\n",tmpdir,
272 filename);
273 fprintf(gnufile,"\n");
274 fclose(gnufile);
275
276 /* Calling gnuplot */
277 sprintf(system_call,"gnuplot -persist %s/%s",tmpdir,gnuname1);
278 if (system(system_call)) {};
279
280 /* Cleaning */
281 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname1);
282 if (remove(tmpfilename)) {};
283 }
284
285 /* Creating postscript terminal gnuplot driver file */
286 sprintf(ps_filename,"%s/%s/%s_%s_singleplot.ps",
287 cpl_parameter_get_string(basedirpar),
288 cpl_parameter_get_string(outdirpar),
289 cpl_parameter_get_string(filenamepar),title);
290
291 sc_basic_initstring(tmpfilename,SC_MAXLEN);
292 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname2);
293 gnufile = fopen(tmpfilename,"w");
294 fprintf(gnufile,"set term postscript enhanced color\n");
295 fprintf(gnufile,"set output \"%s\"\n",ps_filename);
296 fprintf(gnufile,"set nokey\n");
297 fprintf(gnufile,"set tmargin 2\n");
298 fprintf(gnufile,"set bmargin 5\n");
299 fprintf(gnufile,"set lmargin 13\n");
300 fprintf(gnufile,"set rmargin 3\n");
301 fprintf(gnufile,"set xrange [ %g : %g ]\n",plot_xmin,plot_xmax);
302 fprintf(gnufile,"set title \"%s\"\n",title);
303 fprintf(gnufile,"set xlabel \"%s\"\n",x_label);
304 fprintf(gnufile,"set ylabel \"%s\"\n",y_label);
305 fprintf(gnufile,"plot '%s/%s' using 1:2 with lines\n",tmpdir,
306 filename);
307 fprintf(gnufile,"\n");
308 fclose(gnufile);
309
310 /* Calling gnuplot */
311 sprintf(system_call,"gnuplot -persist %s/%s",tmpdir,gnuname2);
312 if (system(system_call)) {};
313
314 /* Cleaning */
315 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname2);
316 if (remove(tmpfilename)) {};
317
318 /* Creating x11 terminal gnuplot driver file */
319 if ( (strcmp(plot_type,"X") == 0) || (strcmp(plot_type,"WX") == 0) ||
320 (strcmp(plot_type,"XW") == 0) )
321 {
322 sc_basic_initstring(tmpfilename,SC_MAXLEN);
323 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname3);
324 gnufile = fopen(tmpfilename,"w");
325 fprintf(gnufile,"set term x11\n");
326 fprintf(gnufile,"set termoption enhanced \n");
327 fprintf(gnufile,"set nokey\n");
328 fprintf(gnufile,"set tmargin 2\n");
329 fprintf(gnufile,"set bmargin 5\n");
330 fprintf(gnufile,"set lmargin 13\n");
331 fprintf(gnufile,"set rmargin 3\n");
332 fprintf(gnufile,"set xrange [ %g : %g ]\n",plot_xmin,plot_xmax);
333 fprintf(gnufile,"set title \"%s\"\n",title);
334 fprintf(gnufile,"set xlabel \"%s\"\n",x_label);
335 fprintf(gnufile,"set ylabel \"%s\"\n",y_label);
336 fprintf(gnufile,"plot '%s/%s' using 1:2 with lines\n",tmpdir,
337 filename);
338 fprintf(gnufile,"\n");
339 fclose(gnufile);
340
341 /* Calling gnuplot */
342 sprintf(system_call,"gnuplot -persist %s/%s",tmpdir,gnuname3);
343 if (system(system_call)) {};
344
345 /* Cleaning */
346 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname3);
347 if (remove(tmpfilename)) {};
348 }
349
350 /* Cleaning */
351 sc_basic_initstring(tmpfilename,SC_MAXLEN);
352 sprintf(tmpfilename,"%s/%s",tmpdir,filename);
353 if (remove(tmpfilename)) {};
354 if (rmdir(tmpdir)) {};
355
356 return CPL_ERROR_NONE;
357}
358
359
360/*****************************************************************************
361 * *
362 * Routine to plot two spectra *
363 * *
364 ****************************************************************************/
365
366void sc_setplottags_double_spec(cpl_parameterlist *plottags,
367 const char *x_column1,
368 const char *y_column1,
369 const char *title1,
370 const char *x_label1,
371 const char *y_label1,
372 const char *x_column2,
373 const char *y_column2,
374 const char *title2,
375 const char *x_label2,
376 const char *y_label2,
377 const cpl_parameterlist *parlist)
378{
412 cpl_parameter *p;
413 const cpl_parameter *cp;
414 char terminal[SC_MAXLEN], basedir[SC_MAXLEN];
415 char outdir[SC_MAXLEN], outname[SC_MAXLEN];
416
417 p = cpl_parameter_new_value("title1",CPL_TYPE_STRING,"","",title1);
418 cpl_parameterlist_append(plottags, p);
419 p = cpl_parameter_new_value("xlabel1",CPL_TYPE_STRING,"","",x_label1);
420 cpl_parameterlist_append(plottags, p);
421 p = cpl_parameter_new_value("ylabel1",CPL_TYPE_STRING,"","",y_label1);
422 cpl_parameterlist_append(plottags, p);
423 p = cpl_parameter_new_value("xcol1",CPL_TYPE_STRING,"","",x_column1);
424 cpl_parameterlist_append(plottags, p);
425 p = cpl_parameter_new_value("ycol1",CPL_TYPE_STRING,"","",y_column1);
426 cpl_parameterlist_append(plottags, p);
427
428 p = cpl_parameter_new_value("title2",CPL_TYPE_STRING,"","",title2);
429 cpl_parameterlist_append(plottags, p);
430 p = cpl_parameter_new_value("xlabel2",CPL_TYPE_STRING,"","",x_label2);
431 cpl_parameterlist_append(plottags, p);
432 p = cpl_parameter_new_value("ylabel2",CPL_TYPE_STRING,"","",y_label2);
433 cpl_parameterlist_append(plottags, p);
434 p = cpl_parameter_new_value("xcol2",CPL_TYPE_STRING,"","",x_column2);
435 cpl_parameterlist_append(plottags, p);
436 p = cpl_parameter_new_value("ycol2",CPL_TYPE_STRING,"","",y_column2);
437 cpl_parameterlist_append(plottags, p);
438
439 cp = cpl_parameterlist_find_const(parlist,"plot_type");
440 sprintf(terminal,"%s",cpl_parameter_get_string(cp));
441 p = cpl_parameter_new_value("terminal",CPL_TYPE_STRING,"","",terminal);
442 cpl_parameterlist_append(plottags, p);
443
444 cp = cpl_parameterlist_find_const(parlist, "inst_dir");
445 sprintf(basedir,"%s",cpl_parameter_get_string(cp));
446 p = cpl_parameter_new_value("inst_dir",CPL_TYPE_STRING,"","",basedir);
447 cpl_parameterlist_append(plottags, p);
448
449 cp = cpl_parameterlist_find_const(parlist, "output_dir");
450 sprintf(outdir,"%s",cpl_parameter_get_string(cp));
451 p = cpl_parameter_new_value("output_dir",CPL_TYPE_STRING,"","",outdir);
452 cpl_parameterlist_append(plottags, p);
453
454 cp = cpl_parameterlist_find_const(parlist, "output_name");
455 sprintf(outname,"%s",cpl_parameter_get_string(cp));
456 p = cpl_parameter_new_value("output_name",CPL_TYPE_STRING,"","",outname);
457 cpl_parameterlist_append(plottags, p);
458
459}
460
461/****************************************************************************/
462/****************************************************************************/
463
464cpl_error_code sc_plot_double_spec(const cpl_table *spec1,
465 const cpl_table *spec2,
466 cpl_parameterlist *plottags)
467{
486 //cpl_errorstate err_state;
487 cpl_error_code err_code=CPL_ERROR_NONE;
488
489 FILE *specfile; /* output ASCII spec file ptr */
490 FILE *gnufile; /* gnuplot driverfile */
491 char filename1[SC_MAXLEN]="plot_spec1.dat"; /* input spec 1 */
492 char filename2[SC_MAXLEN]="plot_spec2.dat"; /* input spec 2 */
493 char gnuname1[SC_MAXLEN] ="double_gnufile_wxt.gnu";
494 char gnuname2[SC_MAXLEN] ="double_gnufile_ps.gnu";
495 char gnuname3[SC_MAXLEN] ="double_gnufile_x11.gnu";
496
497 char tmpdir[SC_MAXLEN];
498 char tmpfilename[SC_MAXLEN];
499 char ps_filename[SC_MAXLEN]; /* Name of postscript file */
500 char plot_type[SC_MAXLEN]; /* plot type selection (plot_creation) */
501
502 char system_call[SC_MAXLEN];
503
504 int len1=0, len2=0; /* table lengtes */
505 //int ncol1=0, ncol2=0; /* number of columns in inputspectables */
506 //int dummy=0; /* dummy return value for system calls */
507 int run=0; /* runnning variable */
508 int gridcheckflag=0; /* flag for checking lambda grid */
509 int dir_exist_flag=0; /* Checking for existence of tmp dir */
510
511 /* Plot limits */
512 double plot_xmin1=0., plot_xmax1=0., plot_xmin2=0., plot_xmax2=0.;
513
514 double lambda=0; /* wavelength of spectrum */
515 double y_value=0; /* y-value of plot (RAD/TRA) */
516
517 /* Plot tags */
518 char title1[SC_MAXLEN]; /* title */
519 char x_label1[SC_MAXLEN],y_label1[SC_MAXLEN]; /* labels */
520 char x_col1[SC_MAXLEN], y_col1[SC_MAXLEN]; /* columns to plot */
521 char title2[SC_MAXLEN]; /* title */
522 char x_label2[SC_MAXLEN],y_label2[SC_MAXLEN]; /* labels */
523 char x_col2[SC_MAXLEN], y_col2[SC_MAXLEN]; /* columns to plot */
524
525 /* directory + filename for output ps file */
526 cpl_parameter *basedirpar, *outdirpar, *filenamepar, *par;
527
528/*--------------------------------------------------------------------------*/
529/* ------------------------------ INITIALISING -----------------------------*/
530/*--------------------------------------------------------------------------*/
531
532 /* Reading plot tags */
533 par=cpl_parameterlist_find(plottags,"title1");
534 sprintf(title1,"%s",cpl_parameter_get_string(par));
535 par=cpl_parameterlist_find(plottags,"xlabel1");
536 sprintf(x_label1,"%s",cpl_parameter_get_string(par));
537 par=cpl_parameterlist_find(plottags,"ylabel1");
538 sprintf(y_label1,"%s",cpl_parameter_get_string(par));
539 par=cpl_parameterlist_find(plottags,"xcol1");
540 sprintf(x_col1,"%s",cpl_parameter_get_string(par));
541 par=cpl_parameterlist_find(plottags,"ycol1");
542 sprintf(y_col1,"%s",cpl_parameter_get_string(par));
543
544 par=cpl_parameterlist_find(plottags,"title2");
545 sprintf(title2,"%s",cpl_parameter_get_string(par));
546 par=cpl_parameterlist_find(plottags,"xlabel2");
547 sprintf(x_label2,"%s",cpl_parameter_get_string(par));
548 par=cpl_parameterlist_find(plottags,"ylabel2");
549 sprintf(y_label2,"%s",cpl_parameter_get_string(par));
550 par=cpl_parameterlist_find(plottags,"xcol2");
551 sprintf(x_col2,"%s",cpl_parameter_get_string(par));
552 par=cpl_parameterlist_find(plottags,"ycol2");
553 sprintf(y_col2,"%s",cpl_parameter_get_string(par));
554
555 par=cpl_parameterlist_find(plottags,"terminal");
556 sprintf(plot_type,"%s",cpl_parameter_get_string(par));
557 basedirpar = cpl_parameterlist_find(plottags, "inst_dir");
558 outdirpar = cpl_parameterlist_find(plottags, "output_dir");
559 filenamepar = cpl_parameterlist_find(plottags, "output_name");
560
561 /* checking error state */
562 //err_state = cpl_errorstate_get();
563
564 /* CHECKING INPUT ------------------------------------------------------*/
565 /* Input spectra will be checked against
566 - same size (#cols, #rows)
567 - same CPL table structure
568 - same wavelength grid
569 - type of input specta
570 */
571 /* Checking table (=input spectra) properties */
572 len1=cpl_table_get_nrow(spec1); /* length of spectrum 1 */
573 //ncol1=cpl_table_get_ncol(spec1); /* # of columns spec 1 */
574
575 len2=cpl_table_get_nrow(spec2); /* length of spectrum 2 */
576 //ncol2=cpl_table_get_ncol(spec2); /* # of columns spec 2 */
577
578 plot_xmin1=cpl_table_get_double(spec1,x_col1,0,NULL);
579 plot_xmax1=cpl_table_get_double(spec1,x_col1,len1-1,NULL);
580 plot_xmin2=cpl_table_get_double(spec2,x_col2,0,NULL);
581 plot_xmax2=cpl_table_get_double(spec2,x_col2,len2-1,NULL);
582
583 /* Checking length of input CPL tables */
584 if (len1 != len2)
585 {
586 cpl_msg_warning(cpl_func,"Input spectra do not have the same size.");
587 }
588
589 /* Checking structure of input CPL tables */
590 if ( cpl_table_compare_structure(spec1,spec2) )
591 {
592 cpl_msg_warning(cpl_func,"Input spectra do not have the same "
593 "structure.");
594 }
595
596 /* Checking if wavelength grid is identical */
597 run=0;
598 for (run=0;run<len1;run++)
599 {
600 if (cpl_table_get_double(spec1,x_col1,run,NULL) !=
601 cpl_table_get_double(spec2,x_col2,run,NULL) )
602 {
603 gridcheckflag=1;
604 }
605 }
606 if (gridcheckflag)
607 {
608 cpl_msg_warning(cpl_func,"Input spectra have differing wavelength "
609 "grids.");
610 }
611
612 /* Checking / Creating tmp-directory */
613 sprintf(tmpdir,"__tmpDIRtmp__");
614 dir_exist_flag=access(tmpdir,EXIST);
615 if (dir_exist_flag == 0){
616 cpl_msg_warning(cpl_func,"Directory %s already exists!",tmpdir);
617 }
618 else
619 {
620 if (mkdir(tmpdir,0777)) {};
621 }
622
623 /* writing .dat files containing spectrum information */
624 sc_basic_initstring(tmpfilename,SC_MAXLEN);
625 sprintf(tmpfilename,"%s/%s",tmpdir,filename1);
626 specfile = fopen(tmpfilename,"w"); /* Science spectrum */
627 for (run=0;run<len1;run++)
628 {
629 lambda=cpl_table_get_double(spec1,x_col1,run,NULL);
630 y_value=cpl_table_get_double(spec1,y_col1,run,NULL);
631 fprintf(specfile,"%5.6g\t%5.6g\n",lambda,y_value);
632 }
633 fclose(specfile);
634
635 sc_basic_initstring(tmpfilename,SC_MAXLEN);
636 sprintf(tmpfilename,"%s/%s",tmpdir,filename2);
637 specfile = fopen(tmpfilename,"w"); /* Fit spectrum */
638 for (run=0;run<len2;run++)
639 {
640 lambda=cpl_table_get_double(spec2,x_col2,run,NULL);
641 y_value=cpl_table_get_double(spec2,y_col2,run,NULL);
642 fprintf(specfile,"%5.6g\t%5.6g\n",lambda,y_value);
643 }
644 fclose(specfile);
645
646/****************************************************************************/
647
648 /* Creating wxt terminal gnuplot file */
649 if ( (strcmp(plot_type,"W") == 0) || (strcmp(plot_type,"WX") == 0) ||
650 (strcmp(plot_type,"XW") == 0) )
651 {
652 sc_basic_initstring(tmpfilename,SC_MAXLEN);
653 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname1);
654 gnufile = fopen(tmpfilename,"w");
655 fprintf(gnufile,"set term wxt\n");
656 fprintf(gnufile,"set termoption enhanced \n");
657 fprintf(gnufile,"# Plotting\n");
658 fprintf(gnufile,"set nokey\n");
659 fprintf(gnufile,"set tmargin 2\n");
660 fprintf(gnufile,"set bmargin 5\n");
661 fprintf(gnufile,"set lmargin 12\n");
662 fprintf(gnufile,"set rmargin 3\n");
663 fprintf(gnufile,"set xrange [ %g : %g ]\n",plot_xmin1,plot_xmax1);
664 fprintf(gnufile,"set termoption font \"Times,7\"\n");
665 fprintf(gnufile,"set multiplot layout 2,1 \n");
666 fprintf(gnufile,"set title \"%s\"\n",title1);
667 fprintf(gnufile,"set xlabel \"%s\"\n",x_label1);
668 fprintf(gnufile,"set ylabel \"%s\" offset 1,0\n",y_label1);
669 fprintf(gnufile,"set style data boxes\n");
670 fprintf(gnufile,"plot '%s/%s' using 1:2 title "
671 " \"%s\" with lines \n",tmpdir,filename1,title1);
672 fprintf(gnufile,"set nokey\n");
673 fprintf(gnufile,"set ylabel \"%s\" offset 1,0\n",y_label2);
674 fprintf(gnufile,"set xlabel \"%s\"\n",x_label2);
675 fprintf(gnufile,"set xrange [ %g : %g ]\n",plot_xmin2,plot_xmax2);
676
677 fprintf(gnufile,"set title \"%s\"\n",title2);
678 fprintf(gnufile,"plot '%s/%s' "
679 "using 1:2 title \"%s\" with lines \n",
680 tmpdir,filename2,title2);
681 fprintf(gnufile,"unset multiplot \n");
682 fclose(gnufile);
683
684 /* Calling gnuplot */
685 sprintf(system_call,"gnuplot -persist %s/%s",tmpdir,gnuname1);
686 if (system(system_call)) {};
687
688 /* Cleaning */
689 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname1);
690 if (remove(tmpfilename)) {};
691 }
692
693 /* Creating postscript terminal gnuplot driver file */
694 sprintf(ps_filename,"%s/%s/%s_doubleplot.ps",
695 cpl_parameter_get_string(basedirpar),
696 cpl_parameter_get_string(outdirpar),
697 cpl_parameter_get_string(filenamepar));
698
699 sc_basic_initstring(tmpfilename,SC_MAXLEN);
700 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname2);
701 gnufile = fopen(tmpfilename,"w");
702
703 fprintf(gnufile,"set term postscript enhanced color\n");
704 fprintf(gnufile,"set output \"%s\"\n", ps_filename);
705 fprintf(gnufile,"# Plotting\n");
706 fprintf(gnufile,"set nokey\n");
707 fprintf(gnufile,"set tmargin 2\n");
708 fprintf(gnufile,"set bmargin 5\n");
709 fprintf(gnufile,"set lmargin 12\n");
710 fprintf(gnufile,"set rmargin 3\n");
711 fprintf(gnufile,"set xrange [ %g : %g ]\n",plot_xmin1,plot_xmax1);
712 fprintf(gnufile,"set termoption font \"Times,12\"\n");
713 fprintf(gnufile,"set multiplot layout 2,1 \n");
714 fprintf(gnufile,"set title \"%s\"\n",title1);
715 fprintf(gnufile,"set xlabel \"%s\"\n",x_label1);
716 fprintf(gnufile,"set ylabel \"%s\" offset 1,0\n",y_label1);
717 fprintf(gnufile,"set style data boxes\n");
718 fprintf(gnufile,"plot '%s/%s' using 1:2 title "
719 " \"%s\" with lines \n",tmpdir,filename1,title1);
720 fprintf(gnufile,"set nokey\n");
721 fprintf(gnufile,"set ylabel \"%s\" offset 1,0\n",y_label2);
722 fprintf(gnufile,"set xlabel \"%s\"\n",x_label2);
723 fprintf(gnufile,"set xrange [ %g : %g ]\n",plot_xmin2,plot_xmax2);
724
725 fprintf(gnufile,"set title \"%s\"\n",title2);
726 fprintf(gnufile,"plot '%s/%s' "
727 "using 1:2 title \"%s\" with lines \n",
728 tmpdir,filename2,title2);
729 fprintf(gnufile,"unset multiplot \n");
730 fclose(gnufile);
731
732 /* Calling gnuplot */
733 sprintf(system_call,"gnuplot -persist %s/%s",tmpdir,gnuname2);
734 if (system(system_call)) {};
735
736 /* Cleaning */
737 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname2);
738 if (remove(tmpfilename)) {};
739
740 /* Creating x11 terminal gnuplot file */
741 if ( (strcmp(plot_type,"X") == 0) || (strcmp(plot_type,"WX") == 0) ||
742 (strcmp(plot_type,"XW") == 0) )
743 {
744 sc_basic_initstring(tmpfilename,SC_MAXLEN);
745 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname3);
746 gnufile = fopen(tmpfilename,"w");
747 fprintf(gnufile,"set term x11\n");
748 fprintf(gnufile,"set termoption enhanced \n");
749 fprintf(gnufile,"# Plotting\n");
750 fprintf(gnufile,"set nokey\n");
751 fprintf(gnufile,"set tmargin 2\n");
752 fprintf(gnufile,"set bmargin 5\n");
753 fprintf(gnufile,"set lmargin 12\n");
754 fprintf(gnufile,"set rmargin 3\n");
755 fprintf(gnufile,"set xrange [ %g : %g ]\n",plot_xmin1,plot_xmax1);
756 fprintf(gnufile,"set termoption font \"Times,7\"\n");
757 fprintf(gnufile,"set multiplot layout 2,1 \n");
758 fprintf(gnufile,"set title \"%s\"\n",title1);
759 fprintf(gnufile,"set xlabel \"%s\"\n",x_label1);
760 fprintf(gnufile,"set ylabel \"%s\" offset 1,0\n",y_label1);
761 fprintf(gnufile,"set style data boxes\n");
762 fprintf(gnufile,"plot '%s/%s' using 1:2 title "
763 " \"%s\" with lines \n",tmpdir,filename1,title1);
764 fprintf(gnufile,"set nokey\n");
765 fprintf(gnufile,"set ylabel \"%s\" offset 1,0\n",y_label2);
766 fprintf(gnufile,"set xlabel \"%s\"\n",x_label2);
767 fprintf(gnufile,"set xrange [ %g : %g ]\n",plot_xmin2,plot_xmax2);
768 fprintf(gnufile,"set title \"%s\"\n",title2);
769 fprintf(gnufile,"plot '%s/%s' "
770 "using 1:2 title \"%s\" with lines \n",
771 tmpdir,filename2,title2);
772 fprintf(gnufile,"unset multiplot \n");
773 fclose(gnufile);
774
775 /* Calling gnuplot */
776 sprintf(system_call,"gnuplot -persist %s/%s",tmpdir,gnuname3);
777 if (system(system_call)) {};
778
779 /* Cleaning */
780 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname3);
781 if (remove(tmpfilename)) {};
782 }
783
784 /* Cleaning */
785 sc_basic_initstring(tmpfilename,SC_MAXLEN);
786 sprintf(tmpfilename,"%s/%s",tmpdir,filename1);
787 if (remove(tmpfilename)) {};
788 sprintf(tmpfilename,"%s/%s",tmpdir,filename2);
789 if (remove(tmpfilename)) {};
790 if (rmdir(tmpdir)) {};
791
792 return err_code;
793}
794
795
796/*****************************************************************************
797 * *
798 * Routine to overplot two spectra and calculate residual *
799 * *
800 ****************************************************************************/
801
802void sc_setplottags_overplot_spec(cpl_parameterlist *plottags,
803 const char *x_column,
804 const char *y_column1,
805 const char *y_column2,
806 const char *specname1,
807 const char *specname2,
808 const char *title,
809 const char *x_label,
810 const char *y_label,
811 const cpl_parameterlist *parlist)
812{
843 cpl_parameter *p;
844 const cpl_parameter *cp;
845 char terminal[SC_MAXLEN], basedir[SC_MAXLEN];
846 char outdir[SC_MAXLEN], outname[SC_MAXLEN];
847
848 p = cpl_parameter_new_value("xcol",CPL_TYPE_STRING,"","",x_column);
849 cpl_parameterlist_append(plottags, p);
850 p = cpl_parameter_new_value("ycol1",CPL_TYPE_STRING,"","",y_column1);
851 cpl_parameterlist_append(plottags, p);
852 p = cpl_parameter_new_value("ycol2",CPL_TYPE_STRING,"","",y_column2);
853 cpl_parameterlist_append(plottags, p);
854
855 p = cpl_parameter_new_value("specname1",CPL_TYPE_STRING,"","",specname1);
856 cpl_parameterlist_append(plottags, p);
857 p = cpl_parameter_new_value("specname2",CPL_TYPE_STRING,"","",specname2);
858 cpl_parameterlist_append(plottags, p);
859 p = cpl_parameter_new_value("title",CPL_TYPE_STRING,"","",title);
860 cpl_parameterlist_append(plottags, p);
861 p = cpl_parameter_new_value("xlabel",CPL_TYPE_STRING,"","",x_label);
862 cpl_parameterlist_append(plottags, p);
863 p = cpl_parameter_new_value("ylabel",CPL_TYPE_STRING,"","",y_label);
864 cpl_parameterlist_append(plottags, p);
865
866 cp=cpl_parameterlist_find_const(parlist,"plot_type");
867 sprintf(terminal,"%s",cpl_parameter_get_string(cp));
868 p = cpl_parameter_new_value("terminal",CPL_TYPE_STRING,"","",terminal);
869 cpl_parameterlist_append(plottags, p);
870
871 cp=cpl_parameterlist_find_const(parlist, "inst_dir");
872 sprintf(basedir,"%s",cpl_parameter_get_string(cp));
873 p = cpl_parameter_new_value("inst_dir",CPL_TYPE_STRING,"","",basedir);
874 cpl_parameterlist_append(plottags, p);
875
876 cp=cpl_parameterlist_find_const(parlist, "output_dir");
877 sprintf(outdir,"%s",cpl_parameter_get_string(cp));
878 p = cpl_parameter_new_value("output_dir",CPL_TYPE_STRING,"","",outdir);
879 cpl_parameterlist_append(plottags, p);
880
881 cp=cpl_parameterlist_find_const(parlist, "output_name");
882 sprintf(outname,"%s",cpl_parameter_get_string(cp));
883 p = cpl_parameter_new_value("output_name",CPL_TYPE_STRING,"","",outname);
884 cpl_parameterlist_append(plottags, p);
885
886}
887
888
889
890/****************************************************************************/
891/****************************************************************************/
892
893cpl_error_code sc_overplot_spec(const cpl_table *spec,
894 cpl_parameterlist *plottags)
895{
918 cpl_error_code err_code=CPL_ERROR_NONE;
919
920 FILE *specfile; /* output ASCII spec file ptr */
921 FILE *gnufile; /* gnuplot driverfile */
922 char filename1[SC_MAXLEN]="sci_spec.dat"; /* input spec 1 */
923 char filename2[SC_MAXLEN]="fit_spec.dat"; /* input spec 2 */
924 char filename3[SC_MAXLEN]="resi_spec.dat"; /* residual spec */
925 char gnuname1[SC_MAXLEN]="overplot_gnufile_wxt.gnu";
926 char gnuname2[SC_MAXLEN]="overplot_gnufile_ps.gnu";
927 char gnuname3[SC_MAXLEN]="overplot_gnufile_x11.gnu";
928 char ps_filename[SC_MAXLEN]; /* Name of postscript file */
929 char tmpdir[SC_MAXLEN];
930 char tmpfilename[SC_MAXLEN];
931 char system_call[SC_MAXLEN]; /* string for system call */
932 char plot_type[SC_MAXLEN]; /* plot type selection (plot_creation) */
933 int dir_exist_flag=0; /* Checking for existence of tmp dir */
934
935 /* Plot limits */
936 double plot_xmin=0., plot_xmax=0., plot_ymin=0., plot_ymax=0., tmp1=0.,
937 tmp2=0., dy=0.;
938 char x_label[SC_MAXLEN],y_label[SC_MAXLEN]; /* labels */
939 char title[SC_MAXLEN]; /* title */
940 char x_col[SC_MAXLEN], y_col1[SC_MAXLEN], y_col2[SC_MAXLEN];
941 char specname1[SC_MAXLEN], specname2[SC_MAXLEN]; /*name of spectra */
942
943 int len1=0; /* table length */
944 int run=0; /* runnning variable */
945 double y_value=0, /* y-value of plot */
946 ymin=0; /* minimum residual */
947
948 /* directory + filename for output ps file */
949 cpl_parameter *basedirpar, *outdirpar, *filenamepar, *par;
950
951/*--------------------------------------------------------------------------*/
952/* ------------------------------ INITIALISING -----------------------------*/
953/*--------------------------------------------------------------------------*/
954
955 /* Reading plot tags */
956 par=cpl_parameterlist_find(plottags,"xcol");
957 sprintf(x_col,"%s",cpl_parameter_get_string(par));
958 par=cpl_parameterlist_find(plottags,"ycol1");
959 sprintf(y_col1,"%s",cpl_parameter_get_string(par));
960 par=cpl_parameterlist_find(plottags,"ycol2");
961 sprintf(y_col2,"%s",cpl_parameter_get_string(par));
962
963 par=cpl_parameterlist_find(plottags,"specname1");
964 sprintf(specname1,"%s",cpl_parameter_get_string(par));
965 par=cpl_parameterlist_find(plottags,"specname2");
966 sprintf(specname2,"%s",cpl_parameter_get_string(par));
967 par=cpl_parameterlist_find(plottags,"title");
968 sprintf(title,"%s",cpl_parameter_get_string(par));
969 par=cpl_parameterlist_find(plottags,"xlabel");
970 sprintf(x_label,"%s",cpl_parameter_get_string(par));
971 par=cpl_parameterlist_find(plottags,"ylabel");
972 sprintf(y_label,"%s",cpl_parameter_get_string(par));
973
974 par=cpl_parameterlist_find(plottags,"terminal");
975 sprintf(plot_type,"%s",cpl_parameter_get_string(par));
976 basedirpar = cpl_parameterlist_find(plottags, "inst_dir");
977 outdirpar = cpl_parameterlist_find(plottags, "output_dir");
978 filenamepar = cpl_parameterlist_find(plottags, "output_name");
979
980 /* CHECKING INPUT ------------------------------------------------------*/
981 /* Checking table (=input spectra) properties */
982 len1=cpl_table_get_nrow(spec); /* length of spectrum */
983
984 /* Checking / Creating tmp-directory */
985 sprintf(tmpdir,"__tmpDIRtmp__");
986 dir_exist_flag=access(tmpdir,EXIST);
987 if (dir_exist_flag == 0){
988 cpl_msg_warning(cpl_func,"Directory %s already exists!",tmpdir);
989 }
990 else
991 {
992 if (mkdir(tmpdir,0777)) {};
993 }
994
995 /* writing .dat files containing spectrum information */
996 /* Science spectrum */
997 sc_basic_initstring(tmpfilename,SC_MAXLEN);
998 sprintf(tmpfilename,"%s/%s",tmpdir,filename1);
999 specfile = fopen(tmpfilename,"w");
1000 for (run=0;run<len1;run++)
1001 {
1002 fprintf(specfile,"%5.6g\t%5.6g\n",
1003 cpl_table_get_double(spec,x_col,run,NULL),
1004 cpl_table_get_double(spec,y_col1,run,NULL));
1005 }
1006 fclose(specfile);
1007
1008 /* Fit spectrum */
1009 sc_basic_initstring(tmpfilename,SC_MAXLEN);
1010 sprintf(tmpfilename,"%s/%s",tmpdir,filename2);
1011 specfile = fopen(tmpfilename,"w");
1012 for (run=0;run<len1;run++)
1013 {
1014 fprintf(specfile,"%5.6g\t%5.6g\n",
1015 cpl_table_get_double(spec,x_col,run,NULL),
1016 cpl_table_get_double(spec,y_col2,run,NULL));
1017 }
1018 fclose(specfile);
1019
1020 /* Residual spectrum: Sci - Fit */
1021 sc_basic_initstring(tmpfilename,SC_MAXLEN);
1022 sprintf(tmpfilename,"%s/%s",tmpdir,filename3);
1023 specfile = fopen(tmpfilename,"w");
1024 for (run=0;run<len1;run++)
1025 {
1026 /* Consider pixels with non-zero weight only */
1027 if (cpl_table_get_double(spec,"weight",run,NULL) > 0) {
1028 y_value=cpl_table_get_double(spec,y_col1,run,NULL)-
1029 cpl_table_get_double(spec,y_col2,run,NULL);
1030 } else {
1031 y_value=0;
1032 }
1033 fprintf(specfile,"%5.6g\t%5.6g\n",
1034 cpl_table_get_double(spec,x_col,run,NULL),y_value);
1035 /* Get minimum for plot limits */
1036 if (y_value < ymin) {
1037 ymin = y_value;
1038 }
1039 }
1040 fclose(specfile);
1041
1042 /* Setting plot limits */
1043 plot_xmin=cpl_table_get_double(spec,x_col,0,NULL);
1044 plot_xmax=cpl_table_get_double(spec,x_col,len1-1,NULL);
1045 tmp1=cpl_table_get_column_min(spec,y_col1);
1046 tmp2=cpl_table_get_column_min(spec,y_col2);
1047 if (tmp1 <= tmp2)
1048 {
1049 plot_ymin = tmp1;
1050 }
1051 else
1052 {
1053 plot_ymin = tmp2;
1054 }
1055 if (ymin < plot_ymin) {
1056 plot_ymin = ymin;
1057 }
1058 tmp1=cpl_table_get_column_max(spec,y_col1);
1059 tmp2=cpl_table_get_column_max(spec,y_col2);
1060 if (tmp1 >= tmp2)
1061 {
1062 plot_ymax = tmp1;
1063 }
1064 else
1065 {
1066 plot_ymax = tmp2;
1067 }
1068 /* Adding + 5% for better looking plot */
1069 dy = plot_ymax - plot_ymin;
1070 plot_ymin = plot_ymin - 0.05 *dy;
1071 plot_ymax = plot_ymax + 0.05 *dy;
1072
1073
1074/****************************************************************************/
1075
1076 /* Creating wxt terminal gnuplot file */
1077 if ( (strcmp(plot_type,"W") == 0) || (strcmp(plot_type,"WX") == 0) ||
1078 (strcmp(plot_type,"XW") == 0) )
1079 {
1080 sc_basic_initstring(tmpfilename,SC_MAXLEN);
1081 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname1);
1082 gnufile = fopen(tmpfilename,"w");
1083
1084 fprintf(gnufile,"set term wxt\n");
1085 fprintf(gnufile,"set termoption enhanced \n");
1086 fprintf(gnufile,"# Plotting\n");
1087 fprintf(gnufile,"set key at screen 0.76, 0.55 autotitle column box "
1088 "samplen 1 left\n");
1089 fprintf(gnufile,"set tmargin 0\n");
1090 fprintf(gnufile,"set bmargin 5\n");
1091 fprintf(gnufile,"set lmargin 12\n");
1092 fprintf(gnufile,"set rmargin 14\n");
1093 fprintf(gnufile,"set xrange [ %g : %g ]\n",plot_xmin,plot_xmax);
1094 fprintf(gnufile,"set yrange [ %g : %g ]\n",plot_ymin,plot_ymax);
1095 fprintf(gnufile,"unset title\n");
1096 fprintf(gnufile,"set termoption font \"Times,7\"\n");
1097 fprintf(gnufile,"set multiplot layout 2,1 title \"%s\" \n",title);
1098 fprintf(gnufile,"set xlabel \"%s\"\n",x_label);
1099 fprintf(gnufile,"set ylabel \"%s\" offset 1,0\n",y_label);
1100 fprintf(gnufile,"set style data boxes\n");
1101 fprintf(gnufile,"plot '%s/%s' using 1:2 title \"%s\" "
1102 "with lines lt -1, '%s/%s' "
1103 "using 1:2 title \"%s\" with lines lt 8\n",
1104 tmpdir,filename1,specname1,tmpdir,filename2,specname2);
1105 fprintf(gnufile,"set key at screen 0.73, 0.1 autotitle column box "
1106 "samplen 1 left\n");
1107 fprintf(gnufile,"set ylabel \"Residual (%s-%s)\" offset 1,0\n",
1108 specname1,specname2);
1109 fprintf(gnufile,"set y2tics border out scale 1,0.5 nomirror norotate"
1110 " offset character 0, 0, 0 autofreq \n");
1111 fprintf(gnufile,"set ytics nomirror\n");
1112 fprintf(gnufile,"set y2tics nomirror textcolor lt 2\n");
1113 fprintf(gnufile,"set ylabel \"Residual (input-best-fit sky)\" offset"
1114 " 1,0\n");
1115 fprintf(gnufile,"set y2label \"Residual (input-best-fit sky)\" "
1116 "textcolor lt 2\n");
1117
1118 fprintf(gnufile,"set xlabel \"%s\"\n",x_label);
1119 fprintf(gnufile,"plot '__tmpDIRtmp__/resi_spec.dat' using 1:2 title"
1120 " \"original scaling\" with lines lw 2 lt -1 "
1121 "axes x1y1,'__tmpDIRtmp__/resi_spec.dat' using 1:2 title "
1122 "\"optimal scaling\" with lines lt 2 axes x2y2\n");
1123/* fprintf(gnufile,"plot '%s/%s' using 1:2 "
1124 "with lines lt -1\n",tmpdir,filename3);*/
1125 fprintf(gnufile,"set xrange [ %g : %g ]\n",plot_xmin,plot_xmax);
1126 fprintf(gnufile,"set yrange [ %g : %g ]\n",plot_ymin,plot_ymax);
1127 fprintf(gnufile,"set autoscale y2\n");
1128 fprintf(gnufile,"set title \n");
1129 fprintf(gnufile,"unset multiplot \n");
1130 fclose(gnufile);
1131
1132 /* Calling gnuplot */
1133 sprintf(system_call,"gnuplot -persist %s/%s",tmpdir,gnuname1);
1134 if (system(system_call)) {};
1135
1136 /* Cleaning */
1137 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname1);
1138 if (remove(tmpfilename)) {};
1139 }
1140
1141 /* Creating postscript terminal gnuplot driver file */
1142
1143 if ( strncmp(cpl_parameter_get_string(outdirpar), "/", 1) == 0 )
1144 {
1145 sprintf(ps_filename,"%s/%s_overplot.ps",
1146 cpl_parameter_get_string(outdirpar),
1147 cpl_parameter_get_string(filenamepar));
1148 }
1149 else
1150 {
1151 sprintf(ps_filename,"%s/%s/%s_overplot.ps",
1152 cpl_parameter_get_string(basedirpar),
1153 cpl_parameter_get_string(outdirpar),
1154 cpl_parameter_get_string(filenamepar));
1155 }
1156
1157 sc_basic_initstring(tmpfilename,SC_MAXLEN);
1158 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname2);
1159 gnufile = fopen(tmpfilename,"w");
1160
1161 fprintf(gnufile,"set term postscript enhanced color\n");
1162 fprintf(gnufile,"set output \"%s\"\n",ps_filename);
1163 fprintf(gnufile,"# Plotting\n");
1164 fprintf(gnufile,"set key at screen 0.70, 0.55 autotitle column box "
1165 "samplen 1 left\n");
1166 fprintf(gnufile,"set tmargin 0\n");
1167 fprintf(gnufile,"set bmargin 5\n");
1168 fprintf(gnufile,"set lmargin 12\n");
1169 fprintf(gnufile,"set rmargin 14\n");
1170 fprintf(gnufile,"set xrange [ %g : %g ]\n",plot_xmin,plot_xmax);
1171 fprintf(gnufile,"set yrange [ %g : %g ]\n",plot_ymin,plot_ymax);
1172 fprintf(gnufile,"unset title\n");
1173 fprintf(gnufile,"set termoption font \"Times,12\"\n");
1174 fprintf(gnufile,"set multiplot layout 2,1 title \"%s\" \n",title);
1175 fprintf(gnufile,"set xlabel \"%s\"\n",x_label);
1176 fprintf(gnufile,"set ylabel \"%s\" offset 1,0\n",y_label);
1177 fprintf(gnufile,"set style data boxes\n");
1178 fprintf(gnufile,"plot '%s/%s' using 1:2 title \"%s\" "
1179 "with lines lt -1, '%s/%s' "
1180 "using 1:2 title \"%s\" with lines lt 8\n",
1181 tmpdir,filename1,specname1,tmpdir,filename2,specname2);
1182
1183 fprintf(gnufile,"set key at screen 0.66, 0.07 autotitle column box "
1184 "samplen 1 left\n");
1185 fprintf(gnufile,"set ylabel \"Residual (%s-%s)\" offset 1,0\n",
1186 specname1,specname2);
1187 fprintf(gnufile,"set y2tics border out scale 1,0.5 nomirror norotate"
1188 " offset character 0, 0, 0 autofreq \n");
1189 fprintf(gnufile,"set style line 1 lt 2 lc rgb \"red\" lw 3\n");
1190 fprintf(gnufile,"set style line 2 lt 1 lc rgb \"green\" lw 1\n");
1191 fprintf(gnufile,"set ytics nomirror\n");
1192 fprintf(gnufile,"set y2tics nomirror textcolor lt 2\n");
1193 fprintf(gnufile,"set ylabel \"Residual (input-best-fit sky)\" offset"
1194 " 1,0\n");
1195 fprintf(gnufile,"set y2label \"Residual (input-best-fit sky)\" "
1196 "textcolor lt 2\n");
1197
1198 fprintf(gnufile,"set xlabel \"%s\"\n",x_label);
1199 fprintf(gnufile,"plot '__tmpDIRtmp__/resi_spec.dat' using 1:2 title"
1200 " \"original scaling\" with lines lw 2 lt -1 "
1201 "axes x1y1,'__tmpDIRtmp__/resi_spec.dat' using 1:2 title "
1202 "\"optimal scaling\" with lines ls 2 axes x2y2\n");
1203/* fprintf(gnufile,"plot '%s/%s' using 1:2 "
1204 "with lines lt -1\n",tmpdir,filename3);*/
1205 fprintf(gnufile,"set xrange [ %g : %g ]\n",plot_xmin,plot_xmax);
1206 fprintf(gnufile,"set yrange [ %g : %g ]\n",plot_ymin,plot_ymax);
1207 fprintf(gnufile,"set autoscale y2\n");
1208 fprintf(gnufile,"set title \n");
1209 fprintf(gnufile,"unset multiplot \n");
1210 fclose(gnufile);
1211/*
1212 fprintf(gnufile,"# Plotting\n");
1213 fprintf(gnufile,"set key at screen 0.77, 0.55 autotitle column box "
1214 "samplen 1 left\n");
1215 fprintf(gnufile,"set tmargin 0\n");
1216 fprintf(gnufile,"set bmargin 5\n");
1217 fprintf(gnufile,"set lmargin 12\n");
1218 fprintf(gnufile,"set rmargin 3\n");
1219 fprintf(gnufile,"set xrange [ %g : %g ]\n",plot_xmin,plot_xmax);
1220 fprintf(gnufile,"set yrange [ %g : %g ]\n",plot_ymin,plot_ymax);
1221 fprintf(gnufile,"unset title\n");
1222 fprintf(gnufile,"set termoption font \"Times,12\"\n");
1223 fprintf(gnufile,"set multiplot layout 2,1 title \"%s\" \n",title);
1224 fprintf(gnufile,"set xlabel \"%s\"\n",x_label);
1225 fprintf(gnufile,"set ylabel \"%s\" offset 1,0\n",y_label);
1226 fprintf(gnufile,"set style data boxes\n");
1227 fprintf(gnufile,"plot '%s/%s' using 1:2 title \"%s\" "
1228 "with lines lt -1, '%s/%s' "
1229 "using 1:2 title \"%s\" with lines lt 8\n",
1230 tmpdir,filename1,specname1,tmpdir,filename2,specname2);
1231 fprintf(gnufile,"set nokey\n");
1232 fprintf(gnufile,"set ylabel \"Residual (%s-%s)\" offset 1,0\n",
1233 specname1,specname2);
1234 fprintf(gnufile,"set xlabel \"%s\"\n",x_label);
1235 fprintf(gnufile,"plot '%s/%s' using 1:2 "
1236 "with lines lt -1\n",tmpdir,filename3);
1237 fprintf(gnufile,"set xrange [ %g : %g ]\n",plot_xmin,plot_xmax);
1238 fprintf(gnufile,"set yrange [ %g : %g ]\n",plot_ymin,plot_ymax);
1239 fprintf(gnufile,"set title \n");
1240 fprintf(gnufile,"unset multiplot \n");
1241 fclose(gnufile);*/
1242
1243 /* Calling gnuplot */
1244 sprintf(system_call,"gnuplot -persist %s/%s",tmpdir,gnuname2);
1245 if (system(system_call)) {};
1246
1247 /* Cleaning */
1248 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname2);
1249 if (remove(tmpfilename)) {};
1250
1251 /* Creating x11 terminal gnuplot file */
1252 if ( (strcmp(plot_type,"X") == 0) || (strcmp(plot_type,"WX") == 0) ||
1253 (strcmp(plot_type,"XW") == 0) )
1254 {
1255 sc_basic_initstring(tmpfilename,SC_MAXLEN);
1256 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname3);
1257 gnufile = fopen(tmpfilename,"w");
1258
1259 fprintf(gnufile,"set term x11\n");
1260 fprintf(gnufile,"set termoption enhanced \n");
1261 fprintf(gnufile,"# Plotting\n");
1262 fprintf(gnufile,"set key at screen 0.72, 0.55 autotitle column box "
1263 "samplen 1 left\n");
1264 fprintf(gnufile,"set tmargin 0\n");
1265 fprintf(gnufile,"set bmargin 5\n");
1266 fprintf(gnufile,"set lmargin 12\n");
1267 fprintf(gnufile,"set rmargin 14\n");
1268 fprintf(gnufile,"set xrange [ %g : %g ]\n",plot_xmin,plot_xmax);
1269 fprintf(gnufile,"set yrange [ %g : %g ]\n",plot_ymin,plot_ymax);
1270 fprintf(gnufile,"unset title\n");
1271 fprintf(gnufile,"set termoption font \"Times,15\"\n");
1272 fprintf(gnufile,"set multiplot layout 2,1 title \"%s\" \n",title);
1273 fprintf(gnufile,"set xlabel \"%s\"\n",x_label);
1274 fprintf(gnufile,"set ylabel \"%s\" offset 1,0\n",y_label);
1275 fprintf(gnufile,"set style data boxes\n");
1276 fprintf(gnufile,"plot '%s/%s' using 1:2 title \"%s\" "
1277 "with lines lt -1, '%s/%s' "
1278 "using 1:2 title \"%s\" with lines lt 8\n",
1279 tmpdir,filename1,specname1,tmpdir,filename2,specname2);
1280 fprintf(gnufile,"set key at screen 0.68, 0.1 autotitle column box "
1281 "samplen 1 left\n");
1282 fprintf(gnufile,"set ylabel \"Residual (%s-%s)\" offset 1,0\n",
1283 specname1,specname2);
1284 fprintf(gnufile,"set y2tics border out scale 1,0.5 nomirror norotate"
1285 " offset character 0, 0, 0 autofreq \n");
1286 fprintf(gnufile,"set ytics nomirror\n");
1287 fprintf(gnufile,"set y2tics nomirror textcolor lt 3\n");
1288 fprintf(gnufile,"set ylabel \"Residual (input-best-fit sky)\" offset"
1289 " 1,0\n");
1290 fprintf(gnufile,"set y2label \"Residual (input-best-fit sky)\" "
1291 "textcolor lt 3\n");
1292
1293 fprintf(gnufile,"set xlabel \"%s\"\n",x_label);
1294 fprintf(gnufile,"plot '__tmpDIRtmp__/resi_spec.dat' using 1:2 title"
1295 " \"original scaling\" with lines lw 2 lt -1 "
1296 "axes x1y1,'__tmpDIRtmp__/resi_spec.dat' using 1:2 title "
1297 "\"optimal scaling\" with lines lt 3 axes x2y2\n");
1298/* fprintf(gnufile,"plot '%s/%s' using 1:2 "
1299 "with lines lt -1\n",tmpdir,filename3);*/
1300 fprintf(gnufile,"set xrange [ %g : %g ]\n",plot_xmin,plot_xmax);
1301 fprintf(gnufile,"set yrange [ %g : %g ]\n",plot_ymin,plot_ymax);
1302 fprintf(gnufile,"set autoscale y2\n");
1303 fprintf(gnufile,"set title \n");
1304 fprintf(gnufile,"unset multiplot \n");
1305 fclose(gnufile);
1306// fprintf(gnufile,"set termoption enhanced \n");
1307// fprintf(gnufile,"# Plotting\n");
1308// fprintf(gnufile,"set key at screen 0.77, 0.55 autotitle column box "
1309// "samplen 1 left\n");
1310// fprintf(gnufile,"set tmargin 0\n");
1311// fprintf(gnufile,"set bmargin 5\n");
1312// fprintf(gnufile,"set lmargin 12\n");
1313// fprintf(gnufile,"set rmargin 3\n");
1314// fprintf(gnufile,"set xrange [ %g : %g ]\n",plot_xmin,plot_xmax);
1315// fprintf(gnufile,"set yrange [ %g : %g ]\n",plot_ymin,plot_ymax);
1316// fprintf(gnufile,"unset title\n");
1317// fprintf(gnufile,"set termoption font \"Times,7\"\n");
1318// fprintf(gnufile,"set multiplot layout 2,1 title \"%s\" \n",title);
1319// fprintf(gnufile,"set xlabel \"%s\"\n",x_label);
1320// fprintf(gnufile,"set ylabel \"%s\" offset 1,0\n",y_label);
1321// fprintf(gnufile,"set style data boxes\n");
1322// fprintf(gnufile,"plot '%s/%s' using 1:2 title \"%s\" "
1323// "with lines lt -1, '%s/%s' "
1324// "using 1:2 title \"%s\" with lines lt 8\n",
1325// tmpdir,filename1,specname1,tmpdir,filename2,specname2);
1326// fprintf(gnufile,"set nokey\n");
1327// fprintf(gnufile,"set ylabel \"Residual (%s-%s)\" offset 1,0\n",
1328// specname1,specname2);
1329// fprintf(gnufile,"set xlabel \"%s\"\n",x_label);
1330// fprintf(gnufile,"plot '%s/%s' using 1:2 "
1331// "with lines lt -1\n",tmpdir,filename3);
1332// fprintf(gnufile,"set xrange [ %g : %g ]\n",plot_xmin,plot_xmax);
1333// fprintf(gnufile,"set yrange [ %g : %g ]\n",plot_ymin,plot_ymax);
1334// fprintf(gnufile,"set title \n");
1335// fprintf(gnufile,"unset multiplot \n");
1336// fclose(gnufile);
1337
1338 /* Calling gnuplot */
1339 sprintf(system_call,"gnuplot -persist %s/%s",tmpdir,gnuname3);
1340 if (system(system_call)) {};
1341
1342 /* Cleaning */
1343 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname3);
1344 if (remove(tmpfilename)) {};
1345 }
1346
1347 /* Cleaning */
1348 sc_basic_initstring(tmpfilename,SC_MAXLEN);
1349 sprintf(tmpfilename,"%s/%s",tmpdir,filename1);
1350 if (remove(tmpfilename)) {};
1351 sprintf(tmpfilename,"%s/%s",tmpdir,filename2);
1352 if (remove(tmpfilename)) {};
1353 sprintf(tmpfilename,"%s/%s",tmpdir,filename3);
1354 if (remove(tmpfilename)) {};
1355 if (rmdir(tmpdir)) {};
1356
1357 return err_code;
1358}
1359
1360/*****************************************************************************
1361 * *
1362 * Routine to plot a histogram *
1363 * *
1364 ****************************************************************************/
1365
1366void sc_setplottags_hist(cpl_parameterlist *plottags, const char *title,
1367 const char *x_label, const char *y_label,
1368 const cpl_parameterlist *parlist)
1369{
1393 cpl_parameter *p;
1394 const cpl_parameter *cp;
1395 char terminal[SC_MAXLEN], basedir[SC_MAXLEN];
1396 char outdir[SC_MAXLEN], outname[SC_MAXLEN];
1397
1398 p = cpl_parameter_new_value("plot_title",CPL_TYPE_STRING,"","",title);
1399 cpl_parameterlist_append(plottags, p);
1400 p = cpl_parameter_new_value("xlabel",CPL_TYPE_STRING,"","",x_label);
1401 cpl_parameterlist_append(plottags, p);
1402 p = cpl_parameter_new_value("ylabel",CPL_TYPE_STRING,"","",y_label);
1403 cpl_parameterlist_append(plottags, p);
1404
1405 cp = cpl_parameterlist_find_const(parlist,"plot_type");
1406 sprintf(terminal,"%s",cpl_parameter_get_string(cp));
1407 p = cpl_parameter_new_value("terminal",CPL_TYPE_STRING,"","",terminal);
1408 cpl_parameterlist_append(plottags, p);
1409
1410 cp = cpl_parameterlist_find_const(parlist, "inst_dir");
1411 sprintf(basedir,"%s",cpl_parameter_get_string(cp));
1412 p = cpl_parameter_new_value("inst_dir",CPL_TYPE_STRING,"","",basedir);
1413 cpl_parameterlist_append(plottags, p);
1414
1415 cp = cpl_parameterlist_find_const(parlist, "output_dir");
1416 sprintf(outdir,"%s",cpl_parameter_get_string(cp));
1417 p = cpl_parameter_new_value("output_dir",CPL_TYPE_STRING,"","",outdir);
1418 cpl_parameterlist_append(plottags, p);
1419
1420 cp = cpl_parameterlist_find_const(parlist, "output_name");
1421 sprintf(outname,"%s",cpl_parameter_get_string(cp));
1422 p = cpl_parameter_new_value("output_name",CPL_TYPE_STRING,"","",outname);
1423 cpl_parameterlist_append(plottags, p);
1424
1425}
1426
1427/****************************************************************************/
1428/****************************************************************************/
1429
1430cpl_error_code sc_plot_hist(const cpl_table *histdat,
1431 cpl_parameterlist *plottags)
1432{
1449 //cpl_errorstate err_state;
1450 cpl_error_code err_code=CPL_ERROR_NONE;
1451
1452 FILE *specfile; /* output ASCII spec file ptr */
1453 FILE *gnufile; /* gnuplot driverfile */
1454 char filename1[SC_MAXLEN]="hist_data.dat"; /* data for histogram */
1455 char gnuname1[SC_MAXLEN]="hist_plot_wxt.gnu";
1456 char gnuname2[SC_MAXLEN]="hist_plot_ps.gnu";
1457 char gnuname3[SC_MAXLEN]="hist_plot_x11.gnu";
1458 char ps_filename[SC_MAXLEN]; /* Name of postscript file */
1459 char tmpdir[SC_MAXLEN];
1460 char tmpfilename[SC_MAXLEN];
1461 char system_call[SC_MAXLEN]; /* string for system call */
1462 char plot_type[SC_MAXLEN]; /* plot type selection (plot_creation) */
1463
1464 int dir_exist_flag=0; /* Checking for existence of tmp dir */
1465 //int dummy=0; /* dummy return value for system calls */
1466 int run=0; /* runnning variable */
1467
1468 double binlims=0.;
1469 int cts=0;
1470
1471 /* Plot tags */
1472 char plot_title[SC_MAXLEN];
1473 char x_colname[SC_MAXLEN], y_colname[SC_MAXLEN];
1474 char err_msg[SC_MAXLEN]; /* error message to be returned */
1475
1476 int len1=0, ncol1=0; /* Dimension of cpl-table 'histdat' */
1477
1478 /* directory + filename for output ps file */
1479 cpl_parameter *basedirpar, *outdirpar, *filenamepar, *par;
1480
1481 cpl_array *column_names1; /* Col. names of input tables */
1482
1483
1484/*--------------------------------------------------------------------------*/
1485/* ------------------------------ INITIALISING -----------------------------*/
1486/*--------------------------------------------------------------------------*/
1487 /* Reading plot tags */
1488 par=cpl_parameterlist_find(plottags,"plot_title");
1489 sprintf(plot_title,"%s",cpl_parameter_get_string(par));
1490 par=cpl_parameterlist_find(plottags,"xlabel");
1491 sprintf(x_colname,"%s",cpl_parameter_get_string(par));
1492 par=cpl_parameterlist_find(plottags,"ylabel");
1493 sprintf(y_colname,"%s",cpl_parameter_get_string(par));
1494 par=cpl_parameterlist_find(plottags,"terminal");
1495 sprintf(plot_type,"%s",cpl_parameter_get_string(par));
1496
1497 /* checking error state */
1498 //err_state = cpl_errorstate_get();
1499
1500 /* CHECKING INPUT ------------------------------------------------------*/
1501 /* Checking table (=input data) properties */
1502 len1=cpl_table_get_nrow(histdat); /* # of bins */
1503 ncol1=cpl_table_get_ncol(histdat); /* # of columns spectrum */
1504 column_names1 = cpl_table_get_column_names(histdat); /* Col names */
1505
1506 /* Checking whether 2 columns for input file exist */
1507 if (ncol1 != 2)
1508 {
1509 sprintf(err_msg,"Number of columns not equal 2 in data file...");
1510 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
1511 "%s", err_msg);
1512 }
1513
1514 /* Checking / Creating tmp-directory */
1515 sprintf(tmpdir,"__tmpDIRtmp__");
1516 dir_exist_flag=access(tmpdir,EXIST);
1517 if (dir_exist_flag == 0){
1518 cpl_msg_warning(cpl_func,"Directory %s already exists!",tmpdir);
1519 }
1520 else
1521 {
1522 if (mkdir(tmpdir,0777)) {};
1523 }
1524
1525 /* writing .dat files containing spectrum information */
1526 /* Science spectrum */
1527 sc_basic_initstring(tmpfilename,SC_MAXLEN);
1528 sprintf(tmpfilename,"%s/%s",tmpdir,filename1);
1529 specfile = fopen(tmpfilename,"w");
1530 for (run=0;run<len1;run++)
1531 {
1532 binlims=cpl_table_get_double(histdat,"bins",run,NULL);
1533 cts=cpl_table_get_int(histdat,"counts",run,NULL);
1534 fprintf(specfile,"%5.3g\t%i\n",binlims,cts);
1535 }
1536 fclose(specfile);
1537
1538 /* Checking plot options */
1539
1540 /* Creating wxt terminal gnuplot driver file */
1541 if ( (strcmp(plot_type,"W") == 0) || (strcmp(plot_type,"WX") == 0) ||
1542 (strcmp(plot_type,"XW") == 0) )
1543 {
1544 sc_basic_initstring(tmpfilename,SC_MAXLEN);
1545 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname1);
1546 gnufile = fopen(tmpfilename,"w");
1547 fprintf(gnufile,"set term wxt\n");
1548 fprintf(gnufile,"set termoption enhanced\n");
1549 fprintf(gnufile,"set title \"%s\"\n",plot_title);
1550 fprintf(gnufile,"set termoption font \"Times,9\"\n");
1551 fprintf(gnufile,"set xlabel '%s'\n",x_colname);
1552 fprintf(gnufile,"set ylabel '%s'\n",y_colname);
1553 fprintf(gnufile,"set boxwidth 0.75 absolute\n");
1554 fprintf(gnufile,"set style fill solid 1.00 border -1\n");
1555 fprintf(gnufile,"set style histogram rowstacked\n");
1556 fprintf(gnufile,"set style data histograms\n");
1557 fprintf(gnufile,"plot '%s/%s' u 1:2 smooth frequency with histeps"
1558 " t \"%s\"\n",tmpdir,filename1,y_colname);
1559 fprintf(gnufile,"unset xlabel\n");
1560 fprintf(gnufile,"unset ylabel\n");
1561 fprintf(gnufile,"\n");
1562 fclose(gnufile);
1563
1564 /* Calling gnuplot */
1565 sprintf(system_call,"gnuplot -persist %s/%s",tmpdir,gnuname1);
1566 if (system(system_call)) {};
1567
1568 /* Cleaning */
1569 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname1);
1570 if (remove(tmpfilename)) {};
1571 }
1572
1573 /* Creating postscript terminal gnuplot driver file */
1574 basedirpar = cpl_parameterlist_find(plottags, "inst_dir");
1575 outdirpar = cpl_parameterlist_find(plottags, "output_dir");
1576 filenamepar = cpl_parameterlist_find(plottags, "output_name");
1577
1578 sprintf(ps_filename,"%s/%s/%s_histogram.ps",
1579 cpl_parameter_get_string(basedirpar),
1580 cpl_parameter_get_string(outdirpar),
1581 cpl_parameter_get_string(filenamepar));
1582
1583 sc_basic_initstring(tmpfilename,SC_MAXLEN);
1584 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname2);
1585 gnufile = fopen(tmpfilename,"w");
1586 fprintf(gnufile,"set term postscript enhanced color\n");
1587 fprintf(gnufile,"set output \"%s\"\n",ps_filename);
1588 fprintf(gnufile,"set title \"%s\"\n",plot_title);
1589 fprintf(gnufile,"set termoption font \"Times,9\"\n");
1590 fprintf(gnufile,"set xlabel '%s'\n",x_colname);
1591 fprintf(gnufile,"set ylabel '%s'\n",y_colname);
1592 fprintf(gnufile,"set boxwidth 0.9 absolute\n");
1593 fprintf(gnufile,"set style fill solid 1.00 border -1\n");
1594 fprintf(gnufile,"set style data histograms\n");
1595 fprintf(gnufile,"plot '%s/%s' u 1:2 smooth frequency with histeps"
1596 " t \"%s\"\n",tmpdir,filename1,y_colname);
1597 fprintf(gnufile,"unset xlabel\n");
1598 fprintf(gnufile,"unset ylabel\n");
1599 fprintf(gnufile,"\n");
1600 fclose(gnufile);
1601
1602 /* Calling gnuplot */
1603 sprintf(system_call,"gnuplot -persist %s/%s",tmpdir,gnuname2);
1604 if (system(system_call)) {};
1605
1606 /* Cleaning */
1607 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname2);
1608 if (remove(tmpfilename)) {};
1609
1610 /* Creating x11 terminal gnuplot driver file */
1611 if ( (strcmp(plot_type,"X") == 0) || (strcmp(plot_type,"WX") == 0) ||
1612 (strcmp(plot_type,"XW") == 0) )
1613 {
1614 sc_basic_initstring(tmpfilename,SC_MAXLEN);
1615 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname3);
1616 gnufile = fopen(tmpfilename,"w");
1617 fprintf(gnufile,"set term x11\n");
1618 fprintf(gnufile,"set termoption enhanced\n");
1619
1620 fprintf(gnufile,"set title \"%s\"\n",plot_title);
1621 fprintf(gnufile,"set termoption font \"Times,9\"\n");
1622 fprintf(gnufile,"set xlabel '%s'\n",x_colname);
1623 fprintf(gnufile,"set ylabel '%s'\n",y_colname);
1624 fprintf(gnufile,"set boxwidth 0.75 absolute\n");
1625 fprintf(gnufile,"set style fill solid 1.00 border -1\n");
1626 fprintf(gnufile,"set style histogram rowstacked\n");
1627 fprintf(gnufile,"set style data histograms\n");
1628 fprintf(gnufile,"plot '%s/%s' u 1:2 smooth frequency with histeps"
1629 " t \"%s\"\n",tmpdir,filename1,y_colname);
1630 fprintf(gnufile,"unset xlabel\n");
1631 fprintf(gnufile,"unset ylabel\n");
1632 fprintf(gnufile,"\n");
1633 fclose(gnufile);
1634
1635 /* Calling gnuplot */
1636 sprintf(system_call,"gnuplot -persist %s/%s",tmpdir,gnuname3);
1637 if (system(system_call)) {};
1638
1639 /* Cleaning */
1640 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname3);
1641 if (remove(tmpfilename)) {};
1642 }
1643
1644 /* Cleaning */
1645 sc_basic_initstring(tmpfilename,SC_MAXLEN);
1646 sprintf(tmpfilename,"%s/%s",tmpdir,filename1);
1647 if (remove(tmpfilename)) {};
1648 if (rmdir(tmpdir)) {};
1649
1650 cpl_array_delete(column_names1);
1651
1652 return err_code;
1653
1654}
1655
1656/*****************************************************************************
1657 * *
1658 * Routine to plot a single spectrum with lines *
1659 * *
1660 ****************************************************************************/
1661
1662
1663
1664void sc_setplottags_single_spec_lines(cpl_parameterlist *plottags,
1665 const char *title,
1666 const char *x_label,
1667 const char *y_label,
1668 const cpl_parameterlist *parlist)
1669{
1692 cpl_parameter *p;
1693 const cpl_parameter *cp;
1694 char terminal[SC_MAXLEN], basedir[SC_MAXLEN];
1695 char outdir[SC_MAXLEN], outname[SC_MAXLEN];
1696
1697 p = cpl_parameter_new_value("plot_title",CPL_TYPE_STRING,"","",title);
1698 cpl_parameterlist_append(plottags, p);
1699 p = cpl_parameter_new_value("xlabel",CPL_TYPE_STRING,"","",x_label);
1700 cpl_parameterlist_append(plottags, p);
1701 p = cpl_parameter_new_value("ylabel",CPL_TYPE_STRING,"","",y_label);
1702 cpl_parameterlist_append(plottags, p);
1703
1704 cp = cpl_parameterlist_find_const(parlist,"plot_type");
1705 sprintf(terminal,"%s",cpl_parameter_get_string(cp));
1706 p = cpl_parameter_new_value("terminal",CPL_TYPE_STRING,"","",terminal);
1707 cpl_parameterlist_append(plottags, p);
1708
1709 cp = cpl_parameterlist_find_const(parlist, "inst_dir");
1710 sprintf(basedir,"%s",cpl_parameter_get_string(cp));
1711 p = cpl_parameter_new_value("inst_dir",CPL_TYPE_STRING,"","",basedir);
1712 cpl_parameterlist_append(plottags, p);
1713
1714 cp = cpl_parameterlist_find_const(parlist, "output_dir");
1715 sprintf(outdir,"%s",cpl_parameter_get_string(cp));
1716 p = cpl_parameter_new_value("output_dir",CPL_TYPE_STRING,"","",outdir);
1717 cpl_parameterlist_append(plottags, p);
1718
1719 cp = cpl_parameterlist_find_const(parlist, "output_name");
1720 sprintf(outname,"%s",cpl_parameter_get_string(cp));
1721 p = cpl_parameter_new_value("output_name",CPL_TYPE_STRING,"","",outname);
1722 cpl_parameterlist_append(plottags, p);
1723
1724}
1725
1726
1727
1728/****************************************************************************/
1729/****************************************************************************/
1730
1731cpl_error_code sc_plot_single_spec_with_lines(const cpl_table *spec,
1732 cpl_parameterlist *plottags)
1733{
1755 FILE *specfile; /* output ASCII spec file ptr */
1756 FILE *gnufile; /* gnuplot driverfile */
1757 char filename[SC_MAXLEN]="sci_single_plot_with_lines_spec.dat";
1758 char gnuname1[SC_MAXLEN]="single_plot_with_lines_gnufile_wxt.gnu";
1759 char gnuname2[SC_MAXLEN]="single_plot_with_lines_gnufile_ps.gnu";
1760 char gnuname3[SC_MAXLEN]="single_plot_with_lines_gnufile_x11.gnu";
1761 char ps_filename[SC_MAXLEN];
1762
1763 char tmpdir[SC_MAXLEN];
1764 char tmpfilename[SC_MAXLEN];
1765 char plot_type[SC_MAXLEN]; /* plot type selection (plot_creation) */
1766
1767 char system_call[SC_MAXLEN]; /* system call */
1768
1769 int len=0; /* table length */
1770 int run=0; /* runnning variable */
1771 int line_run=0; /* running number for lines to plot */
1772 int n_lines=0; /* number of lines in spectrum */
1773 //int ncol=0; /* number of columns in inputspectable */
1774 //int dummy=0; /* dummy return value for system calls */
1775 double lambda=0.; /* wavelength of spectrum */
1776 double line=0.; /* Line */
1777 double y_value=0.; /* y-value of plot (RAD/TRA) */
1778 int dir_exist_flag=0; /* Checking for existence of tmp dir */
1779 /* Plot limits, line marker limits */
1780 double plot_xmin=0., plot_xmax=0.;
1781 double plot_ymin=0., plot_ymax=0.;
1782 double plot_dist_min=0., plot_dist_min_local=0., plot_dist_max=0.;
1783 double plot_label=0.;
1784 double corrfac=0.; /* Correction factor for line labels */
1785
1786 /* plot tags */
1787 char plot_title[SC_MAXLEN];
1788 char x_colname[SC_MAXLEN], y_colname[SC_MAXLEN];
1789
1790 /* directory + filename for output ps file */
1791 cpl_parameter *basedirpar, *outdirpar, *filenamepar, *par;
1792
1793/*--------------------------------------------------------------------------*/
1794/* ------------------------------ INITIALISING -----------------------------*/
1795/*--------------------------------------------------------------------------*/
1796
1797 /* Reading plot tags */
1798 par=cpl_parameterlist_find(plottags,"plot_title");
1799 sprintf(plot_title,"%s",cpl_parameter_get_string(par));
1800 par=cpl_parameterlist_find(plottags,"xlabel");
1801 sprintf(x_colname,"%s",cpl_parameter_get_string(par));
1802 par=cpl_parameterlist_find(plottags,"ylabel");
1803 sprintf(y_colname,"%s",cpl_parameter_get_string(par));
1804 par=cpl_parameterlist_find(plottags,"terminal");
1805 sprintf(plot_type,"%s",cpl_parameter_get_string(par));
1806
1807 /* Checking table (=input spectra) properties */
1808 len=cpl_table_get_nrow(spec); /* length of spectrum */
1809 //ncol=cpl_table_get_ncol(spec); /* # of columns */
1810 plot_xmin=cpl_table_get_double(spec,"lambda",0,NULL);
1811 plot_xmax=cpl_table_get_double(spec,"lambda",len-1,NULL);
1812 corrfac=(plot_xmax-plot_xmin)/100;
1813
1814 plot_ymin=cpl_table_get_column_min(spec,"flux");
1815 plot_ymax=1.5*cpl_table_get_column_max(spec,"flux");
1816 /* Line markers / Line labels */
1817 /* Length of line marker */
1818 plot_dist_max=plot_ymax-(plot_ymax-plot_ymin)/4;
1819 plot_dist_min=(plot_ymax-plot_ymin)/50;
1820 /* Location of labels */
1821 plot_label=plot_dist_max+plot_dist_min;//(plot_ymax-plot_ymin)/10;
1822
1823/* plot_dist_min=1.1*cpl_table_get_column_max(spec,"flux");*/
1824
1825 /* Creating tmp-directory */
1826 sprintf(tmpdir,"__tmpDIRtmp__");
1827 dir_exist_flag=access(tmpdir,EXIST);
1828 if (dir_exist_flag == 0){
1829 cpl_msg_warning(cpl_func,"Directory %s already exists!",tmpdir);
1830 }
1831 else
1832 {
1833 if (mkdir(tmpdir,0777)) {};
1834 }
1835
1836 /* writing .dat file containing spectrum information */
1837 sprintf(tmpfilename,"%s/%s",tmpdir,filename);
1838 specfile = fopen(tmpfilename,"w");
1839 for (run=0;run<len;run++)
1840 {
1841 lambda=cpl_table_get_double(spec,"lambda",run,NULL);
1842 y_value=cpl_table_get_double(spec,"flux",run,NULL);
1843 fprintf(specfile,"%5.6g\t%5.6g\n",lambda,y_value);
1844 }
1845 fclose(specfile);
1846
1847 /* Counting lines */
1848 for(run=0;run<len-1;run++)
1849 {
1850 n_lines=n_lines+cpl_table_get_int(spec,"class",run,NULL);
1851 }
1852 if (n_lines == 0)
1853 {
1854 cpl_msg_warning(cpl_func,"Input spectrum contains no lines "
1855 "to plot!");
1856 }
1857
1858 /* Creating wxt terminal gnuplot driver file */
1859 if ( (strcmp(plot_type,"W") == 0) || (strcmp(plot_type,"WX") == 0) ||
1860 (strcmp(plot_type,"XW") == 0) )
1861 {
1862 sc_basic_initstring(tmpfilename,SC_MAXLEN);
1863 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname1);
1864 gnufile = fopen(tmpfilename,"w");
1865 fprintf(gnufile,"set term wxt\n");
1866 fprintf(gnufile,"set termoption enhanced \n");
1867 fprintf(gnufile,"set nokey\n");
1868 fprintf(gnufile,"set tmargin 2\n");
1869 fprintf(gnufile,"set bmargin 5\n");
1870 fprintf(gnufile,"set lmargin 13\n");
1871 fprintf(gnufile,"set rmargin 3\n");
1872 fprintf(gnufile,"set xrange [ %g : %g ]\n",plot_xmin,plot_xmax);
1873 fprintf(gnufile,"set yrange [ %g : %g ]\n",plot_ymin,plot_ymax);
1874 fprintf(gnufile,"set title \"%s\"\n",plot_title);
1875 fprintf(gnufile,"set xlabel '%s'\n",x_colname);
1876 fprintf(gnufile,"set ylabel '%s'\n",y_colname);
1877 fprintf(gnufile,"set parametric\n");
1878/* fprintf(gnufile,"set trange [ %g : %g ]\n",plot_dist_min,
1879 plot_dist_max);*/
1880 for(run=0;run<len-1;run++)
1881 {
1882 if (cpl_table_get_int(spec,"class",run,NULL) == 2)
1883 {
1884 plot_dist_min_local=cpl_table_get_double(spec,"flux",run,NULL)
1885 +plot_dist_min;
1886 line=cpl_table_get_double(spec,"lambda",run,NULL);
1887 fprintf(gnufile,"line%i=%g\n",line_run,line);
1888 fprintf(gnufile,"set arrow from %g,%g to %g,%g nohead "
1889 "lw 0.5 lc -1\n",line,plot_dist_min_local,
1890 line,plot_dist_max);
1891 fprintf(gnufile,"line%i=%g\n",line_run,line+corrfac);
1892 fprintf(gnufile,"set label \"{/=4 %2.5g}\" at "
1893 "line%i,%g rotate\n",line,line_run,plot_label);
1894 line_run++;
1895 }
1896 if (cpl_table_get_int(spec,"class",run,NULL) == 3)
1897 {
1898 plot_dist_min_local=cpl_table_get_double(spec,"flux",run,NULL)
1899 +plot_dist_min;
1900 line=cpl_table_get_double(spec,"lambda",run,NULL);
1901 fprintf(gnufile,"line%i=%g\n",line_run,line);
1902 fprintf(gnufile,"set arrow from %g,%g to %g,%g nohead lw 1 "
1903 "lc 3\n",line,plot_dist_min_local,
1904 line,plot_dist_max);
1905 fprintf(gnufile,"line%i=%g\n",line_run,line+corrfac);
1906 fprintf(gnufile,"set label \"{/=4 %2.5g}\" at "
1907 "line%i,%g rotate\n",line,line_run,plot_label);
1908 line_run++;
1909 }
1910
1911 }
1912 fprintf(gnufile,"plot '%s/%s' using 1:2 with lines\n",
1913 tmpdir,filename);
1914
1915 fprintf(gnufile,"\n");
1916 fclose(gnufile);
1917
1918 /* Calling gnuplot */
1919 sprintf(system_call,"gnuplot -persist %s/%s",tmpdir,gnuname1);
1920 if (system(system_call)) {};
1921
1922 /* Cleaning */
1923 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname1);
1924 if (remove(tmpfilename)) {};
1925 }
1926
1927 /* Creating postscript terminal gnuplot driver file */
1928 basedirpar = cpl_parameterlist_find(plottags, "inst_dir");
1929 outdirpar = cpl_parameterlist_find(plottags, "output_dir");
1930 filenamepar = cpl_parameterlist_find(plottags, "output_name");
1931
1932 sprintf(ps_filename,"%s/%s/%s_with_lines.ps",
1933 cpl_parameter_get_string(basedirpar),
1934 cpl_parameter_get_string(outdirpar),
1935 cpl_parameter_get_string(filenamepar));
1936
1937
1938 sc_basic_initstring(tmpfilename,SC_MAXLEN);
1939 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname2);
1940 gnufile = fopen(tmpfilename,"w");
1941 fprintf(gnufile,"set term postscript enhanced color\n");
1942 fprintf(gnufile,"set output \"%s\"\n",ps_filename);
1943 fprintf(gnufile,"set nokey\n");
1944 fprintf(gnufile,"set tmargin 2\n");
1945 fprintf(gnufile,"set bmargin 5\n");
1946 fprintf(gnufile,"set lmargin 13\n");
1947 fprintf(gnufile,"set rmargin 3\n");
1948 fprintf(gnufile,"set xrange [ %g : %g ]\n",plot_xmin,plot_xmax);
1949 fprintf(gnufile,"set yrange [ %g : %g ]\n",plot_ymin,plot_ymax);
1950 fprintf(gnufile,"set title \"%s\"\n",plot_title);
1951 fprintf(gnufile,"set xlabel '%s'\n",x_colname);
1952 fprintf(gnufile,"set ylabel '%s'\n",y_colname);
1953 fprintf(gnufile,"set parametric\n");
1954 for(run=0;run<len-1;run++)
1955 {
1956 if (cpl_table_get_int(spec,"class",run,NULL) == 2)
1957 {
1958 plot_dist_min_local=cpl_table_get_double(spec,"flux",run,NULL)
1959 +plot_dist_min;
1960 line=cpl_table_get_double(spec,"lambda",run,NULL);
1961 fprintf(gnufile,"line%i=%g\n",line_run,line);
1962 fprintf(gnufile,"set arrow from %g,%g to %g,%g nohead lw 0.5"
1963 " lc -1\n",line,plot_dist_min_local,line,
1964 plot_dist_max);
1965 fprintf(gnufile,"line%i=%g\n",line_run,line-corrfac/2);
1966 fprintf(gnufile,"set label \"{/=8 %2.5g}\" at "
1967 "line%i,%g rotate\n",line,line_run,plot_label);
1968 line_run++;
1969 }
1970 if (cpl_table_get_int(spec,"class",run,NULL) == 3)
1971 {
1972 plot_dist_min_local=cpl_table_get_double(spec,"flux",run,NULL)
1973 +plot_dist_min;
1974 line=cpl_table_get_double(spec,"lambda",run,NULL);
1975 fprintf(gnufile,"line%i=%g\n",line_run,line);
1976 fprintf(gnufile,"set arrow from %g,%g to %g,%g nohead lw 1 "
1977 "lc 3\n",line,plot_dist_min_local,
1978 line,plot_dist_max);
1979 fprintf(gnufile,"line%i=%g\n",line_run,line-corrfac/2);
1980 fprintf(gnufile,"set label \"{/=8 %2.5g}\" at "
1981 "line%i,%g rotate\n",line,line_run,plot_label);
1982 line_run++;
1983 }
1984
1985 }
1986 fprintf(gnufile,"plot '%s/%s' using 1:2 with lines\n",
1987 tmpdir,filename);
1988
1989 fprintf(gnufile,"\n");
1990 fclose(gnufile);
1991
1992 /* Calling gnuplot */
1993 sprintf(system_call,"gnuplot -persist %s/%s",tmpdir,gnuname2);
1994 if (system(system_call)) {};
1995
1996 /* Cleaning */
1997 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname2);
1998 if (remove(tmpfilename)) {};
1999
2000 /* Creating x11 terminal gnuplot driver file */
2001 if ( (strcmp(plot_type,"X") == 0) || (strcmp(plot_type,"WX") == 0) ||
2002 (strcmp(plot_type,"XW") == 0) )
2003 {
2004 sc_basic_initstring(tmpfilename,SC_MAXLEN);
2005 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname3);
2006 gnufile = fopen(tmpfilename,"w");
2007 fprintf(gnufile,"set term x11\n");
2008 fprintf(gnufile,"set termoption enhanced \n");
2009 fprintf(gnufile,"set nokey\n");
2010 fprintf(gnufile,"set tmargin 2\n");
2011 fprintf(gnufile,"set bmargin 5\n");
2012 fprintf(gnufile,"set lmargin 13\n");
2013 fprintf(gnufile,"set rmargin 3\n");
2014 fprintf(gnufile,"set xrange [ %g : %g ]\n",plot_xmin,plot_xmax);
2015 fprintf(gnufile,"set yrange [ %g : %g ]\n",plot_ymin,plot_ymax);
2016 fprintf(gnufile,"set title \"%s\"\n",plot_title);
2017 fprintf(gnufile,"set xlabel '%s'\n",x_colname);
2018 fprintf(gnufile,"set ylabel '%s'\n",y_colname);
2019 fprintf(gnufile,"set parametric\n");
2020 for(run=0;run<len-1;run++)
2021 {
2022 if (cpl_table_get_int(spec,"class",run,NULL) == 2)
2023 {
2024 plot_dist_min_local=cpl_table_get_double(spec,"flux",run,NULL)
2025 +plot_dist_min;
2026 line=cpl_table_get_double(spec,"lambda",run,NULL);
2027 fprintf(gnufile,"line%i=%g\n",line_run,line);
2028 fprintf(gnufile,"set arrow from %g,%g to %g,%g nohead lw 0.5 "
2029 "lc -1\n",line,plot_dist_min_local,line,
2030 plot_dist_max);
2031 fprintf(gnufile,"line%i=%g\n",line_run,line);
2032 fprintf(gnufile,"set label \"{/=4 %2.5g}\" at "
2033 "line%i,%g rotate\n",line,line_run,plot_label);
2034 line_run++;
2035 }
2036 if (cpl_table_get_int(spec,"class",run,NULL) == 3)
2037 {
2038 plot_dist_min_local=cpl_table_get_double(spec,"flux",run,NULL)
2039 +plot_dist_min;
2040 line=cpl_table_get_double(spec,"lambda",run,NULL);
2041 fprintf(gnufile,"line%i=%g\n",line_run,line);
2042 fprintf(gnufile,"set arrow from %g,%g to %g,%g nohead lw 1 "
2043 "lc 3\n",line,plot_dist_min_local,
2044 line,plot_dist_max);
2045 fprintf(gnufile,"line%i=%g\n",line_run,line);
2046 fprintf(gnufile,"set label \"{/=4 %2.5g}\" at "
2047 "line%i,%g rotate\n",line,line_run,plot_label);
2048 line_run++;
2049 }
2050
2051 }
2052 fprintf(gnufile,"plot '%s/%s' using 1:2 with lines\n",
2053 tmpdir,filename);
2054 fprintf(gnufile,"\n");
2055 fclose(gnufile);
2056
2057 /* Calling gnuplot */
2058 sprintf(system_call,"gnuplot -persist %s/%s",tmpdir,gnuname3);
2059 if (system(system_call)) {};
2060
2061 /* Cleaning */
2062 sprintf(tmpfilename,"%s/%s",tmpdir,gnuname3);
2063 if (remove(tmpfilename)) {};
2064 }
2065
2066 /* Cleaning */
2067 sc_basic_initstring(tmpfilename,SC_MAXLEN);
2068 sprintf(tmpfilename,"%s/%s",tmpdir,filename);
2069 if (remove(tmpfilename)) {};
2070 if (rmdir(tmpdir)) {};
2071
2072 return CPL_ERROR_NONE;
2073}
2074
#define SC_MAXLEN
Definition: sc_basic.h:94
void sc_basic_initstring(char *str, const long n)
Definition: sc_basic.c:2060
cpl_error_code sc_plot_single_spec_with_lines(const cpl_table *spec, cpl_parameterlist *plottags)
Definition: sc_plot.c:1731
void sc_setplottags_overplot_spec(cpl_parameterlist *plottags, const char *x_column, const char *y_column1, const char *y_column2, const char *specname1, const char *specname2, const char *title, const char *x_label, const char *y_label, const cpl_parameterlist *parlist)
Definition: sc_plot.c:802
void sc_setplottags_single_spec_lines(cpl_parameterlist *plottags, const char *title, const char *x_label, const char *y_label, const cpl_parameterlist *parlist)
Definition: sc_plot.c:1664
cpl_error_code sc_overplot_spec(const cpl_table *spec, cpl_parameterlist *plottags)
Definition: sc_plot.c:893
cpl_error_code sc_plot_double_spec(const cpl_table *spec1, const cpl_table *spec2, cpl_parameterlist *plottags)
Definition: sc_plot.c:464
cpl_error_code sc_plot_single_spec(const cpl_table *spec, cpl_parameterlist *plottags)
Definition: sc_plot.c:139
void sc_setplottags_double_spec(cpl_parameterlist *plottags, const char *x_column1, const char *y_column1, const char *title1, const char *x_label1, const char *y_label1, const char *x_column2, const char *y_column2, const char *title2, const char *x_label2, const char *y_label2, const cpl_parameterlist *parlist)
Definition: sc_plot.c:366
void sc_setplottags_hist(cpl_parameterlist *plottags, const char *title, const char *x_label, const char *y_label, const cpl_parameterlist *parlist)
Definition: sc_plot.c:1366
cpl_error_code sc_plot_hist(const cpl_table *histdat, cpl_parameterlist *plottags)
Definition: sc_plot.c:1430
void sc_setplottags_single_spec(cpl_parameterlist *plottags, const char *x_column, const char *y_column, const char *title, const char *x_label, const char *y_label, const cpl_parameterlist *parlist)
Definition: sc_plot.c:66
Header for plotting library.