~pconv-team/polconvertsd/trunk-1

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
/* WEIGHTER - antenna weighting/flagging interface to PolConvert

             Copyright (C) 2015  Ivan Marti-Vidal
             Nordic Node of EU ALMA Regional Center (Onsala, Sweden)
             Max-Planck-Institut fuer Radioastronomie (Bonn, Germany)
  
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
  
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
  
You should have received a copy of the GNU General Public License   
along with this program.  If not, see <http://www.gnu.org/licenses/>
  
*/



#include <sys/types.h>
#include <math.h>
#include <iostream>  
#include <fstream>
#include <cstring>
#include <complex.h>
#include "./Weighter.h"


Weighter::~Weighter(){};

Weighter::Weighter(int nPhase, long *nASDMtimes, long nASDMentries, int *ASDMant, double **ASDMtimes, int *refants, double *time0, double *time1, double* BadTimes, int NBadTimes, FILE *logF) {


 logFile = logF;

 nants = nPhase;
 nASDMEntries = nASDMentries; 
 ntimes = nASDMtimes; 
 ants = ASDMant; 
 JDtimes = ASDMtimes;
 refAnts = refants;
 Time0 = time0;
 Time1 = time1;
 currTime = 0.0;
 currRefAnt = 0;
 badTimes = BadTimes;
 NbadTimes = NBadTimes;

};


/* Returns whether ALMA was effectively phased at this time */
bool Weighter::isPhased(double JDTime){

  int i;
  bool Phased = true;

  for (i=0; i<NbadTimes; i++){
    if(JDTime>=badTimes[2*i] && JDTime<=badTimes[2*i+1]){
      sprintf(message,"Bad time %i: %.8f |  %.8f %.8f!\n",i,JDTime,badTimes[2*i],badTimes[2*i+1]);
      fprintf(logFile,"%s",message);  std::cout<<message; fflush(logFile);
      Phased=false;break;
    };
  };

  return Phased;
};



/* Returns whether the antenna is in the phased sum (true) or not (false) */
bool Weighter::getWeight(int iant, double JDtime){

  long i;
  int j;

  bool inside;

  for (j=0; j<nants; j++){
    if (ants[j] == iant){break;};
  };

  inside = false;

  if(j<nants){
    for (i=0; i<ntimes[j]; i++){
      if (JDtimes[j][2*i]<=JDtime && JDtimes[j][2*i+1]>=JDtime){inside = true; break;};   
    };
  };

//  if(!inside){
//    sprintf(message,"T: %.5f ; ANT: %i ; %i\n",JDtime,iant,inside);
//    fprintf(logFile,"%s",message);fflush(logFile);
//  };

  return inside;

};



// Returns the index of the reference ALMA antenna at a given time.
// Returns -1 if no valid time range is used.
int Weighter::getRefAnt(double JDtime){

  long i;


  if (JDtime == currTime){return currRefAnt;};

  for (i=0; i<nASDMEntries; i++){
    if (Time0[i]<=JDtime && Time1[i]>=JDtime){
      currTime = JDtime; currRefAnt = refAnts[i];
      return currRefAnt;
    };   
  };

// If no refant is found, no X-Y offset will be applied:
  return -1;
};