-
Notifications
You must be signed in to change notification settings - Fork 0
/
MPCBuilder.js
552 lines (486 loc) · 18 KB
/
MPCBuilder.js
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/* MPCReport - Producing observation reports for MPC
Copyright (C) 2023, Arnaud Dupont
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 <https://www.gnu.org/licenses/>.
*/
#include <pjsr/StdButton.jsh>
#include <pjsr/StdIcon.jsh>
#include <pjsr/DataType.jsh>
#define DAY_IN_SECONDS 86400
#define MINUTE_IN_SEC 60
#define MPBUILDERVERSION "0.1.1"
// Define the asteroid, localisalisation informations
var packedNumber = "XXXXX"; // 5 chars
var provisionalDesignation = " "; // default : blank caracters - 7 chars
var discoveryAsterisk = " "; // default : blank caracter , can be '*' - 13rd char
var note1 = " "; // default : blank, F as Faint image, P poor image, ... - https://www.minorplanetcenter.net/iau/info/ObsNote.html
var note2 = "B"; // default B : CMOS - C: CCD
var observationDate = "YYYY MM DD.dddddd"; // starting at 16 cols - 17 chars usually given at 0.00001 days
var observedRA = "HH MM SS.ddd"; // starting at 33 cols - 12 chars - usually given at 0.01s
var observedDecl = "sDD MM SS.dd"; //starting at 45 cols - 12 chars - usually givent at 0.1" 's' is +/-
/*********************************************************************************/
// Define observer and telescope informations
var observatoryCode = "XXX";
var contactName = "E. Garak";
var contactDetails = "[[email protected]]";
var obsDetails1 = "Long. 71 07 41.17 W, Lat. 42 22 54.05 N, Alt. 19m, Google Earth";
var obsDetails2 = "60 Garden Street, Cambridge, MA 02138, USA";
var observers = "A. U. Tomatic";
var measurers = "J.-J. Binks";
var telescopeDetails = "200 mm f/4 reflector + CMOS";
var cataloguesUsed = ["Gaia-DR3"];
var comment = "This is a test observation.";
var observationCount = 1;
var ackWords = "NEW SITE XXX - batch 001 - 2023-06-03 10:50:15 GMT+2";
var acknowledgmentEmails = "[email protected]";
// OTHERS TO DEFINE
var reportSavePath = "Z:/travaux/asteroids/script/MPCReport/tests/MPCReport.";
var debugMode = false; // put to true to activate debug mode
/*********************************************************************************/
var magnitudeBand = "B"; // TODO 'T' for
var observedMagnitude = "12.34"; // "observed magnitude" - usually at 0.1
//Divers
//var timeHMObs = "00 00";
//var decSecTimeObs = "0.00000";
var dateObsISO = "";
var yearObs = 0;
var monthObs = 0; // 0-indexed
var dayObs = 0;
var hoursxxObs = 0;
var minutesxxObs = 0;
var secondsxxObs = 0;
var milliSecondsObs = 0;
//CSV Reading
var csvImageName = new Array();
var csvRA = new Array();
var csvDEC = new Array();
var mpName = new Array();
var found = 0;
var buildADESXML = false;
var buildADESPSV = false;
var buildMPC1992 = false;
var selectedFormat = false;
var adesPSVContent = "";
var adesXMLContent = "";
var MPC1992Content = "";
var isFormatSelected = false;
function logDebug(message) {
if (debugMode) {
console.writeln(message);
}
}
// To space to set the right length
function padStringWithSpaces(str, length) {
let paddedStr = str;
while (paddedStr.length < length) {
paddedStr += " ";
}
return paddedStr;
}
function getKeywords(window) {
let keywords = window.keywords;
let result = {};
for (let i = 0; i < keywords.length; ++i) {
let key = keywords[i];
if (key && (key.name === "DATE-OBS" || key.name === "EXPTIME")) {
result[key.name] = key.value;
}
}
return result;
}
function parseRADEC(posSTR,typePos) {
var pos = posSTR.toString().split(' ');
logDebug("POS : "+ pos);
let parsedPos = "";
for ( let m =0; m < pos.length; m++) { // check each part of RA/DEC
logDebug("posm[m] : " + pos[m].charAt(0));
let tmp = "";
if ((pos[m].charAt(0) == "+") || (pos[m].charAt(0) == "-")) { // DEC first token sDD ?
logDebug("token + - : ");
if ((pos[m].length) < 3){ // if length < 3 : only one digit
tmp = "0" + pos[m].charAt(1);
logDebug("posm[m] : " + pos[m].charAt(1));
tmp = pos[m].charAt(0) + tmp;
logDebug("posm[m] : " + tmp);
}
else { // default - error
tmp = pos[m];
logDebug("posm[m] sans signe : " + tmp );
}
}
else //contains nor + and -
{
logDebug("pos[m] est inf a 10 : "+ pos[m] );
if (pos[m] < 10 && pos[m].charAt(0) != '0' )
tmp = "0" + pos[m];
else
tmp = pos[m];}
logDebug("pos[m] contains '.' ? : "+ tmp );
if (tmp.contains(".",0)) {
switch (typePos) {
case 0 : //RA
tmp = tmp.toFloat().toFixed(2);
break;
case 1 : //DEC
tmp = tmp.toFloat().toFixed(1);
break;
default :
break;
}
logDebug("Rounded = " + tmp);
tmp = (tmp < 10 ? "0" : "") + tmp;
}
if (( m < pos.length-1) )
parsedPos = parsedPos + tmp + " ";
else
parsedPos = parsedPos + tmp;
logDebug("RA / DEC : " + parsedPos);
}
return parsedPos;
}
function parseISODate(dateStr) {
dateStr = dateStr.substring(1, dateStr.length - 1);
dateStr = dateStr + "Z";
logDebug("Date ParseISODate Texte : " + dateStr);
return new Date(dateStr);
}
function formatTimeDigits(value) {
return value < 10 ? "0" + value : value.toString();
}
function calculateFormattedDayObs(dayObs, inDayHour) {
let formattedDayObs = dayObs + inDayHour;
return formattedDayObs.toFixed(5);
}
function formatInDayHour(formattedDayObs) {
return (formattedDayObs < 10 ? "0" : "") + formattedDayObs;
}
// Save a MPC Report using the right extension - depends of value isBuild : 0 - ADES XML ; 1 - ADES PSV ; 2 - MPC1992
function saveReport(isbuild) {
// Save the content to a file
var formatExtension = isbuild === 0 ? "xml" : isbuild === 1 ? "psv" : "txt";
var filePath = reportSavePath + formatExtension;
var file = new File;
file.createForWriting(filePath);
var type = "default";
switch (isbuild) { // TODO Incorrect gestion reprendre buildMPC1992 chaque type peut être produit
case 0 :
type = "ADES XML";
file.outText(adesXMLContent);
break;
case 1 :
type = "ADES PSV";
file.outText(adesPSVContent);
break;
case 2 :
type = "MPC1992";
file.outText(MPC1992Content);
break;
default :
type = "default";
}
file.close();
// Display a message to the user
var message =
"The " + type + " file was created successfully!\n\n" +
"File Path: " + filePath;
new MessageBox(message, type + " file Created", StdIcon_Information).execute();
}
function mpcReportBuild(window,indexCSV)
{
var dateObs = "";
var expTime = 0;
if (window) {
let keywords = getKeywords(window);
if ("DATE-OBS" in keywords && "EXPTIME" in keywords) {
dateObs = keywords["DATE-OBS"];
expTime = keywords["EXPTIME"];
found = 2;
}
logDebug ("MPCReportBuild step2");
if (found < 2) {
new MessageBox(
"The image has no 'DATE-OBS' & 'EXPTIME' keywords",
TITLE,
StdIcon_Error,
StdButton_Ok
).execute();
}
}
logDebug(window.currentView.fullId);
dateObs = new String(dateObs);
dateObsISO = parseISODate(dateObs);
logDebug("Date Texte : " + dateObsISO.toString());
yearObs = dateObsISO.getUTCFullYear();
monthObs = dateObsISO.getUTCMonth() + 1; // 0-indexed
dayObs = dateObsISO.getUTCDate();
hoursxxObs = dateObsISO.getUTCHours();
minutesxxObs = dateObsISO.getUTCMinutes();
secondsxxObs = dateObsISO.getUTCSeconds();
milliSecondsObs = dateObsISO.getUTCMilliseconds();
logDebug("MilliSecondesISO : " + milliSecondsObs);
milliSecondsObs = milliSecondsObs / 1000;
logDebug("Pose : " + expTime);
logDebug("AnneeISO : " + yearObs);
logDebug("MoisISO : " + monthObs);
logDebug("JoursISO : " + dayObs);
logDebug("Heure : " + hoursxxObs);
logDebug("MinutesISO : " + minutesxxObs);
logDebug("SecondesISO : " + secondsxxObs);
logDebug("MilliSecondesISO : " + milliSecondsObs);
expTime = expTime / 2;
logDebug("Pose moitié: " + expTime);
var minCalc = Math.floor(expTime / MINUTE_IN_SEC);
var secCalc = expTime % MINUTE_IN_SEC;
logDebug("minCalc : " + minCalc + " secCalc : " + secCalc);
minutesxxObs = minutesxxObs + minCalc;
secondsxxObs = secondsxxObs + secCalc;
var inDayHour =
(hoursxxObs * 3600 + minutesxxObs * 60 + secondsxxObs + milliSecondsObs) /
DAY_IN_SECONDS;
logDebug(format("inDayHour : %.5f day", inDayHour));
var formattedDayObs = calculateFormattedDayObs(dayObs, inDayHour);
var formattedInDayHour = formatInDayHour(formattedDayObs);
logDebug ("MPCReportBuild step3");
monthObs = formatTimeDigits(monthObs);
observationDate = yearObs + " " + monthObs + " " + formattedInDayHour;
console.writeln("Observation Date UTC : " + observationDate);
// Construction ligne data
buildBodyReport(indexCSV);
}
/*************************************************
* BUILD REPORT HEADERS
***************************************************/
function buildHeaders() {
if (buildADESXML) { // TODO
// XML format
adesXMLContent += '<?xml version="1.0" encoding="UTF-8"?>\n';
adesXMLContent += '<ades - version = 2022>\n';
adesXMLContent += ' <header>\n';
adesXMLContent += ' <cod>ABC</cod>\n';
adesXMLContent += ' <con>' + contactName + ' ' + contactDetails + '</con>\n';
adesXMLContent += ' <obs>' + observers + ', ' + '</obs>\n';
adesXMLContent += ' <mea>' + measurers+ ', ' + '</mea>\n';
adesXMLContent += ' <tel>' + telescopeDetails + '</tel>\n';
adesXMLContent += ' <net>' + cataloguesUsed + ', ' + '</net>\n';
adesXMLContent += ' <bnd>' + magnitudeBand + '</bnd>\n';
adesXMLContent += ' <com>' + comment + '</com>\n';
adesXMLContent += ' <num>' + observationCount + '</num>\n';
adesXMLContent += ' <ack>ACK [' + acknowledgmentEmails + ',' + ']</ack>\n';
adesXMLContent += ' <ac2>' + acknowledgmentEmails + ',' + '</ac2>\n';
adesXMLContent += ' </header>\n';
}
if (buildADESPSV) {
// TODO PSV Header
}
if (buildMPC1992) {
// MPC1992 format
MPC1992Content += 'COD ' + observatoryCode + '\n';
MPC1992Content += 'CON ' + contactName + ' ' + contactDetails + '\n';
MPC1992Content += 'OBS ' + observers + '\n';
MPC1992Content += 'MEA ' + measurers + '\n';
MPC1992Content += 'TEL ' + telescopeDetails + '\n';
MPC1992Content += 'COM ' + obsDetails1 + '\n';
MPC1992Content += 'COM ' + obsDetails2 + '\n';
MPC1992Content += 'NUM ' + observationCount + '\n';
MPC1992Content += 'ACK ' + ackWords +'\n';
MPC1992Content += 'AC2 ' + acknowledgmentEmails + '\n';
MPC1992Content += 'NET ' + cataloguesUsed + '\n';
MPC1992Content += '\n';
}
}
/*************************************************
* BUILD RECORDED DATA
***************************************************/
function buildBodyReport(index) {
if (buildADESXML) {
adesXMLContent += ' <asteroid>\n';
adesXMLContent += ' <packedNumber>' + packedNumber + '</packedNumber>\n';
adesXMLContent += ' <provisionalDesignation>' + provisionalDesignation + '</provisionalDesignation>\n';
adesXMLContent += ' <discoveryAsterisk>' + discoveryAsterisk + '</discoveryAsterisk>\n';
adesXMLContent += ' <note1>' + note1 + '</note1>\n';
adesXMLContent += ' <note2>' + note2 + '</note2>\n';
adesXMLContent += ' <observationDate>' + observationDate + '</observationDate>\n';
adesXMLContent += ' <observedRA>' + observedRA + '</observedRA>\n';
adesXMLContent += ' <observedDecl>' + observedDecl + '</observedDecl>\n';
adesXMLContent += ' <observedMagnitude>' + observedMagnitude + '</observedMagnitude>\n';
adesXMLContent += ' <magnitudeBand>' + magnitudeBand + '</magnitudeBand>\n';
adesXMLContent += ' <observatoryCode>' + observatoryCode + '</observatoryCode>\n';
adesXMLContent += ' </asteroid>\n';
adesXMLContent += '</ades>';
}
if (buildADESPSV) {
// PSV format
adesPSVContent += packedNumber + '|' + provisionalDesignation + '|' + discoveryAsterisk + '|';
adesPSVContent += note1 + '|' + note2 + '|' + observationDate + '|';
adesPSVContent += observedRA + '|' + observedDecl + '| |';
adesPSVContent += observedMagnitude + magnitudeBand + '| |' + observatoryCode + '\n';
}
if (buildMPC1992) {
MPC1992Content += packedNumber ;
MPC1992Content += provisionalDesignation ;
MPC1992Content += discoveryAsterisk ;
MPC1992Content += note1; // quality of observation data
MPC1992Content += note2; // origin of data - C : CCD / B : CMOS - default
MPC1992Content += padStringWithSpaces(observationDate,17); // 17 chars
MPC1992Content += padStringWithSpaces(csvRA[index],12); //12 chars
MPC1992Content += padStringWithSpaces(csvDEC[index],12); //12 chars
MPC1992Content += ' '; // 9 blanks mandatory
MPC1992Content += padStringWithSpaces(observedMagnitude,4) + magnitudeBand;
MPC1992Content += ' ';// 6 blanks mandatory
MPC1992Content += observatoryCode +'\n';
}
}
// lecture d'un fichier CSV avec séparateur ";" en PJSR
function readCSVFile(filePath) {
let file = new File;
try {
file.openForReading(filePath);
//throw new Error("Impossible d'ouvrir le fichier CSV : " + filePath);
}
catch (err) {
console.criticalln(err);
}
let fileContent = File.readTextFile(filePath);
var lines = fileContent.split('\n');
// Find name from filepath
var fileName = File.extractName(filePath);
logDebug("nom fichier csv : " + fileName + " Number of records : " + (lines.length-2));
// Get tokens '
var parts = fileName.split('_');
// check if at lest there's a token
if (parts.length > 0) {
// Read 1st fifth caracters : should be the packed number
let tmpName = parts[0].substring(0,5);
mpName = tmpName;
} else {
console.warningln("Le nom du fichier ne contient pas de parties séparées par '_'");
}
//For each record(line) of csv file
for (let k = 1; k < lines.length - 1 ; k++) { // last line is a one character
var fields = lines[k].split(',');
for (let j = 0; j < fields.length; j++) { // each field of the line
var value = fields[j];
switch (j) { // searching usefull column
case 0 :
csvImageName.push(value);
logDebug("ViewID: " + csvImageName[csvImageName.indexOf(value)]);
break;
case 8 :
let tmpRA = parseRADEC(value,0);
csvRA.push(tmpRA);
logDebug("RA: " + csvRA[csvRA.indexOf(tmpRA)]);
break;
case 9 :
let tmpDEC = parseRADEC(value,1);
csvDEC.push(tmpDEC);
logDebug("DEC: " + csvDEC[csvDEC.indexOf(tmpDEC)]);
break;
default :
break;
}
}
}
file.close();
}
/**************************************************************
* Constructeur
***************************************************************/
function MPCBuilder() {
logDebug ("constructeur MPCBuilder");
/************************************************
* MPCBuilder -- Fonction buildReport
* - read files, initiate variables, build files and save them
**************************************************/
this.buildReport = function (imagesFiles, dataFiles ) {
console.writeln ("Building Header Report ...");
buildHeaders();
// for each image file, we stock dates, and name and then search
for ( let j = 0; j < dataFiles.length; j++ ) { // For each CSV files
let datafilePath = dataFiles[ j ];
try {
logDebug("*******DONNEES CSV *********");
logDebug(datafilePath);
mpName = ""; //reinit of data for each csv
readCSVFile(datafilePath); // read data of the CSV file
packedNumber = mpName;
let fileWindow = null;
logDebug("Number of csv records : " + csvImageName.length);
let isClosed = false;
for ( let l=0; l < csvImageName.length; l++) { // Find pictures used in CSV
logDebug(" " + csvImageName[l].toString());
let tmp = csvImageName[l].toString();
for ( let i = 0; i < imagesFiles.length; i++ ) // search each images files
{
let filePath = imagesFiles[ i ];
isClosed = false;
logDebug("Image to analyse : " + imagesFiles[i] );
fileWindow = ImageWindow.open( filePath )[0]
let nameView = fileWindow.currentView.id;
if (nameView.toString() == tmp) {
logDebug("Image found ! :" + nameView );
mpcReportBuild(fileWindow,csvImageName.indexOf(nameView));
fileWindow.close();
break;}
else {
logDebug(tmp);
logDebug(nameView);
fileWindow.close();
}
logDebug("next image");
}
}
logDebug("go to next csv record");
//scan all lines of csv, reinit for next file
csvImageName = [];
csvRA = [];
csvDEC = [];
}
catch (error) {
console.criticalln(error);
}
}
if (buildMPC1992)
saveReport(2);
if (buildADESXML)
saveReport(0);
if (buildADESPSV)
saveReport(1);
};
this.setFormatReport = function (reportType) {
switch (reportType) {
case 0 :
if (!buildMPC1992)
buildMPC1992 = true;
else
buildMPC1992 = false;
logDebug("MPC1992 Format selected : " + buildMPC1992);
break;
case 2 :
if (!buildADESXML)
buildADESXML = true;
else
buildADESXML = false;
logDebug("ADES XML Format selected : " + buildADESXML);
break;
case 1 :
if (!buildADESPSV)
buildADESPSV = true;
else
buildADESPSV = false;
logDebug("ADES PSV Format selected : " + buildADESPSV);
break;
default :
throw new Error("Report Format Unknown");
}
isFormatSelected = buildMPC1992 | buildADESXML | buildADESPSV ;
return isFormatSelected;
};
}