-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAutoIntegrateUtil.js
More file actions
3157 lines (2849 loc) · 114 KB
/
AutoIntegrateUtil.js
File metadata and controls
3157 lines (2849 loc) · 114 KB
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
AutoIntegrate utility functions.
Copyright (c) 2018-2026 Jarmo Ruuth.
Crop to common area code
Copyright (c) 2022 Jean-Marc Lugrin.
Window name prefix and icon location code
Copyright (c) 2021 rob pfile.
This product is based on software from the PixInsight project, developed
by Pleiades Astrophoto and its contributors (https://pixinsight.com/).
*/
#ifndef AUTOINTEGRATEUTIL_JS
#define AUTOINTEGRATEUTIL_JS
function AutoIntegrateUtil(global)
{
this.__base__ = Object;
this.__base__();
var self = this;
var util = this;
var gui = null;
var par = global.par;
var ppar = global.ppar;
this.loggingEnabled = true;
this.beginLogCallback = null;
this.endLogCallback = null;
/* Set optional GUI object to update GUI components.
*/
function setGUI(aigui)
{
gui = aigui;
}
function init_pixinsight_version()
{
global.pixinsight_version_str = CoreApplication.versionMajor + '.' + CoreApplication.versionMinor + '.' +
CoreApplication.versionRelease + '-' + CoreApplication.versionRevision +
' build ' + CoreApplication.versionBuild;
global.pixinsight_version_num = CoreApplication.versionMajor * 1e6 +
CoreApplication.versionMinor * 1e4 +
CoreApplication.versionRelease * 1e2 +
CoreApplication.versionRevision;
global.pixinsight_build_num = CoreApplication.versionBuild;
}
function runGarbageCollection()
{
var start_time = Date.now();
gc();
var end_time = Date.now();
var time_sec = (end_time-start_time)/1000;
if (time_sec >= 1.0) {
console.writeln("runGarbageCollection, " + time_sec + " sec");
}
}
function checkEvents()
{
processEvents(); // process events to keep GUI responsible
util.runGarbageCollection();
}
/// Init filter sets. We used to have actual Set object but
// use a simple array so we can add object into it.
// There are file sets for each possible filters and
// each array element has file name and used flag.
function initFilterSets()
{
return [
['L', []],
['R', []],
['G', []],
['B', []],
['H', []],
['S', []],
['O', []],
['C', []]
];
}
// find filter set object based on file type
function findFilterSet(filterSet, filetype)
{
for (var i = 0; i < filterSet.length; i++) {
if (filterSet[i][0] == filetype) {
return filterSet[i][1];
}
}
util.throwFatalError("findFilterSet bad filetype " + filetype);
return null;
}
// Add file base name to the filter set object
// We use file base name to detect filter files
function addFilterSetFile(filterSet, filePath, filetype)
{
var basename = File.extractName(filePath);
console.writeln("addFilterSetFile add " + basename + " filter "+ filetype);
filterSet[filterSet.length] = { name: basename, used: false };
}
// Try to find base file name from filter set objects.
// We use simple linear search which should be fine
// for most data sizes.
function findFilterForFile(filterSet, filePath, filename_postfix)
{
var basename = File.extractName(filePath);
if (filename_postfix.length > 0) {
// strip filename_postfix from the end
basename = basename.slice(0, basename.length - filename_postfix.length);
}
console.writeln("findFilterForFile " + basename);
for (var i = 0; i < filterSet.length; i++) {
var filterFileSet = filterSet[i][1];
for (var j = 0; j < filterFileSet.length; j++) {
if (!filterFileSet[j].used && filterFileSet[j].name == basename) {
console.writeln("findFilterForFile filter " + filterSet[i][0]);
filterFileSet[j].used = true;
return filterSet[i][0];
}
}
}
return null;
}
// remove a file from filter set
function removeFilterFile(filterSet, filePath)
{
var basename = File.extractName(filePath);
for (var i = 0; i < filterSet.length; i++) {
var filterFileSet = filterSet[i][1];
for (var j = 0; j < filterFileSet.length; j++) {
if (filterFileSet[j].name == basename) {
filterFileSet.splice(j, 1);
return;
}
}
}
}
function clearFilterFileUsedFlags(filterSet)
{
console.writeln("clearUsedFilterForFiles");
for (var i = 0; i < filterSet.length; i++) {
var filterFileSet = filterSet[i][1];
for (var j = 0; j < filterFileSet.length; j++) {
filterFileSet[j].used = false;
}
}
return null;
}
function setDefaultDirs()
{
global.AutoOutputDir = "AutoOutput";
global.AutoCalibratedDir = "AutoCalibrated";
global.AutoMasterDir = "AutoMaster";
global.AutoProcessedDir = "AutoProcessed";
}
function clearDefaultDirs()
{
global.AutoOutputDir = ".";
global.AutoCalibratedDir = ".";
global.AutoMasterDir = ".";
global.AutoProcessedDir = ".";
}
function ensurePathEndSlash(dir)
{
if (dir.length > 0) {
switch (dir[dir.length-1]) {
case '/':
case '\\':
case ':':
return dir;
default:
return dir + '/';
}
}
return dir;
}
function removePathEndSlash(dir)
{
if (dir.length > 1) {
switch (dir[dir.length-1]) {
case '/':
case '\\':
return dir.slice(0, -1);
default:
return dir;
}
}
return dir;
}
function removePathEndDot(dir)
{
if (dir.length > 0) {
switch (dir.substr(-2, 2)) {
case '/.':
case '\\.':
return dir.slice(0, -2);
default:
return dir;
}
}
return dir;
}
// parse full path from file name appended with '/
function parseNewOutputDir(filePath, subdir)
{
var path = util.ensurePathEndSlash(File.extractDrive(filePath) + File.extractDirectory(filePath));
path = util.ensurePathEndSlash(path + subdir);
path = util.normalizePath(path);
console.writeln("parseNewOutputDir " + path);
return path;
}
// If path is relative and not absolute, we append it to the
// path of the image file
function pathIsRelative(p)
{
var dir = File.extractDirectory(p);
if (dir == null || dir == '') {
return true;
}
switch (dir[0]) {
case '/':
case '\\':
return false;
default:
return true;
}
}
function throwFatalError(txt)
{
util.addProcessingStepAndStatusInfo(txt);
global.run_results.fatal_error = txt;
global.is_processing = global.processing_state.none;
throw new Error(txt);
}
function findWindow(id)
{
if (id == null || id == undefined) {
return null;
}
//return ImageWindow.windowById(id);
var images = ImageWindow.windows;
if (images == null || images == undefined) {
return null;
}
for (var i in images) {
if (images[i].mainView != null
&& images[i].mainView != undefined
&& images[i].mainView.id == id)
{
return images[i];
}
}
return null;
}
function isDefaultImageId(str)
{
// Regular expression to match the pattern "Image" followed by a number
const regex = /^Image\d+$/i;
// Test the string against the regular expression
return regex.test(str);
}
function getFnameIfGeneratedWindowId(imgWin)
{
var filePath = imgWin.filePath;
if (filePath == null || filePath == undefined) {
return null;
}
var fname = File.extractName(filePath);
if (fname == imgWin.mainView.id) {
// fname and view id match, not using a system generated view id
return null;
}
if (!isDefaultImageId(imgWin.mainView.id)) {
// not using a system generated view id, maybe user changed the view id
return null;
}
return fname;
}
function getBaseWindowId(imgWin)
{
var fname = getFnameIfGeneratedWindowId(imgWin);
if (fname != null) {
// We have system generated view id, use the file name
return fname;
}
return imgWin.mainView.id;
}
function findWindowOrFile(id)
{
if (id == null || id == undefined) {
return null;
}
var images = ImageWindow.windows;
if (images == null || images == undefined) {
return null;
}
var w = util.findWindow(id);
if (w != null) {
return w;
}
// Window not found by view id, try to find using a file name
var found_win = null;
for (var i in images) {
if (images[i].mainView == null
|| images[i].mainView == undefined)
{
continue;
}
var fname = getFnameIfGeneratedWindowId(images[i]);
if (fname == null) {
// not using a system generated view id so we
// have already checked the id
continue;
}
// We have a system generated default view id, check if we have a match with the file name
if (fname == id) {
if (found_win != null) {
// Duplicate file name found, we cannot use the file name
util.addCriticalStatus("Duplicate file name " + id + " found, cannot use the file");
return null;
}
found_win = images[i];
}
}
if (found_win != null) {
found_win.mainView.id = id; // Ensure that the window id is the same as the file name
}
return found_win;
}
function findWindowStartsWith(id)
{
if (id == null || id == undefined) {
return null;
}
var images = ImageWindow.windows;
if (images == null || images == undefined) {
return null;
}
for (var i in images) {
if (images[i].mainView != null
&& images[i].mainView != undefined
&& images[i].mainView.id.startsWith(id))
{
return images[i];
}
}
return null;
}
function findWindowRe(re)
{
if (re == null || re == undefined) {
return null;
}
var images = ImageWindow.windows;
if (images == null || images == undefined) {
return null;
}
for (var i in images) {
if (images[i].mainView != null
&& images[i].mainView != undefined
&& images[i].mainView.id.match(re))
{
return images[i];
}
}
return null;
}
function findWindowFromArray(arr)
{
for (var i = 0; i < arr.length; i++) {
if (util.findWindow(arr[i]) != null) {
return true;
}
}
return false;
}
function closeAllWindowsSubstr(id_substr)
{
var images = ImageWindow.windows;
if (images == null || images == undefined) {
return;
}
for (var i in images) {
if (images[i].mainView != null
&& images[i].mainView != undefined
&& images[i].mainView.id.indexOf(id_substr) != -1)
{
images[i].close;
}
}
}
function getWindowList()
{
var windowList = [];
var images = ImageWindow.windows;
if (images == null || images == undefined) {
return windowList;
}
for (var i in images) {
try {
if (images[i].mainView == null || images[i].mainView == undefined) {
continue;
}
windowList[windowList.length] = images[i].mainView.id;
} catch (err) {
// ignore errors
}
}
return windowList;
}
function getWindowListReverse()
{
var windowListReverse = [];
var windowList = util.getWindowList();
for (var i = windowList.length-1; i >= 0; i--) {
if (windowList[i].match(/undo[1-9]*/g) == null) {
windowListReverse[windowListReverse.length] = windowList[i];
}
}
return windowListReverse;
}
function findWindowId(id)
{
var w = util.findWindow(id);
if (w == null) {
return null;
}
return w.mainView.id;
}
function findWindowOrFileId(id)
{
var w = util.findWindowOrFile(id);
if (w == null) {
return null;
}
return w.mainView.id;
}
function windowShowif(id)
{
var w = util.findWindow(id);
if (w != null) {
w.show();
}
}
// Iconify the window, the return value is the window,
// only as a convenience to this.windowIconizeAndKeywordif()
function windowIconizeEx(id, columnCount, iconStartRow, haveIconizedCount)
{
if (id == null) {
return null;
}
if (global.get_flowchart_data) {
return null;
}
console.writeln("windowIconizeEx " + id + " columnCount " + columnCount + " iconStartRow " + iconStartRow + " haveIconizedCount " + haveIconizedCount);
var w = util.findWindow(id);
if (w != null) {
if (w.iconic) {
console.writeln("Window " + id + " is already iconized");
return null;
}
/* Method iconize() will put the icon at the middle position
of the window. To get icons to the top left corner we
first move the window middle position there to get
the icon at the right position. Then we restore the
window position back to old position.
*/
var oldpos = new Point(w.position); // save old position
if (global.iconPoint == null) {
/* Get first icon to upper left corner. */
global.iconPoint = new Point(
-(w.width / 2) + 5 + columnCount * 300,
-(w.height / 2) + 5 + iconStartRow * 32);
//console.writeln("Icon " + id + " start from position " + iconPoint + ", iconStartRow " + iconStartRow + ", columnCount " + columnCount);
} else {
/* Put next icons in a nice row below the first icon.
*/
// global.iconPoint.moveBy(0, 32);
global.iconPoint = new Point(
-(w.width / 2) + 5 + columnCount * 300,
-(w.height / 2) + 5 + iconStartRow * 32 + haveIconizedCount * 32);
// console.writeln("Next icon " + id + " position " + iconPoint + ", iconStartRow " + iconStartRow + ", columnCount " + columnCount);
}
w.position = new Point(global.iconPoint); // set window position to get correct icon position
w.iconize();
w.position = oldpos; // restore window position
}
// console.writeln("windowIconizeEx done");
return w;
}
function windowIconizeFindPosition(id, keep_iconized)
{
var index = 0;
var iconStartRow = 0;
var columnCount = 0;
var prefixlen = 0;
if (id == null) {
return null;
}
// console.writeln("windowIconizeFindPosition " + id);
if (global.get_flowchart_data) {
return null;
}
// See if this window has some known prefix
// If not, we use position 0
for (var i = 0; i < ppar.prefixArray.length; i++) {
// console.writeln("windowIconizeFindPosition check prefix " + ppar.prefixArray[i][1]);
if (ppar.prefixArray[i][1] != "" && id.startsWith(ppar.prefixArray[i][1])) {
if (ppar.prefixArray[i][1].length > prefixlen) {
// console.writeln("windowIconizeFindPosition found prefix " + ppar.prefixArray[i][1]);
prefixlen = ppar.prefixArray[i][1].length;
index = i;
}
}
}
if (ppar.prefixArray.length == 0) {
// No prefixes, add one
ppar.prefixArray[0] = [ 0, "", 0 ];
}
columnCount = ppar.prefixArray[index][0];
iconStartRow = ppar.prefixArray[index][2];
console.writeln("windowIconizeFindPosition " + id + " columnCount " + columnCount + " iconStartRow " + iconStartRow);
var w = windowIconizeEx(id, columnCount, iconStartRow, global.haveIconized);
if (w != null) {
ppar.prefixArray[index][2]++;
global.haveIconized++;
if (!keep_iconized) {
// console.writeln("windowIconizeFindPosition show");
w.show();
}
}
// console.writeln("windowIconizeFindPosition done");
return w;
}
// Iconify the window, the return value is the window,
// only as a convenience to this.windowIconizeAndKeywordif()
function windowIconizeif(id, show_image)
{
if (id == null) {
return null;
}
if (global.get_flowchart_data) {
util.closeOneWindowById(id);
return null;
}
var w = windowIconizeEx(id, global.columnCount, global.iconStartRow, global.haveIconized);
if (w != null) {
global.haveIconized++;
if (show_image) {
w.show();
}
}
return w;
}
function batchWindowSetPosition(id, count)
{
var w = util.findWindow(id);
if (w != null) {
var windowPoint = new Point(
5 + 300 + count * 64,
5 + count * 32);
w.position = new Point(windowPoint);
}
}
function autointegrateProcessingHistory(imageWindow)
{
var keywords = imageWindow.keywords;
var history = {
AutoIntegrate: [],
info: [],
options: [],
steps: [],
enhancements: []
};
var is_history = false;
// AutoIntegrate keyword first
for (var i = 0; i < keywords.length; i++) {
var keyword = keywords[i];
if (keyword.name == 'AutoIntegrate') {
history.AutoIntegrate[history.AutoIntegrate.length] = [ keyword.strippedValue.trim(), keyword.comment.trim() ];
is_history = true;
}
var trimmedComment = keyword.comment.trim();
if (keyword.name == 'HISTORY' && trimmedComment == "AutoIntegrate processing" && keyword.strippedValue.startsWith("Step")) {
history.steps[history.steps.length] = [ keyword.name, keyword.strippedValue.trim(), keyword.comment.trim() ];
is_history = true;
}
if (keyword.name == 'HISTORY' && trimmedComment.startsWith("AutoIntegrate processing info")
&& keyword.strippedValue != "Processing options:")
{
history.info[history.info.length] = [ keyword.name, keyword.strippedValue.trim(), keyword.comment.trim() ];
is_history = true;
}
if (keyword.name == 'HISTORY' && trimmedComment.startsWith("AutoIntegrate processing option")
&& keyword.strippedValue != "Processing options:")
{
history.options[history.options.length] = [ keyword.name, keyword.strippedValue.trim(), keyword.comment.trim() ];
is_history = true;
}
if (keyword.name == 'HISTORY' && trimmedComment.startsWith("AutoIntegrate enhancements")) {
history.enhancements[history.enhancements.length] = [ keyword.name, keyword.strippedValue.trim(), keyword.comment.trim() ];
is_history = true;
}
}
if (is_history) {
return history;
} else {
return null;
}
}
function filterKeywords(imageWindow, keywordname)
{
var oldKeywords = [];
var keywords = imageWindow.keywords;
for (var i = 0; i < keywords.length; i++) {
var keyword = keywords[i];
if (keyword.name != keywordname) {
oldKeywords[oldKeywords.length] = keyword;
}
}
return oldKeywords;
}
function findDrizzleScale(imageWindow)
{
var scale = 0;
var keywords = imageWindow.keywords;
for (var i = 0; i < keywords.length; i++) {
var keyword = keywords[i];
if (keyword.name == 'HISTORY') {
var value = keyword.strippedValue.trim();
if (value.startsWith("DrizzleIntegration.scale:")) {
var parts = value.split(" ");
if (parts.length > 1) {
scale = parseFloat(parts[1]);
break;
}
}
}
}
return scale;
}
function findDrizzle(imgWin)
{
for (var i = 0; i < imgWin.keywords.length; i++) {
switch (imgWin.keywords[i].name) {
case "AutoIntegrateDrizzle":
var value = imgWin.keywords[i].strippedValue.trim();
console.writeln("AutoIntegrateDrizzle=" + value);
var drizzle = parseInt(value);
return drizzle;
default:
break;
}
}
var scale = util.findDrizzleScale(imgWin);
if (scale > 1) {
console.writeln("Using image metadata drizzle scale " + scale);
return scale;
}
return 1;
}
function copyKeywords(imageWindow)
{
var newKeywords = [];
var keywords = imageWindow.keywords;
for (var i = 0; i < keywords.length; i++) {
newKeywords[newKeywords.length] = new FITSKeyword(keywords[i]);
}
return newKeywords;
}
// Overwrite an old keyword or add a new one
function setFITSKeyword(imageWindow, name, value, comment)
{
var oldKeywords = util.filterKeywords(imageWindow, name);
imageWindow.keywords = oldKeywords.concat([
new FITSKeyword(
name,
value,
comment
)
]);
}
// Append a new keyword, allows multiple keywords with same name
function appenFITSKeyword(imageWindow, name, value, comment)
{
imageWindow.keywords = imageWindow.keywords.concat([
new FITSKeyword(
name,
value,
comment
)
]);
}
function getKeywordValue(imageWindow, keywordname)
{
var keywords = imageWindow.keywords;
if (!keywords) {
return null;
}
for (var i = 0; i < keywords.length; i++) {
var keyword = keywords[i];
if (keyword.name == keywordname) {
return keyword.strippedValue.trim();
}
}
return null;
}
function findKeywordName(imageWindow, keywordname)
{
var keywords = imageWindow.keywords;
if (!keywords) {
return null;
}
for (var i = 0; i < keywords.length; i++) {
var keyword = keywords[i];
if (keyword.name == keywordname) {
return true;
}
}
return false;
}
function setFITSKeywordNoOverwrite(imageWindow, name, value, comment)
{
if (util.findKeywordName(imageWindow, name)) {
console.writeln("keyword already set");
return;
}
util.setFITSKeyword(imageWindow, name, value, comment);
}
function setProcessedImageKeyword(imageWindow)
{
console.writeln("setProcessedImageKeyword to " + imageWindow.mainView.id);
util.setFITSKeywordNoOverwrite(
imageWindow,
"AutoIntegrate",
"processedimage",
"AutoIntegrate processed intermediate image");
}
function windowIconizeAndKeywordif(id, show_image, find_prefix)
{
if (find_prefix) {
var w = util.windowIconizeFindPosition(id, true);
} else {
var w = util.windowIconizeif(id);
}
if (w != null) {
// Set processed image keyword. It will not overwrite old
// keyword. If we later set a final image keyword it will overwrite
// this keyword.
setProcessedImageKeyword(w);
}
if (show_image) {
w.show();
}
}
// Add a script window that will be closed when close all is clicked
// Useful for temporary windows that do not have a fixed name
function addScriptWindow(name)
{
global.all_windows[global.all_windows.length] = name;
}
function windowRenameKeepifEx(old_name, new_name, keepif, allow_duplicate_name)
{
if (par.debug.val) {
console.writeln("windowRenameKeepifEx " + old_name + " to " + new_name + ", keepif " + keepif + ", allow_duplicate_name " + allow_duplicate_name);
}
if (global.get_flowchart_data) {
allow_duplicate_name = true;
}
if (old_name == new_name) {
return new_name;
}
var w = util.findWindow(old_name);
if (!w) {
util.throwFatalError("Could not find image " + old_name + " for rename");
}
w.mainView.id = new_name;
if (!keepif) {
util.addScriptWindow(new_name);
}
if (!allow_duplicate_name && w.mainView.id != new_name) {
util.fatalWindowNameFailed("Window rename from " + old_name + " to " + new_name + " failed, name is " + w.mainView.id);
}
if (global.get_flowchart_data && w.mainView.id != new_name) {
global.flowchartWindows[global.flowchartWindows.length] = w.mainView.id;
}
return w.mainView.id;
}
function windowRenameKeepif(old_name, new_name, keepif)
{
return util.windowRenameKeepifEx(old_name, new_name, keepif, false);
}
function windowRename(old_name, new_name)
{
return util.windowRenameKeepif(old_name, new_name, false);
}
function closeOneWindow(w, force_close = true)
{
if (w == null) {
return;
}
if (par.keep_temporary_images.val) {
w.mainView.id = "tmp_" + w.mainView.id;
w.show();
console.writeln("Rename window to " + w.mainView.id);
} else if (force_close) {
// Force close will close the window without asking
if (!global.get_flowchart_data) {
// console.writeln("Force close " + w.mainView.id);
}
w.forceClose();
} else {
// PixInsight will ask if file is changed but not saved
console.writeln("Close " + w.mainView.id);
w.close();
}
}
// close one window
function closeOneWindowById(id, force_close = true)
{
var w = util.findWindow(id);
if (w != null) {
util.closeOneWindow(w, force_close);
}
}
function closeWindowsFromArray(arr)
{
for (var i = 0; i < arr.length; i++) {
util.closeOneWindowById(arr[i]);
}
}
// For the final window, we may have more different names with
// both prefix or postfix added
function closeFinalWindowsFromArray(arr, force_close)
{
for (var i = 0; i < arr.length; i++) {
util.closeOneWindowById(arr[i], force_close);
util.closeOneWindowById(arr[i]+"_stars", force_close);
util.closeOneWindowById(arr[i]+"_starless", force_close);
util.closeOneWindowById(arr[i]+"_enh", force_close);
util.closeOneWindowById(arr[i]+"_enh_starless", force_close);
util.closeOneWindowById(arr[i]+"_enh_stars", force_close);
util.closeOneWindowById(arr[i]+"_enh_combined", force_close);
}
}
function closeTempWindowsForOneImage(id)
{
util.closeOneWindowById(id + "_max");
util.closeOneWindowById(id + "_map");
util.closeOneWindowById(id + "_map_linear_fit_reference");
util.closeOneWindowById(id + "_stars");
util.closeOneWindowById(id + "_map_mask");
util.closeOneWindowById(id + "_map_stars");
util.closeOneWindowById(id + "_map_pm");
util.closeOneWindowById(id + "_mask");
util.closeOneWindowById(id + "_tmp");
util.closeOneWindowById(id + "_solvercopy");
util.closeOneWindowById(id + "_combined_solvercopy");
}
function closeTempWindows()
{
for (var i = 0; i < global.integration_LRGB_windows.length; i++) {
util.closeTempWindowsForOneImage(global.integration_LRGB_windows[i]);
util.closeTempWindowsForOneImage(global.integration_LRGB_windows[i] + "_BE");
}
for (var i = 0; i < global.integration_color_windows.length; i++) {
util.closeTempWindowsForOneImage(global.integration_color_windows[i]);
util.closeTempWindowsForOneImage(global.integration_color_windows[i] + "_BE");
}
util.closeWindowsFromArray(global.temporary_windows);
global.temporary_windows = [];
}
// close all windows from an array
function closeAllWindowsFromArray(arr, keep_base_image = false, print_names = false)
{
for (var i = 0; i < arr.length; i++) {
if (print_names) {
console.writeln("closeAllWindowsFromArray: " + arr[i]);
}
if (util.findWindowStartsWith(arr[i])) {
util.closeOneWindowById(arr[i]+"_stars");
util.closeOneWindowById(arr[i]+"_for_stars");
util.closeOneWindowById(arr[i]+"_for_stars_HT");
util.closeOneWindowById(arr[i]+"_starless");
util.closeOneWindowById(arr[i]+"_map");
util.closeOneWindowById(arr[i]+"_MGC_gradient_model");
util.closeOneWindowById(arr[i]+"_model");
util.closeOneWindowById(arr[i]+"_highpass");
util.closeOneWindowById(arr[i]+"_lowpass");
util.closeOneWindowById(arr[i]+"_DBEsamples");
util.closeOneWindowById(arr[i]+"_map_DBEsamples");
if (!keep_base_image) {
util.closeOneWindowById(arr[i]);
}
if (arr[i].indexOf("Integration_") != -1) {
// For possible old images
util.closeOneWindowById(arr[i] + "_NB_enhanced");
util.closeOneWindowById(arr[i] + "_NB_combine");
util.closeOneWindowById(arr[i] + "_NB_max");
util.closeOneWindowById(arr[i] + "_processed_starless");
util.closeOneWindowById(arr[i] + "_background");
util.closeOneWindowById(arr[i] + "_map_background");
}
if (arr[i].indexOf("AutoMasterDark") != -1) {
// Close all windows starting with AutoMasterDark, as we may have multiple master darks with different integration settings
for (;;) {
var win = util.findWindowStartsWith(arr[i]);
if (win == null) {
break;
}
util.closeOneWindow(win);
}