source: trunk/src/Utils/plottingUtilities.cc @ 208

Last change on this file since 208 was 205, checked in by Matthew Whiting, 18 years ago

Changes here mostly aimed at reducing/removing memory leaks. These were one of:

  • Forgetting to delete something allocated with "new". The Cube destructor has improved with the use of bool variables to keep track of when recon & baseline ahave been allocated.
  • Incorrectly using the wcs->flag parameter (eg. wcsIO had it set to -1 *after* calling wcsini)
  • Not closing a fits file once finished with (dataIO)
  • Allocating the wcsprm structs with calloc before calling wcsini, so that wcsvfree can work (it calls "free", so the memory needs to be allocated with calloc or malloc).

The only other change was the following:

  • A new way of doing the Cube::setCubeStats -- rather than calling the functions in getStats.cc, we explicitly do the calculations. This means we can sort the tempArray that has the BLANKS etc removed. This saves a great deal of memory usage on large FITS files (such as Enno's 2Gb one)
  • The old setCubeStats function is still there but called setCubeStatsOld.
File size: 3.5 KB
Line 
1#include <iostream>
2#include <math.h>
3#include <cpgplot.h>
4#include <Utils/utils.hh>
5
6void plotLine(const float slope, const float intercept)
7{
8  float x1, x2, y1, y2;
9  cpgqwin(&x1,&x2,&y1,&y2);
10  cpgmove(x1,slope*x1+intercept);
11  cpgdraw(x2,slope*x2+intercept);
12}
13
14void lineOfEquality()
15{
16  plotLine(1.,0.);
17}
18
19void lineOfBestFit(int size, float *x, float *y)
20{
21  float a,b,r,erra,errb;
22  linear_regression(size,x,y,0,size-1,a,erra,b,errb,r);
23  plotLine(a,b);
24}
25
26void plotVertLine(const float xval, const int colour, const int style)
27{
28  float x1, x2, y1, y2;
29  cpgqwin(&x1,&x2,&y1,&y2);
30  int currentColour,currentStyle;
31  cpgqci(&currentColour);
32  cpgqls(&currentStyle);
33  if(currentColour!=colour) cpgsci(colour);
34  if(currentStyle !=style)  cpgsls(style);
35  cpgmove(xval,y1);
36  cpgdraw(xval,y2);
37  if(currentColour!=colour) cpgsci(currentColour);
38  if(currentStyle !=style)  cpgsls(currentStyle);
39}
40
41void plotVertLine(const float xval)
42{
43  int colour,style;
44  cpgqci(&colour);
45  cpgqls(&style);
46  plotVertLine(xval,colour,style);
47}
48
49void plotVertLine(const float xval, const int colour)
50{
51  int style;
52  cpgqls(&style);
53  plotVertLine(xval,colour,style);
54}
55
56void plotHorizLine(const float yval, const int colour, const int style)
57{
58  float x1, x2, y1, y2;
59  cpgqwin(&x1,&x2,&y1,&y2);
60  int currentColour,currentStyle;
61  cpgqci(&currentColour);
62  cpgqls(&currentStyle);
63  if(currentColour!=colour) cpgsci(colour);
64  if(currentStyle !=style)  cpgsls(style);
65  cpgmove(x1,yval);
66  cpgdraw(x2,yval);
67  if(currentColour!=colour) cpgsci(currentColour);
68  if(currentStyle !=style)  cpgsls(currentStyle);
69}
70
71void plotHorizLine(const float yval)
72{
73  int colour,style;
74  cpgqci(&colour);
75  cpgqls(&style);
76  plotHorizLine(yval,colour,style);
77}
78
79void plotHorizLine(const float yval, const int colour)
80{
81  int style;
82  cpgqls(&style);
83  plotHorizLine(yval,colour,style);
84}
85
86void lineOfBestFitPB(const int size, const float *x, const float *y)
87{
88  int numSim = int(size * log(float(size)*log(float(size))) );
89  float slope=0, intercept=0;
90  float *xboot = new float[size];
91  float *yboot = new float[size];
92  for(int sim=0;sim<numSim;sim++){
93    for(int i=0;i<size;i++){
94      int point = int((1.*rand())/(RAND_MAX+1.0) * size);
95      xboot[i] = x[point];
96      yboot[i] = y[point];
97    }
98    float a,b,r,erra,errb;
99    linear_regression(size,xboot,yboot,0,size-1,a,erra,b,errb,r);
100    slope += a;
101    intercept += b;
102  }
103  delete [] xboot;
104  delete [] yboot;
105  slope /= float(numSim);
106  intercept /= float(numSim);
107 
108  plotLine(slope,intercept);
109}
110
111void drawContours(const int size, const float *x, const float *y)
112{
113  float x1, x2, y1, y2;
114  cpgqwin(&x1,&x2,&y1,&y2);
115  const int nbin = 30;
116  // widths of bins in x and y directions
117  float xbin = (x2 - x1) / float(nbin-2); // have one bin either side of extrema
118  float ybin = (y2 - y1) / float(nbin-2);
119  float *binnedarray = new float[nbin*nbin];
120  for(int i=0;i<nbin*nbin;i++) binnedarray[i] = 0.;
121  for(int i=0;i<size;i++){
122    int xpos = int( (x[i] - (x1-xbin)) / xbin );
123    int ypos = int( (y[i] - (y1-ybin)) / ybin );
124    if((ypos*nbin+xpos) >= (nbin*nbin))
125      std::cerr <<"\aERROR <drawContours> : pixel position out of range.\n";
126    binnedarray[ypos*nbin+xpos] += 1.;
127  } 
128  float maxct = binnedarray[0];
129  for(int i=1;i<nbin*nbin;i++) if(binnedarray[i]>maxct) maxct=binnedarray[i];
130  float tr[6]={x1-3*xbin/2,xbin,0.,y1-3*ybin/2,0.,ybin};
131  const int ncont = 10;
132  float *cont = new float[ncont];
133  for(int i=0;i<ncont;i++) cont[i] = maxct / pow(2,float(i)/2.);
134  cpgcont(binnedarray,nbin,nbin,1,nbin,1,nbin,cont,ncont,tr);
135  delete [] binnedarray;
136  delete [] cont;
137}
Note: See TracBrowser for help on using the repository browser.