-
Notifications
You must be signed in to change notification settings - Fork 0
/
EZ_LiveStack.js
2419 lines (2133 loc) · 96.8 KB
/
EZ_LiveStack.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
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
// Copyright (C) 2020-2023 S Dimant (aka darkarchon#4313)
// File name: EZ_LiveStack.js
/*
* ------------------- Version 2020-06-03 --------------------
* tested on PixInsight 1.8.8-5 Ripley
*
* This script allows live stacking.
* It is possible to use calibration frames and resume stacks overnight.
* It is possible to save calibrated frames
*
* Copyright information
*
* Made by S. Dimant (aka darkarchon#4313)
* Original EZ Live Stack idea by S. Dimant
*
*/
#feature-id EZ Processing Suite > EZ Live Stack
#define StackProperty "EZLiveStack:ProcessedFiles"
#define ExtensionProperty "EZLiveStack:Extension"
#define PathProperty "EZLiveStack:WatchPath"
#define DarkProperty "EZLiveStack:DarkPath"
#define DarkOptimizeProperty "EZLiveStack:DarkOptimize"
#define DarkExposureProperty "EZLiveStack:DarkExposure"
#define BiasProperty "EZLiveStack:BiasPath"
#define FlatProperty "EZLiveStack:FlatPath"
#define ColorProperty "EZLiveStack:IsCFA"
#define RunSCNRProperty "EZLiveStack:RunSCNR"
#define SCNRAmountProperty "EZLiveStack:RunSCNR:Amount"
#define ABEProperty "EZLiveStack:RunABE"
#define ABEDegreeProperty "EZLiveStack:RunABE:Degree"
#define BayerPatternProperty "EZLiveStack:BayerPat"
#define IgnoreFilterProperty "EZLiveStack:IgnoreFilter"
#define DownscaleProperty "EZLiveStack:DownscaleImages"
#define DownscaleAmountProperty "EZLiveStack:DownscaleImages:Amount"
#define SaveCalibratedFileProperty "EZLiveStack:SaveCalibratedFile"
#define SaveCompressedProperty "EZLiveStack:SaveCalibratedFile:Compressed"
#define SaveInt16Property "EZLiveStack:SaveCalibratedFile:Int16"
#define SaveDebayeredProperty "EZLiveStack:SaveCalibratedFile:Debayered"
#define RunNoiseEvalProperty "EZLiveStack:NoiseEvaluation"
#define NoiseEvalProperty "EZLiveStack:NoiseEvaluation:Values"
#define PedestalProperty "EZLiveStack:Pedestal"
#define CalibratedFilesPrefix "ezc_"
#define DebayeredFilesPrefix "ezd_"
#define FlatStackPrefix "ezflat_"
var NAME = "EZ Live Stack";
var VERSION = "0.14HF5";
var AUTHOR = "S. Dimant";
#include "EZ_Common.js"
var stacks = [];
var fileFilterDict = {};
function onInit() {
showWarning = false;
stacks = [];
}
function onExit() { }
function saveSettings() { }
function generateProcessingInfo() {
return null; // not used
}
// #region Stack
Stack.prototype = new Object;
Stack.prototype.toString = function () {
for (var propName in this) {
propValue = this[propName]
console.writeln(this.constructor.name + "." + propName + " = " + propValue);
}
}
Stack.prototype.viewExists = function() {
return this.mainViewId != null;
}
Stack.prototype.getStackView = function() {
if(this.mainViewId == null) return null;
return View.viewById(this.mainViewId);
}
Stack.prototype.saveSettings = function() {
Settings.write("EZLiveStack.BiasPath", 13, this.biasPath == null ? "" : this.biasPath);
Settings.write("EZLiveStack.DarkPath", 13, this.darkPath == null ? "" : this.darkPath);
Settings.write("EZLiveStack.FlatPath", 13, this.flatPath == null ? "" : this.flatPath);
Settings.write("EZLiveStack.ImageIsColor", 0, this.isCFA);
Settings.write("EZLiveStack.BayerMatrix", 1, this.cfaPattern);
Settings.write("EZLiveStack.Downscale", 0, this.downscaleImages);
Settings.write("EZLiveStack.Downscale.Amount", 1, this.downscaleImagesAmount);
Settings.write("EZLiveStack.RunABE", 0, this.runABE);
Settings.write("EZLiveStack.RunABE.Degree", 1, this.ABEdegree);
Settings.write("EZLiveStack.SaveCalibratedFiles", 0, this.saveCalibratedFile);
Settings.write("EZLiveStack.SaveCalibratedFiles.Compress", 0, this.saveCompressed);
Settings.write("EZLiveStack.SaveCalibratedFiles.Int16", 0, this.saveAs16BitInt);
Settings.write("EZLiveStack.SaveCalibratedFiles.Debayered", 0, this.saveDebayered);
Settings.write("EZLiveStack.OptimizeDark", 0, this.optimizeDark);
Settings.write("EZLiveStack.OptimizeDark.DarkExpTime", 1, this.darkExposureTime);
Settings.write("EZLiveStack.IgnoreFilter", 0, this.ignoreFilter);
Settings.write("EZLiveStack.RunSCNR", 0, this.scnr);
Settings.write("EZLiveStack.RunSCNR.Amount", 10, this.scnrAmount);
Settings.write("EZLiveStack.RunNoiseEvaluation", 0, this.runNoiseEval);
Settings.write("EZLiveStack.Pedestal", 0, 1, this.pedestal);
}
Stack.prototype.storeProperty = function(property, value) {
if(!this.viewExists()) return;
this.getStackView().storeProperty(property, value);
}
Stack.prototype.getProperty = function(property) {
if(!this.viewExists()) return null;
return this.getStackView().propertyValue(property);
}
Stack.prototype.setDarkPath = function(path) {
if(!this.viewExists()) return;
if (path == null || path == "") {
this.darkPath = null;
this.storeProperty(DarkProperty, "");
return;
}
let fileInfo = new FileInfo(path);
if(!fileInfo.isFile) {
writeWarningBlock("Could not load DARK on " + path + ", file does not exist");
return;
}
writeMessageBlock("Loading DARK " + path);
this.darkPath = path;
this.storeProperty(DarkProperty, this.darkPath);
}
Stack.prototype.setBiasPath = function(path) {
if(!this.viewExists()) return;
if (path == null || path == "") {
this.biasPath = null;
this.storeProperty(BiasProperty, "");
return;
}
let fileInfo = new FileInfo(path);
if(!fileInfo.isFile) {
writeWarningBlock("Could not load BIAS on " + path + ", file does not exist");
return;
}
writeMessageBlock("Loading BIAS " + path);
this.biasPath = path;
this.storeProperty(BiasProperty, this.biasPath);
}
Stack.prototype.setFlatPath = function(path) {
if(!this.viewExists()) return;
if (path == null || path == "") {
this.flatPath = null;
this.storeProperty(FlatProperty, "");
return;
}
let fileInfo = new FileInfo(path);
if(!fileInfo.isFile) {
writeWarningBlock("Could not load FLAT on " + path + ", file does not exist");
return;
}
writeMessageBlock("Loading FLAT " + path);
this.flatPath = path;
this.storeProperty(FlatProperty, this.flatPath);
}
Stack.prototype.setDownscaleImages = function(value) {
this.storeProperty(DownscaleProperty, value);
this.downscaleImages = value;
}
Stack.prototype.setDownscaleAmount = function(value) {
this.storeProperty(DownscaleAmountProperty, value);
this.downscaleImagesAmount = value;
}
Stack.prototype.setPathToWatch = function(value) {
this.storeProperty(PathProperty, value);
this.pathToWatch = value;
}
Stack.prototype.setOptimizeDark = function(value) {
this.storeProperty(DarkOptimizeProperty, value);
this.optimizeDark = value;
}
Stack.prototype.setDarkExposureTime = function(value) {
this.storeProperty(DarkExposureProperty, value);
this.darkExposureTime = value;
}
Stack.prototype.setCFA = function(value) {
this.storeProperty(ColorProperty, value);
this.isCFA = value;
}
Stack.prototype.setCFAPattern = function(value) {
this.storeProperty(BayerPatternProperty, value);
this.cfaPattern = value;
}
Stack.prototype.setIgnoreFilter = function(value) {
this.storeProperty(IgnoreFilterProperty, value);
this.ignoreFilter = value;
}
Stack.prototype.setSaveCalibratedFile = function(value) {
this.storeProperty(SaveCalibratedFileProperty, value);
this.saveCalibratedFile = value;
}
Stack.prototype.setSaveCompressed = function(value) {
this.storeProperty(SaveCompressedProperty, value);
this.saveCompressed = value;
}
Stack.prototype.setSaveAs16bit = function(value) {
this.storeProperty(SaveInt16Property, value);
this.saveAs16BitInt = value;
}
Stack.prototype.setSaveDebayered = function(value) {
this.storeProperty(SaveDebayeredProperty, value);
this.saveDebayered = value;
}
Stack.prototype.setRunABE = function(value) {
this.storeProperty(ABEProperty, value);
this.runABE = value;
}
Stack.prototype.setABEDegree = function(value) {
this.storeProperty(ABEDegreeProperty, value);
this.ABEdegree = value;
}
Stack.prototype.setRunSCNR = function(value) {
this.storeProperty(RunSCNRProperty, value);
this.scnr = value;
}
Stack.prototype.setSCNRAmount = function(value) {
this.storeProperty(SCNRAmountProperty, value);
this.scnrAmount = value;
}
Stack.prototype.setRunNoiseEval = function(value) {
this.storeProperty(RunNoiseEvalProperty, value);
this.runNoiseEval = value;
}
Stack.prototype.setPedestal = function(value) {
this.storeProperty(PedestalProperty, value);
this.pedestal = value;
}
Stack.prototype.getPropertyOrSettingOrDefault = function(property, setting, settingType, defValue) {
return getValueOrDefault(this.getProperty(property), readFromSettingsOrDefault(setting, settingType, defValue));
}
function Stack(parent, viewId = null) {
this.__base__ = Object;
this.mainViewId = viewId;
this.parent = parent;
this.processedFiles = [];
this.ignoredFiles = [];
this.newFiles = [];
this.fileName = null;
this.status = "Waiting for Image Selection";
this.calibrated = false;
this.watchingFolder = false;
this.filter = null;
this.export = false;
this.setPathToWatch(getValueOrDefault(this.getProperty(PathProperty), ""));
this.setBiasPath(this.getPropertyOrSettingOrDefault(BiasProperty, "EZLiveStack.BiasPath", 13, null));
this.setFlatPath(this.getPropertyOrSettingOrDefault(FlatProperty, "EZLiveStack.FlatPath", 13, null));
this.setDarkPath(this.getPropertyOrSettingOrDefault(DarkProperty, "EZLiveStack.DarkPath", 13, null));
this.setDownscaleImages(this.getPropertyOrSettingOrDefault(DownscaleProperty, "EZLiveStack.Downscale", 0, false));
this.setDownscaleAmount(this.getPropertyOrSettingOrDefault(DownscaleAmountProperty, "EZLiveStack.Downscale.Amount", 1, 2));
this.setOptimizeDark(this.getPropertyOrSettingOrDefault(DarkOptimizeProperty, "EZLiveStack.OptimizeDark", 0, false));
this.setDarkExposureTime(this.getPropertyOrSettingOrDefault(DarkExposureProperty, "EZLiveStack.OptimizeDark.DarkExpTime", 1, 1));
this.setCFA(this.getPropertyOrSettingOrDefault(ColorProperty, "EZLiveStack.ImageIsColor", 0, false));
this.setCFAPattern(this.getPropertyOrSettingOrDefault(BayerPatternProperty, "EZLiveStack.BayerMatrix", 1, 1));
this.setIgnoreFilter(this.getPropertyOrSettingOrDefault(IgnoreFilterProperty, "EZLiveStack.IgnoreFilter", 0, false));
this.setSaveCalibratedFile(this.getPropertyOrSettingOrDefault(SaveCalibratedFileProperty, "EZLiveStack.SaveCalibratedFiles", 0, false));
this.setSaveCompressed(this.getPropertyOrSettingOrDefault(SaveCompressedProperty, "EZLiveStack.SaveCalibratedFiles.Compress", 0, true));
this.setSaveAs16bit(this.getPropertyOrSettingOrDefault(SaveInt16Property, "EZLiveStack.SaveCalibratedFiles.Int16", 0, false));
this.setSaveDebayered(this.getPropertyOrSettingOrDefault(SaveDebayeredProperty, "EZLiveStack.SaveCalibratedFiles.Debayered", 0, true));
this.setRunABE(this.getPropertyOrSettingOrDefault(ABEProperty, "EZLiveStack.RunABE", 0, false));
this.setABEDegree(this.getPropertyOrSettingOrDefault(ABEDegreeProperty, "EZLiveStack.RunABE.Degree", 1, 2));
this.setRunSCNR(this.getPropertyOrSettingOrDefault(RunSCNRProperty, "EZLiveStack.RunSCNR", 0, false));
this.setSCNRAmount(this.getPropertyOrSettingOrDefault(SCNRAmountProperty, "EZLiveStack.RunSCNR.Amount", 10, 0.7));
this.setRunNoiseEval(this.getPropertyOrSettingOrDefault(RunNoiseEvalProperty, "EZLiveStack.RunNoiseEvaluation", 0, false));
this.setPedestal(this.getPropertyOrSettingOrDefault(PedestalProperty, "EZLiveStack.Pedestal", 1, 0));
}
// #endregion Stack
function execute() {
writeMessageBlock("Exporting Live Stack Image");
for (let i = 0; i < dialog.tabBox.numberOfPages; i++) {
dialog.tabBox.pageControlByIndex(i).stack.export = true;
}
}
function doStarAlign(view, newView) {
var sa = new StarAlignment;
sa.referenceImage = view.id;
return sa.executeOn(newView);
}
function doCalibrate(view, stack) {
if(stack.biasPath == null && stack.darkPath == null && stack.flatPath == null) {
return view.window;
}
startProcessing();
let expTime = view.propertyValue("Instrument:ExposureTime");
writeMessageStart("Calibrating " + view.id);
let biasImage = null;
let darkImage = null;
let flatImage = null;
let bias = 0;
let dark = 0;
let darkScaling = 1;
let flat = 1;
if (stack.biasPath != null) {
biasImage = readImage(stack.biasPath, true);
bias = biasImage.mainView.id;
writeMessageBlock("Calibrating with BIAS " + bias, false, true);
}
if (stack.darkPath != null) {
darkImage = readImage(stack.darkPath);
dark = darkImage.mainView.id;
writeMessageBlock("Calibrating with DARK " + dark, false, true);
// calculate scaling
if (stack.optimizeDark && stack.darkExposureTime != 0) {
darkScaling = expTime / stack.darkExposureTime;
}
}
if (stack.flatPath != null) {
flatImage = readImage(stack.flatPath);
flat = flatImage.mainView.id;
writeMessageBlock("Calibrating with FLAT " + flat, false, true);
}
//console.writeln("(({0}-{1})-({2}*{3}))/{4}".format(view.id, bias, dark, darkScaling, flat));
doPixelMath(view, "((({0}-{1})-({2}*{3}))/{4})*{5}".format(view.id, bias, dark, darkScaling, flat, (flat == 1 ? "1" : "mean(" + flat + ")")))
if(biasImage != null) {
biasImage.forceClose();
}
if(darkImage != null) {
darkImage.forceClose();
}
if(flatImage != null) {
flatImage.forceClose();
}
writeMessageEnd("Done calibration");
stopProcessing();
return view.window;
}
function doImageCalibrate(stack, newFiles) {
if((stack.biasPath == null && stack.darkPath == null && stack.flatPath == null) || newFiles.length == 0) {
return [];
}
let outputFrames = [];
let targetFrames = [];
for(var i = 0;i<newFiles.length;i++) {
let files = new FileFind();
let fileName = (newFiles[i].indexOf(".xisf") <= -1
? newFiles[i].replace(stack.fileExtension, ".xisf")
: newFiles[i]);
files.begin(stack.pathToWatch + "/" + (fileName.indexOf(CalibratedFilesPrefix) >= 0 ? fileName : CalibratedFilesPrefix + fileName));
if(files.isFile) {
targetFrames.push([false, stack.pathToWatch + "/" + newFiles[i]]);
} else {
targetFrames.push([true, stack.pathToWatch + "/" + newFiles[i]]);
}
files.end();
}
gc();
let hasFiles = false;
for (var i = 0;i<targetFrames.length;i++) {
if(targetFrames[i][0] == true) {
hasFiles = true;
break;
}
}
if (!hasFiles) {
return [];
}
let compression = "no-compress-data";
if(stack.saveCompressed) {
compression = "compress-data compression-codec lz4+sh";
}
var calib = new ImageCalibration;
calib.targetFrames = targetFrames;
calib.enableCFA = true;
calib.cfaPattern = ImageCalibration.prototype.Auto;
calib.inputHints = "fits-keywords normalize raw cfa signed-is-physical";
calib.outputHints = "properties fits-keywords no-embedded-data no-resolution " + compression;
calib.pedestal = stack.pedestal;
calib.pedestalMode = ImageCalibration.prototype.Keyword;
calib.pedestalKeyword = "";
calib.overscanEnabled = false;
calib.overscanImageX0 = 0;
calib.overscanImageY0 = 0;
calib.overscanImageX1 = 0;
calib.overscanImageY1 = 0;
calib.overscanRegions = [ // enabled, sourceX0, sourceY0, sourceX1, sourceY1, targetX0, targetY0, targetX1, targetY1
[false, 0, 0, 0, 0, 0, 0, 0, 0],
[false, 0, 0, 0, 0, 0, 0, 0, 0],
[false, 0, 0, 0, 0, 0, 0, 0, 0],
[false, 0, 0, 0, 0, 0, 0, 0, 0]
];
calib.masterBiasEnabled = stack.biasPath == null ? false : true;
calib.masterBiasPath = getValueOrDefault(stack.biasPath, "");
calib.masterDarkEnabled = stack.darkPath == null ? false : true;
calib.masterDarkPath = getValueOrDefault(stack.darkPath, "");
calib.masterFlatEnabled = stack.flatPath == null ? false : true;
calib.masterFlatPath = getValueOrDefault(stack.flatPath, "");
calib.calibrateBias = false;
calib.calibrateDark = false;
calib.calibrateFlat = false;
calib.optimizeDarks = stack.optimizeDark;
calib.darkOptimizationThreshold = 0.00000;
calib.darkOptimizationLow = 3.0000;
calib.darkOptimizationWindow = 1024;
calib.darkCFADetectionMode = ImageCalibration.prototype.DetectCFA;
calib.separateCFAFlatScalingFactors = true;
calib.flatScaleClippingFactor = 0.05;
calib.evaluateNoise = true;
calib.noiseEvaluationAlgorithm = ImageCalibration.prototype.NoiseEvaluation_MRS;
calib.outputDirectory = "";
calib.outputExtension = ".xisf";
calib.outputPrefix = "ezc_";
calib.outputPostfix = "";
calib.outputSampleFormat = stack.saveAs16BitInt ? ImageCalibration.prototype.i16 : ImageCalibration.prototype.f32;
calib.outputPedestal = 0;
calib.overwriteExistingFiles = true;
calib.onError = ImageCalibration.prototype.Continue;
calib.noGUIMessages = true;
calib.executeGlobal();
return outputFrames;
}
function evalNoiseOnStack(stack) {
startProcessing();
writeMessageStart("Evaluating noise");
processEvents();
let stackMainView = stack.getStackView();
let currentNoiseEval = stackMainView.propertyValue(NoiseEvalProperty);
if(currentNoiseEval == null) {
currentNoiseEval = "";
}
if(stack.isCFA) {
stackMainView = extractLightness(stackMainView);
}
let noiseEval = new EZ_ScaledNoiseEvaluation(stackMainView.image);
if(stack.isCFA) {
stackMainView.window.forceClose();
}
processEvents();
if(currentNoiseEval == "") {
currentNoiseEval = noiseEval.sigma;
} else {
currentNoiseEval = currentNoiseEval + ";" + noiseEval.sigma;
}
stack.storeProperty(NoiseEvalProperty, currentNoiseEval);
writeMessageEnd();
stopProcessing();
}
function doDebayerAndDownsample(view, stack, fullFile) {
if(!stack.isCFA && !stack.downscaleImages) {
return view;
}
startProcessing();
writeMessageStart("Processing image");
if (stack.isCFA) {
doImageDebayer(view, stack, fullFile);
if(stack.saveDebayered && stack.saveCalibratedFile) {
let newWindow = readImage(fullFile.replace(CalibratedFilesPrefix, DebayeredFilesPrefix + CalibratedFilesPrefix));
cloneProperties(view, newWindow.currentView);
view.window.forceClose();
view = newWindow.currentView;
} else {
cloneProperties(view, ImageWindow.activeWindow.mainView);
view.window.forceClose();
view = ImageWindow.activeWindow.currentView;
}
}
if(stack.downscaleImages) {
writeMessageBlock("Downscaling image");
var downSample = new IntegerResample;
downSample.zoomFactor = stack.downscaleImagesAmount * -1;
downSample.downsamplingMode = IntegerResample.prototype.Average;
downSample.xResolution = 72.000;
downSample.yResolution = 72.000;
downSample.metric = false;
downSample.forceResolution = false;
downSample.noGUIMessages = false;
view.storeProperty(DownscaleProperty, true);
downSample.executeOn(view);
}
writeMessageEnd("Done processing");
stopProcessing();
return view;
}
function doImageDebayer(view, stack, fullFile) {
let d = new Debayer();
d.evaluateNoise = false;
d.BayerPattern = stack.cfaPattern;
if (stack.saveDebayered && stack.saveCalibratedFile) {
let debayeredFile = fullFile.replace(CalibratedFilesPrefix, DebayeredFilesPrefix + CalibratedFilesPrefix);
let files = new FileFind();
files.begin(debayeredFile);
if(files.isFile) {
writeMessageBlock("Loading previously debayered image");
return;
}
d.evaluateNoise = true;
d.targetItems = [[true, fullFile]];
d.outputPrefix = "ezd_";
d.outputPostfix = "";
writeMessageBlock("Debayering and saving image");
d.executeGlobal();
} else {
writeMessageBlock("Debayering image");
d.executeOn(view);
}
}
var fileWatcher = new FileWatcher();
fileWatcher.onDirectoryChanged = function (dir, initial = false) {
// just wait for 1s unconditionally
waitForS(1);
// find all fitting stacks for the directory
let allFittingStacks = [];
for (let i = 0; i < dialog.tabBox.numberOfPages; i++) {
let tempStack = dialog.tabBox.pageControlByIndex(i).stack;
if (tempStack.pathToWatch == dir && tempStack.watchingFolder) {
allFittingStacks.push(tempStack);
}
}
// get all ignored or processed files to not process them again
let allProcessedFiles = [];
let allIgnoredFiles = [];
for(let i = 0;i<allFittingStacks.length;i++) {
for(let j = 0;j<allFittingStacks[i].processedFiles.length;j++) {
allProcessedFiles.push(allFittingStacks[i].processedFiles[j]);
}
for(let j = 0;j<allFittingStacks[i].ignoredFiles.length;j++) {
allIgnoredFiles.push(allFittingStacks[i].ignoredFiles[j]);
}
}
// find all new files in the directory
let files = new FileFind();
let newFiles = [];
files.begin(dir + "/*");
do {
if (files.isDirectory) { continue; }
let alreadyProcessed = allProcessedFiles.indexOf(files.name) > -1 || allIgnoredFiles.indexOf(files.name) > -1;
// ignore ezc_ and ezd_ files
if (!alreadyProcessed
&& files.name.indexOf(CalibratedFilesPrefix) <= -1
&& files.name.indexOf(DebayeredFilesPrefix) <= -1
&& files.name.indexOf('.') != 0) {
newFiles.push(files.name);
}
} while (files.next());
// fall out if there are no new files
if(newFiles.length == 0) return;
// check if any stacks are currently processing
for (let i = 0; i < dialog.tabBox.numberOfPages; i++) {
let tempStack = dialog.tabBox.pageControlByIndex(i).stack;
if(tempStack.isProcessingFiles) {
writeWarningBlock("Detected a new file but another stack is currently integrating, aborting");
return;
}
}
// set all related stacks to processing, they potentially are
for(let i = 0;i<allFittingStacks.length;i++) {
allFittingStacks[i].isProcessingFiles = true;
}
if(!initial) {
// wait for settletime as defined
let settleTime = readFromSettingsOrDefault("EZLiveStack.SettleTime", 1, 10);
writeMessageStart("Detected file changes in " + dir + ", settling " + settleTime + "s");
waitForS(settleTime);
}
newFiles = [];
// let's just search again and assign after the settletime...
writeMessageBlock("Determining stacks for new files + filter");
files.begin(dir + "/*");
do {
if (files.isDirectory) { continue; }
let alreadyProcessed = allProcessedFiles.indexOf(files.name) > -1 || allIgnoredFiles.indexOf(files.name) > -1;
// ignore ezc_ and ezd_ files
if (!alreadyProcessed
&& files.name.indexOf(CalibratedFilesPrefix) <= -1
&& files.name.indexOf(DebayeredFilesPrefix) <= -1
&& files.name.indexOf('.') != 0) {
let fullFile = dir + "/" + files.name;
let newFilter = getFilterFromFile(fullFile);
for(let j = 0;j<allFittingStacks.length;j++) {
let stack = allFittingStacks[j];
if (newFilter == stack.filter || stack.ignoreFilter) {
stack.processedFiles.push(files.name);
stack.newFiles.push(files.name);
}
}
}
} while (files.next());
processEvents();
// stack all new files into their respective stacks
for(let i = 0;i<allFittingStacks.length;i++) {
if(allFittingStacks[i].newFiles.length > 0) {
allFittingStacks[i].isProcessingFiles = true;
processEvents();
integrateFiles(allFittingStacks[i], false);
processEvents();
allFittingStacks[i].isProcessingFiles = false;
} else {
allFittingStacks[i].isProcessingFiles = false;
}
}
writeMessageEnd();
}
function getFilterFromFile(path) {
if(!(path in fileFilterDict)) {
console.noteln(path);
let newWindow = readImage(path);
let newFilter = newWindow.mainView.readPropertyOrKeyword("Instruments:Filter:Name", "FILTER");
fileFilterDict[path] = newFilter;
newWindow.forceClose();
return newFilter;
} else {
return fileFilterDict[path];
}
}
function stopWatchingFolder(stack) {
if (stack.watchingFolder && stack.pathToWatch != "") {
fileWatcher.removePath(stack.pathToWatch);
stack.watchingFolder = false;
writeMessageBlock("File watcher stopped monitoring " + stack.pathToWatch);
}
}
function startWatchingFolder(stack) {
stack.watchingFolder = true;
writeMessageStart("Initializing File Watcher on " + stack.pathToWatch);
if (!stack.calibrated) {
startProcessing();
let newView = null;
let fullFile = stack.pathToWatch + "/" + CalibratedFilesPrefix + stack.processedFiles[0].replace(stack.fileExtension, ".xisf");
// handle calibrated and non calibrated files
if(!stack.saveCalibratedFile) {
newView = doCalibrate(stack.getStackView(), stack).mainView;
} else {
doImageCalibrate(stack, [stack.processedFiles[0]]);
newView = readImage(fullFile).mainView;
cloneProperties(stack.getStackView(), newView);
stack.getStackView().window.forceClose();
newView.id = stack.mainViewId;
stack.parent.SetView(newView);
}
newView = doDebayerAndDownsample(newView, stack, fullFile);
stack.calibrated = true;
if (newView.id != stack.mainViewId) {
newView.window.hide();
newView.id = stack.mainViewId;
stack.parent.SetView(newView);
}
if(stack.runNoiseEval && stack.getProperty(NoiseEvalProperty) == null) {
evalNoiseOnStack(stack);
}
stopProcessing();
}
fileWatcher.addPath(stack.pathToWatch);
fileWatcher.onDirectoryChanged(stack.pathToWatch, true);
writeMessageEnd("File Watcher watching " + stack.pathToWatch);
stack.status = "Monitoring";
stack.isProcessingFiles = false;
}
function integrateFiles(stack) {
startProcessing();
stack.status = "Integrating";
writeMessageStart("Integrating files");
// calibrate when saving
if(stack.saveCalibratedFile) {
writeMessageBlock("Calibrating all files");
stack.status = "Calibrating and saving";
processEvents();
doImageCalibrate(stack, stack.newFiles);
}
writeMessageBlock("Integrating into stack");
let i = 0;
while (stack.newFiles.length > 0) {
let stackMainView = stack.getStackView();
i++;
let newFile = stack.newFiles.shift();
let fullFile = stack.pathToWatch + "/" + (stack.saveCalibratedFile
? CalibratedFilesPrefix +
(newFile.indexOf(".xisf") <= -1
? newFile.replace(stack.fileExtension, ".xisf")
: newFile)
: newFile);
fullFile = fullFile.replace(CalibratedFilesPrefix + CalibratedFilesPrefix, CalibratedFilesPrefix);
let prefix = "({0}/{1})".format(i, stack.newFiles.length + i);
let newWindow = null;
try {
newWindow = readImage(fullFile);
} catch (e) {
stack.processedFiles.removeItem(newFile);
stack.newFiles.removeItem(newFile);
stack.ignoredFiles.push(newFile);
writeWarningBlock("Failed to parse file into image. Ignoring file.");
continue;
}
processEvents();
if (!stack.saveCalibratedFile) {
stack.status = prefix + " Calibrating";
processEvents();
newWindow = doCalibrate(newWindow.mainView, stack);
}
stack.status = prefix + " Processing";
processEvents();
newWindow = doDebayerAndDownsample(newWindow.mainView, stack, fullFile).window;
stack.status = prefix + " Aligning";
processEvents();
// star align
if (!doStarAlign(stackMainView, newWindow.mainView)) {
newWindow.forceClose();
stack.processedFiles.removeItem(newFile);
stack.ignoredFiles.push(newFile);
writeWarningBlock("Could not align image, continuing...");
continue;
}
newWindow.forceClose();
newWindow = ImageWindow.activeWindow;
newWindow.mainView.id = "AlignedImage";
// integrate
stack.status = prefix + " Integrating";
processEvents();
doPixelMath(stackMainView,
"iif({0}>({1}*2), {1}, iif({0}==0, {1}, (({1}*{2})+{0})/{3}))".format(newWindow.mainView.id,
stackMainView.id,
(stack.processedFiles.length - stack.newFiles.length) - 1,
(stack.processedFiles.length - stack.newFiles.length)),
!stack.isCFA);
newWindow.forceClose();
let stackPropertyValue = stackMainView.propertyValue(StackProperty);
stackMainView.storeProperty(StackProperty,
(stackPropertyValue == null ? "" : (stackPropertyValue + ","))
+ newFile);
if(stack.runNoiseEval) {
evalNoiseOnStack(stack);
}
}
writeMessageEnd("Integration complete.");
stack.isProcessingFiles = false;
stack.newFiles = [];
stack.status = "Monitoring";
gc();
stopProcessing();
}
function colorStack(stack, viewR, viewG, viewB, doScnr = true) {
startProcessing();
// star align everything to R
if (!doStarAlign(viewR, viewG)) {
writeWarningBlock("Could not align G image, aborting");
stack.isProcessingFiles = false;
stopProcessing();
throw new Error("Could not align G channel");
}
let viewGAligned = ImageWindow.activeWindow.mainView;
if (!doStarAlign(viewR, viewB)) {
writeWarningBlock("Could not align B image, aborting");
viewGAligned.window.forceClose();
stack.isProcessingFiles = false;
stopProcessing();
throw new Error("Could not align B channel");
}
let viewBAligned = ImageWindow.activeWindow.mainView;
let channelCombo = new ChannelCombination();
channelCombo.channels = [
[true, viewR.id],
[true, viewGAligned.id],
[true, viewBAligned.id]
]
channelCombo.executeGlobal();
viewGAligned.window.forceClose();
viewBAligned.window.forceClose();
let combinedView = ImageWindow.activeWindow.mainView;
combinedView.id = "_ez_temp_LiveStack_color";
doSTF(combinedView, null, true);
if(doScnr) {
let scnr = new SCNR();
scnr.executeOn(combinedView);
}
stopProcessing();
return combinedView;
}
function setFilter(view, stack) {
let filter = view.readPropertyOrKeyword("Instrument:Filter:Name", "FILTER");
stack.filter = filter;
}
function customizeDialog() {
//#region Init Stuff
dialog.infoBox.text = "<b>EZ Live Stack:</b> Select a file in a folder as reference. All remaining files in the folder will be stacked. The folder will be watched for new files and stack them.";
dialog.onExit = function () {
for (let i = 0; i < dialog.tabBox.numberOfPages; i++) {
let stack = dialog.tabBox.pageControlByIndex(i).stack;
if (stack == null) continue;
stopWatchingFolder(stack);
if (stack.viewExists()) {
if (stack.export == false
&& stack.previousStackName == null) {
stack.getStackView().window.forceClose();
} else {
let view = stack.getStackView();
if (stack.previousStackName == null)
view.id = sanitizeViewId("_ez_LS_Filter_" + (stack.filter == null ? "None" : stack.filter) + "_" + new Date().toISOString());
else
view.id = stack.previousStackName;
view.window.show();
}
}
}
}
dialog.onEmptyMainView = function () { }
dialog.onSelectedMainView = function () { }
dialog.onEvaluate = function () { }
dialog.canEvaluate = function () {
return false;
}
dialog.canRun = function () {
return false;
}
dialog.bindings = function () {
}
dialog.isColorStacking = false;
dialog.doScnr = true;
dialog.mainViewSelector.hide();
//#endregion Init Stuff
dialog.tutorialPrerequisites = ["Readable folder with images"];
dialog.tutorialSteps = [
"Select a reference image with 'Start new live stack', images from that folder with the same filter (if able to read from metadata) will be stacked",
"Select calibration frames and debayer options",
"Press 'Start Watching Folder' to monitor the folder of the reference file for new files",
"Once a new image is detected, EZ Live Stack will align and integrate it into the current image",
"If you have multiple mono stacks running at the same time, assign the tabs to the R/G/B channels in '4. Live RGB Stack' and press 'Start RGB stacking'",
"If you want to continue with a stack later, select 'Export Current Live Stack'.",
"Selecting a view from the dropdown of a previous live stack will load in all previously used calibration files and settings.",
"<b>When loading in a previous live stack with calibration frames: make sure that calibration frames are still present on the same paths as you had them when you made a live stack or remove them</b>",
"Icon explanation; Square: idle, Circle: monitoring, Triangle: stacking "
];
dialog.selectMainReferenceButton = new PushButton(dialog);
with (dialog.selectMainReferenceButton) {
text = "Start new Live Stack";
toolTip = "Selects Main Reference File";
icon = dialog.scaledResource(":/icons/window-new.png");
bindings = function () {
this.enabled = true; //!getValueOrDefault(dialog.CurrentStack().watchingFolder, false);
}
onClick = function () {
let fileDialog = new OpenFileDialog();
fileDialog.multipleSelections = false;
fileDialog.loadImageFilters();
if (fileDialog.execute()) {
if(fileDialog.fileName.indexOf(DebayeredFilesPrefix) >= 0) {
dialog.showWarningDialog("Please select a non-debayered, non-calibrated file as reference, if calibrated or debayered frames are present the calibration and debayering will not happen again but use those files instead",
"Cannot use debayered or calibrated files as reference",
"Fine");
return;
}
if(fileDialog.fileName.indexOf(CalibratedFilesPrefix) >= 0) {
dialog.showWarningDialog("Please select a non-calibrated file as reference, if calibrated are present the calibration will not happen again but use those files instead",
"Cannot use calibrated files as reference",
"Fine");
return;
}
dialog.setMainReference(fileDialog.fileName);
}
this.hasFocus = false;
}
}
dialog.selectMonitoringDirectoryButton = new PushButton(dialog);
with (dialog.selectMonitoringDirectoryButton) {
text = "Change Monitoring Folder";
toolTip = "Adjusts the folder to monitor";
icon = dialog.scaledResource(":/icons/folder.png");
bindings = function () {
this.enabled = getValueOrDefault(dialog.CurrentStack().pathToWatch, null) != null
&& fileWatcher.directories.indexOf(getValueOrDefault(dialog.CurrentStack().pathToWatch, "default")) == -1;
}
onClick = function () {