-
Notifications
You must be signed in to change notification settings - Fork 0
/
preProcessSimple.C
432 lines (398 loc) · 15.1 KB
/
preProcessSimple.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
// Copyright (C) 2006,2007,2008,2009, George Hobbs, Russel Edwards
/*
* This file is part of TEMPO2.
*
* TEMPO2 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.
* TEMPO2 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 TEMPO2. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* If you use TEMPO2 then please acknowledge it by citing
* Hobbs, Edwards & Manchester (2006) MNRAS, Vol 369, Issue 2,
* pp. 655-672 (bibtex: 2006MNRAS.369..655H)
* or Edwards, Hobbs & Manchester (2006) MNRAS, VOl 372, Issue 4,
* pp. 1549-1574 (bibtex: 2006MNRAS.372.1549E) when discussing the
* timing model.
*/
#include <stdio.h>
#include <stdlib.h>
#include "tempo2.h"
#include <math.h>
#include <string.h>
void preProcessSimple (pulsar *psr)
{
const char *CVS_verNum = "$Id$";
if (displayCVSversion == 1) CVSdisplayVersion("preProcessSimple.C","preProcessSimple()",CVS_verNum);
preProcessSimple1 (psr, 0, -1);
preProcessSimple2 (psr, 0, 0, 0, 0);
preProcessSimple3 (psr);
}
void preProcessSimple1 (pulsar *psr, int tempo1, double thelast)
{
logdbg("In preProcessSimple1");
ECLIPTIC_OBLIQUITY = ECLIPTIC_OBLIQUITY_VAL;
int i = 0;
/* Check whitening */
if (psr->param[param_wave_om].paramSet[0] == 1 && psr->param[param_wave_om].val[0] == 0.0) /* Set fundamental frequency */
{
longdouble first=-1,last=-1;
for (i=0;i<psr->nobs;i++)
{
if (psr->obsn[i].deleted==0 && first==-1) first=psr->obsn[i].sat;
if (psr->obsn[i].deleted==0 && last==-1) last=psr->obsn[i].sat;
if (psr->obsn[i].deleted==0 && psr->obsn[i].sat < first) first = psr->obsn[i].sat;
if (psr->obsn[i].deleted==0 && psr->obsn[i].sat > last) last = psr->obsn[i].sat;
}
psr->param[param_wave_om].val[0] = 2.0*M_PI/(last-first)/
(1.0+4.0/(double)(psr->nWhite));
fprintf(stderr, "%.3le\n", (double)psr->param[param_wave_om].val[0]);
}
/* Check whitening */
if (psr->param[param_wave_dm].paramSet[0] == 1 && psr->param[param_wave_dm].val[0] == 0.0) /* Set fundamental frequency */
{
longdouble first=-1,last=-1;
for (i=0;i<psr->nobs;i++)
{
if (psr->obsn[i].deleted==0 && first==-1) first=psr->obsn[i].sat;
if (psr->obsn[i].deleted==0 && last==-1) last=psr->obsn[i].sat;
if (psr->obsn[i].deleted==0 && psr->obsn[i].sat < first) first = psr->obsn[i].sat;
if (psr->obsn[i].deleted==0 && psr->obsn[i].sat > last) last = psr->obsn[i].sat;
}
psr->param[param_wave_dm].val[0] = 2.0*M_PI/(last-first)/
(1.0+4.0/(double)(psr->nWhite));
}
/* Set tempo emulation mode */
if (psr->param[param_ephver].paramSet[0]==1)
{
if (psr->param[param_ephver].val[0] < 5)
{
printf("************************************************* \n");
printf("Warning: you are running in tempo1 emulation mode \n");
printf("************************************************* \n");
tempo1 = 1;
}
else
tempo1 = 0;
}
else if (tempo1 == 1)
{
psr->param[param_ephver].paramSet[0]=1;
psr->param[param_ephver].val[0]=2;
}
else
{
psr->param[param_ephver].paramSet[0]=1;
psr->param[param_ephver].val[0]=5;
}
if (thelast>0)
/* Define the start parameter based upon the last observation */
{
psr->param[param_start].val[0] = psr->obsn[psr->nobs-1].sat-thelast;
psr->param[param_start].fitFlag[0] = 1;
psr->param[param_start].paramSet[0] = 1;
}
psr->tempo1 = 0;
if (tempo1 == 1)
{
psr->tempo1 = 1;
psr->units = TDB_UNITS;
psr->timeEphemeris = FB90_TIMEEPH;
psr->dilateFreq = 0;
psr->planetShapiro=0;
psr->t2cMethod = T2C_TEMPO;
psr->correctTroposphere = 0;
psr->ne_sw = 9.961;
ECLIPTIC_OBLIQUITY = 84381.412;
}
/* XXXX Hack!! -- removed - should use transform plugin*/
/* Problem, if a function is not called then the compiler does not include
* it in the library - so the plugin cannot find it */
if (psr->units==100) /* SI_UNITS) */
transform_units(psr, TDB_UNITS, SI_UNITS);
}
void preProcessSimple2 (pulsar *psr,
float startdmmjd, int ndm, float* dmvals, int trimonly)
{
logdbg("In preProcessSimple2");
int i=0, j=0, k=0;
for (i=0;i<psr->nobs;i++)
{
//psr->obsn[i].efac = 1.0;
//psr->obsn[i].equad = 0.0;
/* Check whether any error = 0 */
if (psr->fitMode==1 && psr->obsn[i].toaErr==0)
{
printf("Error: the uncertainty for TOA %d (MJD %f) is equal to zero\n ",i+1,(double)psr->obsn[i].sat);
exit(1);
}
for (k=0; k<6; k++) /* zero these vectors for first call to tt2tb */
{
psr->obsn[i].earthMoonBary_ssb[k] = 0.0;
psr->obsn[i].earthMoonBary_earth[k] = 0.0;
psr->obsn[i].observatory_earth[k] = 0.0;
psr->obsn[i].earth_ssb[k] = 0.0;
}
// Correctly taking note of the phase offset
psr->obsn[i].sat += ((psr->obsn[i].phaseOffset/psr->param[param_f].val[0])/SECDAY);
for (k=0;k<psr->nToffset;k++) /* Calculate time offsets */
{
char offsetSite[256], obsSite[256];
lookup_observatory_alias(psr->tOffsetSite[k], offsetSite);
lookup_observatory_alias(psr->obsn[i].telID, obsSite);
if ((psr->tOffset_f1[k]==0.0 || (psr->obsn[i].freq > psr->tOffset_f1[k])) &&
(psr->tOffset_f2[k]==0.0 || (psr->obsn[i].freq < psr->tOffset_f2[k])) &&
(psr->tOffset_t1[k]==0.0 || (psr->obsn[i].sat > psr->tOffset_t1[k])) &&
(psr->tOffset_t2[k]==0.0 || (psr->obsn[i].sat < psr->tOffset_t2[k])) &&
(strcmp(offsetSite,"0")==0 ||
strcmp(offsetSite,obsSite)==0))
{
int use=1;
/* Check for flags */
if (strlen(psr->tOffsetFlags[k])>0)
{
char *myStr,str1[1000],flagID[100];
use=0;
strcpy(str1,psr->tOffsetFlags[k]);
myStr = strtok(str1," ");
do {
if (myStr[0]=='-' && (myStr[1]<48 || myStr[1]>57)) /* Look for flag */
{
strcpy(flagID,myStr);
myStr = strtok(NULL," ");
for (int l=0;l<psr->obsn[i].nFlags;l++)
{
if (strcmp(flagID,psr->obsn[i].flagID[l])==0
&& strcmp(myStr,psr->obsn[i].flagVal[l])==0)
{
use=1;
break;
}
}
}
} while ((myStr = strtok(NULL," "))!=NULL);
}
if (use==1) {psr->obsn[i].sat += psr->tOffset[k]/SECDAY;}
}
}
/* Check filtering */
if (strlen(psr->filterStr)>0)
{
char flag[100],filtS[1000],*filt;
strcpy(filtS,psr->filterStr);
filt = strtok(filtS," ");
strcpy(flag,filt);
while (filt != NULL)
{
if (filt[0]=='-') strcpy(flag,filt);
else
{
for (j=0;j<psr->obsn[i].nFlags;j++)
{
if (strcmp(psr->obsn[i].flagID[j],flag)==0 &&
strcmp(psr->obsn[i].flagVal[j],filt)==0)
psr->obsn[i].deleted=1;
}
}
filt = strtok(NULL," ");
}
}
/* Check filtering for 'pass' */
if (strlen(psr->passStr)>0)
{
char flag[100],filtS[1000],*filt;
int found;
strcpy(filtS,psr->passStr);
filt = strtok(filtS," ");
found=0;
while ( filt != NULL && found==0)
{
if (filt[0]=='-') // Have a flag
strcpy(flag,filt);
else
{
for (j=0;j<psr->obsn[i].nFlags;j++)
{
if (strcmp(psr->obsn[i].flagID[j],flag)==0 &&
strcmp(psr->obsn[i].flagVal[j],filt)==0)
{
found=1;
break;
}
}
}
filt = strtok(NULL," ");
}
if (found==0)
psr->obsn[i].deleted=1;
}
/* CHECK JUMPS */
for (k=1;k<=psr->nJumps;k++)
{
int yes=0;
char str1[100],str2[100],str3[100],str4[100],str5[100];
double val1, val2;
/* Must parse jump string to determine whether this observation should jump or not */
int nread = sscanf(psr->jumpStr[k],"%s %s %s",str1,str2,str3);
if (strcasecmp(str1,"MJD")==0 || strcasecmp(str1,"FREQ")==0)
{
sscanf(str2,"%lf",&val1);
sscanf(str3,"%lf",&val2);
if (strcasecmp(str1,"MJD")==0) {
if (psr->obsn[i].sat >= val1 && psr->obsn[i].sat < val2) {
yes=1;
}
}
else if (strcasecmp(str1,"FREQ")==0) {
if (psr->obsn[i].freq >= val1 && psr->obsn[i].freq < val2) {
yes=1;
}
}
}
if (strcasecmp(str1,"NAME")==0 && strstr(psr->obsn[i].fname,str2)!=NULL)
yes=1;
if (strcasecmp(str1,"TEL")==0)
{
char selectedSite[256], obsSite[256];
lookup_observatory_alias(str2, selectedSite);
lookup_observatory_alias(psr->obsn[i].telID, obsSite);
if (strcasecmp(selectedSite, obsSite)!=0)
yes=1;
}
else if (str1[0]=='-')
{
int jj;
for (jj=0;jj<psr->obsn[i].nFlags;jj++)
{
if (strcmp(psr->obsn[i].flagID[jj],str1)==0 &&
strcmp(psr->obsn[i].flagVal[jj],str2)==0)
yes=1;
}
}
if (yes==1) {psr->obsn[i].jump[psr->obsn[i].obsNjump]=k; (psr->obsn[i].obsNjump)++;}
}
// Check for time offset flags
for (k=0;k<psr->obsn[i].nFlags;k++)
{
if (strcmp(psr->obsn[i].flagID[k],"-to")==0)
{
longdouble v;
v = parse_longdouble(psr->obsn[i].flagVal[k]);
psr->obsn[i].sat += v/SECDAY;
}
}
// Check for dm updates
if (startdmmjd && ndm && dmvals)
{
//float dm; //not needed any more
if ((int)(psr->obsn[i].sat-startdmmjd+0.5) > 0)
{
if ((int)(psr->obsn[i].sat-startdmmjd+0.5) >= ndm)
psr->obsn[i].deleted=1;
else if (trimonly == 0) // set flags only if really correcting for dm, not only trimming the data
{
strcpy(psr->obsn[i].flagID[psr->obsn[i].nFlags],"-dm");
sprintf(psr->obsn[i].flagVal[psr->obsn[i].nFlags],"%g",dmvals[(int)(psr->obsn[i].sat-startdmmjd+0.5)]);
psr->obsn[i].nFlags++;
if (psr->obsn[i].nFlags >= MAX_FLAGS)
{
printf("Number of different flags in the .tim file > MAX_FLAGS (%d)\n",MAX_FLAGS);
exit(1);
}
//dm = dmvals[(int)(psr->obsn[i].sat-startdmmjd+0.5)];
}
}
else
{
psr->obsn[i].deleted=1;
}
}
}
logdbg("Complete preProcessSimple2");
}
void preProcessSimple3 (pulsar *psr)
{
int i,j,k,l;
logdbg("Start preProcessSimple3");
//
// Check fjump
//
if (strlen(psr->fjumpID)>0)
{
char val[MAX_JUMPS][16];
int nval[MAX_JUMPS];
int nf=0;
int found;
// Find flag corresponding to longest data set
for (i=0;i<psr->nobs;i++)
{
if (psr->obsn[i].deleted==0)
{
for (k=0;k<psr->obsn[i].nFlags;k++)
{
if (strcmp(psr->obsn[i].flagID[k],psr->fjumpID)==0)
{
found=0;
for (l=0;l<nf;l++)
{
if (strcmp(psr->obsn[i].flagVal[k],val[l])==0)
{
found=1;
break;
}
}
if (found==0)
{
strcpy(val[nf],psr->obsn[i].flagVal[k]);
nf++;
if (nf >= MAX_JUMPS)
{
printf("preProcessSimple3: Number of different flags in the .tim file > MAX_JUMPS (%d)\n",MAX_JUMPS);
exit(1);
}
}
}
}
}
}
// Now create jumps
for (l=1;l<nf;l++)
{
psr->nJumps++;
sprintf(psr->jumpStr[psr->nJumps],"%s %s",psr->fjumpID,val[l]);
for (i=0;i<psr->nobs;i++)
{
for (k=0;k<psr->obsn[i].nFlags;k++)
{
if (strcmp(psr->obsn[i].flagVal[k],val[l])==0)
psr->obsn[i].jump[(psr->obsn[i].obsNjump)++]=psr->nJumps;
}
}
psr->fitJump[psr->nJumps]=1;
}
}
// Now link phase jumps to a particular site-arrival-time
for (i=0;i<psr->nPhaseJump;i++)
{
if ((psr->phaseJumpID[i] = -1))
{
// Find closest TOA
if (i==0) printf("WARNING: Use of phase jumps => .tim file must be sorted in time order\n");
for (k=1;k<psr->nobs;k++)
{
if ((double)psr->obsn[k].sat > (double)psr->phaseJump[i])
{
psr->phaseJumpID[i]=k-1;
break;
}
}
}
}
logdbg("Complete preProcessSimple3");
}