AnitaGpsCanvasMaker.cxx
1 #include <fstream>
9 #include <iostream>
10 #include "MagicDisplayConventions.h"
11 #include "AnitaGpsCanvasMaker.h"
12 #include "AnitaConventions.h"
13 #include "AnitaGeomTool.h"
14 #include "UsefulAnitaEvent.h"
15 #include "RawAnitaHeader.h"
16 #include "Adu5Pat.h"
17 #include "Adu5Sat.h"
18 #include "Adu5Vtg.h"
19 #include "G12Pos.h"
20 #include "G12Sat.h"
21 
22 
23 #include "TString.h"
24 #include "TImage.h"
25 #include "TObjArray.h"
26 #include "TObjString.h"
27 #include "TVector3.h"
28 #include "TROOT.h"
29 #include "TPaveText.h"
30 #include "TPad.h"
31 #include "TText.h"
32 #include "TLatex.h"
33 #include "TGraph.h"
34 #include "WaveformGraph.h"
35 #include "TStyle.h"
36 #include "TCanvas.h"
37 #include "TAxis.h"
38 #include "TH1.h"
39 #include "THStack.h"
40 #include "TList.h"
41 #include "TObject.h"
42 #include "TTimeStamp.h"
43 #include "TMath.h"
44 #include "TMarker.h"
45 #include "TMultiGraph.h"
46 
48 TMultiGraph *mgSat=0;
49 
50 
52 {
53  //Default constructor
54  fgInstance=this;
55  fDisplayOpt=MagicDisplayGpsDisplay::kSatView;
56  fNumG12Sat=0;
57  fNumAdu5ASat=0;
58  fNumAdu5BSat=0;
59  fReloadMap=1;
60 
61 }
62 
64 {
65  //Default destructor
66 }
67 
68 
69 
70 //______________________________________________________________________________
72 {
73  //static function
75 }
76 
77 
78 TPad *AnitaGpsCanvasMaker::getGpsMainCanvas(Adu5Sat *adu5ASatPtr, Adu5Sat *adu5BSatPtr, G12Sat *g12SatPtr, Adu5Pat *adu5APatPtr, Adu5Pat *adu5BPatPtr, G12Pos *g12PosPtr, TPad *useCan)
79 {
80  static MagicDisplayGpsDisplay::MagicDisplayGpsDisplay_t lastDisplayOpt=MagicDisplayGpsDisplay::kNotAView;
81  TPad *canGps;
82  TPad *plotPad;
83  if(!useCan) {
84  canGps = (TPad*) gROOT->FindObject("canGps");
85  if(!canGps) {
86  canGps = new TCanvas("canGps","canGps",1000,600);
87  }
88  canGps->Clear();
89  canGps->SetTopMargin(0);
90  plotPad = new TPad("canGpsMain","canGpsMain",0,0,1,0.9);
91  plotPad->Draw();
92  }
93  else {
94  plotPad=useCan;
95  }
96  plotPad->cd();
97 
98  if(fDisplayOpt!=lastDisplayOpt) {
99  plotPad->Clear();
100  fReloadMap=1;
101  }
102  lastDisplayOpt=fDisplayOpt;
103 
104  if(fDisplayOpt==MagicDisplayGpsDisplay::kSatView) {
105  addSatellitesToTimeArray(adu5ASatPtr,adu5BSatPtr,g12SatPtr);
106  getSatelliteView(adu5ASatPtr,adu5BSatPtr,g12SatPtr,plotPad);
107  }
108  else if(fDisplayOpt==MagicDisplayGpsDisplay::kMapView) {
109  if(adu5APatPtr)
110  getMapView(adu5APatPtr,plotPad);
111  else if(adu5BPatPtr)
112  getMapView(adu5BPatPtr,plotPad);
113  else if(g12PosPtr) {
114  Adu5Pat tempPat;
115  tempPat.latitude=g12PosPtr->latitude;
116  tempPat.longitude=g12PosPtr->longitude;
117  tempPat.altitude=g12PosPtr->altitude;
118  getMapView(&tempPat,plotPad);
119  }
120  else {
121  std::cout << "No ADU5 attitude data available will give up";
122  return plotPad;
123  }
124  }
125  if(useCan)
126  return plotPad;
127  else
128  return canGps;
129 }
130 
131 TPad *AnitaGpsCanvasMaker::getGpsInfoCanvas(Adu5Sat *adu5ASatPtr, Adu5Sat *adu5BSatPtr, G12Sat *g12SatPtr, Adu5Pat *adu5APatPtr, Adu5Pat *adu5BPatPtr, G12Pos *g12PosPtr,TPad *useCan)
132 {
133 
134  char textLabel[180];
135  TPad *topPad;
136  if(!useCan) {
137  topPad = new TPad("padGpsInfo","padGpsInfo",0.2,0.9,0.8,1);
138  topPad->Draw();
139  }
140  else {
141  topPad=useCan;
142  }
143  topPad->Clear();
144  topPad->SetTopMargin(0.05);
145  topPad->Divide(2,1);
146  topPad->cd(1);
147  TPaveText *leftPave = new TPaveText(0.05,0,0.95,0.9);
148  leftPave->SetBorderSize(0);
149  leftPave->SetFillColor(0);
150  leftPave->SetTextAlign(13);
151  if(adu5APatPtr) {
152  sprintf(textLabel,"Run: %d",adu5APatPtr->run);
153  TText *runText = leftPave->AddText(textLabel);
154  runText->SetTextColor(50);
155  TTimeStamp g12PosTime((time_t)adu5APatPtr->realTime,0);
156  sprintf(textLabel,"Time: %s",g12PosTime.AsString("s"));
157  TText *timeText = leftPave->AddText(textLabel);
158  timeText->SetTextColor(1);
159  }
160  else if(adu5BPatPtr) {
161  sprintf(textLabel,"Run: %d",adu5BPatPtr->run);
162  TText *runText = leftPave->AddText(textLabel);
163  runText->SetTextColor(50);
164  TTimeStamp g12PosTime((time_t)adu5BPatPtr->realTime,0);
165  sprintf(textLabel,"Time: %s",g12PosTime.AsString("s"));
166  TText *timeText = leftPave->AddText(textLabel);
167  timeText->SetTextColor(1);
168 
169  }
170  else if(g12PosPtr) {
171  sprintf(textLabel,"Run: %d",g12PosPtr->run);
172  TText *runText = leftPave->AddText(textLabel);
173  runText->SetTextColor(50);
174  TTimeStamp g12PosTime((time_t)g12PosPtr->realTime,0);
175  sprintf(textLabel,"Time: %s",g12PosTime.AsString("s"));
176  TText *timeText = leftPave->AddText(textLabel);
177  timeText->SetTextColor(1);
178 
179  }
180  leftPave->Draw();
181 
182  topPad->cd(2);
183  TPaveText *rightPave = new TPaveText(0.05,0,0.95,0.9);
184  rightPave->SetBorderSize(0);
185  rightPave->SetFillColor(0);
186  rightPave->SetTextAlign(13);
187  if(g12SatPtr)
188  sprintf(textLabel,"G12 Sats: %d",g12SatPtr->numSats);
189  else if(g12PosPtr)
190  sprintf(textLabel,"G12 Sats: %d",g12PosPtr->numSats);
191  TText *g12Text = rightPave->AddText(textLabel);
192  g12Text->SetTextColor(1);
193  sprintf(textLabel,"ADU5 A Sats: %d %d %d %d",adu5ASatPtr->numSats[0],
194  adu5ASatPtr->numSats[1],adu5ASatPtr->numSats[2],adu5ASatPtr->numSats[3]);
195  TText *adu5aText = rightPave->AddText(textLabel);
196  adu5aText->SetTextColor(1);
197  sprintf(textLabel,"ADU5 B Sats: %d %d %d %d",adu5BSatPtr->numSats[0],
198  adu5BSatPtr->numSats[1],adu5BSatPtr->numSats[2],adu5BSatPtr->numSats[3]);
199  TText *adu5bText = rightPave->AddText(textLabel);
200  adu5bText->SetTextColor(1);
201  rightPave->Draw();
202  return topPad;
203 
204 }
205 
206 void AnitaGpsCanvasMaker::getSatelliteView(Adu5Sat *adu5ASatPtr, Adu5Sat *adu5BSatPtr, G12Sat *g12SatPtr, TPad *plotPad)
207 {
208  if(mgSat) delete mgSat;
209  plotPad->Clear();
210  plotPad->Divide(1,2);
211  TPad *topHalf=(TPad*)plotPad->cd(1);
212  topHalf->Divide(3,1);
213  TPad *g12Pad=(TPad*)topHalf->cd(1);
214  g12SatPtr->getCirclePlot(g12Pad);
215  g12Pad->SetEditable(0);
216  TPad *adu5aPad=(TPad*)topHalf->cd(2);
217  adu5ASatPtr->getCirclePlot(adu5aPad,"ADU5A");
218  adu5aPad->SetEditable(0);
219  TPad *adu5bPad=(TPad*)topHalf->cd(3);
220  adu5BSatPtr->getCirclePlot(adu5bPad,"ADU5B");
221  adu5bPad->SetEditable(0);
222  plotPad->cd(2);
223  mgSat = new TMultiGraph();
224  Int_t shouldDraw=0;
225  if(fNumG12Sat>0) {
226  TGraph *grG12=new TGraph(fNumG12Sat,fG12SatTimeArray,fG12SatNumArray);
227  grG12->SetLineColor(getNiceColour(0));
228  grG12->SetMarkerColor(getNiceColour(0));
229  grG12->SetMarkerStyle(getMarker(0));
230  grG12->SetName("G12");
231  mgSat->Add(grG12,"p");
232  shouldDraw++;
233  }
234  char graphName[180];
235 
236  for(int ant=0;ant<4;ant++) {
237  if(fNumAdu5ASat) {
238  TGraph *grAdu5=new TGraph(fNumAdu5ASat,fAdu5ASatTimeArray,fAdu5ASatNumArray[ant]);
239  grAdu5->SetLineColor(getNiceColour(1));
240  grAdu5->SetMarkerColor(getNiceColour(1));
241  grAdu5->SetMarkerStyle(getMarker(1+ant));
242  sprintf(graphName,"ADU5A_%d",ant+1);
243  grAdu5->SetName(graphName);
244  mgSat->Add(grAdu5,"p");
245  shouldDraw++;
246  }
247  if(fNumAdu5BSat) {
248  TGraph *grAdu5=new TGraph(fNumAdu5BSat,fAdu5BSatTimeArray,fAdu5BSatNumArray[ant]);
249  grAdu5->SetLineColor(getNiceColour(2));
250  grAdu5->SetMarkerColor(getNiceColour(2));
251  grAdu5->SetMarkerStyle(getMarker(2+ant));
252  sprintf(graphName,"ADU5B_%d",ant+1);
253  grAdu5->SetName(graphName);
254  mgSat->Add(grAdu5,"p");
255  shouldDraw++;
256  }
257  }
258  if(shouldDraw) {
259  mgSat->SetTitle("Number of Satellites Used");
260  mgSat->Draw("ap");
261  mgSat->GetXaxis()->SetTitle("Time");
262  mgSat->GetYaxis()->SetTitle("Number");
263  mgSat->GetXaxis()->SetTimeDisplay(1);
264  gPad->Update();
265  gPad->Modified();
266  }
267 
268 }
269 
270 
271 
272 void AnitaGpsCanvasMaker::addSatellitesToTimeArray(Adu5Sat *adu5ASatPtr, Adu5Sat *adu5BSatPtr, G12Sat *g12SatPtr) {
273  if(adu5ASatPtr) {
274  if(fNumAdu5ASat>=MAX_GPS_TIME_POINTS) {
275  //We'll average everythin
276  fNumAdu5ASat=0;
277  for(int i=0;i<MAX_GPS_TIME_POINTS/2;i++) {
278  fAdu5ASatTimeArray[i]=0.5*(fAdu5ASatTimeArray[2*i]+fAdu5ASatTimeArray[(2*i)+1]);
279  for(int ant=0;ant<4;ant++) {
280  fAdu5ASatNumArray[ant][i]=0.5*(fAdu5ASatNumArray[ant][2*i]+fAdu5ASatNumArray[ant][(2*i)+1]);
281  }
282  fNumAdu5ASat++;
283  }
284  }
285  fAdu5ASatTimeArray[fNumAdu5ASat]=adu5ASatPtr->realTime;
286  for(int ant=0;ant<4;ant++) {
287  fAdu5ASatNumArray[ant][fNumAdu5ASat]=adu5ASatPtr->numSats[ant];
288  }
289  fNumAdu5ASat++;
290  }
291  if(adu5BSatPtr) {
292  if(fNumAdu5BSat>=MAX_GPS_TIME_POINTS) {
293  //We'll average everythin
294  fNumAdu5BSat=0;
295  for(int i=0;i<MAX_GPS_TIME_POINTS/2;i++) {
296  fAdu5BSatTimeArray[i]=0.5*(fAdu5BSatTimeArray[2*i]+fAdu5BSatTimeArray[(2*i)+1]);
297  for(int ant=0;ant<4;ant++) {
298  fAdu5BSatNumArray[ant][i]=0.5*(fAdu5BSatNumArray[ant][2*i]+fAdu5BSatNumArray[ant][(2*i)+1]);
299  }
300  fNumAdu5BSat++;
301  }
302  }
303  fAdu5BSatTimeArray[fNumAdu5BSat]=adu5BSatPtr->realTime;
304  for(int ant=0;ant<4;ant++) {
305  fAdu5BSatNumArray[ant][fNumAdu5BSat]=adu5BSatPtr->numSats[ant];
306  }
307  fNumAdu5BSat++;
308  }
309 
310  if(g12SatPtr) {
311  if(fNumG12Sat>=MAX_GPS_TIME_POINTS) {
312  //We'll average everythin
313  fNumG12Sat=0;
314  for(int i=0;i<MAX_GPS_TIME_POINTS/2;i++) {
315  fG12SatTimeArray[i]=0.5*(fG12SatTimeArray[2*i]+fG12SatTimeArray[(2*i)+1]);
316  fG12SatNumArray[i]=0.5*(fG12SatNumArray[2*i]+fG12SatNumArray[(2*i)+1]);
317  }
318  fNumG12Sat++;
319  }
320  // std::cout << g12SatPtr->realTime << "\n";
321  fG12SatTimeArray[fNumG12Sat]=g12SatPtr->realTime;
322  fG12SatNumArray[fNumG12Sat]=g12SatPtr->numSats;
323  fNumG12Sat++;
324  }
325 
326 
327 }
328 
329 #define xSize 750
330 #define ySize 625
331 
332 void AnitaGpsCanvasMaker::getMapView(Adu5Pat *adu5PatPtr, TPad *plotPad)
333 {
334  // std::cout << "fReloadMap " << fReloadMap << "\n";
335  if(fReloadMap) {
336  char mapFileName[FILENAME_MAX];
337  plotPad->Clear();
338  // plotPad->SetCanvasSize(750,625);
339  plotPad->SetTopMargin(0);
340  plotPad->SetBottomMargin(0);
341  plotPad->SetLeftMargin(0);
342  plotPad->SetRightMargin(0);
343  char *installDir=getenv("ANITA_UTIL_INSTALL_DIR");
344  if(installDir) {
345  sprintf(mapFileName,"%s/share/anitaMap/antarcticaIceMap.png",installDir);
346  }
347  else {
348  sprintf(mapFileName,"antarcticaIceMap.png");
349  }
350  TImage *img = TImage::Open(mapFileName); //Will have to put this somewhere world visible at some point
351  if(!img) {
352  std::cout << "Couldn't create an image\n";
353  return;
354  }
355  img->SetConstRatio(kFALSE);
356  img->Draw("");
357  }
358  fReloadMap=0;
359  //For now just draw one point
360  float xBalloon,yBalloon;
361  float tempLong=adu5PatPtr->longitude;
362  static int wentNegative=0;
363  static int loop=0;
364  static int lastChange=0;
365  static float lastLong=0;
366  static int point=0;
367 
368  // tempLong-=7;
369  // if(tempLong<-180) tempLong+=360;
370 
371  getRelXYFromLatLong(adu5PatPtr->latitude,tempLong,xBalloon,yBalloon);
372 
373  if(tempLong<0) wentNegative=1;
374 
375  if(lastLong>167 || lastLong<0) {
376 
377  if(tempLong<167 && tempLong>0) {
378  if(point>lastChange+10) {
379  if(wentNegative) {
380  loop++;
381  lastChange=point;
382  wentNegative=0;
383  }
384  }
385  }
386  }
387  lastLong=tempLong;
388  int gpsPointerColours[5]={6,2,1,49,46};
389 
390  TMarker *mark = new TMarker();//375./xSize,312.5./ySize,21);
391  if(loop>=0 && loop<=4)
392  mark->SetMarkerColor(gpsPointerColours[loop]);
393  mark->SetMarkerStyle(26);
394  mark->SetMarkerSize(1);
395  mark->DrawMarker(xBalloon,yBalloon);
396 
397  lastLong=tempLong;
398  point++;
399 }
400 
401 
402 
403 const float TrueScaleLat=71;
404 const float CentralMeridian=0;
405 const float RadiusOfEarth=6378.1e3; //Metres
406 const float xOffest=375;
407 const float yOffset=312.5;
408 const float scale=271.5/2.19496e+06;
409 
410 void AnitaGpsCanvasMaker::getXYFromLatLong(double latitude, double longitude,
411  double &x, double &y)
412 {
413  //Negative longitude is west
414  // //All latitudes assumed south
415  double absLat=TMath::Abs(latitude);
416  double r=RadiusOfEarth*TMath::Cos((90.-TrueScaleLat)*TMath::DegToRad())*TMath::Tan((90-absLat)*TMath::DegToRad());
417  y=r*TMath::Cos(longitude*TMath::DegToRad());
418  x=r*TMath::Sin(longitude*TMath::DegToRad());
419 }
420 
421 
422 //const float xSize=750;
423 //const float ySize=625;
424 
425 void AnitaGpsCanvasMaker::getRelXYFromLatLong(float latitude, float longitude,
426  float &x, float &y)
427 {
428  //Negative longitude is west
429  // //All latitudes assumed south
430  float absLat=TMath::Abs(latitude);
431  float r=RadiusOfEarth*TMath::Cos((90.-TrueScaleLat)*TMath::DegToRad())*TMath::Tan((90-absLat)*TMath::DegToRad());
432  y=r*TMath::Cos(longitude*TMath::DegToRad());
433  x=r*TMath::Sin(longitude*TMath::DegToRad());
434 
435  y*=scale;
436  y+=yOffset;
437  y/=ySize;
438  x*=scale;
439  x+=xOffest;
440  x/=xSize;
441 
442 }
~AnitaGpsCanvasMaker()
Destructor.
Adu5Pat – The ADU5 Position and Attitude Data.
Definition: Adu5Pat.h:26
Adu5Sat – The ADU5 Satellite Information.
Definition: Adu5Sat.h:23
TPad * getGpsMainCanvas(Adu5Sat *adu5SatAPtr, Adu5Sat *adu5SatBPtr, G12Sat *g12SatPtr, Adu5Pat *adu5PatAPtr, Adu5Pat *adu5PatBPtr, G12Pos *g12PosPtr, TPad *useCan=0)
Returns the main GPS canvas.
UInt_t numSats
Number of visible satellites.
Definition: G12Pos.h:38
Int_t run
Run number assigned offline.
Definition: G12Pos.h:33
Float_t latitude
In degrees.
Definition: Adu5Pat.h:42
UInt_t numSats
Number of satellites visible.
Definition: G12Sat.h:37
void getCirclePlot(TPad *padSat, const char *title=0)
Creates a 2D elevation-azimuth plot.
Definition: Adu5Sat.cxx:54
G12Pos – The position data from the G12.
Definition: G12Pos.h:23
static AnitaGpsCanvasMaker * Instance()
Instance generator.
Float_t longitude
In degrees.
Definition: Adu5Pat.h:43
G12Sat – The satellite data from the G12.
Definition: G12Sat.h:24
UInt_t realTime
Time from the GPS unit.
Definition: Adu5Pat.h:37
UInt_t realTime
Time in unixtime from CPU.
Definition: G12Pos.h:34
AnitaGpsCanvasMaker()
Constructor.
UInt_t numSats[4]
The number of satellites each of the antennas can see.
Definition: Adu5Sat.h:36
UInt_t realTime
Time in unixtime.
Definition: G12Sat.h:35
Float_t latitude
Latitude in degrees.
Definition: G12Pos.h:39
Float_t altitude
In metres.
Definition: Adu5Pat.h:44
void getCirclePlot(TPad *padSat)
Fills the TPad with a 2D azimuth-elevation plot.
Definition: G12Sat.cxx:53
enum MagicDisplayGpsDisplay::EMagicDisplayGpsDisplay MagicDisplayGpsDisplay_t
Enumeration for plot style.
Float_t longitude
Longitude in degrees.
Definition: G12Pos.h:40
Float_t altitude
Altitude in metres.
Definition: G12Pos.h:41
The GPS canvas maker.
TPad * getGpsInfoCanvas(Adu5Sat *adu5SatAPtr, Adu5Sat *adu5SatBPtr, G12Sat *g12SatPtr, Adu5Pat *adu5PatAPtr, Adu5Pat *adu5PatBPtr, G12Pos *g12PosPtr, TPad *useCan)
Returns the GPS info canvas, the canvas can be provided in useCan
static AnitaGpsCanvasMaker * fgInstance
Protect against multiple instances.