forked from grapeot/PixInsightMonoScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreprocessing.js
More file actions
executable file
·358 lines (330 loc) · 12.1 KB
/
Preprocessing.js
File metadata and controls
executable file
·358 lines (330 loc) · 12.1 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
// Configurations. Change this before execution.
// WARNING: the master darks fed into this script should be UNCALIBRATED
// Simple integrate the dark frames to get the master dark
// This script only supports monochrome workflow because the current WBPP is already good enough for OSC cameras
let rootDir = 'D:/Media/Photos/2020/20201229 Heart/Raw';
let outDir = rootDir + '/../Test';
let darkLibrary = {
'-10C30s': "D:/Media/Photos/DarkFlatLibrary/Dark/6200MM/100gain30s_FF/MasterDarkUncalibrated.xisf",
'-10C45s': "D:/Media/Photos/DarkFlatLibrary/Dark/6200MM/100gain45s_FF/MasterDarkUncalibrated.xisf",
'-10C180s': "D:/Media/Photos/DarkFlatLibrary/Dark/6200MM/100gain45s_FF/MasterDarkUncalibrated.xisf",
'0C300s': "D:/Media/Photos/DarkFlatLibrary/Dark/6200MM/100gain300s_FF/MasterDarkUncalibrated.xisf"
};
let defaultBias = "D:/Media/Photos/DarkFlatLibrary/Bias/6200MM_100gain_FF/masterBias-BINNING_1.xisf";
let channels = [
{'channel': 'Sii', 'light': 'IC1805S', 'flat': 'IC1805S_Flat', 'dark': darkLibrary['-10C180s']},
{'channel': 'Oiii', 'light': 'IC1805O', 'flat': 'IC1805O_Flat', 'dark': darkLibrary['-10C180s']},
{'channel': 'Ha', 'light': 'IC1805Ha', 'flat': 'IC1805H_Flat', 'dark': darkLibrary['-10C180s']}
];
let main = function() {
let firstCalibratedLight = '';
let allRegisteredFrames = [];
for (let channel_i = 0; channel_i < channels.length; channel_i++) {
// Since we will do flat calibration in the light integration stage, we directly integrate to get the flat
let rawFlats = findFits(rootDir + '/' + channels[channel_i]['flat']);
let masterFlatFn = outDir + '/' + channels[channel_i]['channel'] + '_flat.xisf';
integrate(rawFlats, masterFlatFn);
// light calibration and integration
inFiles = findFits(rootDir + '/' + channels[channel_i]['light']);
let calibratedLights = calibrate(inFiles, outDir, defaultBias, channels[channel_i]['dark'], masterFlatFn);
if (firstCalibratedLight == '') {
firstCalibratedLight = calibratedLights[0];
}
// star alignment
let registeredLights = starAlign(calibratedLights, firstCalibratedLight, outDir);
// image integration
let masterLightFn = outDir + '/' + channels[channel_i]['channel'] + '_light.xisf';
integrate(registeredLights, masterLightFn);
console.noteln(channels);
// housekeeping
channels[channel_i]['registeredLights'] = registeredLights;
channels[channel_i]['masterLight'] = masterLightFn;
registeredLights.forEach(function(x) { allRegisteredFrames.push(x); });
}
// final L integration
let lightFrameFn = outDir + '/AllL_light.xisf';
integrate(allRegisteredFrames, lightFrameFn);
}
let findFits = function(dir) {
return searchDirectory(dir + '/*.fit');
};
// save an image to a file
let saveFile = function(filePath, imageWindow) {
let F = new FileFormat( ".xisf", false /*toRead*/ , true /*toWrite*/ );
if ( F.isNull )
throw new Error( "No installed file format can write " + ".xisf" + " files." ); // shouldn't happen
let f = new FileFormatInstance( F );
if ( f.isNull )
throw new Error( "Unable to instantiate file format: " + F.name );
let outputHints = "properties fits-keywords no-compress-data block-alignment 4096 max-inline-block-size 3072 no-embedded-data no-resolution up-bottom";
if ( !f.create( filePath, outputHints ) )
throw new Error( "Error creating output file: " + filePath );
let d = new ImageDescription;
d.bitsPerSample = 32;
d.ieeefpSampleFormat = true;
if ( !f.setOptions( d ) )
throw new Error( "Unable to set output file options: " + filePath );
if ( F.canStoreImageProperties )
if ( F.supportsViewProperties )
imageWindow.mainView.exportProperties( f );
if ( F.canStoreKeywords )
f.keywords = imageWindow.keywords;
if ( !f.writeImage( imageWindow.mainView.image ) )
throw new Error( "Error writing output file: " + filePath );
f.close();
};
let integrate = function(infiles, outfile) {
let inputs = [];
for (let i = 0; i < infiles.length; i++) {
// enabled, path, drizzlePath, localNormalizationDataPath
inputs.push([true, infiles[i], "", ""]);
}
let P = new ImageIntegration;
P.images = inputs;
P.inputHints = "fits-keywords normalize raw cfa signed-is-physical";
P.combination = ImageIntegration.prototype.Average;
P.weightMode = ImageIntegration.prototype.NoiseEvaluation;
P.weightKeyword = "";
P.weightScale = ImageIntegration.prototype.WeightScale_BWMV;
P.adaptiveGridSize = 16;
P.adaptiveNoScale = false;
P.ignoreNoiseKeywords = false;
P.normalization = ImageIntegration.prototype.AdditiveWithScaling;
P.rejection = ImageIntegration.prototype.WinsorizedSigmaClip;
P.rejectionNormalization = ImageIntegration.prototype.Scale;
P.minMaxLow = 1;
P.minMaxHigh = 1;
P.pcClipLow = 0.200;
P.pcClipHigh = 0.100;
P.sigmaLow = 4.000;
P.sigmaHigh = 3.000;
P.winsorizationCutoff = 5.000;
P.linearFitLow = 5.000;
P.linearFitHigh = 4.000;
P.esdOutliersFraction = 0.30;
P.esdAlpha = 0.05;
P.esdLowRelaxation = 1.50;
P.ccdGain = 1.00;
P.ccdReadNoise = 10.00;
P.ccdScaleNoise = 0.00;
P.clipLow = true;
P.clipHigh = true;
P.rangeClipLow = true;
P.rangeLow = 0.000000;
P.rangeClipHigh = false;
P.rangeHigh = 0.980000;
P.mapRangeRejection = true;
P.reportRangeRejection = false;
P.largeScaleClipLow = false;
P.largeScaleClipLowProtectedLayers = 2;
P.largeScaleClipLowGrowth = 2;
P.largeScaleClipHigh = false;
P.largeScaleClipHighProtectedLayers = 2;
P.largeScaleClipHighGrowth = 2;
P.generate64BitResult = false;
P.generateRejectionMaps = false;
P.generateIntegratedImage = true;
P.generateDrizzleData = false;
P.closePreviousImages = false;
P.bufferSizeMB = 16;
P.stackSizeMB = 1024;
P.autoMemorySize = true;
P.autoMemoryLimit = 0.75;
P.useROI = false;
P.roiX0 = 0;
P.roiY0 = 0;
P.roiX1 = 0;
P.roiY1 = 0;
P.useCache = true;
P.evaluateNoise = true;
P.mrsMinDataFraction = 0.010;
P.subtractPedestals = false;
P.truncateOnOutOfRange = false;
P.noGUIMessages = true;
P.showImages = false;
P.useFileThreads = true;
P.fileThreadOverload = 1.00;
P.useBufferThreads = true;
P.maxBufferThreads = 8;
let ok = P.executeGlobal();
if ( ok )
{
let window = ImageWindow.windowById( P.integrationImageId );
saveFile(outfile, window);
return outfile;
}
else
{
console.warningln( " ** Warning: Image Integration failed" );
}
}
let calibrate = function (infiles, outdir, bias="", dark="", flat="") {
let inputs = [];
for (let i = 0; i < infiles.length; i++) {
// enabled, path
inputs.push([true, infiles[i]]);
}
let P = new ImageCalibration;
P.targetFrames = inputs;
P.enableCFA = false;
P.cfaPattern = ImageCalibration.prototype.Auto;
P.inputHints = "fits-keywords normalize raw cfa signed-is-physical";
P.outputHints = "properties fits-keywords no-compress-data no-embedded-data no-resolution";
P.pedestal = 0;
P.pedestalMode = ImageCalibration.prototype.Keyword;
P.pedestalKeyword = "";
P.overscanEnabled = false;
P.overscanImageX0 = 0;
P.overscanImageY0 = 0;
P.overscanImageX1 = 0;
P.overscanImageY1 = 0;
P.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]
];
P.masterBiasEnabled = bias != "";
P.masterBiasPath = bias;
P.masterDarkEnabled = dark != "";
P.masterDarkPath = dark;
P.masterFlatEnabled = flat != "";
P.masterFlatPath = flat;
P.calibrateBias = false;
P.calibrateDark = true;
P.calibrateFlat = true;
P.optimizeDarks = true;
P.darkOptimizationThreshold = 0.00000;
P.darkOptimizationLow = 3.0000;
P.darkOptimizationWindow = 1024;
P.darkCFADetectionMode = ImageCalibration.prototype.DetectCFA;
P.separateCFAFlatScalingFactors = true;
P.flatScaleClippingFactor = 0.05;
P.evaluateNoise = true;
P.noiseEvaluationAlgorithm = ImageCalibration.prototype.NoiseEvaluation_MRS;
P.outputDirectory = outdir;
P.outputExtension = ".xisf";
P.outputPrefix = "";
P.outputPostfix = "_c";
P.outputSampleFormat = ImageCalibration.prototype.f32;
P.outputPedestal = 0;
P.overwriteExistingFiles = false;
P.onError = ImageCalibration.prototype.Continue;
P.noGUIMessages = true;
/*
* Read-only properties
*
P.outputData = [ // outputFilePath, darkScalingFactorRK, darkScalingFactorG, darkScalingFactorB, noiseEstimateRK, noiseEstimateG, noiseEstimateB, noiseFractionRK, noiseFractionG, noiseFractionB, noiseAlgorithmRK, noiseAlgorithmG, noiseAlgorithmB
];
*/
let ok = P.executeGlobal();
if ( ok )
{
let output = [];
for ( let j = 0; j < P.outputData.length; ++j )
output.push(P.outputData[j][0]);
return output;
}
else
{
console.warningln( " ** Warning: Image Calibration failed" );
}
};
let starAlign = function(infiles, refFile, outdir) {
let inputs = [];
for (let i = 0; i < infiles.length; i++) {
// enabled, isfile, path
inputs.push([true, true, infiles[i]]);
}
let P = new StarAlignment;
P.structureLayers = 5;
P.noiseLayers = 0;
P.hotPixelFilterRadius = 1;
P.noiseReductionFilterRadius = 0;
P.sensitivity = 0.100;
P.peakResponse = 0.80;
P.maxStarDistortion = 0.500;
P.upperLimit = 1.000;
P.invert = false;
P.distortionModel = "";
P.undistortedReference = false;
P.distortionCorrection = false;
P.distortionMaxIterations = 20;
P.distortionTolerance = 0.005;
P.distortionAmplitude = 2;
P.localDistortion = true;
P.localDistortionScale = 256;
P.localDistortionTolerance = 0.050;
P.localDistortionRejection = 2.50;
P.localDistortionRejectionWindow = 64;
P.localDistortionRegularization = 0.010;
P.matcherTolerance = 0.0500;
P.ransacTolerance = 2.00;
P.ransacMaxIterations = 2000;
P.ransacMaximizeInliers = 1.00;
P.ransacMaximizeOverlapping = 1.00;
P.ransacMaximizeRegularity = 1.00;
P.ransacMinimizeError = 1.00;
P.maxStars = 0;
P.fitPSF = StarAlignment.prototype.FitPSF_DistortionOnly;
P.psfTolerance = 0.50;
P.useTriangles = false;
P.polygonSides = 5;
P.descriptorsPerStar = 20;
P.restrictToPreviews = true;
P.intersection = StarAlignment.prototype.MosaicOnly;
P.useBrightnessRelations = false;
P.useScaleDifferences = false;
P.scaleTolerance = 0.100;
P.referenceImage = refFile;
P.referenceIsFile = true;
P.targets = inputs;
P.inputHints = "";
P.outputHints = "";
P.mode = StarAlignment.prototype.RegisterMatch;
P.writeKeywords = true;
P.generateMasks = false;
P.generateDrizzleData = false;
P.generateDistortionMaps = false;
P.frameAdaptation = false;
P.randomizeMosaic = false;
P.noGUIMessages = true;
P.useSurfaceSplines = false;
P.extrapolateLocalDistortion = true;
P.splineSmoothness = 0.050;
P.pixelInterpolation = StarAlignment.prototype.Auto;
P.clampingThreshold = 0.30;
P.outputDirectory = outdir;
P.outputExtension = ".xisf";
P.outputPrefix = "";
P.outputPostfix = "_r";
P.maskPostfix = "_m";
P.distortionMapPostfix = "_dm";
P.outputSampleFormat = StarAlignment.prototype.SameAsTarget;
P.overwriteExistingFiles = false;
P.onError = StarAlignment.prototype.Continue;
P.useFileThreads = true;
P.fileThreadOverload = 1.20;
P.maxFileReadThreads = 8;
P.maxFileWriteThreads = 8;
let ok = P.executeGlobal();
if (ok) {
let images = new Array;
for ( let c = 0; c < P.outputData.length; ++c )
{
let filePath = P.outputData[ c ][ 0 ]; // outputData.outputImage
if ( filePath != "" )
if ( File.exists( filePath ) )
images.push( filePath );
else
{
console.warningln( "** Warning: File does not exist after image registration: " + filePath );
}
}
return images;
}
else {
console.warningln( "** Warning: Error registering light frames." );
}
}
main();