RTModel 2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1398 @@
1
+ // InitCond.cpp : main project file.
2
+ // This program finds the peaks in the data and sets initial conditions for fitting
3
+
4
+ #define _CRT_SECURE_NO_WARNINGS
5
+ #define _USE_MATH_DEFINES
6
+ #include <stdio.h>
7
+ #include <string.h>
8
+ #include <math.h>
9
+ #include <malloc.h>
10
+ #include <regex>
11
+ #include <filesystem>
12
+
13
+ using namespace std;
14
+ using namespace std::filesystem;
15
+
16
+ #ifdef _WIN32
17
+ char systemslash = '\\';
18
+ #else
19
+ char systemslash = '/';
20
+ #endif
21
+
22
+ // Main global parameters
23
+ int nobspeaks=2; // Number of peaks in the observed light curve to be considered for setting initial conditions.
24
+ double sigmathr=5.; // Number of sigmas for spline approximation
25
+ double peakthr = 10.; // Number of sigmnas necessary for a deviation to be identified as a maximum or a minimum.
26
+ int maxoldmodels = 4; // Maximum number of old models to include in new run as initial conditions
27
+ bool override = false; // Override peak identification and manually set peak times
28
+ bool nostatic = false; // No static models will be calculated.
29
+ bool noparallax = false; // Only orbital motion models will be calculated.
30
+ int usesatellite = 0; // Satellite to be used for initial conditions. Ground telescopes by default.
31
+
32
+ // Structure datapoint: stores time (t), flux (y), error (yerr), significance (sig), time range for the uncertainty (tl-tr)
33
+ struct datapoint{
34
+ datapoint *prev,*next;
35
+ int i;
36
+ double t,y,tl,tr,yerr,sig;
37
+ };
38
+
39
+ // Structure dataset: a list of datapoints with obvious functions
40
+ struct dataset{
41
+ dataset *prev,*next;
42
+ datapoint *first,*last;
43
+ int length;
44
+
45
+ dataset();
46
+ ~dataset();
47
+ datapoint *addpoint(int,double,double,double);
48
+ void addpoint(double *,double *,double *,int,int,int);
49
+ void remove(datapoint *);
50
+ void clone(datapoint *);
51
+ };
52
+
53
+ int main(int argc, char *argv[])
54
+ {
55
+ char eventname[256] = "";
56
+ regex filebest;
57
+ char fileinit[256]="";
58
+ char command[256], buffer[256];
59
+ double value,value2;
60
+ double tv,yv,errv,t0,tE,t1,t2,tasy,mean,sw;
61
+ double *t,*y,*err,*tt,*yy,*eerr;
62
+ double maxdev, curdev, asydev=0,w1,w2;
63
+ FILE *f;
64
+ int flag,np,nps,npc,*nnp,nfil,ifil,dn,satellite;
65
+ double fint;
66
+ dataset **peaklist, *cpeaks, *newpeaks;
67
+ datapoint *p,*pm,*pmm,*pl,*pr,*pasy=0, *highestpeak, *minimum, *startsection, *endsection, *sectionpeak;
68
+
69
+
70
+ // Directory preliminaries. Reads event name from arguments.
71
+
72
+ printf("******************************************\n");
73
+ printf("************* InitCond **********\n");
74
+ printf("******************************************\n\n\n");
75
+ printf("***This program finds peaks and sets initial conditions for fitting\n\n");
76
+
77
+ auto exedir = current_path();
78
+
79
+ if (argc > 1) {
80
+ strcpy(eventname, argv[1]);
81
+ }
82
+ else {
83
+ printf("\n\nEvent name? ");
84
+ scanf("%s", eventname);
85
+ //sprintf(eventname, "WDC10193");
86
+ }
87
+
88
+ printf("\n\n- Event: %s\n", eventname);
89
+
90
+ current_path(eventname);
91
+
92
+
93
+ if (exists("InitCond")) {
94
+ current_path("InitCond");
95
+ auto searchstring = regex("Init.*");
96
+ for (auto const& itr : directory_iterator(".")) {
97
+ string curfile = (itr).path().filename().string();
98
+ if (regex_match(curfile, searchstring)) {
99
+ printf("\n\nInitial conditions already selected");
100
+ return 0;
101
+ }
102
+ itr;
103
+ }
104
+ }
105
+ current_path(eventname);
106
+
107
+ // Look for previous runs to include previously found best models
108
+
109
+ auto searchstring = regex("run.*");
110
+ int lastrun = -1;
111
+ string runstring = "";
112
+ for (auto const& itr : directory_iterator(".")) {
113
+ string curfile = (itr).path().filename().string();
114
+ if (regex_match(curfile, searchstring)) {
115
+ int currun = atoi(curfile.c_str() + 4);
116
+ if (currun > lastrun) {
117
+ lastrun = currun;
118
+ runstring = curfile;
119
+ }
120
+ }
121
+ }
122
+
123
+ // Reading InitCond.ini and set parameters accordingly
124
+
125
+ if (exists("ini")) {
126
+ current_path("ini");
127
+ f = fopen("InitCond.ini", "r");
128
+ if (f != 0) {
129
+ printf("\n\n- Reading options in InitCond.ini");
130
+ while (!feof(f)) {
131
+ int red = fscanf(f, "%s %s %lf", command, buffer, &value);
132
+ if (red < 1) {
133
+ command[0] = 0;
134
+ //if (red != 0) {
135
+ // printf("\n\n!!! Bad command in Reader.ini");
136
+ // return -1;
137
+ //};
138
+ }
139
+ if (strcmp(command, "override") == 0) {
140
+ fscanf(f, " %lf", &value2);
141
+ override = true;
142
+ t1 = value;
143
+ t2 = value2;
144
+ }
145
+ if (strcmp(command, "npeaks") == 0) {
146
+ nobspeaks = (int) value;
147
+ }
148
+ if (strcmp(command, "peakthreshold") == 0) {
149
+ peakthr = value;
150
+ }
151
+ if (strcmp(command, "oldmodels") == 0) {
152
+ maxoldmodels = (int) value;
153
+ }
154
+ if (strcmp(command, "nostatic") == 0) {
155
+ nostatic = true;
156
+ }
157
+ if (strcmp(command, "noparallax") == 0) {
158
+ noparallax = true;
159
+ nostatic = true;
160
+ }
161
+ if (strcmp(command, "usesatellite") == 0) {
162
+ usesatellite = (int) value;
163
+ }
164
+ }
165
+ fclose(f);
166
+ }
167
+ else {
168
+ printf("\n\n- Default options:");
169
+ }
170
+ current_path(eventname);
171
+ }
172
+
173
+ printf("\nNumber of peaks to be considered in light curve %d", nobspeaks);
174
+ printf("\nThreshold for peak identification %lf", peakthr);
175
+ sigmathr = peakthr * 0.5;
176
+ printf("\nOld models to be included in next run %d", maxoldmodels);
177
+ if(noparallax) printf("\nOnly orbital motion models will be initialized");
178
+ if (nostatic && !noparallax) printf("\nStatic models will not be initialized");
179
+
180
+ // Check for Override. If you do not like the initial conditions found by this algorithm,
181
+ // you may optopnally override them by writing the times of two peaks in InitCond.ini.
182
+
183
+ if(override){
184
+ // Use hand-made initial conditions if desired.
185
+ printf("\n- Overriding peak identification -\n");
186
+ newpeaks = new dataset;
187
+ pl = new datapoint;
188
+ pl->t = t1;
189
+ pr = new datapoint;
190
+ pr->t = t2;
191
+ pl->next = pr;
192
+ pl->prev = 0;
193
+ pr->prev=pl;
194
+ pr->next = 0;
195
+ newpeaks->first=pl;
196
+ newpeaks->length = 2;
197
+ newpeaks->last = pr;
198
+ }
199
+ else { // Normal flow if override is not used
200
+
201
+ /* Reading the curve to fit */
202
+
203
+ printf("\n\n- Reading data \n");
204
+
205
+ f = fopen("LCToFit.txt", "r");
206
+ fscanf(f, "%d", &np);
207
+
208
+ nfil = 1;
209
+ dn = 0;
210
+ for (int i = 0; i < np; i++) {
211
+ fscanf(f, "%d %lf %lf %lf %d", &(ifil), &(tv), &(yv), &(errv), &(satellite));
212
+ if (satellite != usesatellite) {
213
+ np--;
214
+ i--;
215
+ }
216
+ else {
217
+ if ((i != 0) && (ifil != dn)) {
218
+ nfil++;
219
+ }
220
+ }
221
+ dn = ifil;
222
+ }
223
+ fclose(f);
224
+ np++;
225
+
226
+ tt = (double*)malloc(sizeof(double) * np);
227
+ yy = (double*)malloc(sizeof(double) * np);
228
+ eerr = (double*)malloc(sizeof(double) * np);
229
+ nnp = (int*)calloc(sizeof(int), nfil);
230
+ peaklist = (dataset**)malloc(sizeof(dataset*) * nfil);
231
+
232
+ f = fopen("LCToFit.txt", "r");
233
+ fscanf(f, "%d", &np);
234
+
235
+ nfil = 0;
236
+ dn = 0;
237
+ for (int i = 0; i < np; i++) {
238
+ fscanf(f, "%d %lf %lf %lf %d", &(ifil), &(tt[i]), &(yy[i]), &(eerr[i]), &(satellite));
239
+ if (satellite > 0) {
240
+ np--;
241
+ i--;
242
+ }
243
+ else {
244
+ if ((i != 0) && (ifil != dn)) {
245
+ nfil++;
246
+ }
247
+ nnp[nfil]++;
248
+ }
249
+ dn = ifil;
250
+ }
251
+ nfil++;
252
+ fclose(f);
253
+
254
+ // End of data reading. Here is the relevant information:
255
+ // nfil = number of datasets (one per telescope and filter)
256
+ // nnp[nfil] = number of points in each dataset
257
+ // np = total number of points
258
+ // tt[np] = array of all times of observations. Data are sorted by dataset and then by time.
259
+ // yy[np] = Corresponding fluxes
260
+ // eerr[np] = Corresponding errors
261
+ // peaklist[nfil] will contain the list of peaks for each dataset.
262
+
263
+ // tasy = time of more relevant asymmetry (to be calculated later)
264
+ // asydev = relevance of the asymmetry
265
+ fint = -1.e100;
266
+ tasy = 0;
267
+ asydev = 0;
268
+ f = fopen("spline.txt", "w");
269
+ fclose(f);
270
+ ///////////////////////////////////
271
+ // Here we start the main algorithm
272
+ printf("\n-- Processing\n");
273
+
274
+ // Cycle on each dataset
275
+ for (int ic = 0; ic < nfil; ic++) {
276
+ printf("\n- Dataset: %d", ic);
277
+ // Find starting point in the arrays for this dataset
278
+ npc = 0;
279
+ for (int i = 0; i < ic; i++) {
280
+ npc += nnp[i];
281
+ }
282
+ // t, y and err point to the first point in the dataset and will be used henceforth
283
+ t = &(tt[npc]);
284
+ y = &(yy[npc]);
285
+ err = &(eerr[npc]);
286
+ npc = nnp[ic]; // npc = Number of points in this dataset
287
+ fint = (t[npc - 1] > fint) ? t[npc - 1] : fint; // fint = Last observation time, useful for setting initial conditions on incomplete events
288
+
289
+ //// This block is used to skip all data points in the previous years for the analysis.
290
+ // // It is useful to avoid picking spurious peaks in the baseline of previous years.
291
+ // // Feel free to remove it or elaborate on this (e.g. by selecting only the peak season)
292
+ // dn = 0;
293
+ // while (t[dn]<5197 + (yr - 2010)*365.25) dn++;
294
+ // t = &(t[dn]);
295
+ // y = &(y[dn]);
296
+ // err = &(err[dn]);
297
+ // npc -= dn;
298
+
299
+ // printf("\n+ Smoothing data\n");
300
+ //// Each data point is replaced by the weighted mean with the previous and following points.
301
+ // dept1=t[0];
302
+ // depy1=y[0];
303
+ // deperr1=err[0];
304
+ // sw = mean=0;
305
+ // for(int i=1;i<=npc-2;i++){
306
+ // dept2=t[i];
307
+ // depy2=y[i];
308
+ // deperr2=err[i];
309
+ // err[i]=pow(1/(deperr1*deperr1)+1/(deperr2*deperr2)+1/(err[i+1]*err[i+1]),-0.5);
310
+ // y[i]=(depy1/(deperr1*deperr1)+depy2/(deperr2*deperr2)+y[i+1]/(err[i+1]*err[i+1]))*err[i]*err[i];
311
+ // t[i]=(dept1/(deperr1*deperr1)+dept2/(deperr2*deperr2)+t[i+1]/(err[i+1]*err[i+1]))*err[i]*err[i];
312
+ // dept1=dept2;
313
+ // depy1=depy2;
314
+ // deperr1=deperr2;
315
+ // sw += 1 / (deperr2*deperr2);
316
+ // mean += depy2 / (deperr2*deperr2);
317
+ // }
318
+ // mean /= sw; // mean = mean flux for this dataset.
319
+
320
+ // Calculation of the mean for the dataset
321
+
322
+ sw = mean = 0;
323
+ for (int i = 0; i <= npc - 1; i++) {
324
+ sw += 1 / (err[i] * err[i]);
325
+ mean += y[i] / (err[i] * err[i]);
326
+ }
327
+ mean /= sw;
328
+
329
+ // Spline approximation
330
+ printf("\n- Spline approximation");
331
+ peaklist[ic] = new dataset;
332
+ cpeaks = peaklist[ic];
333
+ cpeaks->addpoint(-1, t[0] - 1, mean, 1 / sqrt(sw));
334
+ cpeaks->addpoint(npc, t[npc - 1] + 1, mean, 1 / sqrt(sw));
335
+ // cpeaks = spline approximating the light curve. We start from a straight horizontal line at the mean flux.
336
+ int iter = 0, flag = 0, imax, jmax;
337
+ datapoint* curpeak;
338
+ // Cycle until maxdev becomes less than sigmathr or all points have been added.
339
+ while (iter < npc && (maxdev > sigmathr || flag < 2)) {
340
+ iter++;
341
+ imax = 0;
342
+ maxdev = 0;
343
+ curpeak = cpeaks->first;
344
+ // Cycle on all points in the dataset
345
+ for (int i = 1; i < npc - 1; i++) {
346
+ if (t[i] > curpeak->next->t) curpeak = curpeak->next;
347
+ // curdev = deviation of the current point from the spline
348
+ w1 = (t[i] - curpeak->t) / (curpeak->next->t - curpeak->t);
349
+ w2 = 1 - w1;
350
+ curdev = fabs((y[i] - curpeak->y - (curpeak->next->y - curpeak->y) * w1) / sqrt(err[i] * err[i] + curpeak->next->yerr * curpeak->next->yerr * w1 * w1 + curpeak->yerr * curpeak->yerr * w2 * w2));
351
+ if (curdev > maxdev) {
352
+ maxdev = curdev;
353
+ imax = i;
354
+ }
355
+ }
356
+ // point with maximum deviation is added to the spline
357
+ cpeaks->addpoint(imax, t[imax], y[imax], err[imax]);
358
+
359
+ imax = 10000;
360
+ jmax = 0;
361
+ // maxdev = 1.e100;
362
+ // Significance of each peak in the spline is updated
363
+ for (curpeak = cpeaks->first->next; curpeak->next; curpeak = curpeak->next) {
364
+ // curdev = deviation of current point in the spline from the spline without this point
365
+ w1 = (curpeak->t - curpeak->prev->t) / (curpeak->next->t - curpeak->prev->t);
366
+ w2 = 1 - w1;
367
+ curdev = curpeak->y - curpeak->prev->y - (curpeak->next->y - curpeak->prev->y) * w1;
368
+ // Only for positive deviations (concavities in the spline) we calculate the significance
369
+ if (curdev > 0) {
370
+ flag = (flag > jmax - imax) ? flag : jmax - imax;
371
+ imax = jmax;
372
+ curpeak->sig = curdev / sqrt(curpeak->yerr * curpeak->yerr + curpeak->next->yerr * curpeak->next->yerr * w1 * w1 + curpeak->prev->yerr * curpeak->prev->yerr * w2 * w2);
373
+ }
374
+ else {
375
+ curpeak->sig = 0;
376
+ }
377
+ jmax++;
378
+ }
379
+
380
+ }
381
+
382
+ // Print all points in the spline with their significance (0 for convex sections, positive for concave sections)
383
+ f = fopen("spline.txt", "a+");
384
+ for (curpeak = cpeaks->first; curpeak; curpeak = curpeak->next) {
385
+ printf("\n+ t: %lf y: %lg sig: %lf", curpeak->t, curpeak->y, curpeak->sig);
386
+ fprintf(f, "%lf %lg %lf\n", curpeak->t, curpeak->y, curpeak->sig);
387
+ }
388
+ fclose(f);
389
+
390
+
391
+ // Find highest peak
392
+ highestpeak = cpeaks->first->next;
393
+ for (curpeak = cpeaks->first->next; curpeak->next; curpeak = curpeak->next) {
394
+ if (curpeak->y > highestpeak->y) {
395
+ highestpeak = curpeak;
396
+ }
397
+ }
398
+ tv = highestpeak->t;
399
+ // Find global minimum
400
+ minimum = cpeaks->first->next;
401
+ for (curpeak = cpeaks->first->next; curpeak->next; curpeak = curpeak->next) {
402
+ if (curpeak->y < minimum->y) {
403
+ minimum = curpeak;
404
+ }
405
+ }
406
+ // Calculate prominence of highest peak with respect to global minimum of this dataset
407
+ highestpeak->sig = (highestpeak->y - minimum->y) / sqrt(highestpeak->yerr * highestpeak->yerr + minimum->yerr * minimum->yerr);
408
+ printf("\n- Highest peak\nt: %lf y: %lg sig: %lf", tv, highestpeak->y, highestpeak->sig);
409
+
410
+
411
+ // Store maximal asymmetry for later use
412
+ curpeak = cpeaks->last;
413
+ int it = 0;
414
+ for (it = 0; it < npc - 1 && 2 * tv - t[it] > curpeak->t; it++);
415
+ for (it = it; it < npc - 1; it++) {
416
+ t1 = 2 * tv - t[it];
417
+ while (t1 < curpeak->t && curpeak->prev) {
418
+ curpeak = curpeak->prev;
419
+ }
420
+ if (!curpeak->prev) break;
421
+ curdev = (y[it] - curpeak->y - (curpeak->next->y - curpeak->y) / (curpeak->next->t - curpeak->t) * (t1 - curpeak->t)) / err[it];
422
+ if (curdev > asydev) {
423
+ asydev = curdev;
424
+ tasy = t[it];
425
+ }
426
+ }
427
+ printf("\nasy: %lf %lf", tasy, asydev);
428
+
429
+ // Find all other peaks
430
+ curpeak = cpeaks->first->next;
431
+ while (curpeak && curpeak->sig <= 0) curpeak = curpeak->next; //Jump to next concave section
432
+ while (curpeak) {
433
+ //Find start and end of the concave section
434
+ startsection = curpeak->prev;
435
+ while (curpeak->next && curpeak->sig > 0) curpeak = curpeak->next;
436
+ endsection = curpeak;
437
+ // Find peak in this concave section
438
+ sectionpeak = 0;
439
+ curpeak = startsection->next;
440
+ while (sectionpeak == 0 && curpeak != endsection) {
441
+ if (curpeak->y > curpeak->prev->y && curpeak->y > curpeak->next->y) {
442
+ sectionpeak = curpeak;
443
+ }
444
+ else {
445
+ curpeak = curpeak->next;
446
+ }
447
+ }
448
+ // Find minimum between this peak and highest peak
449
+ if (sectionpeak != 0 && sectionpeak != highestpeak) {
450
+ if (sectionpeak->t < highestpeak->t) {
451
+ minimum = sectionpeak->next;
452
+ curpeak = sectionpeak->next;
453
+ while (curpeak != highestpeak) {
454
+ if (curpeak->y < minimum->y) minimum = curpeak;
455
+ curpeak = curpeak->next;
456
+ }
457
+ }
458
+ else {
459
+ minimum = sectionpeak->prev;
460
+ curpeak = sectionpeak->prev;
461
+ while (curpeak != highestpeak) {
462
+ if (curpeak->y < minimum->y) minimum = curpeak;
463
+ curpeak = curpeak->prev;
464
+ }
465
+ }
466
+ // Calculate prominence of this peak with respect to minimum with highest peak
467
+ sectionpeak->sig = (sectionpeak->y - minimum->y) / sqrt(sectionpeak->yerr * sectionpeak->yerr + minimum->yerr * minimum->yerr);
468
+ }
469
+ // Calculate alternative prominence with respect to ends of the concave section.
470
+ maxdev = 0;
471
+ if (startsection->prev == 0) startsection = startsection->next;
472
+ if (endsection->next == 0) endsection = endsection->prev;
473
+ curpeak = startsection->next;
474
+ while (curpeak != endsection) {
475
+ w1 = (curpeak->t - startsection->t) / (endsection->t - startsection->t);
476
+ w2 = 1 - w1;
477
+ curdev = curpeak->y - startsection->y - (endsection->y - startsection->y) * w1;
478
+ curdev /= sqrt(curpeak->yerr * curpeak->yerr + endsection->yerr * endsection->yerr * w1 * w1 + startsection->yerr * startsection->yerr * w2 * w2);
479
+ if (curdev > maxdev) {
480
+ if (sectionpeak == 0) sectionpeak = curpeak;
481
+ maxdev = curdev;
482
+ }
483
+ curpeak = curpeak->next;
484
+ }
485
+ // Choose higher prominence between the two calculations
486
+ if (sectionpeak && maxdev > sectionpeak->sig) sectionpeak->sig = maxdev;
487
+ if (sectionpeak == 0) {
488
+ startsection->sig = endsection->sig = 0;
489
+ }
490
+
491
+ // Remove other points in the same concave section of this peak
492
+ curpeak = startsection->next;
493
+ while (curpeak != endsection) {
494
+ curpeak = curpeak->next;
495
+ if (curpeak->prev != sectionpeak) cpeaks->remove(curpeak->prev);
496
+ }
497
+ /*
498
+ curpeak = sectionpeak->prev;
499
+ while (curpeak != startsection) {
500
+ curpeak = curpeak->prev;
501
+ cpeaks->remove(curpeak->next);
502
+ }
503
+ curpeak = sectionpeak->next;
504
+ while (curpeak->next && curpeak != endsection) {
505
+ curpeak = curpeak->next;
506
+ cpeaks->remove(curpeak->prev);
507
+ }*/
508
+ curpeak = endsection->next;
509
+ while (curpeak && curpeak->sig <= 0) curpeak = curpeak->next; //Jump to next concave section
510
+ }
511
+
512
+
513
+
514
+ //
515
+ //// Identify most representative point (peak) in concave sections and remove other points
516
+ // for (curpeak = cpeaks->first; curpeak; curpeak = curpeak->next) {
517
+ // // Every time we meet two consecutive points with sig>0, we save only one and remove the other
518
+ // if (curpeak->sig > 1.e-100 && curpeak->prev->sig > 1.e-100) {
519
+ // maxdev = curpeak->prev->y - curpeak->prev->prev->y - (curpeak->next->y - curpeak->prev->prev->y) / (curpeak->next->t - curpeak->prev->prev->t)*(curpeak->prev->t - curpeak->prev->prev->t);
520
+ // curdev = curpeak->y - curpeak->prev->prev->y - (curpeak->next->y - curpeak->prev->prev->y) / (curpeak->next->t - curpeak->prev->prev->t)*(curpeak->t - curpeak->prev->prev->t);
521
+ // // maxdev is the deviation of the previous point, curdev is for the current point
522
+ // flag = 0;
523
+ // if (curpeak->prev->y > curpeak->prev->prev->y && curpeak->y > curpeak->next->y) {
524
+ // flag = (curpeak->prev->y > curpeak->y) ? 1 : 2;
525
+ // }
526
+ // // flag is 0 for concave sections without a true peak, 1 if previous point is a peak, 2 if current point is a peak
527
+ // // Current point is removed if flag=1 or flag=0 and the previous point has higher deviation
528
+ // // Otherwise, previous point is removed
529
+ // if ((flag==0 && curpeak->prev->sig > curpeak->sig) || (flag==1)){
530
+ // curpeak = curpeak->prev;
531
+ // cpeaks->remove(curpeak->next);
532
+ // curdev = maxdev;
533
+ // }
534
+ // else {
535
+ // cpeaks->remove(curpeak->prev);
536
+ // }
537
+ // // Significance is recalculated correspondingly
538
+ // curpeak->sig = curdev / curpeak->yerr;
539
+ // }
540
+ // }
541
+
542
+ // Calculate uncertainty interval of peaks
543
+ for (curpeak = cpeaks->first->next; curpeak->next; curpeak = curpeak->next) {
544
+
545
+ if (curpeak->sig > 1.e-100) {
546
+
547
+ if (curpeak->prev == cpeaks->first) {
548
+ curpeak->tl = -1.e100; // This peak is at the beginning of light curve
549
+ }
550
+ else {
551
+ //For high significance peak we find the first data point that would give a deviation lower than sigmathr and use this as tl.
552
+ int i = curpeak->prev->i;
553
+ curdev = 1.e100;
554
+ while (curdev > sigmathr) {
555
+ i++;
556
+ w1 = (curpeak->t - t[i]) / (curpeak->next->t - t[i]);
557
+ w2 = 1 - w1;
558
+ curdev = curpeak->y - y[i] - (curpeak->next->y - y[i]) * w1;
559
+ curdev /= sqrt(curpeak->yerr * curpeak->yerr + curpeak->next->yerr * curpeak->next->yerr * w1 * w1 + err[i] * err[i] * w2 * w2);
560
+ }
561
+ curpeak->tl = t[i - 1];
562
+ }
563
+
564
+ // Right side of the uncertainty interval. Analogous to left side.
565
+ if (curpeak->next == cpeaks->last) {
566
+ curpeak->tr = 1.e100;
567
+ }
568
+ else {
569
+ int i = curpeak->next->i;
570
+ curdev = 1.e100;
571
+ while (curdev > sigmathr) {
572
+ i--;
573
+ w1 = (curpeak->t - t[i]) / (curpeak->prev->t - t[i]);
574
+ w2 = 1 - w1;
575
+ curdev = curpeak->y - y[i] - (curpeak->prev->y - y[i]) * w1;
576
+ curdev /= sqrt(curpeak->yerr * curpeak->yerr + curpeak->prev->yerr * curpeak->prev->yerr * w1 * w1 + err[i] * err[i] * w2 * w2);
577
+ }
578
+ curpeak->tr = t[i + 1];
579
+ }
580
+ }
581
+ }
582
+
583
+ // Removing anything that is not a maximum
584
+ for (curpeak = cpeaks->first; curpeak; curpeak = p) {
585
+ p = curpeak->next;
586
+ if (curpeak->sig <=peakthr*0.01) cpeaks->remove(curpeak);
587
+ }
588
+
589
+ // cpeaks now only contains a list of maxima with their significance and time interval
590
+ printf("\nAll peaks in this dataset");
591
+ for (p = cpeaks->first; p; p = p->next) {
592
+ printf("\n/^\\ %lf %lf %lf %le %lf", p->t, p->tl, p->tr, p->y, p->sig);
593
+ }
594
+
595
+ }
596
+ // At the end of this cycle, peaklist is an array with the lists of peaks for each dataset
597
+ // including their significance and time interval.
598
+
599
+
600
+ // Joining all peak lists
601
+
602
+ printf("\n\n- Joining all peak lists\n");
603
+ // All peaks from different datasets are put together in the same list
604
+
605
+ cpeaks = new dataset;
606
+ for (int ic = 0; ic < nfil; ic++) {
607
+ for (p = peaklist[ic]->first; p; p = p->next) {
608
+ cpeaks->clone(p);
609
+ }
610
+ }
611
+
612
+ for (p = cpeaks->first; p; p = p->next) {
613
+ printf("\n%lf %lf %lf %le %lf", p->t, p->tl, p->tr, p->y, p->sig);
614
+ }
615
+
616
+ // Ordering peaks by significance
617
+
618
+ printf("\n\n- Ordering peaks by significance \n");
619
+
620
+ p = cpeaks->first;
621
+ while (p) {
622
+ datapoint* q;
623
+ pm = p->next;
624
+ flag = 0;
625
+ while (pm) {
626
+ q = pm->next;
627
+ if (pm->sig > p->sig) {
628
+ pm->prev->next = pm->next;
629
+ if (pm->next) {
630
+ pm->next->prev = pm->prev;
631
+ }
632
+ else cpeaks->last = pm->prev;
633
+ if (p->prev) {
634
+ p->prev->next = pm;
635
+ }
636
+ else cpeaks->first = pm;
637
+ pm->prev = p->prev;
638
+ p->prev = pm;
639
+ pm->next = p;
640
+ p = pm;
641
+ }
642
+ pm = q;
643
+ }
644
+ p = p->next;
645
+ }
646
+
647
+ for (p = cpeaks->first; p; p = p->next) {
648
+ printf("\n%lf %lf %lf %le %lf", p->t, p->tl, p->tr, p->y, p->sig);
649
+ }
650
+
651
+ // Reducing list to independent peaks (necessary if more than one dataset exists)
652
+
653
+ printf("\n\n- Reducing peak lists\n");
654
+
655
+ // Check for peak pairs falling in both uncertainty ranges or partial peaks
656
+
657
+ for (pm = cpeaks->first; pm; pm = pm->next) {
658
+ for (p = pm->next; p; p = p->next) {
659
+ if ((p->t<pm->tr && p->t>pm->tl && pm->t<p->tr && pm->t>p->tl) || (p->tl < -1.e99 && pm->t < p->tr) || (p->tr > 1.e99 && pm->t > p->tl) || (pm->tl < -1.e99 && p->t < pm->tr) || (pm->tr > 1.e99 && p->t > pm->tl)) {
660
+ w1 = 1 / (pm->tr - pm->tl);
661
+ w2 = 1 / (p->tr - p->tl);
662
+ //if (p->sig > pm->sig) pm->sig = p->sig; //pm->sig = (pm->sig*w1+p->sig*w2) / (w1 + w2);
663
+ if ((p->tr - p->tl < pm->tr - pm->tl) || (p->tr > 1.e99 && pm->tr > 1.e99 && p->tl > pm->tl) || (p->tl < -1.e99 && pm->tl < -1.e99 && p->tr < pm->tr)) {
664
+ pm->t = p->t;
665
+ //pm->sig = p->sig;
666
+ }
667
+ pm->sig += p->sig;
668
+ if (p->tl > pm->tl) pm->tl = p->tl;
669
+ if (p->tr < pm->tr) pm->tr = p->tr;
670
+ pmm = p->prev;
671
+ cpeaks->remove(p);
672
+ p = pmm;
673
+ }
674
+ }
675
+ }
676
+
677
+ for (p = cpeaks->first; p; p = p->next) {
678
+ printf("\n%lf %lf %lf %le %lf", p->t, p->tl, p->tr, p->y, p->sig);
679
+ }
680
+
681
+ // Check for peak pairs where only one falls in the other's uncertainty range
682
+
683
+ printf("\n\n- Reducing peak lists 2\n");
684
+
685
+ for (pm = cpeaks->first; pm; pm = pm->next) {
686
+ for (p = pm->next; p; p = p->next) {
687
+ if ((p->t<pm->tr && p->t>pm->tl) || (pm->t<p->tr && pm->t>p->tl)) {
688
+ w1 = 1 / (pm->tr - pm->tl);
689
+ w2 = 1 / (p->tr - p->tl);
690
+ pm->sig += p->sig;//if (p->sig > pm->sig) pm->sig = p->sig;//pm->sig= (pm->sig*w1 + p->sig*w2) / (w1 + w2);
691
+ if (p->tl > pm->tl) pm->tl = p->tl;
692
+ if (p->tr < pm->tr) pm->tr = p->tr;
693
+ if (p->t < pm->tr && p->t>pm->tl) pm->t = p->t;
694
+ pmm = p->prev;
695
+ cpeaks->remove(p);
696
+ p = pmm;
697
+ }
698
+ }
699
+ }
700
+
701
+ for (p = cpeaks->first; p; p = p->next) {
702
+ printf("\n%lf %lf %lf %le %lf", p->t, p->tl, p->tr, p->y, p->sig);
703
+ }
704
+
705
+
706
+ newpeaks = cpeaks;
707
+
708
+ // Delete used data
709
+ for (int ic = 0; ic < nfil; ic++) {
710
+ for (p = peaklist[ic]->first; p; p = pm) {
711
+ pm = p->next;
712
+ delete p;
713
+ }
714
+ }
715
+ free(peaklist);
716
+ free(tt);
717
+ free(yy);
718
+ free(eerr);
719
+ free(nnp);
720
+
721
+ // Removing peaks less significant than peakthr and beyond nobspeaks
722
+ // Note that we may decide to use more than 2 peaks in the observed light curve to check for more initial conditions.
723
+ // nobspeaks is set to 2 at the beginning of the code, but can be changed if necessary
724
+
725
+ p = newpeaks->first->next;
726
+ for (int i = 1; i < nobspeaks && p && p->sig > peakthr; i++) {
727
+ p = p->next;
728
+ }
729
+ while (p) {
730
+ pm = p;
731
+ p = p->next;
732
+ newpeaks->remove(pm);
733
+ delete pm;
734
+ }
735
+
736
+ printf("\nfint: %lf", fint);
737
+
738
+ // If not enough relevant peaks, use largest asymmetry as second peak
739
+ if (newpeaks->first->next == 0 && tasy > 1) {
740
+ printf("\n\nUsing maximal asymmetry %lf", tasy);
741
+ newpeaks->addpoint(0, tasy, asydev, 1);
742
+ datapoint* p;
743
+ p = newpeaks->first;
744
+ newpeaks->first = p->next;
745
+ newpeaks->first->prev = 0;
746
+ newpeaks->first->next = p;
747
+ p->prev = newpeaks->first;
748
+ p->next = 0;
749
+ }
750
+
751
+ }
752
+ // At this point, newpeaks contains the list of peaks to be used to generate initial conditions ordered by significance
753
+ // We are ready for matching to the template library
754
+
755
+ // Reading old best models, if any
756
+ // This block is used by to include the best models from previous run in the set of initial conditions for the next run.
757
+ // Conventions
758
+ // First letter: P for single-source-single-lens, L for binary lens, B for binary source
759
+ // Second letter: S for static, X for parallax, O for orbital motion
760
+ // Three numbers for models sorted by increasing chi square
761
+ // .txt
762
+
763
+ current_path(eventname);
764
+ if(!exists("InitCond")) create_directory("InitCond");
765
+ current_path("InitCond");
766
+
767
+ searchstring = regex(".*Init.*\\.txt");
768
+ for (auto const& itr : directory_iterator(".")) {
769
+ string curfile = (itr).path().filename().string();
770
+ if (regex_match(curfile, searchstring)) {
771
+ remove(curfile);
772
+ }
773
+ }
774
+
775
+
776
+ // Single lens - Single Source initial conditions
777
+
778
+ current_path(eventname);
779
+
780
+ dn = 0;
781
+ if (nostatic) {
782
+ filebest = regex("PX.*\\.txt");
783
+ strcpy(fileinit, "InitCondPX.txt");
784
+ nps = 6;
785
+ }
786
+ else {
787
+ filebest = regex("PS.*\\.txt");
788
+ strcpy(fileinit, "InitCondPS.txt");
789
+ nps = 4;
790
+ }
791
+ tt = (double*)malloc(sizeof(double) * nps * maxoldmodels);
792
+ if (exists(runstring + "\\Models")) {
793
+ current_path(runstring + "\\Models");
794
+ for (auto const& itr : directory_iterator(".")) {
795
+ string curfile = (itr).path().filename().string();
796
+ if (regex_match(curfile, filebest)) {
797
+ f = fopen(curfile.c_str(), "r");
798
+ for (int j = 0; j < nps; j++) {
799
+ fscanf(f, "%le", &tt[dn * nps + j]);
800
+ }
801
+ fclose(f);
802
+ dn++;
803
+ }
804
+ }
805
+ }
806
+ printf("\n- Writing initial conditions for fitting to %s\n\n",fileinit);
807
+ current_path(eventname);
808
+ current_path("InitCond");
809
+ f = fopen(fileinit, "w");
810
+ int nu0 = 3, ntE = 5, nrho = 4;
811
+ // First we write the number of peaks used (only 1) and the number of initial conditions that are going to be generated
812
+ if (nostatic) {
813
+ fprintf(f, "%d %d\n", 1, nu0 * ntE * nrho*2 + dn);
814
+ }
815
+ else {
816
+ fprintf(f, "%d %d\n", 1, nu0* ntE* nrho + dn);
817
+ }
818
+ // Then we write the characteristics of the peaks used
819
+ p = newpeaks->first;
820
+ fprintf(f, "%le %le %le %le %le\n", p->t, p->tl, p->tr, p->y, p->sig);
821
+ // First we write the initial conditions from previous best models
822
+ for (int i = 0; i < dn; i++) {
823
+ for (int j = 0; j < nps; j++) {
824
+ fprintf(f, "%le ", tt[i * nps + j]);
825
+ }
826
+ fprintf(f, "\n");
827
+ }
828
+ // Here we write the initial conditions by matching the newpeaks to the peaks recorded in the template library
829
+ for (int iu = 0; iu < nu0; iu++) {
830
+ for (int itE = 0; itE < ntE; itE++) {
831
+ for (int ir = 0; ir < nrho; ir++) {
832
+ // {u0, tE, t0, Rs}
833
+ if (nostatic) {
834
+ fprintf(f, "%le %le %le %le 0.0 0.0\n", pow(10., -2. + iu), pow(10., -1. + itE), p->t, pow(10., -3. + 1. * ir));
835
+ fprintf(f, "%le %le %le %le 0.0 0.0\n", -pow(10., -2. + iu), pow(10., -1. + itE), p->t, pow(10., -3. + 1. * ir));
836
+ }
837
+ else {
838
+ fprintf(f, "%le %le %le %le\n", pow(10., -2. + iu), pow(10., -1. + itE), p->t, pow(10., -3. + 1. * ir));
839
+ }
840
+ }
841
+ }
842
+ }
843
+ fclose(f);
844
+ free(tt);
845
+ current_path(eventname);
846
+
847
+ if (!(nostatic)) {
848
+ printf("\n- Writing initial conditions for fitting with Parallax to PreInitCondPX.txt\n\n");
849
+
850
+ dn = 0;
851
+ tt = (double*)malloc(sizeof(double) * 6 * maxoldmodels);
852
+ filebest = regex("PX.*\\.txt");
853
+ if (exists(runstring + "\\Models")) {
854
+ current_path(runstring + "\\Models");
855
+ for (auto const& itr : directory_iterator(".")) {
856
+ if (dn >= maxoldmodels) break;
857
+ string curfile = (itr).path().filename().string();
858
+ if (regex_match(curfile, filebest)) {
859
+ f = fopen(curfile.c_str(), "r");
860
+ for (int j = 0; j < 6; j++) {
861
+ fscanf(f, "%le", &tt[dn * 6 + j]);
862
+ }
863
+ fclose(f);
864
+ dn++;
865
+ }
866
+ }
867
+ }
868
+ current_path(eventname);
869
+ current_path("InitCond");
870
+
871
+ f = fopen("PreInitCondPX.txt", "w");
872
+ fprintf(f, "%d\n", dn);
873
+ for (int i = 0; i < dn; i++) {
874
+ for (int j = 0; j < 6; j++) {
875
+ fprintf(f, "%le ", tt[i * 6 + j]);
876
+ }
877
+ fprintf(f, "\n");
878
+ }
879
+ fclose(f);
880
+ free(tt);
881
+ }
882
+
883
+ // Binary Source initial conditions
884
+
885
+ current_path(eventname);
886
+
887
+ dn = 0;
888
+ if (nostatic) {
889
+ filebest = regex("BO.*\\.txt");
890
+ strcpy(fileinit, "InitCondBO.txt");
891
+ nps = 10;
892
+ }
893
+ else {
894
+ filebest = regex("BS.*\\.txt");
895
+ strcpy(fileinit, "InitCondBS.txt");
896
+ nps = 7;
897
+ }
898
+ tt = (double*)malloc(sizeof(double) * nps * maxoldmodels);
899
+ if (exists(runstring + "\\Models")) {
900
+ current_path(runstring + "\\Models");
901
+ for (auto const& itr : directory_iterator(".")) {
902
+ if (dn >= maxoldmodels) break;
903
+ string curfile = (itr).path().filename().string();
904
+ if (regex_match(curfile, filebest)) {
905
+ f = fopen(curfile.c_str(), "r");
906
+ for (int j = 0; j < nps; j++) {
907
+ fscanf(f, "%le", &tt[dn * nps + j]);
908
+ }
909
+ fclose(f);
910
+ dn++;
911
+ }
912
+ }
913
+ }
914
+ printf("\n- Writing initial conditions for fitting to %s\n\n",fileinit);
915
+
916
+ current_path(eventname);
917
+ current_path("InitCond");
918
+
919
+ f = fopen(fileinit, "w");
920
+ nu0 = 3, ntE = 5;
921
+ int nFR = 3;
922
+ // First we write the number of peaks used and the number of initial conditions that are going to be generated
923
+ if (nostatic) {
924
+ fprintf(f, "%d %d\n", newpeaks->length, nu0 * nu0 * ntE * nFR * (newpeaks->length * (newpeaks->length - 1) / 2)*2 + dn);
925
+ }
926
+ else {
927
+ fprintf(f, "%d %d\n", newpeaks->length, nu0* nu0* ntE* nFR* (newpeaks->length* (newpeaks->length - 1) / 2) + dn);
928
+ }
929
+ // Then we write the characteristics of the peaks used
930
+ for (p = newpeaks->first; p; p = p->next) {
931
+ fprintf(f, "%le %le %le %le %le\n", p->t, p->tl, p->tr, p->y, p->sig);
932
+ }
933
+ // First we write the initial conditions from previous best models
934
+ for (int i = 0; i < dn; i++) {
935
+ for (int j = 0; j < nps; j++) {
936
+ fprintf(f, "%le ", tt[i * nps + j]);
937
+ }
938
+ fprintf(f, "\n");
939
+ }
940
+ // Here we write the initial conditions by matching the newpeaks
941
+ for (pl = newpeaks->first; pl->next; pl = pl->next) {
942
+ for (pr = pl->next; pr; pr = pr->next) {
943
+ for (int iu = 0; iu < nu0; iu++) {
944
+ for (int iu2 = 0; iu2 < nu0; iu2++) {
945
+ for (int itE = 0; itE < ntE; itE++) {
946
+ for (int iFR = 0; iFR < nFR; iFR++) {
947
+ if (nostatic) {
948
+ //{u0, t0, log_tE, log_Rs, xi1, xi2, omega, inc, phi, log_qs}
949
+ double u01 = pow(10., -2. + iu);
950
+ double u02 = pow(10., -2. + iu2);
951
+ double tE = pow(10., -1. + itE);
952
+ double qs = pow(10., (-1. + iFR) / 4.0);
953
+ fprintf(f, "%le %le %le %le %le %le %le %le %le %le\n", u01, pl->t, tE, 0.0001,(pr->t - pl->t)/ tE/(1+qs)*qs, (-u02+u01)/ (1 + qs) * qs, 0.000001, 0.0001, 0.00001, qs);
954
+ fprintf(f, "%le %le %le %le %le %le %le %le %le %le\n", u01, pl->t, tE, 0.0001, (pr->t - pl->t) / tE / (1 + qs) * qs, (u02 + u01) / (1 + qs) * qs, 0.000001, 0.0001, 0.00001, qs);
955
+ }
956
+ else {
957
+ // {tE, fluxratio, u0_1, u0_2, t0_1, t0_2, rho}
958
+ fprintf(f, "%le %le %le %le %le %le %le\n", pow(10., -1. + itE), pow(10., -1. + iFR), pow(10., -2. + 2 * iu), pow(10., -2. + 2 * iu2), pl->t, pr->t, 0.0001);
959
+ }
960
+ }
961
+ }
962
+ }
963
+ }
964
+ }
965
+ }
966
+ fclose(f);
967
+ free(tt);
968
+ current_path("..");
969
+
970
+ if (!(nostatic)) {
971
+ printf("\n- Writing initial conditions for fitting with Xallarap to PreInitCondBO.txt\n\n");
972
+
973
+ dn = 0;
974
+ tt = (double*)malloc(sizeof(double) * 10 * maxoldmodels);
975
+ filebest = regex("BO.*\\.txt");
976
+ if (exists(runstring + "\\Models")) {
977
+ current_path(runstring + "\\Models");
978
+ for (auto const& itr : directory_iterator(".")) {
979
+ if (dn >= maxoldmodels) break;
980
+ string curfile = (itr).path().filename().string();
981
+ if (regex_match(curfile, filebest)) {
982
+ f = fopen(curfile.c_str(), "r");
983
+ for (int j = 0; j < 10; j++) {
984
+ fscanf(f, "%le", &tt[dn * 10 + j]);
985
+ }
986
+ fclose(f);
987
+ dn++;
988
+ }
989
+ }
990
+ }
991
+
992
+ current_path(eventname);
993
+ current_path("InitCond");
994
+
995
+ f = fopen("PreInitCondBO.txt", "w");
996
+ fprintf(f, "%d\n", dn);
997
+ for (int i = 0; i < dn; i++) {
998
+ for (int j = 0; j < 10; j++) {
999
+ fprintf(f, "%le ", tt[i * 10 + j]);
1000
+ }
1001
+ fprintf(f, "\n");
1002
+ }
1003
+ fclose(f);
1004
+ free(tt);
1005
+ }
1006
+
1007
+
1008
+ // Binary lens initial conditions
1009
+
1010
+ current_path(eventname);
1011
+ current_path("InitCond");
1012
+
1013
+ dn = 0;
1014
+ if (noparallax) {
1015
+ filebest = regex("LO.*\\.txt");
1016
+ strcpy(fileinit, "PreInitCondLO.txt");
1017
+ nps = 12;
1018
+ }
1019
+ else {
1020
+ if (nostatic) {
1021
+ filebest = regex("LX.*\\.txt");
1022
+ strcpy(fileinit, "PreInitCondLX.txt");
1023
+ nps = 9;
1024
+ }
1025
+ else {
1026
+ filebest = regex("LS.*\\.txt");
1027
+ strcpy(fileinit, "PreInitCondLS.txt");
1028
+ nps = 7;
1029
+ }
1030
+ }
1031
+
1032
+ current_path(eventname);
1033
+
1034
+ dn=0;
1035
+ tt=(double *) malloc(sizeof(double)*nps*maxoldmodels);
1036
+ if (exists(runstring + "\\Models")) {
1037
+ current_path(runstring + "\\Models");
1038
+ for (auto const& itr : directory_iterator(".")) {
1039
+ if (dn >= maxoldmodels) break;
1040
+ string curfile = (itr).path().filename().string();
1041
+ if (regex_match(curfile, filebest)) {
1042
+ f = fopen(curfile.c_str(), "r");
1043
+ for (int j = 0; j < nps; j++) {
1044
+ fscanf(f, "%le", &tt[dn * nps + j]);
1045
+ }
1046
+ fclose(f);
1047
+ dn++;
1048
+ }
1049
+ }
1050
+ }
1051
+ printf("\n- Writing initial conditions for fitting to %s\n\n",fileinit);
1052
+
1053
+ // Reading parameters for initial conditions from the template library
1054
+ current_path(exedir);
1055
+ current_path("..");
1056
+ current_path("data");
1057
+ f=fopen("TemplateLibrary.txt","r");
1058
+ fscanf(f,"%d",&np);
1059
+ printf("\nTemplates in library: %d",np);
1060
+ yy=(double *) malloc(sizeof(double)*np*7); // yy will contain all the information read from TemplateLibrary.txt
1061
+ for(int i=0;i<np*7;i++){
1062
+ fscanf(f,"%lf",&yy[i]);
1063
+ }
1064
+ fclose(f);
1065
+
1066
+ current_path(eventname);
1067
+ current_path("InitCond");
1068
+
1069
+ f=fopen(fileinit,"w");
1070
+ // First we write the number of peaks used and the number of initial conditions that are going to be generated
1071
+ if (nostatic) {
1072
+ fprintf(f, "%d %d\n", newpeaks->length, np * (newpeaks->length * (newpeaks->length - 1))*2 + dn);
1073
+ }
1074
+ else {
1075
+ fprintf(f, "%d %d\n", newpeaks->length, np * (newpeaks->length * (newpeaks->length - 1)) + dn);
1076
+ }
1077
+ // Then we write the characteristics of the peaks used
1078
+ for(p=newpeaks->first;p;p=p->next){
1079
+ fprintf(f,"%le %le %le %le %le\n",p->t,p->tl,p->tr,p->y,p->sig);
1080
+ }
1081
+ // First we write the initial conditions from previous best models
1082
+ for(int i=0;i<dn;i++){
1083
+ for(int j=0;j<nps;j++){
1084
+ fprintf(f,"%le ",tt[i*nps+j]);
1085
+ }
1086
+ fprintf(f,"\n");
1087
+ }
1088
+ // Here we write the initial conditions by matching the newpeaks to the peaks recorded in the template library
1089
+ for(int i=0;i<np;i++){
1090
+ for(pl=newpeaks->first;pl->next;pl=pl->next){
1091
+ for(pr=pl->next;pr;pr=pr->next){
1092
+ t1=(pl->t<pr->t)? pl->t : pr->t;
1093
+ t2=(pl->t<pr->t)? pr->t : pl->t;
1094
+ tE=(t2-t1)/(yy[i*7+6]-yy[i*7+5]); // yy[i*7+6] and yy[i*7+5] are the peak times of the ith template
1095
+ t0=t2-tE*yy[i*7+6];
1096
+ for(int j=0;j<5;j++){
1097
+ fprintf(f,"%le ",yy[i*7+j]); // We use the s,q,u0,alpha,rho parameters from the template
1098
+ }
1099
+ fprintf(f,"%le %le",tE,t0); // and use tE and t0 from the time matching
1100
+ if (nostatic) {
1101
+ fprintf(f, " 0.0 0.0"); // parallax for nostatic
1102
+ if (noparallax) {
1103
+ fprintf(f, " 0.0 0.0 0.0001"); // starting parameters for orbital motion
1104
+ }
1105
+ }
1106
+ fprintf(f, "\n");
1107
+
1108
+ if (nostatic) { // Reflected initial condition for nostatic
1109
+ yy[i * 7 + 2] = -yy[i * 7 + 2];
1110
+ yy[i * 7 + 3] = -yy[i * 7 + 3];
1111
+ for (int j = 0; j < 5; j++) {
1112
+ fprintf(f, "%le ", yy[i * 7 + j]);
1113
+ }
1114
+ fprintf(f, "%le %le", tE, t0);
1115
+ fprintf(f, " 0.0 0.0"); // parallax for nostatic
1116
+ if (noparallax) {
1117
+ fprintf(f, " 0.0 0.0 0.0001"); // starting parameters for orbital motion
1118
+ }
1119
+ fprintf(f, "\n");
1120
+ }
1121
+
1122
+ tE=(t1-t2)/(yy[i*7+6]-yy[i*7+5]); // We also include the time-reverse matching
1123
+ t0=t1-tE*yy[i*7+6];
1124
+ yy[i*7+2]=-yy[i*7+2]; //u0 and alpha are reversed
1125
+ yy[i*7+3]=yy[i*7+3]+M_PI;
1126
+ tE=-tE;
1127
+ for(int j=0;j<5;j++){
1128
+ fprintf(f,"%le ",yy[i*7+j]);
1129
+ }
1130
+ fprintf(f, "%le %le", tE, t0); // and use tE and t0 from the time matching
1131
+ if (nostatic) {
1132
+ fprintf(f, " 0.0 0.0"); // parallax for nostatic
1133
+ if (noparallax) {
1134
+ fprintf(f, " 0.0 0.0 0.0001"); // starting parameters for orbital motion
1135
+ }
1136
+ }
1137
+ fprintf(f, "\n");
1138
+
1139
+ if (nostatic) { // Reflected initial condition for nostatic
1140
+ yy[i * 7 + 2] = -yy[i * 7 + 2];
1141
+ yy[i * 7 + 3] = -yy[i * 7 + 3];
1142
+ for (int j = 0; j < 5; j++) {
1143
+ fprintf(f, "%le ", yy[i * 7 + j]);
1144
+ }
1145
+ fprintf(f, "%le %le", tE, t0);
1146
+ fprintf(f, " 0.0 0.0"); // parallax for nostatic
1147
+ if (noparallax) {
1148
+ fprintf(f, " 0.0 0.0 0.0001"); // starting parameters for orbital motion
1149
+ }
1150
+ fprintf(f, "\n");
1151
+ }
1152
+ }
1153
+ }
1154
+ }
1155
+ fclose(f);
1156
+ free(tt);
1157
+
1158
+
1159
+ // Reading old best parallaxmodels, if any
1160
+
1161
+ current_path(eventname);
1162
+
1163
+ if (!nostatic) {
1164
+ printf("\n- Writing initial conditions for fitting with Parallax to PreInitCondLX.txt\n\n");
1165
+
1166
+ dn = 0;
1167
+ tt = (double*)malloc(sizeof(double) * 9 * maxoldmodels);
1168
+ filebest = regex("LX.*\\.txt");
1169
+ if (exists(runstring + "\\Models")) {
1170
+ current_path(runstring + "\\Models");
1171
+ for (auto const& itr : directory_iterator(".")) {
1172
+ if (dn >= maxoldmodels) break;
1173
+ string curfile = (itr).path().filename().string();
1174
+ if (regex_match(curfile, filebest)) {
1175
+ f = fopen(curfile.c_str(), "r");
1176
+ for (int j = 0; j < 9; j++) {
1177
+ fscanf(f, "%le", &tt[dn * 9 + j]);
1178
+ }
1179
+ fclose(f);
1180
+ dn++;
1181
+ }
1182
+ }
1183
+ }
1184
+
1185
+ current_path(eventname);
1186
+ current_path("InitCond");
1187
+
1188
+ f = fopen("PreInitCondLX.txt", "w");
1189
+ fprintf(f, "%d\n", dn);
1190
+ for (int i = 0; i < dn; i++) {
1191
+ for (int j = 0; j < 9; j++) {
1192
+ fprintf(f, "%le ", tt[i * 9 + j]);
1193
+ }
1194
+ fprintf(f, "\n");
1195
+ }
1196
+ fclose(f);
1197
+ free(tt);
1198
+ }
1199
+
1200
+ // Reading old best orbitalmodels, if any
1201
+
1202
+ current_path(eventname);
1203
+
1204
+ if (!noparallax) {
1205
+ printf("\n- Writing initial conditions for fitting with Orbital Motion to PreInitCondLO.txt\n\n");
1206
+
1207
+ dn = 0;
1208
+ tt = (double*)malloc(sizeof(double) * 12 * maxoldmodels);
1209
+ filebest = regex("LO.*\\.txt");
1210
+ if (exists(runstring + "\\Models")) {
1211
+ current_path(runstring + "\\Models");
1212
+ for (auto const& itr : directory_iterator(".")) {
1213
+ if (dn >= maxoldmodels) break;
1214
+ string curfile = (itr).path().filename().string();
1215
+ if (regex_match(curfile, filebest)) {
1216
+ f = fopen(curfile.c_str(), "r");
1217
+ for (int j = 0; j < 12; j++) {
1218
+ fscanf(f, "%le", &tt[dn * 12 + j]);
1219
+ }
1220
+ fclose(f);
1221
+ dn++;
1222
+ }
1223
+ }
1224
+ }
1225
+
1226
+ current_path(eventname);
1227
+ current_path("InitCond");
1228
+
1229
+ f = fopen("PreInitCondLO.txt", "w");
1230
+ fprintf(f, "%d\n", dn);
1231
+ for (int i = 0; i < dn; i++) {
1232
+ for (int j = 0; j < 12; j++) {
1233
+ fprintf(f, "%le ", tt[i * 12 + j]);
1234
+ }
1235
+ fprintf(f, "\n");
1236
+ }
1237
+ fclose(f);
1238
+ free(tt);
1239
+ }
1240
+
1241
+
1242
+ printf("\n---- Done");
1243
+ //Sleep(5000l);
1244
+
1245
+
1246
+ free(yy);
1247
+ delete newpeaks;
1248
+
1249
+ return 0;
1250
+ }
1251
+
1252
+ ////////////////////////////////
1253
+ /////////////////////////////
1254
+ ///////////////////////////
1255
+ // Methods in dataset and datapoint structures
1256
+ ///////////////////////////
1257
+ ////////////////////////////
1258
+
1259
+ dataset::dataset(){
1260
+ length=0;
1261
+ prev=next=0;
1262
+ first=last=0;
1263
+ }
1264
+
1265
+ dataset::~dataset(){
1266
+ datapoint *p,*q;
1267
+
1268
+ delete next;
1269
+ for(p=first;p;p=q){
1270
+ q=p->next;
1271
+ delete p;
1272
+ }
1273
+ }
1274
+
1275
+ datapoint *dataset::addpoint(int i,double t, double y,double err){
1276
+ datapoint *p;
1277
+ p=new datapoint;
1278
+ p->i = i;
1279
+ p->t=t;
1280
+ p->y=y;
1281
+ p->yerr = err;
1282
+ if(length){
1283
+ if (p->t < first->t) {
1284
+ first->prev = p;
1285
+ p->next = first;
1286
+ p->prev = 0;
1287
+ first = p;
1288
+ }else{
1289
+ if (p->t >= last->t) {
1290
+ last->next = p;
1291
+ p->prev = last;
1292
+ p->next = 0;
1293
+ last = p;
1294
+ }
1295
+ else {
1296
+ datapoint *scan = first;
1297
+ while (scan->next->t < t) scan = scan->next;
1298
+ scan->next->prev = p;
1299
+ p->next = scan->next;
1300
+ scan->next = p;
1301
+ p->prev = scan;
1302
+ }
1303
+ }
1304
+ }else{
1305
+ first=last=p;
1306
+ p->prev=0;
1307
+ p->next = 0;
1308
+ }
1309
+ p->tl=p->tr=0;
1310
+ p->sig=0;
1311
+ length++;
1312
+ return p;
1313
+ }
1314
+
1315
+ void dataset::addpoint(double *t, double *y,double *err,int i,int n,int sign){
1316
+ datapoint *p;
1317
+ int flag,j;
1318
+ p=new datapoint;
1319
+ if(length){
1320
+ p->prev=last;
1321
+ last->next=p;
1322
+ last=p;
1323
+ }else{
1324
+ first=last=p;
1325
+ p->prev=0;
1326
+ }
1327
+ p->next=0;
1328
+ length++;
1329
+
1330
+ p->t=t[i];
1331
+ p->y=y[i];
1332
+ p->yerr=err[i];
1333
+ p->sig=0;
1334
+
1335
+ flag=0;
1336
+ j=i-1;
1337
+ while(flag<2){
1338
+ if((j<0)||(sign*(p->y-y[j])>sigmathr*(err[j]+p->yerr))){
1339
+ flag++;
1340
+ }else flag=0;
1341
+ j--;
1342
+ }
1343
+ if(j<0){
1344
+ p->tl=-1.e100;
1345
+ }else{
1346
+ p->tl=t[j+1];
1347
+ }
1348
+
1349
+ flag=0;
1350
+ j=i+1;
1351
+ // double xx,tt;
1352
+ while(flag<2){
1353
+ //xx=(sign*(p->y-y[j])/(err[j]+p->yerr));
1354
+ //tt=t[j];
1355
+ if((j>=n) || (sign*(p->y-y[j])>sigmathr*(err[j]+p->yerr))){
1356
+ flag++;
1357
+ }else flag=0;
1358
+ j++;
1359
+ }
1360
+ if(j>=n){
1361
+ p->tr=1.e100;
1362
+ }else{
1363
+ p->tr=t[j-1];
1364
+ }
1365
+
1366
+
1367
+ //if(i<=1){
1368
+ // p->terr=-1.e100;
1369
+ //}else{
1370
+ // if(i>=n-2){
1371
+ // p->terr=1.e100;
1372
+ // }else{
1373
+ // p->terr=(t[i+2]-t[i-2])/2;
1374
+ // }
1375
+ //}
1376
+ }
1377
+
1378
+ void dataset::remove(datapoint *p){
1379
+ if(first==p){
1380
+ first=p->next;
1381
+ }else{
1382
+ p->prev->next=p->next;
1383
+ }
1384
+ if(last==p){
1385
+ last=p->prev;
1386
+ }else{
1387
+ p->next->prev=p->prev;
1388
+ }
1389
+ length--;
1390
+ }
1391
+
1392
+ void dataset::clone(datapoint *p){
1393
+ datapoint *q;
1394
+ q=addpoint(p->i,p->t,p->y,p->yerr);
1395
+ q->sig=p->sig;
1396
+ q->tl=p->tl;
1397
+ q->tr=p->tr;
1398
+ }