/
cu_camera.pas
785 lines (755 loc) · 32.2 KB
/
cu_camera.pas
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
unit cu_camera;
{$mode objfpc}{$H+}
{
Copyright (C) 2015 Patrick Chevalley
http://www.ap-i.net
pch@ap-i.net
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 <http://www.gnu.org/licenses/>.
}
// {$define camera_debug}
interface
uses cu_fits, cu_mount, cu_wheel, cu_focuser, u_global, u_utils, indiapi, math, u_translation,
LCLVersion, Classes, Forms, SysUtils, ExtCtrls;
type
TVideoRecordMode=(rmDuration,rmFrame,rmUnlimited);
T_camera = class(TComponent)
protected
FCameraInterface: TDevInterface;
FStatus: TDeviceStatus;
FWheelStatus: TDeviceStatus;
FonMsg,FonDeviceMsg: TNotifyMsg;
FonExposureProgress: TNotifyNum;
FonFilterChange: TNotifyNum;
FonFrameChange: TNotifyEvent;
FonTemperatureChange: TNotifyNum;
FonCoolerChange: TNotifyBool;
FonStatusChange: TNotifyEvent;
FonFilterNameChange: TNotifyEvent;
FonWheelStatusChange: TNotifyEvent;
FonNewImage, FonNewExposure: TNotifyEvent;
FonVideoFrame: TNotifyEvent;
FonAbortExposure,FonCameraDisconnected: TNotifyEvent;
FonVideoPreviewChange,FonVideoSizeChange,FonVideoRateChange: TNotifyEvent;
FonFPSChange,FonVideoExposureChange : TNotifyEvent;
FImgStream: TMemoryStream;
FVideoStream: TMemoryStream;
FFilterNames: TStringList;
FObjectName: string;
Fdevice: string;
FImageFormat: string;
FCameraXSize,FCameraYSize: integer;
FFits: TFits;
FStackCount: integer;
FStackAlign: boolean;
FStackAlignX,FStackAlignY,FStackStarX,FStackStarY: double;
FMount: T_mount;
Fwheel: T_wheel;
FFocuser: T_focuser;
FTimeOut: integer;
FAutoLoadConfig: boolean;
FhasVideo: boolean;
FVerticalFlip: boolean;
FASCOMFlipImage: boolean;
FAddFrames: boolean;
FVideoSizes, FVideoRates:TStringList;
FTemperatureRampActive, FCancelTemperatureRamp: boolean;
FIndiTransfert: TIndiTransfert;
FIndiTransfertDir,FIndiTransfertPrefix: string;
FhasGain,FhasGainISO,FCanSetGain,FhasCfaInfo: boolean;
FGainMin, FGainMax: integer;
FISOList: TStringList;
FhasFastReadout, FhasReadOut: boolean;
FReadOutList: TStringList;
Ftimestart,Ftimeend,FMidExposureTime: double;
FImgNum:PtrInt;
Fexptime: double;
procedure msg(txt: string; level:integer=3);
procedure NewImage;
procedure TryNextExposure(Data: PtrInt);
procedure WriteHeaders;
procedure NewVideoFrame;
procedure WriteVideoHeader(width,height,naxis,bitpix: integer);
function GetBinX:integer; virtual; abstract;
function GetBinY:integer; virtual; abstract;
procedure SetFrametype(f:TFrameType); virtual; abstract;
function GetFrametype:TFrameType; virtual; abstract;
function GetBinXrange:TNumRange; virtual; abstract;
function GetBinYrange:TNumRange; virtual; abstract;
function GetExposureRange:TNumRange; virtual; abstract;
function GetTemperatureRange:TNumRange; virtual; abstract;
function GetTemperature: double; virtual; abstract;
procedure SetTemperature(value:double); virtual; abstract;
procedure SetTemperatureRamp(value:double);
procedure SetTemperatureRampAsync(Data: PtrInt);
function GetCooler: boolean; virtual; abstract;
procedure SetCooler(value:boolean); virtual; abstract;
procedure SetFilter(num:integer); virtual; abstract;
function GetFilter:integer; virtual; abstract;
procedure SetFilterNames(value:TStringList); virtual; abstract;
function GetMaxX: double; virtual; abstract;
function GetMaxY: double; virtual; abstract;
function GetMaxADU: double; virtual; abstract;
function GetPixelSize: double; virtual; abstract;
function GetPixelSizeX: double; virtual; abstract;
function GetPixelSizeY: double; virtual; abstract;
function GetBitperPixel: double; virtual; abstract;
function GetColor: boolean; virtual; abstract;
function GetImageFormat: string; virtual; abstract;
procedure SetTimeout(num:integer); virtual; abstract;
function GetVideoPreviewRunning: boolean; virtual; abstract;
function GetMissedFrameCount: cardinal; virtual; abstract;
function GetVideoRecordDuration:integer; virtual; abstract;
procedure SetVideoRecordDuration(value:integer); virtual; abstract;
function GetVideoRecordFrames:integer; virtual; abstract;
procedure SetVideoRecordFrames(value:integer); virtual; abstract;
function GetVideoSize:string;virtual; abstract;
procedure SetVideoSize(value:string); virtual; abstract;
function GetVideoRate:string;virtual; abstract;
procedure SetVideoRate(value:string); virtual; abstract;
function GetFPS:double;virtual; abstract;
function GetVideoRecordDir:string; virtual; abstract;
procedure SetVideoRecordDir(value:string); virtual; abstract;
function GetVideoRecordFile:string; virtual; abstract;
procedure SetVideoRecordFile(value:string); virtual; abstract;
function GetVideoExposure:integer; virtual; abstract;
function GetVideoGain:integer; virtual; abstract;
function GetVideoGamma:integer; virtual; abstract;
function GetVideoBrightness:integer; virtual; abstract;
procedure SetVideoExposure(value:integer); virtual; abstract;
procedure SetVideoGain(value:integer); virtual; abstract;
procedure SetVideoGamma(value:integer); virtual; abstract;
procedure SetVideoBrightness(value:integer); virtual; abstract;
function GetVideoExposureRange:TNumRange; virtual; abstract;
function GetVideoGainRange:TNumRange; virtual; abstract;
function GetVideoGammaRange:TNumRange; virtual; abstract;
function GetVideoBrightnessRange:TNumRange; virtual; abstract;
function GetVideoPreviewDivisor:integer; virtual; abstract;
procedure SetVideoPreviewDivisor(value:integer); virtual; abstract;
procedure SetGain(value: integer); virtual; abstract;
function GetGain: integer; virtual; abstract;
procedure SetReadOutMode(value: integer); virtual; abstract;
function GetReadOutMode: integer; virtual; abstract;
private
lockvideoframe: boolean;
TempFinal: Double;
TempNow,TempRamp: double;
Nstep: integer;
RampTimer: TTimer;
procedure RampTimerTimer(Sender: TObject);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
Procedure Connect(cp1: string; cp2:string=''; cp3:string=''; cp4:string=''; cp5:string=''; cp6:string=''); virtual; abstract;
Procedure Disconnect; virtual; abstract;
Procedure SetBinning(binX,binY: integer); virtual; abstract;
Procedure StartExposure(exptime: double); virtual; abstract;
Procedure AbortExposure; virtual; abstract;
procedure SetFrame(x,y,width,height: integer); virtual; abstract;
procedure GetFrame(out x,y,width,height: integer; refresh:boolean=false); virtual; abstract;
procedure GetFrameRange(out xr,yr,widthr,heightr: TNumRange); virtual; abstract;
procedure ResetFrame; virtual; abstract;
procedure CfaInfo(out OffsetX, OffsetY: integer; out CType: string); virtual; abstract;
function CheckGain: boolean; virtual; abstract;
Procedure SetActiveDevices(focuser,filters,telescope: string); virtual; abstract;
procedure StartVideoPreview; virtual; abstract;
procedure StopVideoPreview; virtual; abstract;
procedure StartVideoRecord(mode:TVideoRecordMode); virtual; abstract;
procedure StopVideoRecord; virtual; abstract;
property DeviceName: string read FDevice;
property Fits: TFits read FFits write FFits;
property Mount: T_mount read FMount write FMount;
property Wheel: T_wheel read Fwheel write Fwheel;
property Focuser: T_focuser read FFocuser write FFocuser;
property ObjectName: string read FObjectName write FObjectName;
property CameraInterface: TDevInterface read FCameraInterface;
property Status: TDeviceStatus read FStatus;
property WheelStatus: TDeviceStatus read FWheelStatus;
property ImgStream: TMemoryStream read FImgStream;
property ImageFormat: string read GetImageFormat;
property AddFrames: boolean read FAddFrames write FAddFrames;
property VerticalFlip: boolean read FVerticalFlip;
property ASCOMFlipImage: boolean read FASCOMFlipImage write FASCOMFlipImage;
property hasVideo: boolean read FhasVideo;
property VideoStream: TMemoryStream read FVideoStream;
property VideoPreviewRunning: boolean read GetVideoPreviewRunning;
property MissedFrameCount: Cardinal read GetMissedFrameCount;
property VideoRecordDuration: integer read GetVideoRecordDuration write SetVideoRecordDuration;
property VideoRecordFrames: integer read GetVideoRecordFrames write SetVideoRecordFrames;
property VideoSizes:TStringList read FVideoSizes;
property VideoRates:TStringList read FVideoRates;
property VideoSize:string read GetVideoSize write SetVideoSize;
property VideoRate:string read GetVideoRate write SetVideoRate;
property VideoRecordDir:string read GetVideoRecordDir write SetVideoRecordDir;
property VideoRecordFile:string read GetVideoRecordFile write SetVideoRecordFile;
property VideoExposure: integer read GetVideoExposure write SetVideoExposure;
property VideoExposureRange: TNumRange read GetVideoExposureRange;
property VideoGain: integer read GetVideoGain write SetVideoGain;
property VideoGainRange: TNumRange read GetVideoGainRange;
property VideoGamma: integer read GetVideoGamma write SetVideoGamma;
property VideoGammaRange: TNumRange read GetVideoGammaRange;
property VideoBrightness: integer read GetVideoBrightness write SetVideoBrightness;
property VideoBrightnessRange: TNumRange read GetVideoBrightnessRange;
property VideoPreviewDivisor: integer read GetVideoPreviewDivisor write SetVideoPreviewDivisor;
property Cooler: boolean read GetCooler write SetCooler;
property Temperature: double read GetTemperature write SetTemperatureRamp;
property TemperatureRampActive: Boolean read FTemperatureRampActive;
property BinX: Integer read getBinX;
property BinY: Integer read getBinY;
property FrameType: TFrameType read GetFrametype write SetFrametype;
property BinXrange: TNumRange read GetbinXrange;
property BinYrange: TNumRange read GetbinYrange;
property ExposureRange: TNumRange read GetExposureRange;
property TemperatureRange: TNumRange read GetTemperatureRange;
property MaxX: double read GetMaxX;
property MaxY: double read GetMaxY;
property MaxADU: double read GetMaxADU;
property PixelSize: double read GetPixelSize;
property PixelSizeX: double read GetPixelSizeX;
property PixelSizeY: double read GetPixelSizeY;
property BitperPixel: double read GetBitperPixel;
property Color: boolean read GetColor;
property FPS: double read GetFPS;
property StackCount: integer read FStackCount;
property Filter: integer read GetFilter write SetFilter;
property FilterNames: TStringList read FFilterNames write SetFilterNames;
property Timeout: integer read FTimeout write SetTimeout;
property AutoLoadConfig: boolean read FAutoLoadConfig write FAutoLoadConfig;
property IndiTransfert: TIndiTransfert read FIndiTransfert write FIndiTransfert;
property IndiTransfertDir: string read FIndiTransfertDir write FIndiTransfertDir;
property Gain: integer read GetGain write SetGain;
property CanSetGain: boolean read FCanSetGain write FCanSetGain;
property hasGain: boolean read FhasGain;
property GainMin: integer read FGainMin;
property GainMax: integer read FGainMax;
property hasGainISO: boolean read FhasGainISO;
property ISOList: TStringList read FISOList;
property LastExposureTime:double read Fexptime;
property hasCfaInfo: boolean read FhasCfaInfo;
property hasReadOut: boolean read FhasReadOut;
property ReadOutList: TStringList read FReadOutList;
property ReadOutMode: integer read GetReadOutMode write SetReadOutMode;
property onMsg: TNotifyMsg read FonMsg write FonMsg;
property onDeviceMsg: TNotifyMsg read FonDeviceMsg write FonDeviceMsg;
property onExposureProgress: TNotifyNum read FonExposureProgress write FonExposureProgress;
property onTemperatureChange: TNotifyNum read FonTemperatureChange write FonTemperatureChange;
property onCoolerChange: TNotifyBool read FonCoolerChange write FonCoolerChange;
property onFilterChange: TNotifyNum read FonFilterChange write FonFilterChange;
property onStatusChange: TNotifyEvent read FonStatusChange write FonStatusChange;
property onFrameChange: TNotifyEvent read FonFrameChange write FonFrameChange;
property onFilterNameChange: TNotifyEvent read FonFilterNameChange write FonFilterNameChange;
property onWheelStatusChange: TNotifyEvent read FonWheelStatusChange write FonWheelStatusChange;
property onNewImage: TNotifyEvent read FonNewImage write FonNewImage;
property onNewExposure: TNotifyEvent read FonNewExposure write FonNewExposure;
property onVideoFrame: TNotifyEvent read FonVideoFrame write FonVideoFrame;
property onCameraDisconnected: TNotifyEvent read FonCameraDisconnected write FonCameraDisconnected;
property onAbortExposure: TNotifyEvent read FonAbortExposure write FonAbortExposure;
property onVideoPreviewChange: TNotifyEvent read FonVideoPreviewChange write FonVideoPreviewChange;
property onVideoSizeChange: TNotifyEvent read FonVideoSizeChange write FonVideoSizeChange;
property onVideoRateChange: TNotifyEvent read FonVideoRateChange write FonVideoRateChange;
property onFPSChange: TNotifyEvent read FonFPSChange write FonFPSChange;
property onVideoExposureChange: TNotifyEvent read FonVideoExposureChange write FonVideoExposureChange;
end;
const CameraTimeout=60; // in seconds, must be enough to download image from any camers
implementation
uses
{$if lcl_major > 1}
LazSysUtils;
{$else}
LazUTF8SysUtils;
{$endif}
constructor T_camera.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FImgNum:=0;
FTimeOut:=100;
FIndiTransfert:=itNetwork;
FIndiTransfertDir:=defTransfertPath;
FIndiTransfertPrefix:='ccdciel_tmp';
FVerticalFlip:=false;
FASCOMFlipImage:=false;
FStatus := devDisconnected;
FFilterNames:=TStringList.Create;
FImgStream:=TMemoryStream.Create;
FAddFrames:=false;
FhasVideo:=false;
FVideoStream:=TMemoryStream.Create;;
lockvideoframe:=false;
FVideoSizes:=TStringList.Create;
FVideoRates:=TStringList.Create;
FTemperatureRampActive:=false;
FCancelTemperatureRamp:=false;
FStackCount:=0;
FISOList:=TStringList.Create;
FGainMin:=0;
FGainMax:=0;
FhasGain:=false;
FhasGainISO:=false;
FhasCfaInfo:=false;
FReadOutList:=TStringList.Create;
FhasFastReadout:=false;
FhasReadOut:=false;
FImageFormat:='.fits';
Fexptime:=0;
RampTimer:=TTimer.Create(self);
RampTimer.Enabled:=false;
RampTimer.Interval:=1000;
RampTimer.OnTimer:=@RampTimerTimer;
end;
destructor T_camera.Destroy;
begin
FImgStream.Free;
FFilterNames.Free;
FVideoStream.Free;
FVideoSizes.Free;
FVideoRates.Free;
FISOList.Free;
FReadOutList.Free;
inherited Destroy;
end;
procedure T_camera.msg(txt: string; level:integer=3);
begin
if Assigned(FonMsg) then FonMsg(Fdevice+': '+txt,level);
end;
procedure T_camera.SetTemperatureRamp(value:double);
begin
if TemperatureSlope>0 then begin
TempFinal:=value;
Application.QueueAsyncCall(@SetTemperatureRampAsync,0);
end else begin
msg(Format(rsSetTemperatu, [formatfloat(f1, TempDisplay(TemperatureScale,value))+TempLabel]));
SetTemperature(value);
end;
end;
procedure T_camera.SetTemperatureRampAsync(Data: PtrInt);
var TempStart,tsl: double;
TempStep: integer;
begin
if TemperatureScale=0 then
tsl:=TemperatureSlope
else
tsl:=TemperatureSlope*5/9; // F -> C
TempStep:=round(60.0/tsl);
if TempStep<1 then TempStep:=1;
if FTemperatureRampActive then begin
FCancelTemperatureRamp:=true;
msg(rsTemperatureR,1);
exit;
end;
msg(Format(rsSetTemperatu2, [formatfloat(f1, TempDisplay(TemperatureScale,TempFinal))+TempLabel]));
FTemperatureRampActive:=true;
TempStart:=GetTemperature;
TempNow:=TempStart;
if TempFinal>TempStart then
TempRamp:=1.0
else
TempRamp:=-1.0;
Nstep:=round(abs(TempStart-TempFinal));
TempNow:=TempNow+TempRamp;
SetTemperature(TempNow);
dec(Nstep);
RampTimer.Interval:=TempStep*1000;
RampTimer.Enabled:=true;
end;
procedure T_camera.RampTimerTimer(Sender: TObject);
begin
RampTimer.Enabled:=false;
if Nstep>0 then begin
if FCancelTemperatureRamp then begin
FCancelTemperatureRamp:=false;
FTemperatureRampActive:=false;
msg(rsTemperatureR2,0);
if Assigned(FonTemperatureChange) then FonTemperatureChange(GetTemperature);
exit;
end;
TempNow:=TempNow+TempRamp;
SetTemperature(TempNow);
dec(Nstep);
RampTimer.Enabled:=true;
end
else begin
SetTemperature(TempFinal);
msg(rsSetTemperatu3);
FTemperatureRampActive:=false;
if Assigned(FonTemperatureChange) then FonTemperatureChange(GetTemperature);
end;
end;
procedure T_camera.NewImage;
var f:TFits;
xi,yi,xc,yc,ri: integer;
xs,ys,hfd,fwhm,vmax,snr,bg,bgdev,flux : double;
alok: boolean;
begin
if FAddFrames then begin // stack preview frames
// load temporary image
f:=TFits.Create(nil);
f.Stream:=ImgStream;
f.LoadStream;
// convert 8bit to 16bit to avoid quick overflow
if f.HeaderInfo.bitpix=8 then
f.Bitpix8to16;
// substract dark if loaded and compatible
if f.SameFormat(FFits.DarkFrame) then
f.Math(FFits.DarkFrame,moSub);
// check frame is compatible
if FFits.SameFormat(f) then begin
if FStackAlign then begin
// align frame on ref star
alok:=false;
xi:=round(FStackStarX);
yi:=round(FStackStarY);
f.FindStarPos(xi,yi,50,xc,yc,ri,vmax,bg,bgdev);
if vmax>0 then begin
f.GetHFD2(xc,yc,2*ri,xs,ys,bg,bgdev,hfd,fwhm,vmax,snr,flux,false);
if ((hfd>0)and(Undersampled or (hfd>0.8))) and (hfd<10) then begin
f.Shift(FStackAlignX-xs,FStackAlignY-ys);
FStackStarX:=xs;
FStackStarY:=ys;
alok:=true;
end;
end;
if not alok then msg(rsAlignmentSta,0);
end;
FFits.Math(f,moAdd); // add frame
inc(FStackCount);
end
else begin
FFits.Math(f,moAdd,true); // start a new stack
FStackCount:=1;
FStackAlign:=false;
// search alignment star
FFits.FindBrightestPixel(FFits.HeaderInfo.naxis1 div 2, FFits.HeaderInfo.naxis2 div 2,min(FFits.HeaderInfo.naxis1,FFits.HeaderInfo.naxis2) div 2,20,xi,yi,vmax);
if vmax>0 then begin
FFits.FindStarPos(xi,yi,20,xc,yc,ri,vmax,bg,bgdev);
if vmax>0 then begin
FFits.GetHFD2(xc,yc,2*ri,xs,ys,bg,bgdev,hfd,fwhm,vmax,snr,flux,false);
if ((hfd>0)and(Undersampled or (hfd>0.8))) and (hfd<10) then begin
FStackAlign:=true;
FStackAlignX:=xs;
FStackAlignY:=ys;
FStackStarX:=xs;
FStackStarY:=ys;
msg(Format(rsStackingWith, [inttostr(round(xs)), inttostr(round(ys))
]));
end;
end;
end;
if not FStackAlign then msg(rsNoAlignmentS,0);
end;
// update image
FFits.Header.Assign(f.Header);
WriteHeaders;
f.free;
if Assigned(FonNewImage) then FonNewImage(self);
end
else begin // normal capture
FStackCount:=0;
{$ifdef camera_debug}msg('load stream');{$endif}
Ffits.Stream:=ImgStream;
Ffits.LoadStream;
{$ifdef camera_debug}msg('write headers');{$endif}
WriteHeaders;
{$ifdef camera_debug}msg('apply correction');{$endif}
FFits.ApplyDark;
FFits.ApplyBPM;
{$ifdef camera_debug}msg('display image');{$endif}
if Assigned(FonNewImage) then FonNewImage(self);
end;
end;
procedure T_camera.TryNextExposure(Data: PtrInt);
begin
if EarlyNextExposure and Assigned(FonNewExposure) and(not Autofocusing) then begin
if CameraProcessingImage and (CameraProcessingNum=FImgNum-1) then begin
sleep(10);
CheckSynchronize;
Application.QueueAsyncCall(@TryNextExposure,Data);
end
else begin
CameraProcessingImage:=true;
CameraProcessingNum:=data;
FonNewExposure(self);
end;
end;
end;
procedure T_camera.WriteHeaders;
var origin,observer,telname,objname,siso,CType: string;
focal_length,pixscale1,pixscale2,ccdtemp,st,ra,de: double;
hbitpix,hnaxis,hnaxis1,hnaxis2,hnaxis3,hbin1,hbin2,cgain,focuserpos: integer;
hfilter,hframe,hinstr,hdateobs,hcomment1 : string;
hbzero,hbscale,hdmin,hdmax,hra,hdec,hexp,hpix1,hpix2,hairmass,focusertemp: double;
haz,hal: double;
gamma,offset,OffsetX,OffsetY: integer;
Frx,Fry,Frwidth,Frheight: integer;
hasfocusertemp,hasfocuserpos: boolean;
begin
// get header values from camera (set by INDI driver)
if not Ffits.Header.Valueof('BITPIX',hbitpix) then hbitpix:=Ffits.HeaderInfo.bitpix;
if not Ffits.Header.Valueof('NAXIS',hnaxis) then hnaxis:=Ffits.HeaderInfo.naxis;
if not Ffits.Header.Valueof('NAXIS1',hnaxis1) then hnaxis1:=Ffits.HeaderInfo.naxis1;
if not Ffits.Header.Valueof('NAXIS2',hnaxis2) then hnaxis2:=Ffits.HeaderInfo.naxis2;
if not Ffits.Header.Valueof('NAXIS3',hnaxis3) then hnaxis3:=Ffits.HeaderInfo.naxis3;
if not Ffits.Header.Valueof('BZERO',hbzero) then hbzero:=Ffits.HeaderInfo.bzero;
if not Ffits.Header.Valueof('BSCALE',hbscale) then hbscale:=Ffits.HeaderInfo.bscale;
if not Ffits.Header.Valueof('EXPTIME',hexp) then hexp:=-1;
if not Ffits.Header.Valueof('PIXSIZE1',hpix1) then hpix1:=-1;
if not Ffits.Header.Valueof('PIXSIZE2',hpix2) then hpix2:=-1;
if not Ffits.Header.Valueof('XBINNING',hbin1) then hbin1:=-1;
if not Ffits.Header.Valueof('YBINNING',hbin2) then hbin2:=-1;
if (hpix1>0) and (hbin1>0) then hpix1:=hpix1*hbin1;
if (hpix2>0) and (hbin2>0) then hpix2:=hpix2*hbin2;
if hpix1<0 then if not Ffits.Header.Valueof('XPIXSZ',hpix1) then hpix1:=-1;
if hpix2<0 then if not Ffits.Header.Valueof('YPIXSZ',hpix2) then hpix2:=-1;
if not Ffits.Header.Valueof('FRAME',hframe) then hframe:='Light ';
if not Ffits.Header.Valueof('FILTER',hfilter) then hfilter:='';
if not Ffits.Header.Valueof('DATAMIN',hdmin) then hdmin:=Ffits.HeaderInfo.dmin;
if not Ffits.Header.Valueof('DATAMAX',hdmax) then hdmax:=Ffits.HeaderInfo.dmax;
if not Ffits.Header.Valueof('INSTRUME',hinstr) then hinstr:='';
if not Ffits.Header.Valueof('DATE-OBS',hdateobs) then hdateobs:=FormatDateTime(dateisoshort,NowUTC);
if not Ffits.Header.Valueof('AIRMASS',hairmass) then hairmass:=-1;
if not Ffits.Header.Valueof('COMMENT',hcomment1) then hcomment1:='';
if copy(hcomment1,1,14)='FITS (Flexible' then hcomment1:='';
// get other values
hra:=NullCoord; hdec:=NullCoord;
if (FMount<>nil)and(Fmount.Status=devConnected) then begin
hra:=Fmount.RA;
hdec:=Fmount.Dec;
MountToJ2000(Fmount.EquinoxJD,hra,hdec);
hra:=15*hra;
end;
haz:=NullCoord; hal:=NullCoord;
if (hra<>NullCoord)and(hdec<>NullCoord) then begin
ra:=deg2rad*hra;
de:=deg2rad*hdec;
J2000ToApparent(ra,de);
st:=SidTimT(FMidExposureTime+ObsTimeZone/24);
Eq2Hz(st-ra,de,haz,hal) ;
Refraction(hal,true);
haz:=round(100*rad2deg*rmod(haz+pi,pi2))/100;
hal:=round(100*rad2deg*hal)/100;
hairmass:=round(10000*AirMass(hal))/10000;
end;
if (hfilter='')and(Fwheel<>nil)and(Fwheel.Status=devConnected) then begin
hfilter:=Fwheel.FilterNames[Fwheel.Filter];
end;
ccdtemp:=Temperature;
objname:=FObjectName;
if config<>nil then begin
origin:=config.GetValue('/Info/ObservatoryName','');
observer:=config.GetValue('/Info/ObserverName','');
telname:=config.GetValue('/Info/TelescopeName','');
if config.GetValue('/Astrometry/FocaleFromTelescope',true)
then
focal_length:=Fmount.FocaleLength
else
focal_length:=config.GetValue('/Astrometry/FocaleLength',0);
if not config.GetValue('/Astrometry/PixelSizeFromCamera',true) then begin
hpix1:=config.GetValue('/Astrometry/PixelSize',5.0);
hpix2:=hpix1;
if (hbin1>0) then hpix1:=hpix1*hbin1;
if (hbin2>0) then hpix2:=hpix2*hbin2;
end;
end;
if (focal_length<1) then msg(rsErrorUnknowT,0);
if (focal_length>50000) then msg('Error: Is the telescope focal length really '+FormatFloat(f0,focal_length)+'mm ?',0);
try
GetFrame(Frx,Fry,Frwidth,Frheight);
except
Frwidth:=0;
end;
try
gamma:=GetVideoGamma;
offset:=GetVideoBrightness;
except
gamma:=NullInt;
offset:=NullInt;
end;
try
cgain:=NullInt;
if FhasGain then cgain:=GetGain;
except
cgain:=NullInt;
end;
try
siso:='';
if FhasGainISO then siso:=FISOList[GetGain];
except
siso:='';
end;
hasfocuserpos:=false;
try
if (FFocuser<>nil)and(FFocuser.hasAbsolutePosition) then begin
focuserpos:=FFocuser.Position;
hasfocuserpos:=true;
end;
except
hasfocuserpos:=false;
end;
hasfocusertemp:=false;
try
if (FFocuser<>nil)and(FFocuser.hasTemperature) then begin
focusertemp:=FFocuser.Temperature;
hasfocusertemp:=true;
end;
except
hasfocuserpos:=false;
end;
CType:='';
try
if FhasCfaInfo then begin
CfaInfo(OffsetX,OffsetY,CType);
if FASCOMFlipImage and (not odd(FCameraYSize)) then
OffsetY:=(OffsetY+1) mod 2;
end;
except
end;
// write new header
Ffits.Header.ClearHeader;
Ffits.Header.Add('SIMPLE',true,'file does conform to FITS standard');
Ffits.Header.Add('BITPIX',hbitpix,'number of bits per data pixel');
Ffits.Header.Add('NAXIS',hnaxis,'number of data axes');
Ffits.Header.Add('NAXIS1',hnaxis1 ,'length of data axis 1');
Ffits.Header.Add('NAXIS2',hnaxis2 ,'length of data axis 2');
if hnaxis=3 then Ffits.Header.Add('NAXIS3',hnaxis3 ,'length of data axis 3');;
Ffits.Header.Add('EXTEND',true,'FITS dataset may contain extensions');
Ffits.Header.Add('BZERO',hbzero,'offset data range to that of unsigned short');
Ffits.Header.Add('BSCALE',hbscale,'default scaling factor');
if hdmax>0 then begin
Ffits.Header.Add('DATAMIN',hdmin,'Minimum value');
Ffits.Header.Add('DATAMAX',hdmax,'Maximum value');
end;
Ffits.Header.Add('DATE',FormatDateTime(dateisoshort,NowUTC),'Date data written');
if origin<>'' then Ffits.Header.Add('ORIGIN',origin,'Observatory name');
Ffits.Header.Add('SITELAT',ObsLatitude,'Observatory latitude');
Ffits.Header.Add('SITELONG',-ObsLongitude,'Observatory longitude'); //Internal longitude is East negative for historical reason
if observer<>'' then Ffits.Header.Add('OBSERVER',observer,'Observer name');
if telname<>'' then Ffits.Header.Add('TELESCOP',telname,'Telescope used for acquisition');
if hinstr<>'' then Ffits.Header.Add('INSTRUME',hinstr,'Instrument used for acquisition');
if hfilter<>'' then Ffits.Header.Add('FILTER',hfilter,'Filter');
Ffits.Header.Add('SWCREATE','CCDciel '+ccdciel_version+'-'+RevisionStr,'');
if objname<>'' then Ffits.Header.Add('OBJECT',objname,'Observed object name');
Ffits.Header.Add('IMAGETYP',hframe,'Image Type');
Ffits.Header.Add('DATE-OBS',hdateobs,'UTC start date of observation');
if hexp>0 then Ffits.Header.Add('EXPTIME',hexp,'[s] Total Exposure Time');
if FStackCount>1 then Ffits.Header.Add('STACKCNT',FStackCount,'Number of stacked frames');
if cgain<>NullInt then Ffits.Header.Add('GAIN',cgain,'Camera gain setting in manufacturer units');
if siso<>'' then Ffits.Header.Add('GAIN',siso,'Camera ISO');
if gamma<>NullInt then Ffits.Header.Add('GAMMA',gamma,'Video gamma');
if offset<>NullInt then Ffits.Header.Add('OFFSET',offset,'Video offset,brightness');
if hpix1>0 then Ffits.Header.Add('XPIXSZ',hpix1 ,'[um] Pixel Size X, binned');
if hpix2>0 then Ffits.Header.Add('YPIXSZ',hpix2 ,'[um] Pixel Size Y, binned');
if hpix1>0 then Ffits.Header.Add('PIXSIZE1',hpix1 ,'[um] Pixel Size X, binned');
if hpix2>0 then Ffits.Header.Add('PIXSIZE2',hpix2 ,'[um] Pixel Size Y, binned');
if hbin1>0 then Ffits.Header.Add('XBINNING',hbin1 ,'Binning factor X');
if hbin2>0 then Ffits.Header.Add('YBINNING',hbin2 ,'Binning factor Y');
if CType<>'' then begin
Ffits.Header.Add('XBAYROFF',OffsetX ,'X offset of Bayer array');
Ffits.Header.Add('YBAYROFF',OffsetY ,'Y offset of Bayer array');
Ffits.Header.Add('BAYERPAT',CType ,'Bayer color pattern');
end;
Ffits.Header.Add('FOCALLEN',focal_length,'[mm] Telescope focal length');
if ccdtemp<>NullCoord then Ffits.Header.Add('CCD-TEMP',ccdtemp ,'CCD temperature (Celsius)');
if Frwidth<>0 then begin
Ffits.Header.Add('FRAMEX',Frx,'Frame start x');
Ffits.Header.Add('FRAMEY',Fry,'Frame start y');
Ffits.Header.Add('FRAMEHGT',Frheight,'Frame height');
Ffits.Header.Add('FRAMEWDH',Frwidth,'Frame width');
end;
if (haz<>NullCoord)and(hal<>NullCoord) then begin
Ffits.Header.Add('CENTAZ',haz,'[deg] Azimuth of center of image');
Ffits.Header.Add('CENTALT',hal,'[deg] Altitude of center of image');
Ffits.Header.Add('OBJCTAZ',haz,'[deg] Azimuth of center of image');
Ffits.Header.Add('OBJCTALT',hal,'[deg] Altitude of center of image');
end;
if hairmass>0 then Ffits.Header.Add('AIRMASS',hairmass ,'Airmass');
if hasfocuserpos then Ffits.Header.Add('FOCUSPOS',focuserpos ,'Focuser position in steps');
if hasfocusertemp then Ffits.Header.Add('FOCUSTEM',focusertemp ,'Focuser temperature (Celsius)');
if (hra<>NullCoord)and(hdec<>NullCoord) then begin
Ffits.Header.Add('EQUINOX',2000.0,'');
Ffits.Header.Add('RA',hra,'[deg] Telescope pointing RA');
Ffits.Header.Add('DEC',hdec,'[deg] Telescope pointing DEC');
Ffits.Header.Add('OBJCTRA',trim(RAToStrB(hra/15)),'[hh mm ss] Telescope pointing RA');
Ffits.Header.Add('OBJCTDEC',trim(DEToStrB(hdec)),'[+dd mm ss] Telescope pointing DEC');
if (hpix1>0)and(hpix2>0)and(focal_length>0) then begin
pixscale1:=3600*rad2deg*arctan(hpix1/1000/focal_length);
pixscale2:=3600*rad2deg*arctan(hpix2/1000/focal_length);
Ffits.Header.Add('SECPIX1',pixscale1,'image scale arcseconds per pixel');
Ffits.Header.Add('SECPIX2',pixscale2,'image scale arcseconds per pixel');
Ffits.Header.Add('SCALE',pixscale1,'image scale arcseconds per pixel');
end;
end;
if hcomment1<>'' then Ffits.Header.Add('COMMENT',hcomment1 ,'');
Ffits.Header.Add('END','','');
Ffits.GetFitsInfo;
end;
procedure T_camera.NewVideoFrame;
var x,y,w,h: integer;
begin
if lockvideoframe then exit;
lockvideoframe:=true;
try
GetFrame(x,y,w,h);
FVideoStream.Position:=0;
if Color then begin
WriteVideoHeader(w,h,3,8);
end else begin
WriteVideoHeader(w,h,2,8);
end;
FFits.VideoStream:=FVideoStream;
if Assigned(FonVideoFrame) then FonVideoFrame(self);
finally
lockvideoframe:=false;
end;
end;
procedure T_camera.WriteVideoHeader(width,height,naxis,bitpix: integer);
var
hbitpix,hnaxis,hnaxis1,hnaxis2,hnaxis3: integer;
hframe,hdateobs : string;
hbzero,hbscale,hdmin,hdmax: double;
begin
// simplified video header
hbitpix:=bitpix;
hnaxis:=naxis;
if naxis=2 then begin
hnaxis1:=width;
hnaxis2:=height;
end;
if naxis=3 then begin
hnaxis1:=4; // 32bit video stream from INDI
hnaxis2:=width;
hnaxis3:=height;
end;
hframe:='Video ';
hbzero:=0;
hbscale:=1;
hdmin:=0;
hdmax:=0;
hdateobs:=FormatDateTime(dateisoshort,NowUTC);
// write new header
Ffits.Header.ClearHeader;
Ffits.Header.Add('SIMPLE',true,'file does conform to FITS standard');
Ffits.Header.Add('BITPIX',hbitpix,'number of bits per data pixel');
Ffits.Header.Add('NAXIS',hnaxis,'number of data axes');
Ffits.Header.Add('NAXIS1',hnaxis1 ,'length of data axis 1');
Ffits.Header.Add('NAXIS2',hnaxis2 ,'length of data axis 2');
if hnaxis=3 then Ffits.Header.Add('NAXIS3',hnaxis3 ,'length of data axis 3');;
Ffits.Header.Add('EXTEND',true,'FITS dataset may contain extensions');
Ffits.Header.Add('BZERO',hbzero,'offset data range to that of unsigned short');
Ffits.Header.Add('BSCALE',hbscale,'default scaling factor');
Ffits.Header.Add('DATAMIN',hdmin,'Minimum value');
Ffits.Header.Add('DATAMAX',hdmax,'Maximum value');
Ffits.Header.Add('DATE',FormatDateTime(dateisoshort,NowUTC),'Date data written');
Ffits.Header.Add('SWCREATE','CCDciel '+ccdciel_version+'-'+RevisionStr,'');
Ffits.Header.Add('IMAGETYP',hframe,'Image Type');
Ffits.Header.Add('DATE-OBS',hdateobs,'UTC start date of observation');
Ffits.Header.Add('END','','');
Ffits.GetFitsInfo;
end;
end.