forked from hpavlov/tangra3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrmDefineFitsCube3D.cs
403 lines (339 loc) · 15 KB
/
frmDefineFitsCube3D.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
using nom.tam.fits;
using Tangra.Controller;
using Tangra.Helpers;
using Tangra.Model.Config;
using Tangra.Model.Helpers;
namespace Tangra.Video.FITS
{
public partial class frmDefineFitsCube3D : Form
{
public FITSTimeStampReader TimeStampReader { get; private set; }
public int WidthIndex { get; private set; }
public int HeightIndex { get; private set; }
public int FrameIndex { get; private set; }
public short MinPixelValue { get; private set; }
public uint[] Pixels { get; private set; }
public int BZero { get; private set; }
public uint MaxPixelValue { get; private set; }
public bool HasNegativePixels { get; private set; }
public int BitPix { get; private set; }
public int NegPixCorrection { get; private set; }
public bool FlipVertically { get; private set; }
public bool FlipHorizontally { get; private set; }
private BasicHDU m_ImageHDU;
private bool m_PixelMappingReviewed;
private int m_BelowZeroCorr;
public frmDefineFitsCube3D()
{
InitializeComponent();
}
private VideoController m_VideoController;
private string m_FilesHash;
private string m_CardNamesHash;
private List<HeaderEntry> m_AllCards = new List<HeaderEntry>();
private FitsTimestampHelper m_TimeStampHelper;
public frmDefineFitsCube3D(BasicHDU imageHDU, string filesHash, VideoController videoController)
: this()
{
m_VideoController = videoController;
m_ImageHDU = imageHDU;
m_FilesHash = filesHash;
var hasher = new SHA1CryptoServiceProvider();
hasher.Initialize();
var orderedCardNames = new List<string>();
var cursor = m_ImageHDU.Header.GetCursor();
while (cursor.MoveNext())
{
var card = m_ImageHDU.Header.FindCard((string)cursor.Key);
if (card != null)
{
m_AllCards.Add(new HeaderEntry(card));
}
orderedCardNames.Add((string)cursor.Key);
}
orderedCardNames.Sort();
byte[] combinedCardNamesBytes = Encoding.UTF8.GetBytes(string.Join("|", orderedCardNames));
var hash = hasher.ComputeHash(combinedCardNamesBytes, 0, combinedCardNamesBytes.Length);
m_CardNamesHash = Convert.ToBase64String(hash);
cbxNaxisOrder.SelectedIndex = 0;
cbxExposure.Items.AddRange(m_AllCards.ToArray());
cbxExposureUnits.Items.Clear();
cbxExposureUnits.Items.AddRange(Enum.GetNames(typeof(TangraConfig.ExposureUnit)));
cbxExposureUnits.SelectedIndex = 0;
m_TimeStampHelper = new FitsTimestampHelper(m_FilesHash, m_AllCards, UseRecentFitsConfig);
ucTimestampControl.Initialise(m_AllCards, m_FilesHash, m_CardNamesHash, m_TimeStampHelper);
m_TimeStampHelper.TryIdentifyPreviousConfigApplyingForCurrentFiles();
}
private void UpdateSizesLabels()
{
lblFrames.Text = m_ImageHDU.Axes[FrameIndex].ToString();
lblHeight.Text = m_ImageHDU.Axes[HeightIndex].ToString();
lblWidth.Text = m_ImageHDU.Axes[WidthIndex].ToString();
short minPixelValue;
uint maxPixelValue;
bool hasNegativePixels;
int bpp;
int bzero;
uint[] pixels;
m_VideoController.SetCursor(Cursors.WaitCursor);
try
{
ParseFirstFrame(out pixels, out minPixelValue, out maxPixelValue, out bpp, out bzero, out hasNegativePixels);
}
finally
{
m_VideoController.SetCursor(Cursors.Default);
}
if (hasNegativePixels)
{
NegPixCorrection = minPixelValue;
}
else
{
// No requirement to review the pixel mapping if there are no negative pixels
// We can go with the defaults
m_PixelMappingReviewed = true;
NegPixCorrection = 0;
}
Pixels = pixels;
MinPixelValue = minPixelValue;
MaxPixelValue = maxPixelValue;
BZero = bzero;
HasNegativePixels = hasNegativePixels;
BitPix = bpp;
}
private void cbxNaxisOrder_SelectedIndexChanged(object sender, EventArgs e)
{
switch (cbxNaxisOrder.SelectedIndex)
{
case 0:
FrameIndex = 0;
HeightIndex = 1;
WidthIndex = 2;
break;
case 1:
FrameIndex = 0;
WidthIndex = 1;
HeightIndex = 2;
break;
case 2:
WidthIndex = 0;
HeightIndex = 1;
FrameIndex = 2;
break;
case 3:
HeightIndex = 0;
WidthIndex = 1;
FrameIndex = 2;
break;
case 4:
WidthIndex = 0;
FrameIndex = 1;
HeightIndex = 2;
break;
case 5:
HeightIndex = 0;
FrameIndex = 1;
WidthIndex = 2;
break;
}
UpdateSizesLabels();
}
private void cbxExposure_SelectedIndexChanged(object sender, EventArgs e)
{
var entry = cbxExposure.SelectedItem as HeaderEntry;
if (entry != null)
tbxExposureValue.Text = entry.Card.ToString();
else
tbxExposureValue.Text = string.Empty;
VerifyExposure();
}
private bool m_ExposureValid = false;
private void VerifyExposure()
{
m_ExposureValid = false;
var entry = cbxExposure.SelectedItem as HeaderEntry;
if (entry != null)
{
string value = entry.Card.Value;
m_ExposureValid = m_TimeStampHelper.VerifyExposure(value);
}
if (m_ExposureValid)
pbxExposureOK.BringToFront();
else
pbxExposureWarning.BringToFront();
}
private void UseRecentFitsConfig(TangraConfig.FITSFieldConfig config)
{
if (config.ExposureHeader != null)
{
for (int i = 0; i < cbxExposure.Items.Count; i++)
{
if (((HeaderEntry)cbxExposure.Items[i]).Card.Key == config.ExposureHeader)
{
cbxExposure.SelectedIndex = i;
break;
}
}
}
ucTimestampControl.UseRecentFitsConfig(config);
cbxFlipHorizontally.Checked = config.FlipHorizontally;
cbxFlipVertically.Checked = config.FlipVertically;
if (config.FileHash == m_FilesHash || m_CardNamesHash == config.CardNamesHash)
{
cbxExposureUnits.SelectedIndex = (int)config.ExposureUnit;
}
else
{
cbxExposureUnits.SelectedIndex = -1;
}
}
private void btnOK_Click(object sender, EventArgs e)
{
if (!ucTimestampControl.ValidateInput())
return;
if (!m_ExposureValid)
{
MessageBox.Show("Please choose valid settings for the exposure.");
return;
}
if (cbxExposureUnits.SelectedIndex == -1)
{
MessageBox.Show("Please choose exposure units.");
cbxExposureUnits.Focus();
return;
}
if (HasNegativePixels && !m_PixelMappingReviewed)
{
if (!ReviewPixelMapping())
{
MessageBox.Show("As there are negative pixels you need to confirm the pixel value mapping before continuing.");
btnPixelValueMapping.Focus();
return;
}
}
var singleTimeStampConfig = ucTimestampControl.GetSelectedInput();
var config = new TangraConfig.FITSFieldConfig()
{
ExposureHeader = cbxExposure.Text,
ExposureUnit = (TangraConfig.ExposureUnit)cbxExposureUnits.SelectedIndex,
IsTimeStampAndExposure = true,
TimeStampIsDateTimeParts = singleTimeStampConfig.TimeStampIsDateTimeParts,
TimeStampHeader = singleTimeStampConfig.TimeStampHeader,
TimeStampFormat = singleTimeStampConfig.TimeStampFormat,
TimeStampHeader2 = singleTimeStampConfig.TimeStampHeader2,
TimeStampFormat2 = singleTimeStampConfig.TimeStampFormat2,
TimeStampType = singleTimeStampConfig.TimeStampType,
FlipHorizontally = cbxFlipHorizontally.Checked,
FlipVertically = cbxFlipVertically.Checked
};
TimeStampReader = new FITSTimeStampReader(config);
config.FileHash = m_FilesHash;
config.CardNamesHash = m_CardNamesHash;
TangraConfig.Settings.RecentFITSFieldConfig.Register(config);
TangraConfig.Settings.Save();
FlipVertically = cbxFlipVertically.Checked;
FlipHorizontally = cbxFlipHorizontally.Checked;
DialogResult = DialogResult.OK;
Close();
}
private bool ReviewPixelMapping()
{
var frm = new frmDefinePixelMapping(Pixels, NegPixCorrection, BZero, MinPixelValue, MaxPixelValue, BitPix);
if (m_VideoController.ShowDialog(frm) != DialogResult.OK)
return false;
NegPixCorrection = frm.NegPixCorrection;
MaxPixelValue = frm.MaxPixelValue;
BitPix = frm.BitPix;
m_PixelMappingReviewed = true;
return true;
}
private bool Load16BitFitsFileWithNegativePixels(string fileName, IFITSTimeStampReader timeStampReader,
out uint[,] pixels, out int width, out int height, out uint medianValue, out Type pixelDataType,
out bool hasNegativePixels, out short minValue, out uint maxValue,
FITSHelper.CheckOpenedFitsFileCallback callback)
{
m_BelowZeroCorr = Math.Max(BZero, Math.Abs(MinPixelValue) - BZero);
return Load16BitFitsFileImpl(fileName, timeStampReader,
out pixels, out width, out height, out medianValue, out pixelDataType, out hasNegativePixels,
out minValue, out maxValue,
callback, m_BelowZeroCorr);
}
private bool Load16BitFitsFile(string fileName, IFITSTimeStampReader timeStampReader,
out uint[,] pixels, out int width, out int height, out uint medianValue, out Type pixelDataType,
out bool hasNegativePixels, out short minValue, out uint maxValue,
FITSHelper.CheckOpenedFitsFileCallback callback)
{
return Load16BitFitsFileImpl(fileName, timeStampReader,
out pixels, out width, out height, out medianValue, out pixelDataType, out hasNegativePixels,
out minValue, out maxValue,
callback, null);
}
private bool Load16BitFitsFileImpl(string fileName, IFITSTimeStampReader timeStampReader,
out uint[,] pixels, out int width, out int height, out uint medianValue, out Type pixelDataType, out bool hasNegativePixels, out short minValue, out uint maxValue,
FITSHelper.CheckOpenedFitsFileCallback callback, int? negPixelsBZero)
{
float frameExposure;
width = m_ImageHDU.Axes[WidthIndex];
height = m_ImageHDU.Axes[HeightIndex];
return FITSHelper.LoadFitsDataInternal(
m_ImageHDU,
GetFirstFramePixelArray(), fileName, timeStampReader,
out pixels, out medianValue, out pixelDataType, out frameExposure, out hasNegativePixels, out minValue, out maxValue, callback,
(Array dataArray, int h, int w, double bzero, out uint[,] ppx, out uint median, out Type dataType, out bool hasNegPix, out short minV, out uint maxV) =>
{
ppx = FITSHelper.Load16BitImageData(dataArray, m_ImageHDU.Axes[HeightIndex], m_ImageHDU.Axes[WidthIndex], negPixelsBZero ?? (int)bzero, out median, out dataType, out hasNegPix, out minV, out maxV);
});
}
private Array GetFirstFramePixelArray()
{
return FITSHelper2.GetPixelsFrom3DCube(
(Array)m_ImageHDU.Data.DataArray, 0,
FrameIndex, HeightIndex, WidthIndex,
m_ImageHDU.Axes[FrameIndex], m_ImageHDU.Axes[HeightIndex], m_ImageHDU.Axes[WidthIndex]);
}
private void ParseFirstFrame(out uint[] pixelsFlat, out short minPixelValue, out uint maxPixelValue, out int bpp, out int bzero, out bool hasNegativePixels)
{
int width;
int height;
DateTime? timestamp;
double? exposure;
int bz = 0;
var cards = new Dictionary<string, string>();
FITSHelper.Load16BitFitsFile(null, Load16BitFitsFile, null,
(hdu) =>
{
var cursor = hdu.Header.GetCursor();
bz = FITSHelper.GetBZero(hdu);
while (cursor.MoveNext())
{
HeaderCard card = hdu.Header.FindCard((string)cursor.Key);
if (card != null && !string.IsNullOrWhiteSpace(card.Key) && card.Key != "END")
{
if (cards.ContainsKey(card.Key))
cards[card.Key] += "\r\n" + card.Value;
else
cards.Add(card.Key, card.Value);
}
}
}, out pixelsFlat, out width, out height, out bpp, out timestamp, out exposure, out minPixelValue, out maxPixelValue, out hasNegativePixels);
bzero = bz;
MinPixelValue = minPixelValue;
BZero = bz;
FITSHelper.Load16BitFitsFile(null, Load16BitFitsFileWithNegativePixels, null, null, out pixelsFlat, out width, out height, out bpp, out timestamp, out exposure, out minPixelValue, out maxPixelValue, out hasNegativePixels);
}
private void btnPixelValueMapping_Click(object sender, EventArgs e)
{
ReviewPixelMapping();
}
}
}