-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUVES_plot_cspec.new.c
2900 lines (2867 loc) · 119 KB
/
UVES_plot_cspec.new.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
* Plot out final combined spectrum
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "UVES_popler.h"
#include "fit.h"
#include "stats.h"
#include "sort.h"
#include "gamm.h"
#include "input.h"
#include "memory.h"
#include "const.h"
#include "error.h"
int UVES_plot_cspec(spectrum *spec, int nspec, cspectrum *cspec, cplot *cp,
rplot *rp, action **act, int *nact, int *nact_save,
params *par) {
double frac=0.0,rejsigu=0.0,rejsigl=0.0,scale=0.0,sum=0.0,pe=0.0;
double ew=0.0,eew=0.0,cwl=0.0,cwin=0.0;
double wl1=0.0,wl2=0.0;
double sscale=SSCALE,bscale=BSCALE;
double *dat=NULL,*err=NULL,*efl=NULL,*med=NULL;
double *fx=NULL,*fy=NULL,*fy2=NULL;
double *fco=NULL;
float swl=0.0,ewl=0.0;
float flmin=0.0,flmax=0.0;
float csqmin=0.0,csqmax=0.0;
float ncbmin=0.0,ncbmax=0.0;
float shflmin=INFIN,shflmax=0.0;
float pgx=0.0,pgy=0.0,pgxo=0.0,pgyo=0.0;
float step=0.0,clickx=0.0,fscale=0.0,cdist=0.0,ftemp=0.0;
float ftmp;
float *shwl=NULL,*shfl=NULL;
float *wl=NULL,*fl=NULL,*er=NULL,*co=NULL,*csq=NULL,*ccsq=NULL;
float *no=NULL,*ne=NULL,*ncb=NULL,*nccb=NULL;
float *or=NULL;
int nxyp=0,cact=0,ndat=0;
int sp=0,ep=0,cnp=0,np=0,shnp=0;
int csp=0,cep=0,cosp,coep=0;
int nor=0,ncon=0,npcon=0,nscon=0,tscon=0,idx=0;
int fsp=0,fep=0,fnp=0,fbsp=0,fbep=0,fbsnp=0,fbenp=0,fosp=0,foep=0;
int plval=0,status=0,soact=0,sclinit=-1,con_redo=1;
int fit_typ=0,fit_ord=0,fit_rs=0,clicknp=0;
int i=0,j=0,k=0,l=0,m=0,firstfit=0,selpix=0;
int *fst=NULL;
int **con=NULL,**scon=NULL;
char pgch[SHORTLEN]="p";
char query[QUERYLEN]="\0";
statset stdat;
twoxyp *xyp=NULL;
dbletwoint *rank=NULL;
extern plotenv plenv;
/* Determine some global initial values and allocate memory for plotting
arrays */
cp->scalclip=par->scalclip; cp->scalerr=par->scalerr;
cp->exit=0;
np=cspec->np; shnp=(double)np/cp->sfac;
cact=(par->replay) ? rp->cact : *nact-1;
/* Allocate memory and fill arrays for low-dispersion version of spectrum */
if ((shwl=farray(shnp))==NULL)
errormsg("UVES_plot_cspec(): Cannot allocate memory\n\
\tfor shwl plotting array of size %d",shnp);
if ((shfl=farray(shnp))==NULL)
errormsg("UVES_plot_cspec(): Cannot allocate memory\n\
\tfor shfl plotting array of size %d",shnp);
for (i=0,j=0; i<shnp; i++,j+=(int)cp->sfac) {
shwl[i]=cspec->wl[j]; shfl[i]=cspec->fl[j];
}
for (i=0; i<np; i++) {
if (cspec->st[i]>0) {
shflmin=(MIN(cspec->fl[i],shflmin)); shflmax=(MAX(cspec->fl[i],shflmax));
}
}
/* Allocate memory for plotting arrays */
if ((wl=farray(np))==NULL)
errormsg("UVES_plot_cspec(): Cannot allocate memory\n\
\tfor wl plotting array of size %d",np);
if ((fl=farray(np))==NULL)
errormsg("UVES_plot_cspec(): Cannot allocate memory\n\
\tfor fl plotting array of size %d",np);
if ((er=farray(np))==NULL)
errormsg("UVES_plot_cspec(): Cannot allocate memory\n\
\tfor er plotting array of size %d",np);
if ((co=farray(np))==NULL)
errormsg("UVES_plot_cspec(): Cannot allocate memory\n\
\tfor co plotting array of size %d",np);
if ((no=farray(np))==NULL)
errormsg("UVES_plot_cspec(): Cannot allocate memory\n\
\tfor no plotting array of size %d",np);
if ((ne=farray(np))==NULL)
errormsg("UVES_plot_cspec(): Cannot allocate memory\n\
\tfor ne plotting array of size %d",np);
if ((csq=farray(np))==NULL)
errormsg("UVES_plot_cspec(): Cannot allocate memory\n\
\tfor csq plotting array of size %d",np);
if ((ccsq=farray(np))==NULL)
errormsg("UVES_plot_cspec(): Cannot allocate memory\n\
\tfor ccsq plotting array of size %d",np);
if ((ncb=farray(np))==NULL)
errormsg("UVES_plot_cspec(): Cannot allocate memory\n\
\tfor ncb plotting array of size %d",np);
if ((nccb=farray(np))==NULL)
errormsg("UVES_plot_cspec(): Cannot allocate memory\n\
\tfor nccb plotting array of size %d",np);
/* Fill plotting arrays */
for (i=0; i<np; i++) {
wl[i]=cspec->wl[i]; fl[i]=cspec->fl[i]; er[i]=cspec->er[i];
co[i]=cspec->co[i]; csq[i]=cspec->csq[i]; ccsq[i]=cspec->ccsq[i];
no[i]=cspec->no[i]; ne[i]=cspec->ne[i];
ncb[i]=(float)cspec->ncb[i]; nccb[i]=(float)cspec->nccb[i];
}
/* Set up plotting parameters */
if (!cp->ep) {
/** Previous combined plot info not set so set it up here **/
/* Set wavelength and plotting limits */
sp=MAX(0,(MIN(((np-cp->np)/2),(np-cp->np)))); ep=MIN((np-1),(sp+cp->np-1));
cnp=ep-sp+1; swl=wl[sp]; ewl=wl[ep];
flmax=1.1*fjmax(&(fl[sp]),cnp); flmin=MIN((-0.1*flmax),(fjmin(&(fl[sp]),cnp)));
}
else {
sp=cp->sp; ep=cp->ep; swl=cp->swl; ewl=cp->ewl; cnp=cp->np;
flmin=cp->ymn; flmax=cp->ymx;
}
plval=cp->plval;
csqmin=fjmin(&(csq[sp]),cnp); csqmax=fjmax(&(csq[sp]),cnp);
ncbmin=fjmin(&(ncb[sp]),cnp); ncbmax=fjmax(&(ncb[sp]),cnp);
plenv.xmin[0]=shwl[0]; plenv.xmax[0]=shwl[shnp-1];
plenv.ymax[0]=1.1*shflmax; plenv.ymin[0]=-0.1*plenv.ymax[0];
plenv.xmin[1]=swl; plenv.xmax[1]=ewl;
plenv.ymax[1]=1.1*(MAX(1.0,ncbmax)); plenv.ymin[1]=-0.1*plenv.ymax[1];
plenv.ymax[2]=1.1*(MAX(0.5,(MIN(5.0,csqmax))));
plenv.ymin[2]=-0.1*plenv.ymax[2];
plenv.ymin[3]=flmin; plenv.ymax[3]=flmax;
plenv.ymin[4]=-0.2; plenv.ymax[4]=1.3;
/* Find longest order and allocate memory to order array */
for (i=0,k=0; i<nspec; i++) {
for (j=0; j<spec[i].nor; j++) {
if (spec[i].or[j].nuse>=MINUSE) {
if (spec[i].or[j].nrdp>nor) nor=spec[i].or[j].nrdp;
k++;
}
}
}
if ((or=farray(nor))==NULL)
errormsg("UVES_plot_cspec(): Cannot allocate memory\n\
\tfor or plotting array of size %d",nor);
/* Allocate memory to contribution matrix */
if ((con=imatrix(k,7))==NULL)
errormsg("UVES_plot_cspec(): Cannot allocate memory\n\
\tfor con matrix of size %dx%d",k,7);
/* Allocate memory to selected contribution matrix */
if ((scon=imatrix(k,2))==NULL)
errormsg("UVES_plot_cspec(): Cannot allocate memory\n\
\tfor scon matrix of size %dx%d",k,2);
/* Allocate memory for rank array of structures */
if (!(rank=(dbletwoint *)malloc((size_t)(k*sizeof(dbletwoint)))))
errormsg("UVES_plot_cspec(): Cannot allocate memory for rank\n\
\tarray of length %d",k);
/* Select correct PGPLOT device and advance to next page */
cpgslct(cp->pgid); cpgpage();
/** Enter plotting loop **/
while (!cp->exit) {
/* Begin plotting buffer */
cpgbbuf(); cpgeras();
/* Only allow plotting of contributing orders when some exist */
if (!nspec) plval=0;
/* Detect any changes in size of plotting window */
cpgqvsz(1,&ftmp,&(plenv.wwidth),&ftmp,&(plenv.wasp)); plenv.wasp/=plenv.wwidth;
/* Find which orders contribute to the combined spectrum over the
relevant wavelength range */
if (con_redo) {
for (i=0,k=0; i<nspec; i++) {
for (j=0; j<spec[i].nor; j++) {
if ((ep>=spec[i].or[j].csidx && sp<=spec[i].or[j].ceidx) &&
spec[i].or[j].nuse>=MINUSE) {
for (l=(MAX(sp,spec[i].or[j].csidx))-spec[i].or[j].csidx,m=0,sum=0.0;
l<=(MIN(ep,spec[i].or[j].ceidx))-spec[i].or[j].csidx; l++) {
if (spec[i].or[j].rdst[l]==1) {
sum+=spec[i].or[j].rdfl[l]/spec[i].or[j].rder[l]; m++;
}
}
if (!cp->irank) {
rank[k].a=(m) ? sum/(double)m+DRNDTOL*(double)k : DRNDTOL*(double)k;
} else if (cp->irank==1) rank[k].a=(double)spec[i].or[j].crank;
else if (cp->irank==2) rank[k].a=(double)spec[i].or[j].ceidx;
rank[k].i=i; rank[k].j=j; k++;
}
}
}
ncon=k;
/* Sort the rank array so that we can rank plotted orders in
terms of SNR or combination rank */
if (nspec) qsort(rank,ncon,sizeof(dbletwoint),qsort_dbletwointarray);
/* Now rank contributing orders in terms of SNR or combination rank */
for (i=0,j=1,soact=0; i<ncon; i++) {
con[i][0]=rank[i].i; con[i][1]=rank[i].j;
con[i][2]=MAX(sp,spec[con[i][0]].or[con[i][1]].csidx);
con[i][3]=MIN(ep,spec[con[i][0]].or[con[i][1]].ceidx);
con[i][4]=con[i][3]-con[i][2]+1;
con[i][5]=j=(PMOD(j,1,14,2));
if (tscon) {
for (k=0; k<nscon; k++) {
if (scon[k][0]==con[i][0] && scon[k][1]==con[i][1]) {
con[i][6]=1; break;
}
}
if (k==nscon) con[i][6]=0;
} else if (par->replay && rp->exit>=3) {
if (con[i][0]==(*act)[rp->cact].i[0] &&
con[i][1]==(*act)[rp->cact].i[1]) {
if ((*act)[rp->cact].act==COACT || (*act)[rp->cact].act==UOACT ||
(*act)[rp->cact].act==FOACT || (*act)[rp->cact].act==IOACT)
con[i][6]=plval=1;
else if ((*act)[rp->cact].act==SOACT) con[i][6]=soact=plval=1;
} else if (soact) con[i][6]=1;
} else con[i][6]=1;
}
}
/* Plot low-dispersion version of spectrum */
cpgsvp(plenv.vpl,plenv.vpr,cp->vpd0,cp->vpu0); cpgsci(0);
cpgswin(plenv.xmin[0],plenv.xmax[0],plenv.ymin[0],plenv.ymax[0]);
cpgsci(15);
cpgrect(plenv.xmin[0],plenv.xmax[0],plenv.ymin[0],plenv.ymax[0]);
cpgsci(5);
cpgsch(0.3*plenv.ch); cpgbox("BCTS",0.0,0,"BC",0.0,0); cpgsch(plenv.ch);
cpgsci(7); cpgsch(0.8*plenv.ch); cpgbox("N",0.0,0,"",0.0,0);
cpgsci(3); cpgmtxt("B",2.5,0.5,0.5,plenv.xlab[0]);
cpgsci(1); cpgsch(plenv.ch); cpgbin(shnp,shwl,shfl,1);
cpgsci(3); cpgslw(2.0*plenv.lw); cpgsfs(2);
cpgrect(plenv.xmin[1],plenv.xmax[1],plenv.ymin[3],plenv.ymax[3]);
cpgslw(plenv.lw); cpgsfs(1);
/* Plot the pixel-number spectra (before and after sigma-clipping) */
cpgsvp(plenv.vpl,plenv.vpr,cp->vpd1,cp->vpu1); cpgsci(0);
cpgswin(plenv.xmin[1],plenv.xmax[1],plenv.ymin[1],plenv.ymax[1]);
cpgsci(15);
cpgrect(plenv.xmin[1],plenv.xmax[1],plenv.ymin[1],plenv.ymax[1]);
cpgsci(5); cpgsch(0.7*plenv.ch); cpgbox("BCTS",0.0,0,"BCTS",0.0,0);
cpgsci(7); cpgsch(0.8*plenv.ch); cpgbox("N",0.0,0,"",0.0,0);
cpgsch(0.6*plenv.ch); cpgbox("",0.0,0,"NV",0.0,0);
cpgsci(3); cpgsch(0.8*plenv.ch); cpglab(" ",plenv.ylab[1]," ");
cpgsci(2); cpgbin(cnp,&(wl[sp]),&(ncb[sp]),1);
cpgsci(1); cpgbin(cnp,&(wl[sp]),&(nccb[sp]),1);
/* Plot the chisq. spectra (before and after sigma-clipping) */
cpgsvp(plenv.vpl,plenv.vpr,cp->vpd2,cp->vpu2); cpgsci(0);
cpgswin(plenv.xmin[1],plenv.xmax[1],plenv.ymin[2],plenv.ymax[2]);
cpgsci(15);
cpgrect(plenv.xmin[1],plenv.xmax[1],plenv.ymin[2],plenv.ymax[2]);
cpgsci(5); cpgsch(0.7*plenv.ch); cpgbox("BCTS",0.0,0,"BCTS",0.0,0);
cpgsci(7); cpgsch(0.6*plenv.ch); cpgbox("",0.0,0,"NV",0.0,0);
cpgsci(3); cpgsch(0.8*plenv.ch); cpglab(" ",plenv.ylab[2]," ");
cpgsci(2); cpgbin(cnp,&(wl[sp]),&(csq[sp]),1);
cpgsci(1); cpgbin(cnp,&(wl[sp]),&(ccsq[sp]),1);
/* Plot the combined spectrum or contributing spectra */
cpgsvp(plenv.vpl,plenv.vpr,cp->vpd3,cp->vpu3); cpgsci(0);
cpgswin(plenv.xmin[1],plenv.xmax[1],plenv.ymin[3],plenv.ymax[3]);
cpgsci(15);
cpgrect(plenv.xmin[1],plenv.xmax[1],plenv.ymin[3],plenv.ymax[3]);
cpgsci(5);
cpgsch(0.9*plenv.ch); cpgbox("BCTS",0.0,0,"BCTS",0.0,0); cpgsch(plenv.ch);
cpgsci(3); cpgsch(0.8*plenv.ch); cpglab(" ",plenv.ylab[3]," ");
if (plval) {
npcon=0; for (i=0,l=0; i<ncon; i++) {
if (con[i][6]) {
npcon++; l=i;
for (j=0,k=con[i][2]-spec[con[i][0]].or[con[i][1]].csidx;
j<con[i][4]; j++,k++)
or[j]=spec[con[i][0]].or[con[i][1]].rdfl[k];
cpgsci(con[i][5]); cpgbin(con[i][4],&(wl[con[i][2]]),or,1);
for (j=0,k=con[i][2]-spec[con[i][0]].or[con[i][1]].csidx;
j<con[i][4]; j++,k++) {
switch (spec[con[i][0]].or[con[i][1]].rdst[k]) {
case RCLIP: cpgpt(1,&(wl[con[i][2]+j]),&(or[j]),12); break;
case OCLIP: cpgpt(1,&(wl[con[i][2]+j]),&(or[j]),3); break;
case ACLIP: cpgpt(1,&(wl[con[i][2]+j]),&(or[j]),8); break;
case LCLIP: cpgpt(1,&(wl[con[i][2]+j]),&(or[j]),4); break;
case NCLIP: cpgpt(1,&(wl[con[i][2]+j]),&(or[j]),5); break;
case SCLIP: cpgpt(1,&(wl[con[i][2]+j]),&(or[j]),6); break;
case CCLIP: cpgpt(1,&(wl[con[i][2]+j]),&(or[j]),7); break;
}
}
}
}
if (npcon==1) {
for (j=0,k=con[l][2]-spec[con[l][0]].or[con[l][1]].csidx; j<con[l][4];
j++,k++) or[j]=spec[con[l][0]].or[con[l][1]].rdme[k];
cpgsci((PMOD(con[l][5],3,14,2))); cpgline(con[l][4],&(wl[con[l][2]]),or);
for (j=0,k=con[l][2]-spec[con[l][0]].or[con[l][1]].csidx; j<con[l][4];
j++,k++) or[j]=spec[con[l][0]].or[con[l][1]].rder[k];
cpgsci((PMOD(con[l][5],1,14,2))); cpgline(con[l][4],&(wl[con[l][2]]),or);
cpgsci((PMOD(con[l][5],2,14,2)));
} else cpgsci(3);
if (npcon) cpgline(cnp,&(wl[sp]),&(co[sp]));
} else {
cpgsci(1); cpgbin(cnp,&(wl[sp]),&(fl[sp]),1);
cpgsci(2); cpgline(cnp,&(wl[sp]),&(er[sp]));
cpgsci(3); cpgline(cnp,&(wl[sp]),&(co[sp])); cpgsci(7);
for (i=sp; i<=ep; i++) {
switch (cspec->st[i]) {
case 1: break;
case RCLIP: cpgpt(1,&(wl[i]),&(fl[i]),12); break;
case NCLIP: cpgpt(1,&(wl[i]),&(fl[i]),5); break;
case CCLIP: cpgpt(1,&(wl[i]),&(fl[i]),7); break;
}
}
}
cpgsci(5); cpgsls(4);
cpgmove(plenv.xmin[1],0.0); cpgdraw(plenv.xmax[1],0.0); cpgsls(1);
if (par->replay && rp->exit>=3 && (*act)[rp->cact].act<=UCACT) {
cpgsci(7); cpgsfs(2);
cpgrect((float)(*act)[rp->cact].d[0],(float)(*act)[rp->cact].d[1],
(float)(*act)[rp->cact].d[2],(float)(*act)[rp->cact].d[3]);
cpgsfs(1);
}
/* Plot the normalized spectrum */
cpgsvp(plenv.vpl,plenv.vpr,cp->vpd4,cp->vpu4); cpgsci(0);
cpgswin(plenv.xmin[1],plenv.xmax[1],plenv.ymin[4],plenv.ymax[4]);
cpgsci(15);
cpgrect(plenv.xmin[1],plenv.xmax[1],plenv.ymin[4],plenv.ymax[4]);
cpgsci(5); cpgsch(0.7*plenv.ch); cpgbox("BCTS",0.0,0,"BCTS",0.0,0);
cpgsci(7); cpgsch(0.6*plenv.ch); cpgbox("",0.0,0,"NV",0.0,0);
cpgsci(3); cpgsch(0.8*plenv.ch); cpglab(" ",plenv.ylab[4]," ");
cpgsci(1); cpgbin(cnp,&(wl[sp]),&(no[sp]),1);
cpgsci(2); cpgline(cnp,&(wl[sp]),&(ne[sp]));
cpgsci(5); cpgsch(plenv.ch); cpgsls(4);
cpgmove(plenv.xmin[1],0.0); cpgdraw(plenv.xmax[1],0.0);
cpgmove(plenv.xmin[1],1.0); cpgdraw(plenv.xmax[1],1.0); cpgsls(1);
/* If required, plot a panel with buttons for switching on and off
contributing spectra */
if (plval && ncon>1) {
cpgsvp(plenv.vpl,plenv.vpr,plenv.vpu,1.04*plenv.vpu);
cpgswin(0.0,1.0,0.0,1.0); cpgslw(2.0*plenv.lw);
cpgsch(plenv.ch); cpgsci(1);
if (!cp->irank) cpgmtxt("B",-0.3,0.007,0.0,"S N R");
else if (cp->irank==1) cpgmtxt("B",-0.3,0.013,0.0,"RANK");
else if (cp->irank==2) cpgmtxt("B",-0.3,0.007,0.0,"O'LAP");
step=0.9/(double)ncon;
for (i=0; i<ncon; i++) {
cpgsci(con[i][5]); if (!con[i][6]) cpgsfs(2);
cpgrect(0.1+(float)i*step,0.1+(float)(i+1)*step,0.02,1.0);
cpgsfs(1);
}
cpgslw(plenv.lw);
}
/* Flush the buffer */
cpgebuf();
/* No need to go further if replaying certain actions */
if (par->replay && rp->exit==4) { rp->exit=0; break; }
/** Main outer selection loop - waiting for navigation/editing
instructions from user **/
cpgsvp(plenv.vpl,plenv.vpr,cp->vpd0,cp->vpu4);
cpgswin(plenv.vpl,plenv.vpr,cp->vpd0,cp->vpu4);
/* If in replay mode, decide whether interaction with Spectrum
Navigator is necessary */
if (par->replay && rp->exit==3) {
if ((*act)[rp->cact].act==FOACT || (*act)[rp->cact].act==FCACT)
sprintf(pgch,"f");
else if ((*act)[rp->cact].act==IOACT || (*act)[rp->cact].act==ICACT)
sprintf(pgch,"i");
}
/* If normal mode then await the user's instructions */
else cpgband(0,0,0.0,0.0,&pgx,&pgy,pgch);
/* Give usage instructions if asked */
if (!strncmp(pgch,"?",1)) {
if (plval) fprintf(stderr,"\
Options for upper selection panel:\n\
Left mouse : Selects single order to plot\n\
Middle mouse : Selects only higher S/N orders\n\
Right mouse : Toggles plotting of selected order\n\
2 or 1 : Down-scales order by factor %7.3lf or %7.2lf (act)\n\
3 or 4 : Up-scales order by factor %7.3lf or %7.2lf (act)\n\
e : Selects orders in same exposure as selected order\n\
i : Identify spectrum & order numbers & write to screen\n\
l : Selects only lower S/N orders\n\
n : Selects next highest S/N order & de-selects current;\n\
also works on main selection panel.\n\
p : Selects next lowest S/N order & de-selects current;\n\
also works on main selection panel.\n\
x : Selects orders in all spectra other than that selected\n",
sscale,bscale,sscale,bscale);
fprintf(stderr,"\
Options for main selection panel:\n\
Left mouse on lower panel : Selects lower panel for navigation\n\
Left mouse on flux panel : Selects flux panel for navigation\n\
Space bar : Refreshes the panel. Good after window resizing\n\
N : Derive new automatic continuum for entire spectrum\n\
with command-line param.s depending on combmeth\n\
a : Autorescale orders after manually scaling one or\n\
more (order scaling combination method only; act)\n\
c : Adjust sig-clipping level for autorescaling orders\n\
d : Delete or add spectra to plotted and saved comb.\n\
e : Adjust scaling error threshold for autorescaling\n\
f : Fit new continuum to combined spectrum or\n\
single order with normal polynomial fit (*; act)\n\
i : Interpolate new continuum to combined spectrum or\n\
single order with spline function (*; act)\n\
k : Keep selected spectra only when navigating\n\
m : Mirror last action on currently displayed order(s);\n\
Works for order cont. fits and order scaling only.\n\
o : Toggle ranking of orders in SNR or combination rank\n\
when using order scaling combination method\n\
r : Recombine contributing spectra to form new\n\
combined spectrum (act)\n\
s : Save actions in UPL file & save FITS 1-D spectrum\n\
u : Undo last action\n\
v : Plot source spectra with pixel status symbols\n\
w : Toggle plotting of combined spectrum in white in\n\
front, behind or off when viewing source spectra\n\
y : Re-scale y-axis to suit local (combined spectrum)\n\
extrema\n\
. : Expand y-scale by factor of 2\n\
, : Shrink y-scale by factor of 2\n\
> : Expand x-axis by factor of 2 (twice as many pixels)\n\
< : Shrink x-axis by factor of 2 (half as many pixels)\n\
q : Quit screen and exit program\n\n");
}
else if (!strncmp(pgch,"A",1) && pgy<cp->vpu0) {
/* Use low-dispersion spectrum to navigate combined spectrum */
cpgsvp(plenv.vpl,plenv.vpr,cp->vpd0,cp->vpu0);
cpgswin(plenv.xmin[0],plenv.xmax[0],plenv.ymin[0],plenv.ymax[0]);
cpgsci(7); cpgslw(2.0*plenv.lw);
cpgsch(0.3*plenv.ch); cpgbox("BCTS",0.0,0,"BC",0.0,0); cpgsch(plenv.ch);
cpgslw(plenv.lw); cpgband(7,0,0.0,0.0,&pgx,&pgy,pgch);
if (!strncmp(pgch,"?",1)) {
fprintf(stderr,"\
Options for navigation using 'thumbnail' spectrum:\n\
Left mouse : Re-centre detailed spectrum (flux panel) at selected\n\
wavelength\n\
Middle mouse: Re-centre detailed spectrum (as above) and re-size\n\
window to suit new local extrema\n\
Right mouse : Select custom window region.\n\
Right mouse again selects second corner of new window,\n\
any other entry to abort selection.\n\n");
cpgband(7,0,0.0,0.0,&pgx,&pgy,pgch);
}
if (!strncmp(pgch,"D",1)) {
pgx=(MIN(wl[np-1],(MAX(pgx,wl[0]))));
sp=MAX(0,(MIN((idxfval(wl,np,pgx)-cp->np/2),(np-cp->np))));
ep=sp+cp->np-1; cnp=ep-sp+1;
plenv.xmin[1]=wl[sp]; plenv.xmax[1]=wl[ep];
plenv.ymax[1]=1.1*(MAX(1.0,(fjmax(&(ncb[sp]),cnp))));
plenv.ymin[1]=-0.1*plenv.ymax[1];
plenv.ymax[2]=1.1*(MAX(0.5,(MIN(5.0,(fjmax(&(csq[sp]),cnp))))));
plenv.ymin[2]=-0.1*plenv.ymax[2];
plenv.ymax[3]=fjmax(&(fl[sp]),cnp);
plenv.ymin[3]=MIN((-0.1*plenv.ymax[3]),(fjmin(&(fl[sp]),cnp)));
con_redo=1;
}
else if (!strncmp(pgch,"X",1)) {
strcpy(pgch,"\0"); cpgsci(2); pgxo=pgx; pgyo=pgy;
cpgband(2,0,pgxo,pgyo,&pgx,&pgy,pgch);
if (!strncmp(pgch,"X",1)) {
if (pgxo>pgx) { FSWAP(pgxo,pgx); }
if (pgyo>pgy) { FSWAP(pgyo,pgy); }
pgxo=(MAX(pgxo,wl[0])); pgx=(MIN(pgx,wl[np-1]));
sp=MAX(0,(MIN((idxfval(wl,np,pgxo)),(np-2))));
ep=MAX(sp+1,(MIN((idxfval(wl,np,pgx)),(np-1)))); cnp=ep-sp+1;
plenv.xmin[1]=wl[sp]; plenv.xmax[1]=wl[ep];
plenv.ymax[1]=1.1*(MAX(1.0,(fjmax(&(ncb[sp]),cnp))));
plenv.ymin[1]=-0.1*plenv.ymax[1];
plenv.ymax[2]=1.1*(MAX(0.5,(MIN(5.0,(fjmax(&(csq[sp]),cnp))))));
plenv.ymin[2]=-0.1*plenv.ymax[2];
plenv.ymax[3]=MAX(pgyo,pgy); plenv.ymin[3]=MIN(pgyo,pgy);
con_redo=1;
}
else con_redo=0;
}
else if (!strncmp(pgch,"A",1)) {
pgx=(MIN(wl[np-1],(MAX(pgx,wl[0]))));
sp=MAX(0,(MIN((idxfval(wl,np,pgx)-cnp/2),(np-cnp)))); ep=sp+cnp-1;
plenv.xmin[1]=wl[sp]; plenv.xmax[1]=wl[ep];
plenv.ymax[1]=1.1*(MAX(1.0,(fjmax(&(ncb[sp]),cnp))));
plenv.ymin[1]=-0.1*plenv.ymax[1];
plenv.ymax[2]=1.1*(MAX(0.5,(MIN(5.0,(fjmax(&(csq[sp]),cnp))))));
plenv.ymin[2]=-0.1*plenv.ymax[2];
plenv.ymax[3]=fjmax(&(fl[sp]),cnp);
plenv.ymin[3]=MIN((-0.1*plenv.ymax[3]),(fjmin(&(fl[sp]),cnp)));
con_redo=1;
}
else con_redo=0;
}
else if (pgy>cp->vpd3 && pgy<cp->vpu3 &&
(!strncmp(pgch,"A",1) ||
(!strncmp(pgch,"m",1) && plval && cact>-1 && (*act)[cact].act==COACT))) {
cpgsvp(plenv.vpl,plenv.vpr,cp->vpd3,cp->vpu3);
cpgswin(plenv.xmin[1],plenv.xmax[1],plenv.ymin[3],plenv.ymax[3]);
cpgsci(7); cpgslw(2.0*plenv.lw);
cpgsch(0.9*plenv.ch); cpgbox("BCTS",0.0,0,"BCTS",0.0,0);
cpgsch(plenv.ch); cpgslw(plenv.lw); cpgband(7,0,0.0,0.0,&pgx,&pgy,pgch);
if (!strncmp(pgch,"?",1)) {
fprintf(stderr,"\
Options for navigation/editing using detailed spectrum (flux panel):\n\
Left mouse : Re-centre panel at selected wavelength\n\
Middle mouse: Re-centre panel (as above) and re-size window to suit\n\
new local extrema\n\
Right mouse : Select custom window region.\n\
Right mouse again selects second corner of new window,\n\
any other entry to abort selection.\n\
Space bar : Write out to terminal information about cursor position.\n\
b : Define new bottom limit for plot\n\
c : Clip pixels from either combined or contributing spectra (act).\n\
c again selects second corner of clip window,\n\
any other entry to abort selection.\n\
l : Define new left limit for plot\n\
s : Calculate normalized statistics over region (*)\n\
r : Define new right limit for plot\n\
t : Define new top limit for plot\n\
u : Unclip pixels from either combined or contributing spectra (act).\n\
u again selects second corner of clip window,\n\
any other entry to abort selection.\n\
w : Find equivalent width between this mark and next (*)\n\n");
cpgband(7,0,0.0,0.0,&pgx,&pgy,pgch);
}
if (!strncmp(pgch,"D",1)) {
pgx=(MIN(wl[np-1],(MAX(pgx,wl[0]))));
sp=MAX(0,(MIN((idxfval(wl,np,pgx)-cp->np/2),(np-cp->np))));
ep=sp+cp->np-1; cnp=ep-sp+1;
plenv.xmin[1]=wl[sp]; plenv.xmax[1]=wl[ep];
plenv.ymax[1]=1.1*(MAX(1.0,(fjmax(&(ncb[sp]),cnp))));
plenv.ymin[1]=-0.1*plenv.ymax[1];
plenv.ymax[2]=1.1*(MAX(0.5,(MIN(5.0,(fjmax(&(csq[sp]),cnp))))));
plenv.ymin[2]=-0.1*plenv.ymax[2];
plenv.ymax[3]=fjmax(&(fl[sp]),cnp);
plenv.ymin[3]=MIN((-0.1*plenv.ymax[3]),(fjmin(&(fl[sp]),cnp)));
con_redo=1;
}
else if (!strncmp(pgch,"X",1)) {
strcpy(pgch,"\0"); cpgsci(2); pgxo=pgx; pgyo=pgy;
cpgband(2,0,pgx,pgy,&pgx,&pgy,pgch);
if (!strncmp(pgch,"X",1)) {
if (pgxo>pgx) { FSWAP(pgxo,pgx); }
if (pgyo>pgy) { FSWAP(pgyo,pgy); }
pgxo=(MAX(pgxo,wl[0])); pgx=(MIN(pgx,wl[np-1]));
sp=MAX(0,(MIN((idxfval(wl,np,pgxo)),(np-2))));
ep=MAX(0,(MIN((idxfval(wl,np,pgx)),(np-1)))); cnp=ep-sp+1;
plenv.xmin[1]=wl[sp]; plenv.xmax[1]=wl[ep];
plenv.ymax[1]=1.1*(MAX(1.0,(fjmax(&(ncb[sp]),cnp))));
plenv.ymin[1]=-0.1*plenv.ymax[1];
plenv.ymax[2]=1.1*(MAX(0.5,(MIN(5.0,(fjmax(&(csq[sp]),cnp))))));
plenv.ymin[2]=-0.1*plenv.ymax[2];
plenv.ymax[3]=MAX(pgyo,pgy); plenv.ymin[3]=MIN(pgyo,pgy);
con_redo=1;
}
}
else if (!strncmp(pgch,"A",1)) {
pgx=(MIN(wl[np-1],(MAX(pgx,wl[0]))));
sp=MAX(0,(MIN((idxfval(wl,np,pgx)-cnp/2),(np-cnp)))); ep=sp+cnp-1;
plenv.xmin[1]=wl[sp]; plenv.xmax[1]=wl[ep];
plenv.ymax[1]=1.1*(MAX(1.0,(fjmax(&(ncb[sp]),cnp))));
plenv.ymin[1]=-0.1*plenv.ymax[1];
plenv.ymax[2]=1.1*(MAX(0.5,(MIN(5.0,(fjmax(&(csq[sp]),cnp))))));
plenv.ymin[2]=-0.1*plenv.ymax[2];
con_redo=1;
}
else if (!strncmp(pgch,"l",1)) {
sp=MAX(0,(MIN((idxfval(wl,np,pgx)),(ep-1)))); cnp=ep-sp+1;
plenv.xmin[1]=wl[sp];
plenv.ymax[1]=1.1*(MAX(1.0,(fjmax(&(ncb[sp]),cnp))));
plenv.ymin[1]=-0.1*plenv.ymax[1];
plenv.ymax[2]=1.1*(MAX(0.5,(MIN(5.0,(fjmax(&(csq[sp]),cnp))))));
plenv.ymin[2]=-0.1*plenv.ymax[2];
con_redo=1;
}
else if (!strncmp(pgch,"s",1)) {
if (!plval || npcon==1) {
strcpy(pgch,"\0"); cpgsci(3); pgxo=pgx; pgyo=pgy;
cpgband(6,0,pgx,pgy,&pgx,&pgy,pgch);
if (!strncmp(pgch,"?",1)) {
fprintf(stderr,"Options for simple mathematics (flux panel):\n\
m : Calculate statistics on normalized data between previous and this 'm'.\n\n");
cpgband(6,0,pgx,pgy,&pgx,&pgy,pgch);
}
if (!strncmp(pgch,"s",1)) {
if (pgxo>pgx) { FSWAP(pgxo,pgx); } if (pgyo>pgy) { FSWAP(pgyo,pgy); }
/* Find start and ending pixels on the screen */
csp=((csp=idxdval(&(cspec->rwl[sp]),cnp,pgxo))==-1) ? ep : csp+sp;
cep=((cep=idxdval(&(cspec->rwl[sp]),cnp,pgx))==-1) ? ep : cep+sp;
/* Decide whether calculating statistics is feasible given
current plot state */
if (plval && npcon==1) {
/** Calculate statistics for a single order **/
/* Find which spectrum and order we're dealing with */
i=0; while (!con[i][6]) i++;
/* Find start and ending pixels in relevant order */
cosp=(MAX(con[i][2],csp))-spec[con[i][0]].or[con[i][1]].csidx;
coep=(MIN(con[i][3],cep))-spec[con[i][0]].or[con[i][1]].csidx;
k=coep-cosp+1;
/* Allocate memory for data arrays */
if ((dat=darray(k))==NULL)
errormsg("UVES_plot_cspec(): Could not allocate memory\n\
\tfor dat array of size %d",k);
if ((err=darray(k))==NULL)
errormsg("UVES_plot_cspec(): Could not allocate memory\n\
\tfor err array of size %d",k);
if ((efl=darray(k))==NULL)
errormsg("UVES_plot_cspec(): Could not allocate memory\n\
\tfor efl array of size %d",k);
if ((med=darray(k))==NULL)
errormsg("UVES_plot_cspec(): Could not allocate memory\n\
\tfor med array of size %d",k);
/* Fill data arrays */
for (j=cosp,ndat=0; j<=coep; j++) {
if (spec[con[i][0]].or[con[i][1]].rdst[j]==1 &&
spec[con[i][0]].or[con[i][1]].rdco[j]!=0.0) {
dat[ndat]=spec[con[i][0]].or[con[i][1]].rdfl[j]/
spec[con[i][0]].or[con[i][1]].rdco[j];
err[ndat]=spec[con[i][0]].or[con[i][1]].rder[j]/
spec[con[i][0]].or[con[i][1]].rdco[j];
efl[ndat]=spec[con[i][0]].or[con[i][1]].rdef[j]/
spec[con[i][0]].or[con[i][1]].rdco[j];
med[ndat++]=spec[con[i][0]].or[con[i][1]].rdme[j]/
spec[con[i][0]].or[con[i][1]].rdco[j];
}
}
if (ndat) {
/* Calculate statistics */
if (!stats(dat,err,efl,med,NULL,ndat,2,&stdat))
errormsg("UVES_plot_cspec(): Error returned from stats()");
pe=(ndat>1) ? gammq(0.5*(double)(ndat-1),0.5*stdat.chisq) : 0.0;
/* Printf statistics to screen */
fprintf(stderr,"Ntot=%6d Nval=%6d Spec=%d Order=%d\n",k,ndat,
con[i][0]+1,con[i][1]+1);
fprintf(stderr," m=%9.6lf +/- %8.6lf rms=%8.6lf med=%9.6lf\n",
stdat.mean,stdat.emean,stdat.rms,stdat.med);
fprintf(stderr,
" wm=%9.6lf +/- %8.6lf me=%8.6lf chisqe=%8.6lf pe=%8.6lf\n",
stdat.wmean,stdat.ewmean,stdat.meansig,stdat.rchisq,pe);
} else
fprintf(stderr,"No valid pixels in this order in specified range");
/* Clean up */
free(dat); free(err); free(efl); free(med);
}
if (!plval) {
/** Calculate statistics for a combined spectrum **/
k=cep-csp+1;
/* Find number of valid pixels */
for (j=csp,ndat=0; j<=cep; j++) if (cspec->st[j]==1) ndat++;
if (ndat) {
/* Calculate statistics */
if (!stats(&(cspec->no[csp]),&(cspec->ne[csp]),&(cspec->nf[csp]),
NULL,&(cspec->st[csp]),k,2,&stdat))
errormsg("UVES_plot_cspec(): Error returned from stats()");
pe=(ndat>1) ? gammq(0.5*(double)(ndat-1),0.5*stdat.chisq) : 0.0;
/* Printf statistics to screen */
fprintf(stderr,"Ntot=%6d Nval=%6d\n",k,ndat);
fprintf(stderr," m=%9.6lf +/- %8.6lf rms=%8.6lf med=%9.6lf\n",
stdat.mean,stdat.emean,stdat.rms,stdat.med);
fprintf(stderr,
" wm=%9.6lf +/- %8.6lf me=%8.6lf chisqe=%8.6lf pe=%8.6lf\n",
stdat.wmean,stdat.ewmean,stdat.meansig,stdat.rchisq,pe);
} else
fprintf(stderr,
"No valid pixels in combined spectrum in specified range");
}
}
}
}
else if (!strncmp(pgch,"w",1)) {
if (!plval || npcon==1) {
strcpy(pgch,"\0"); cpgsci(3); pgxo=pgx; pgyo=pgy;
cpgband(6,0,pgx,pgy,&pgx,&pgy,pgch);
if (!strncmp(pgch,"?",1)) {
fprintf(stderr,"Options for find equivalent width (flux panel):\n\
w : Calculate equivalent width between previous and this 'w'.\n\n");
cpgband(6,0,pgx,pgy,&pgx,&pgy,pgch);
}
if (!strncmp(pgch,"w",1)) {
if (pgxo>pgx) { FSWAP(pgxo,pgx); } if (pgyo>pgy) { FSWAP(pgyo,pgy); }
/* Find start and ending pixels on the screen */
csp=((csp=idxdval(&(cspec->rwl[sp]),cnp,pgxo))==-1) ? ep : csp+sp;
cep=((cep=idxdval(&(cspec->rwl[sp]),cnp,pgx))==-1) ? ep : cep+sp;
/* Decide whether calculating EWs is feasible given
current plot state */
if (plval && npcon==1) {
/** Calculate EWs for a single order **/
/* Find which spectrum and order we're dealing with */
i=0; while (!con[i][6]) i++;
/* Find start and ending pixels in relevant order */
csp=(MAX(con[i][2],csp)); cep=(MIN(con[i][3],cep));
cosp=csp-spec[con[i][0]].or[con[i][1]].csidx;
coep=cep-spec[con[i][0]].or[con[i][1]].csidx; ndat=coep-cosp+1;
/* Allocate memory for data arrays */
if ((err=darray(ndat))==NULL)
errormsg("UVES_plot_cspec(): Could not allocate memory\n\
\tfor err array of size %d",ndat);
/* Fill data arrays */
for (j=cosp,ndat=0,k=0; j<=coep; j++) {
if (spec[con[i][0]].or[con[i][1]].rdst[j]==1 &&
spec[con[i][0]].or[con[i][1]].rdco[j]!=0.0) {
err[ndat++]=spec[con[i][0]].or[con[i][1]].rder[j]; k++;
} else err[ndat++]=-INFIN;
}
/* Calculate EWs using the unwieghted method */
ew=eew=0.0;
cwl=0.5*(double)(pgx+pgxo); cwin=C_C_K*(double)(pgx-pgxo)/cwl;
if (!EW(&(cspec->wl[csp]),
&(spec[con[i][0]].or[con[i][1]].rdfl[cosp]),err,
&(spec[con[i][0]].or[con[i][1]].rdco[cosp]),ndat,&cwl,
cwin,par->disp,par->disp,NULL,NULL,NULL,&ew,&eew,NULL,NULL,
NULL,NULL,NBTOL,0,0))
warnmsg("UVES_plot_cspec(): Error returned from EW() when\n\
\tfinding unweighted EW for section %f to %f of order %d in file\n\t%s",pgx,pgxo,
con[i][1],spec[con[i][0]].file);
/* Print EWs to screen */
fprintf(stderr,"ws=%10.4f we=%10.4f Ntot=%6d Nval=%6d Spec=%d \
Order=%d\n",pgxo,pgx,ndat,k,con[i][0]+1,con[i][1]+1);
fprintf(stderr,"ew=%.6lg eew=%.6lg\n\n",ew,eew);
/* Clean up */
free(err);
}
if (!plval) {
/** Calculate statistics for a combined spectrum **/
ndat=cep-csp+1;
/* Allocate memory for data arrays */
if ((err=darray(ndat))==NULL)
errormsg("UVES_plot_cspec(): Could not allocate memory\n\
\tfor err array of size %d",ndat);
/* Fill data arrays */
for (j=csp,ndat=0,k=0; j<=cep; j++) {
if (cspec->st[j]==1) { err[ndat++]=cspec->er[j]; k++; }
else err[ndat++]=-INFIN;
}
/* Calculate EWs using the unwieghted method */
ew=eew=0.0;
cwl=0.5*(double)(pgx+pgxo); cwin=C_C_K*(double)(pgx-pgxo)/cwl;
if (!EW(&(cspec->wl[csp]),&(cspec->fl[csp]),err,&(cspec->co[csp]),
ndat,&cwl,cwin,par->disp,par->disp,NULL,NULL,NULL,&ew,&eew,
NULL,NULL,NULL,NULL,NBTOL,0,0))
warnmsg("UVES_plot_cspec(): Error returned from EW() when\n\
\tfinding unweighted EW for section %f to %f in combined spectrum",pgx,pgxo);
/* Print EWs to screen */
fprintf(stderr,"ws=%10.4f we=%10.4f Ntot=%6d Nval=%6d\n",pgxo,pgx,
ndat,k);
fprintf(stderr,"ew=%.6lg eew=%.6lg\n\n",ew,eew);
/* Clean up */
free(err);
}
}
}
}
else if (!strncmp(pgch,"r",1)) {
ep=MIN((np-1),(MAX((idxfval(wl,np,pgx)),(sp+1)))); cnp=ep-sp+1;
plenv.xmax[1]=wl[ep];
plenv.ymax[1]=1.1*(MAX(1.0,(fjmax(&(ncb[sp]),cnp))));
plenv.ymin[1]=-0.1*plenv.ymax[1];
plenv.ymax[2]=1.1*(MAX(0.5,(MIN(5.0,(fjmax(&(csq[sp]),cnp))))));
plenv.ymin[2]=-0.1*plenv.ymax[2];
con_redo=1;
}
else if (!strncmp(pgch,"t",1)) {
plenv.ymax[3]=(pgy>plenv.ymin[3]) ? pgy : plenv.ymax[3];
con_redo=0;
}
else if (!strncmp(pgch,"b",1)) {
plenv.ymin[3]=(pgy<plenv.ymax[3]) ? pgy : plenv.ymin[3];
con_redo=0;
}
else if (!strncmp(pgch," ",1)) {
if (!plval || npcon==1) {
/* Find pixel on the screen */
csp=((csp=idxdval(&(cspec->rwl[sp]),cnp,pgx))==-1) ? ep : csp+sp;
if (plval && npcon==1) {
/** Write out cursor information for a single order **/
/* Find which spectrum and order we're dealing with */
i=0; while (!con[i][6]) i++;
/* Find pixel in relevant order */
cosp=(MAX(con[i][2],csp))-spec[con[i][0]].or[con[i][1]].csidx;
/* Write out relevant information for cursor position */
fprintf(stderr,"x=%12.6lf y=%-.8g\n",pgx,pgy);
fprintf(stderr,"w=%12.6lf Spec=%d Order=%d\n",cspec->wl[csp],
con[i][0]+1,con[i][1]+1);
fprintf(stderr,"Order : f=%-.8g e=%-.8g ef=%-.8g c=%9.6lf \
n=%6d\n",spec[con[i][0]].or[con[i][1]].rdfl[cosp],
spec[con[i][0]].or[con[i][1]].rder[cosp],
spec[con[i][0]].or[con[i][1]].rdef[cosp],
spec[con[i][0]].or[con[i][1]].rdco[cosp],cosp+1);
if (spec[con[i][0]].or[con[i][1]].rdco[cosp]!=0.0)
fprintf(stderr," nf=%9.6lf ne=%.8lg nef=%.8lg\n",
spec[con[i][0]].or[con[i][1]].rdfl[cosp]/
spec[con[i][0]].or[con[i][1]].rdco[cosp],
spec[con[i][0]].or[con[i][1]].rder[cosp]/
spec[con[i][0]].or[con[i][1]].rdco[cosp],
spec[con[i][0]].or[con[i][1]].rdef[cosp]/
spec[con[i][0]].or[con[i][1]].rdco[cosp]);
fprintf(stderr,"Combined: f=%-.8g e=%-.8g ef=%-.8g c=%9.6lf \
n=%6d\n",cspec->fl[csp],cspec->er[csp],cspec->ef[csp],cspec->co[csp],csp+1);
fprintf(stderr," nf=%9.6lf ne=%.8lg nef=%.8lg\n",
cspec->no[csp],cspec->ne[csp],cspec->nf[csp]);
if (cspec->st[csp]==1) {
fprintf(stderr," N(bef)=%d chisq(bef)=%9.6lf p(bef)=%8.6lf\n",
cspec->ncb[csp],cspec->csq[csp],
gammq(0.5*(double)(cspec->ncb[csp]-1),0.5*cspec->csq[csp]*
(double)(cspec->ncb[csp]-1)));
fprintf(stderr," N(aft)=%d chisq(aft)=%9.6lf p(aft)=%8.6lf\n\n",
cspec->nccb[csp],cspec->ccsq[csp],
gammq(0.5*(double)(cspec->nccb[csp]-1),0.5*cspec->ccsq[csp]*
(double)(cspec->nccb[csp]-1)));
}
}
if (!plval) {
/** Write out cursor information for combined spectrum **/
fprintf(stderr,"x=%12.6lf y=%-.8g\n",pgx,pgy);
fprintf(stderr,"w=%12.6lf f=%-.8g e=%-.8g ef=%-.8g c=%9.6lf n=%6d\n",
cspec->wl[csp],cspec->fl[csp],cspec->er[csp],cspec->ef[csp],
cspec->co[csp],csp+1);
fprintf(stderr," nf=%9.6lf ne=%.8lg nef=%.8lg\n",
cspec->no[csp],cspec->ne[csp],cspec->nf[csp]);
if (cspec->st[csp]==1) {
fprintf(stderr,"N(bef)=%d chisq(bef)=%9.6lf p(bef)=%8.6lf\n",
cspec->ncb[csp],cspec->csq[csp],
gammq(0.5*(double)(cspec->ncb[csp]-1),0.5*cspec->csq[csp]*
(double)(cspec->ncb[csp]-1)));
fprintf(stderr,"N(aft)=%d chisq(aft)=%9.6lf p(aft)=%8.6lf\n\n",
cspec->nccb[csp],cspec->ccsq[csp],
gammq(0.5*(double)(cspec->nccb[csp]-1),0.5*cspec->ccsq[csp]*
(double)(cspec->nccb[csp]-1)));
}
}
} else fprintf(stderr,"x=%12.6lf y=%-.8g\n\n",pgx,pgy);
con_redo=0;
}
else if (!strncmp(pgch,"c",1) ||
(!strncmp(pgch,"m",1) && plval && cact>-1 && (*act)[cact].act==COACT)) {
/* Clip pixels */
cpgsci(3);
if (strncmp(pgch,"m",1)) {
strcpy(pgch,"\0"); pgxo=pgx; pgyo=pgy; cpgband(2,0,pgx,pgy,&pgx,&pgy,pgch);
} else {
pgxo=(*act)[cact].d[0]; pgyo=(*act)[cact].d[2];
pgx=(*act)[cact].d[1]; pgy=(*act)[cact].d[3];
strcpy(pgch,"c\0");
}
if (!strncmp(pgch,"c",1)) {
if (pgxo>pgx) { FSWAP(pgxo,pgx); } if (pgyo>pgy) { FSWAP(pgyo,pgy); }
csp=((csp=idxdval(&(cspec->rwl[sp]),cnp,pgxo))==-1) ? ep : csp+sp;
cep=((cep=idxdval(&(cspec->rwl[sp]),cnp,pgx))==-1) ? ep : cep+sp;
if (plval) {
/* Clip pixels from individual orders */
for (i=0,k=0; i<ncon; i++) {
if (con[i][6] && (con[i][2]<=cep && con[i][3]>=csp)) {
cosp=(MAX(con[i][2],csp))-spec[con[i][0]].or[con[i][1]].csidx;
coep=(MIN(con[i][3],cep))-spec[con[i][0]].or[con[i][1]].csidx;
for (j=cosp,selpix=0; j<=coep; j++) {
if (spec[con[i][0]].or[con[i][1]].rdfl[j]<pgy &&
spec[con[i][0]].or[con[i][1]].rdfl[j]>pgyo) {
spec[con[i][0]].or[con[i][1]].ordst[j]=
spec[con[i][0]].or[con[i][1]].rdst[j];
spec[con[i][0]].or[con[i][1]].rdst[j]=CCLIP; selpix++;
}
}
/* Remember action by filling in an action report */
if (selpix) {
if (!(*nact)) {
if (!(*act=(action *)malloc((size_t)(sizeof(action)))))
errormsg("UVES_plot_cspec(): Cannot allocate memory for\n\
\tact array of size 1");
}
else
if (!(*act=(action *)
realloc(*act,(size_t)((*nact+1)*sizeof(action)))))
errormsg("UVES_plot_cspec(): Cannot increase memory for\n\
\tact array to size %d",*nact+1);
/* If in paused replay mode then shift later actions to end */
if (par->replay && rp->exit==2) {
for (j=*nact-1; j>=rp->cact; j--) (*act)[j+1]=(*act)[j];
cact=rp->cact++; (*nact)++;
} else cact=(*nact)++;
(*act)[cact].act=COACT; (*act)[cact].nxyp=0;
(*act)[cact].d[0]=pgxo; (*act)[cact].d[1]=pgx;
(*act)[cact].d[2]=pgyo; (*act)[cact].d[3]=pgy;
(*act)[(cact)].i[0]=con[i][0]; (*act)[(cact)].i[1]=con[i][1];
(*act)[cact].rcmb=0; (*act)[cact].val=1; k++;
}
}
}
/* If some new actions were added, record how many */
if (*nact) (*act)[cact].nordact=k;
}
else {
/* Clip pixels from combined spectrum */
for (j=csp,selpix=0; j<=cep; j++) {
if (cspec->fl[j]<pgy && cspec->fl[j]>pgyo) {
cspec->ost[j]=cspec->st[j]; cspec->st[j]=CCLIP; selpix++;
cspec->no[j]=1.0; cspec->ne[j]=cspec->nf[j]=-INFIN;
no[j]=1.0; ne[j]=-1.0;
}
}
/* Remember action by filling in an action report */
if (selpix) {
if (!(*nact)) {
if (!(*act=(action *)malloc((size_t)(sizeof(action)))))
errormsg("UVES_plot_cspec(): Cannot allocate memory for\n\
\tact array of size 1");
}
else
if (!(*act=(action *)
realloc(*act,(size_t)((*nact+1)*sizeof(action)))))
errormsg("UVES_plot_cspec(): Cannot increase memory for\n\
\tact array to size %d",*nact+1);
/* If in paused replay mode then shift later actions to end */
if (par->replay && rp->exit==2) {
for (j=*nact-1; j>=rp->cact; j--) (*act)[j+1]=(*act)[j];
cact=rp->cact++; (*nact)++;
} else cact=(*nact)++;
(*act)[cact].act=CCACT; (*act)[cact].nxyp=0; (*act)[cact].d[0]=pgxo;
(*act)[cact].d[1]=pgx; (*act)[cact].d[2]=pgyo; (*act)[cact].d[3]=pgy;
(*act)[cact].rcmb=0; (*act)[cact].nordact=1; (*act)[cact].val=1;
}
}
}
con_redo=0; cp->rccsp=1;
}
else if (!strncmp(pgch,"u",1)) {
/* Unclip pixels */
strcpy(pgch,"\0"); cpgsci(3); pgxo=pgx; pgyo=pgy;
cpgband(2,0,pgx,pgy,&pgx,&pgy,pgch);
if (!strncmp(pgch,"u",1)) {
if (pgxo>pgx) { FSWAP(pgxo,pgx); } if (pgyo>pgy) { FSWAP(pgyo,pgy); }
csp=((csp=idxdval(&(cspec->rwl[sp]),cnp,pgxo))==-1) ? ep : csp+sp;
cep=((cep=idxdval(&(cspec->rwl[sp]),cnp,pgx))==-1) ? ep : cep+sp;
if (plval) {
/* Unclip pixels from individual orders */
for (i=0,k=0; i<ncon; i++) {
if (con[i][6] && (con[i][2]<=cep && con[i][3]>=csp)) {
cosp=(MAX(con[i][2],csp))-spec[con[i][0]].or[con[i][1]].csidx;
coep=(MIN(con[i][3],cep))-spec[con[i][0]].or[con[i][1]].csidx;
for (j=cosp,selpix=0; j<=coep; j++) {
if (spec[con[i][0]].or[con[i][1]].rdfl[j]<pgy &&
spec[con[i][0]].or[con[i][1]].rdfl[j]>pgyo) {
spec[con[i][0]].or[con[i][1]].ordst[j]=
spec[con[i][0]].or[con[i][1]].rdst[j];
spec[con[i][0]].or[con[i][1]].rdst[j]=1; selpix++;
}
}
/* Remember action by filling in an action report */
if (selpix) {
if (!(*nact)) {
if (!(*act=(action *)malloc((size_t)(sizeof(action)))))
errormsg("UVES_plot_cspec(): Cannot allocate memory for\n\
\tact array of size 1");
}
else
if (!(*act=(action *)
realloc(*act,(size_t)((*nact+1)*sizeof(action)))))
errormsg("UVES_plot_cspec(): Cannot increase memory for\n\
\tact array to size %d",*nact+1);
/* If in paused replay mode then shift later actions to end */
if (par->replay && rp->exit==2) {
for (j=*nact-1; j>=rp->cact; j--) (*act)[j+1]=(*act)[j];
cact=rp->cact++; (*nact)++;
} else cact=(*nact)++;
(*act)[cact].act=UOACT; (*act)[cact].nxyp=0;
(*act)[cact].d[0]=pgxo; (*act)[cact].d[1]=pgx;
(*act)[cact].d[2]=pgyo; (*act)[cact].d[3]=pgy;
(*act)[(cact)].i[0]=con[i][0]; (*act)[(cact)].i[1]=con[i][1];
(*act)[cact].rcmb=0; (*act)[cact].val=1; k++;
}
}
}
/* If some new actions were added, record how many */
if (*nact) (*act)[cact].nordact=k;
}
else {
/* Unclip pixels from combined spectrum */
for (j=csp,selpix=0; j<=cep; j++) {
if (cspec->fl[j]<pgy && cspec->fl[j]>pgyo) {
cspec->ost[j]=cspec->st[j]; cspec->st[j]=1; selpix++;
cspec->no[j]=cspec->fl[j]/cspec->co[j];
cspec->ne[j]=cspec->er[j]/cspec->co[j];
cspec->nf[j]=cspec->ef[j]/cspec->co[j];