-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathastrocam.cpp
More file actions
7012 lines (6184 loc) · 282 KB
/
astrocam.cpp
File metadata and controls
7012 lines (6184 loc) · 282 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file astrocam.cpp
* @brief contains the functions for the AstroCam interface
* @author David Hale <dhale@astro.caltech.edu>
* @details The main server object is instantiated in src/server.cpp and
* defined extern here so that static functions can access it. The
* static functions are run in std::threads which means class objects
* are otherwise be unavailable to them. The interface class is
* accessible through this because Camera::Server server inherits
* AstroCam::Interface.
*
*/
#include "camerad.h"
#include "message_keys.h"
extern Camera::Server server;
namespace AstroCam {
/**** AstroCam::Interface::publish_snapshot *********************************/
/**
* @brief publish a snapshot of my telemetry
* @param[out] retstring optional pointer to buffer for return string
*
*/
void Interface::publish_snapshot(std::string *retstring) {
const std::string function("AstroCam::Interface::publish_snapshot");
nlohmann::json jmessage_out;
// build JSON message with my telemetry
jmessage_out[Key::SOURCE] = Topic::CAMERAD;
jmessage_out[Key::Camerad::READY] = this->can_expose.load();
jmessage_out[Key::Camerad::SHUTTERTIME] = this->camera.shutter.get_duration();
// publish JSON message
try {
this->publisher->publish(jmessage_out);
}
catch (const std::exception &e) {
logwrite(function, "ERROR: "+std::string(e.what()));
return;
}
// if a retstring buffer was supplied then return the JSON message
if (retstring) {
*retstring=jmessage_out.dump();
retstring->append(JEOF);
}
}
/**** AstroCam::Interface::publish_snapshot *********************************/
long NewAstroCam::new_expose( std::string nseq_in ) {
logwrite( "NewAstroCam::new_expose", nseq_in );
return( NO_ERROR );
}
/**** AstroCam::Interface::exposure_progress ********************************/
/**
* @brief polls and broadcasts shutter timer progress
*
*/
void Interface::exposure_progress() {
long remaintime=0, delaytime=0;
std::string message;
message.reserve(32);
do {
// poll and report once per second
std::this_thread::sleep_for( std::chrono::seconds(1) );
// poll
this->camera.shutter_timer.progress(remaintime, delaytime);
if ( delaytime==0 ) return;
message = "EXPTIME:" + std::to_string(remaintime) + " "
+ std::to_string(delaytime) + " "
+ std::to_string(static_cast<long>(100*(1-static_cast<double>(remaintime)/delaytime)));
// broadcast
std::thread( &AstroCam::Interface::handle_queue2, this, std::move(message) ).detach();
}
while ( remaintime > 0 );
}
/**** AstroCam::Interface::exposure_progress ********************************/
/***** AstroCam::Callback::exposeCallback ***********************************/
/**
* @brief called by CArcDevice::expose() during the exposure
* @param[in] devnum device number passed to API on expose
* @param[in] uiElapsedTime actually number of millisec remaining
*
* This is the callback function invoked by the ARC API,
* arc::gen3::CArcDevice::expose() during the exposure.
* After sending the expose (SEX) command, the API polls the controller
* using the RET command.
*
*/
void Callback::exposeCallback( int devnum, std::uint32_t uiElapsedTime, std::uint32_t uiExposureTime ) {
std::string function = "AstroCam::Callback::exposeCallback";
std::stringstream message;
message << "ELAPSEDTIME_" << devnum << ":" << uiElapsedTime;
if ( uiExposureTime != 0x1BAD1BAD ) message<< " EXPTIME:" << uiExposureTime;
std::thread( std::ref(AstroCam::Interface::handle_queue), message.str() ).detach();
return;
}
/***** AstroCam::Callback::exposeCallback ***********************************/
/***** AstroCam::Callback::readCallback *************************************/
/**
* @brief called by CArcDevice::expose() during readout
* @param[in] expbuf exposure buffer number passed to API on expose
* @param[in] devnum device number passed to API on expose
* @param[in] uiPixelCount number of pixels read ( getPixelCount() )
*
* This is the callback function invoked by the ARC API,
* arc::gen3::CArcDevice::expose() when bInReadout is true.
*
*/
void Callback::readCallback( int expbuf, int devnum, std::uint32_t uiPixelCount, std::uint32_t uiImageSize ) {
std::string message;
message.reserve(36);
message = "PIXELCOUNT_" + std::to_string(devnum) + ":"
+ std::to_string(uiPixelCount) + " "
+ std::to_string(uiImageSize) + " "
+ std::to_string(static_cast<long>(100*uiPixelCount/uiImageSize));
std::thread( std::ref(AstroCam::Interface::handle_queue), message ).detach();
return;
}
/***** AstroCam::Callback::readCallback *************************************/
/***** AstroCam::Callback::frameCallback ************************************/
/**
* @brief called by CArcDevice::expose() when a frame has been received
* @param[in] expbuf exposure buffer number passed to API on expose
* @param[in] devnum device number passed to API on expose
* @param[in] fpbcount frames per buffer count ( uiFPBCount ) wraps to 0 at FPB
* @param[in] fcount actual frame counter ( uiPCIFrameCount = getFrameCount() )
* @param[in] rows image rows ( uiRows )
* @param[in] cols image columns ( uiCols )
* @param[in] buffer pointer to PCI image buffer
*
* This is the callback function invoked by the ARC API,
* arc::gen3::CArcDevice::expose() when a new frame is received.
* Spawn a separate thread to handle the incoming frame.
*
* incoming frame from ARC -> frameCallback()
*
*/
void Callback::frameCallback( int expbuf, int devnum, std::uint32_t fpbcount, std::uint32_t fcount,
std::uint32_t rows, std::uint32_t cols, void* buffer ) {
if ( ! server.useframes ) fcount=1; // prevents fcount from being a bad value when firmware doesn't support frames
std::stringstream message;
if ( server.controller.find(devnum) == server.controller.end() ) {
message.str(""); message << "ERROR in AstroCam::Callback::frameCallback (fatal): devnum " << devnum
<< " not in controller configuration";
std::thread( std::ref(AstroCam::Interface::handle_queue), message.str() ).detach();
return;
}
message << "FRAMECOUNT_" << devnum << ":" << fcount << " rows=" << rows << " cols=" << cols;
std::thread( std::ref(AstroCam::Interface::handle_queue), message.str() ).detach();
logwrite( "Callback::frameCallback", message.str() );
server.add_framethread(); // framethread_count is incremented because a thread has been added
std::thread( std::ref(AstroCam::Interface::handle_frame), expbuf, devnum, fpbcount, fcount, buffer ).detach();
return;
}
/***** AstroCam::Callback::frameCallback ************************************/
/***** AstroCam::Callback::ftCallback ***************************************/
/**
* @brief called by CArcDevice::frame_transfer() when a frame transfer has been done
* @details set/clear control flags and start the readout waveforms
* @param[in] expbuf exposure buffer number passed to API
* @param[in] devnum device number passed to API
*
*/
void Callback::ftCallback( int expbuf, int devnum ) {
std::stringstream message;
message << "FRAMETRANSFER_" << devnum << ":complete";
std::thread( std::ref(AstroCam::Interface::handle_queue), message.str() ).detach();
logwrite( "Callback::ftCallback", message.str() );
if ( server.controller.find(devnum) == server.controller.end() ) {
message.str(""); message << "ERROR in AstroCam::Callback::ftCallback (fatal): devnum " << devnum
<< " not in controller configuration";
std::thread( std::ref(AstroCam::Interface::handle_queue), message.str() ).detach();
return;
}
server.controller.at(devnum).in_frametransfer = false;
server.exposure_pending( devnum, false ); // this also does the notify
server.controller.at(devnum).in_readout = true;
server.state_monitor_condition.notify_all();
// Trigger the readout waveforms here.
//
try {
server.controller.at(devnum).pArcDev->readout( expbuf,
devnum,
server.controller.at(devnum).info.axes[_ROW_],
server.controller.at(devnum).info.axes[_COL_],
server.camera.abortstate,
server.controller.at(devnum).pCallback
);
}
catch ( const std::exception &e ) { // arc::gen3::CArcDevice::readout may throw an exception
message.str(""); message << "ERROR starting readout for " << server.controller.at(devnum).devname
<< " channel " << server.controller.at(devnum).channel << ": " << e.what();
std::thread( std::ref(AstroCam::Interface::handle_queue), message.str() ).detach();
return;
}
return;
}
/***** AstroCam::Callback::ftCallback ***************************************/
/***** AstroCam::Interface::interface ***************************************/
/**
* @brief returns the interface
* @param[out] iface reference to string to return the interface type as a string
* @return NO_ERROR
*
*/
long Interface::interface(std::string &iface) {
std::string function = "AstroCam::Interface::interface";
iface = "AstroCam";
logwrite(function, iface);
return(NO_ERROR);
}
/***** AstroCam::Interface::interface ***************************************/
/***** AstroCam::Interface::state_monitor_thread ****************************/
void Interface::state_monitor_thread(Interface& interface) {
std::string function = "AstroCam::Interface::state_monitor_thread";
std::stringstream message;
std::vector<int> selectdev;
// notify that the thread is running
//
logwrite(function, "starting");
{
std::unique_lock<std::mutex> state_lock(interface.state_lock);
interface.state_monitor_thread_running = true;
}
interface.state_monitor_condition.notify_all();
logwrite(function, "running");
while ( true ) {
std::unique_lock<std::mutex> state_lock(interface.state_lock);
interface.state_monitor_condition.wait(state_lock);
while ( interface.is_camera_idle() ) {
selectdev.clear();
message.str(""); message << "enabling detector idling for channel(s)";
for ( const auto &dev : interface.active_devnums ) {
logwrite(function, std::to_string(dev));
if ( interface.controller.at(dev).connected ) {
selectdev.push_back(dev);
message << " " << interface.controller.at(dev).channel;
}
}
if ( selectdev.size() > 0 ) {
long ret = interface.do_native( selectdev, std::string("IDL") );
logwrite( function, (ret==NO_ERROR ? "NOTICE: " : "ERROR")+message.str() );
}
// Wait for the conditions to change before checking again
interface.state_monitor_condition.wait( state_lock );
}
}
// notify that the thread is terminating
// should be impossible
//
{
std::unique_lock<std::mutex> state_lock(interface.state_lock);
interface.state_monitor_thread_running = false;
}
interface.state_monitor_condition.notify_all();
logwrite(function, "terminating");
return;
}
/***** AstroCam::Interface::state_monitor_thread ****************************/
/***** AstroCam::Interface::make_image_keywords *****************************/
void Interface::make_image_keywords( int dev ) {
std::string function = "AstroCam::Interface::make_image_keywords";
std::stringstream message;
auto chan = this->controller.at(dev).channel;
auto rows = this->controller.at(dev).info.axes[_ROW_];
auto cols = this->controller.at(dev).info.axes[_COL_];
auto osrows = this->controller.at(dev).osrows;
auto oscols = this->controller.at(dev).oscols;
auto skiprows = this->controller.at(dev).skiprows;
auto skipcols = this->controller.at(dev).skipcols;
int binspec, binspat;
this->controller.at(dev).physical_to_logical(this->controller.at(dev).info.binning[_ROW_],
this->controller.at(dev).info.binning[_COL_],
binspat,
binspec);
this->controller.at(dev).info.systemkeys.add_key( "AMP_ID", this->controller.at(dev).info.readout_name, "readout amplifier", EXT, chan );
this->controller.at(dev).info.systemkeys.add_key( "FT", this->controller.at(dev).have_ft, "frame transfer used", EXT, chan );
this->controller.at(dev).info.systemkeys.add_key( "IMG_ROWS", this->controller.at(dev).info.axes[_ROW_], "image rows", EXT, chan );
this->controller.at(dev).info.systemkeys.add_key( "IMG_COLS", this->controller.at(dev).info.axes[_COL_]-oscols, "image cols", EXT, chan );
this->controller.at(dev).info.systemkeys.add_key( "OS_ROWS", osrows, "overscan rows", EXT, chan );
this->controller.at(dev).info.systemkeys.add_key( "OS_COLS", oscols, "overscan cols", EXT, chan );
this->controller.at(dev).info.systemkeys.add_key( "SKIPROWS", skiprows, "skipped rows", EXT, chan );
this->controller.at(dev).info.systemkeys.add_key( "SKIPCOLS", skipcols, "skipped cols", EXT, chan );
int L=0, B=0;
switch ( this->controller[ dev ].info.readout_type ) {
case L1: B=1; break;
case U1: L=1; B=1; break;
case U2: L=1; break;
case SPLIT1: B=1; break;
case FT1: B=1; break;
default: L=0; B=0;
}
int ltv2 = B * osrows;
int ltv1 = L * oscols;
//message.str(""); message << "[DEBUG] B=" << B << " L=" << L << " osrows=" << osrows << " oscols=" << oscols
// << " binning_row=" << binspat << " binning_col=" << binspec
// << " ltv2=" << ltv2 << " ltv1=" << ltv1;
//logwrite(function,message.str() );
this->controller.at(dev).info.systemkeys.add_key( "LTV2", ltv2, "", EXT, chan );
this->controller.at(dev).info.systemkeys.add_key( "LTV1", ltv1, "", EXT, chan );
this->controller.at(dev).info.systemkeys.add_key( "CRPIX1A", ltv1+1, "", EXT, chan );
this->controller.at(dev).info.systemkeys.add_key( "CRPIX2A", ltv2+1, "", EXT, chan );
this->controller.at(dev).info.systemkeys.add_key( "BINSPEC", binspec, "binning in spectral direction", EXT, chan );
this->controller.at(dev).info.systemkeys.add_key( "BINSPAT", binspat, "binning in spatial direction", EXT, chan );
this->controller.at(dev).info.systemkeys.add_key( "CDELT1A",
this->controller.at(dev).info.dispersion*binspec,
"Dispersion in Angstrom/pixel", EXT, chan );
this->controller.at(dev).info.systemkeys.add_key( "CRVAL1A",
this->controller.at(dev).info.minwavel,
"Reference value in Angstrom", EXT, chan );
// These keys are for proper mosaic display.
// Adjust GAPY to taste.
//
int GAPY=20;
int crval2 = ( this->controller.at(dev).info.axes[_ROW_] / binspat + GAPY ) * dev;
this->controller.at(dev).info.systemkeys.add_key( "CRPIX1", 0, "", EXT, chan );
this->controller.at(dev).info.systemkeys.add_key( "CRPIX2", 0, "", EXT, chan );
this->controller.at(dev).info.systemkeys.add_key( "CRVAL1", 0, "", EXT, chan );
this->controller.at(dev).info.systemkeys.add_key( "CRVAL2", crval2, "", EXT, chan );
// Add ___SEC keywords to the extension header for this channel
//
std::stringstream sec;
/* 01-24-2025 ***
sec.str(""); sec << "[" << this->controller.at(dev).info.region_of_interest[0] << ":" << this->controller.at(dev).info.region_of_interest[1]
<< "," << this->controller.at(dev).info.region_of_interest[2] << ":" << this->controller.at(dev).info.region_of_interest[3] << "]";
this->controller.at(dev).info.systemkeys.add_key( "CCDSEC", sec.str(), "physical format of CCD", EXT, chan );
sec.str(""); sec << "[" << this->controller.at(dev).info.region_of_interest[0] + skipcols << ":" << cols
<< "," << this->controller.at(dev).info.region_of_interest[2] + skiprows << ":" << rows << "]";
this->controller.at(dev).info.systemkeys.add_key( "DATASEC", sec.str(), "section containing the CCD data", EXT, chan );
sec.str(""); sec << '[' << cols << ":" << cols+oscols
<< "," << this->controller.at(dev).info.region_of_interest[2] + skiprows << ":" << rows+osrows << "]";
this->controller.at(dev).info.systemkeys.add_key( "BIASSEC", sec.str(), "overscan section", EXT, chan );
*** */
sec.str(""); sec << "[" << oscols+1-2 // -2 is KLUDGE FACTOR
<< ":" << cols-2 // -2 is KLUDGE FACTOR
<< "," << this->controller.at(dev).info.region_of_interest[2] + skiprows << ":" << rows << "]";
this->controller.at(dev).info.systemkeys.add_key( "DATASEC", sec.str(), "section containing the CCD data", EXT, chan );
sec.str(""); sec << "[" << this->controller.at(dev).info.region_of_interest[0] << ":" << oscols-2 // -2 is KLUDGE FACTOR
<< "," << this->controller.at(dev).info.region_of_interest[2] + skiprows << ":" << rows << "]";
this->controller.at(dev).info.systemkeys.add_key( "BIASSEC", sec.str(), "overscan section", EXT, chan );
return;
}
/***** AstroCam::Interface::make_image_keywords *****************************/
/***** AstroCam::Interface::parse_spec_info *********************************/
/**
* @brief parse the SPEC_INFO config line
* @param[in] args line from config file, expected "<chan> <disp> <wavel>"
* @return ERROR|NO_ERROR
*
*/
long Interface::parse_spec_info( std::string args ) {
const std::string function("AstroCam::Interface::parse_spec_info");
std::vector<std::string> tokens;
Tokenize( args, tokens, " " );
if ( tokens.size() != 3 ) {
logwrite(function, "ERROR invalid number of args. expected SPEC_INFO={CHAN DISP WAVEL}");
return ERROR;
}
// Parse the three values from the args string
try {
int dev = devnum_from_chan(tokens.at(0));
this->controller.at(dev).info.dispersion = std::stod(tokens.at(1));
this->controller.at(dev).info.minwavel = std::stod(tokens.at(2));
}
catch(const std::exception &e) {
logwrite(function, "ERROR parsing SPEC_INFO config: "+std::string(e.what()));
return ERROR;
}
return NO_ERROR;
}
/***** AstroCam::Interface::parse_spec_info *********************************/
/***** AstroCam::Interface::parse_det_geometry ******************************/
/**
* @brief parse the DETECTOR_GEOMETRY config line
* @details Expected "CHAN PHYSSPAT PHYSSPEC ROWS COLS OSROWS OSCOLS BINROWS BINCOLS"
* @param[in] args line from config file
* @return ERROR|NO_ERROR
*
*/
long Interface::parse_det_geometry( std::string args ) {
const std::string function("AstroCam::Interface::parse_det_geometry");
std::ostringstream message;
std::vector<std::string> tokens;
Tokenize( args, tokens, " " );
if ( tokens.size() != 9 ) {
logwrite(function, "ERROR invalid number of args. expected CHAN PHYSSPAT PHYSSPEC ROWS COLS OSROWS OSCOLS BINROWS BINCOLS");
return ERROR;
}
int dev;
try {
// validate channel
dev = devnum_from_chan(tokens.at(0));
std::string spat = to_uppercase(tokens.at(1));
std::string spec = to_uppercase(tokens.at(2));
// require physical axis args be "ROW" or "COL" and unique
if ( ((spat != "ROW") && (spat != "COL")) || ((spec != "ROW") && (spec != "COL")) ) {
throw std::runtime_error("expected ROW|COL");
}
if (spec==spat) throw std::runtime_error("PHYSSPAT/PHYSSPEC must be unique");
this->controller.at(dev).spat_axis = (spat=="ROW" ? Controller::ROW : Controller::COL);
this->controller.at(dev).spec_axis = (spec=="ROW" ? Controller::ROW : Controller::COL);
}
catch(const std::exception &e) {
logwrite(function, "ERROR parsing DETECTOR_GEOMETRY config: "+std::string(e.what()));
return ERROR;
}
// make a new string that removes the orientation arguments
// this will become the config line to pass to image_size
std::string line, retstring;
for (size_t i=0; i<9; i++) {
if (i==1||i==2) continue;
if (!line.empty()) line += ' ';
line += tokens[i];
}
long error = this->image_size(line, retstring);
// save these as the defaults
//
this->controller[dev].defcols = this->controller[dev].detcols;
this->controller[dev].defrows = this->controller[dev].detrows;
this->controller[dev].defoscols = this->controller[dev].oscols;
this->controller[dev].defosrows = this->controller[dev].osrows;
this->controller[dev].defbincols = this->controller[dev].info.binning[_COL_];
this->controller[dev].defbinrows = this->controller[dev].info.binning[_ROW_];
return error;
}
/***** AstroCam::Interface::parse_det_geometry ******************************/
/***** AstroCam::Interface::parse_controller_config *************************/
/**
* @brief parses the CONTROLLER keyword from config file
* @param[in] args expected format is "PCIDEV CHAN FT FIRMWARE READOUT"
*
* Each camera controller is defined by
* '''
* CONTROLLER=(PCIDEV CHAN FT FIRMWARE READOUT)
* where PCIDEV is the PCI device number
* CHAN is the spectrographic channel {U,R,I,G}
* ID is the CCD Identifier (E.G. serial number, name, etc.)
* FT is {yes|no} to indicate if Frame Transfer is supported
* FIRMWARE is the default firmware to load
* READOUT is the default readout amplifier {U1,L1,U2,L2,SPLIT1,SPLIT2,QUAD,FT1,FT2}
* '''
*
* This function parses the arg string, checks for valid inputs, and
* uses the values to create a controller STL map entry for each device.
*
*/
long Interface::parse_controller_config( std::string args ) {
const std::string function("AstroCam::Interface::parse_controller_config");
std::ostringstream message;
logwrite( function, args );
std::istringstream iss(args);
int readout_type=-1;
uint32_t readout_arg=0xBAD;
bool readout_valid=false;
int dev;
std::string chan, id, firm, amp, ft;
bool have_ft;
if (!(iss >> dev
>> chan
>> id
>> ft
>> firm
>> amp)) {
logwrite(function, "ERROR bad config. expected { PCIDEV CHAN ID FT FIRMWARE READOUT }");
return ERROR;
}
if (ft=="yes") have_ft=true;
else if (ft=="no") have_ft=false;
else {
logwrite(function, "ERROR. FT expected { yes | no }");
return ERROR;
}
// Check the PCIDEV number is in expected range
//
if ( dev < 0 || dev > 3 ) {
message << "ERROR: bad PCIDEV " << dev << ". Expected {0,1,2,3}";
this->camera.log_error( function, message.str() );
return ERROR;
}
// Check that READOUT has a match in the list of known readout amps.
//
for ( const auto &source : this->readout_source ) {
if ( source.first.compare( amp ) == 0 ) { // found a match
readout_valid = true;
readout_arg = source.second.readout_arg; // get the arg associated with this match
readout_type = source.second.readout_type; // get the type associated with this match
}
}
if ( !readout_valid || readout_type==-1 || readout_arg==0xBAD ) {
message.str(""); message << "ERROR: bad READOUT " << amp << " for CHAN " << chan << ". Expected { ";
for ( const auto &source : this->readout_source ) message << source.first << " ";
message << "}";
logwrite( function, message.str() );
return( ERROR );
}
// Create a vector of configured device numbers
//
this->configured_devnums.push_back( dev );
// The Controller class holds the Camera::Information class and the FITS_file class,
// as well as wrappers for calling the functions inside the FITS_file class.
//
// The first four come from the config file, the rest are defaults
//
// create a local reference indexed by dev
Controller &con = this->controller[dev];
con.devnum = dev; // device number
con.channel = chan; // spectrographic channel
con.ccd_id = id; // CCD identifier
con.have_ft = have_ft; // frame transfer supported?
con.firmware = firm; // firmware file
/*
arc::gen3::CArcDevice* pArcDev = NULL; // create a generic CArcDevice pointer
pArcDev = new arc::gen3::CArcPCI(); // point this at a new CArcPCI() object ///< TODO @todo implement PCIe option
Callback* pCB = new Callback(); // create a pointer to a Callback() class object
this->controller[dev].pArcDev = pArcDev; // set the pointer to this object in the public vector
this->controller[dev].pCallback = pCB; // set the pointer to this object in the public vector
*/
con.pArcDev = ( new arc::gen3::CArcPCI() ); // set the pointer to this object in the public vector
con.pCallback = ( new Callback() ); // set the pointer to this object in the public vector
con.devname = ""; // device name
con.connected = false; // not yet connected
con.is_imsize_set = false; // need to set image_size
con.firmwareloaded = false; // no firmware loaded
con.info.readout_name = amp;
con.info.readout_type = readout_type;
con.readout_arg = readout_arg;
this->exposure_pending( dev, false );
con.in_readout = false;
con.in_frametransfer = false;
this->state_monitor_condition.notify_all();
// configured by config file, can never be reversed unless it is removed from the config file
con.configured = true;
// configured and active. this state can be reversed on command or failure to connect
// active alone isn't connected, but if not connected then it's not active
con.active = true;
// Header keys specific to this controller are stored in the controller's extension
//
con.info.systemkeys.add_key( "CCD_ID", id, "CCD identifier parse", EXT, chan );
con.info.systemkeys.add_key( "FT", ft, "frame transfer used", EXT, chan );
con.info.systemkeys.add_key( "AMP_ID", amp, "CCD readout amplifier ID", EXT, chan );
con.info.systemkeys.add_key( "SPEC_ID", chan, "spectrograph channel", EXT, chan );
con.info.systemkeys.add_key( "DEV_ID", dev, "detector controller PCI device ID", EXT, chan );
// FITS_file* pFits = new FITS_file(); // create a pointer to a FITS_file class object
// this->controller.at(dev).pFits = pFits; // set the pointer to this object in the public vector
#ifdef LOGLEVEL_DEBUG
message.str("");
message << "[DEBUG] pointers for dev " << dev << ": "
<< " pArcDev=" << std::hex << std::uppercase << this->controller.at(dev).pArcDev
<< " pCB=" << std::hex << std::uppercase << this->controller.at(dev).pCallback;
logwrite(function, message.str());
#endif
return( NO_ERROR );
}
/***** AstroCam::Interface::parse_controller_config *************************/
/***** AstroCam::Interface::parse_activate_commands *************************/
/**
* @brief parses the ACTIVATE_COMMANDS keywords from config file
* @details This gets the list of native commands needed to send when
* (de)activating a controller channel.
* @param[in] args expected format is "CHAN CMD [, CMD, CMD, ...]"
*
*/
long Interface::parse_activate_commands(std::string args) {
const std::string function("AstroCam::Interface::parse_activate_commands");
logwrite(function, args);
std::istringstream iss(args);
// get the channel
std::string chan;
if (!std::getline(iss, chan, ' ')) {
logwrite(function, "ERROR bad config. expected <chan> <cmd>, <cmd>, ...");
return ERROR;
}
// get device number for that channel
int dev;
try {
dev = devnum_from_chan(chan);
}
catch(const std::exception &e) {
logwrite(function, "ERROR: "+std::string(e.what()));
return ERROR;
}
// get the list of commands
std::string cmdlist;
if (!std::getline(iss, cmdlist)) {
logwrite(function, "ERROR bad config. expected <chan> <cmd>, <cmd>, ...");
return ERROR;
}
// get a pointer to this configured controller
auto pcontroller = this->get_controller(dev);
if (!pcontroller) {
logwrite(function, "ERROR bad controller for channel "+chan);
return ERROR;
}
// tokenize inserts each command into a vector element
Tokenize(cmdlist, pcontroller->activate_commands, ",");
return NO_ERROR;
}
/***** AstroCam::Interface::parse_activestate_commands **********************/
/***** AstroCam::Interface::devnum_from_chan ********************************/
/**
* @brief return the devnum associated with a channel name
* @param[out] chan reference to channel name
* @return devnum device number
* @throws std::runtime_error
*
*/
int Interface::devnum_from_chan( const std::string &chan ) {
int devnum=-1;
for ( const auto &con : this->controller ) {
if ( !con.second.configured ) continue; // skip controllers not configured
if ( con.second.channel == chan ) { // check to see if it matches a configured channel.
devnum = con.second.devnum;
break;
}
}
if (devnum < 0) {
throw std::runtime_error("no devnum configured for channel \"" + chan + "\"");
}
if ( this->controller.find(devnum) == this->controller.end() ) {
std::ostringstream oss;
oss << "device " << devnum << " not found in controller configuration";
throw std::runtime_error(oss.str());
}
return devnum;
}
/***** AstroCam::Interface::devnum_from_chan ********************************/
/***** AstroCam::Interface::extract_dev_chan ********************************/
/**
* @brief extract a dev#, channel name, and optional string from provided args
* @details The dev# | chan must exist in a configured controller object.
* The additional string is optional and can be anything.
* @param[in] args expected: <dev#>|<chan> [ <string> ]
* @param[out] dev reference to returned dev#
* @param[out] chan reference to returned channel
* @param[out] retstring carries error message, or any remaining strings, if present
*
*/
long Interface::extract_dev_chan( std::string args, int &dev, std::string &chan, std::string &retstring ) {
std::string function = "AstroCam::Interface::extract_dev_chan";
std::stringstream message;
// make sure input references are initialized
//
dev=-1;
chan.clear();
retstring.clear();
std::vector<std::string> tokens;
std::string tryme; // either a dev or chan, TBD
// Tokenize args to extract requested <dev>|<chan> and <string>, as appropriate
//
Tokenize( args, tokens, " " );
size_t ntok = tokens.size();
if ( ntok < 1 ) { // need at least one token
logwrite( function, "ERROR: bad arguments. expected <dev> | <chan> [ <string> ]" );
retstring="invalid_argument";
return ERROR;
}
else
if ( ntok == 1 ) { // dev|chan only
tryme = tokens[0];
}
else { // dev|chan plus string
tryme = tokens[0];
retstring.clear();
for ( size_t i=1; i<ntok; i++ ) { // If more than one token in string, then put
retstring.append( tokens[i] ); // them all back together and return as one string.
retstring.append( ( i+1 < ntok ? " " : "" ) );
}
}
// Try to convert the "tryme" to integer. If successful then it's a dev#,
// and if that fails then check if it's a known channel.
//
try {
dev = std::stoi( tryme ); // convert to integer
if ( dev < 0 ) { // don't let the user input a negative number
logwrite( function, "ERROR: dev# must be >= 0" );
retstring="invalid_argument";
return ERROR;
}
}
catch ( std::out_of_range & ) { logwrite( function, "ERROR: out of range" ); retstring="exception"; return ERROR; }
catch ( std::invalid_argument & ) { } // ignore this for now, it just means "tryme" is not a number
// If the stoi conversion failed then we're out here with a negative dev.
// If it succeeded then all we have is a number >= 0.
// But now check that either the dev# is a known devnum or the tryme is a known channel.
//
for ( const auto &con : this->controller ) {
if (!con.second.configured) continue; // skip controllers not configured
if ( con.second.channel == tryme ) { // check to see if it matches a configured channel.
dev = con.second.devnum;
chan = tryme;
break;
}
if ( con.second.devnum == dev ) { // check for a known dev#
chan = con.second.channel;
break;
}
}
// By now, these must both be known.
//
if ( dev < 0 || chan.empty() || this->controller.find(dev)==this->controller.end() ) {
message.str(""); message << "unrecognized channel or device \"" << tryme << "\"";
logwrite( function, message.str() );
#ifdef LOGLEVEL_DEBUG
message.str(""); message << "[DEBUG] dev=" << dev << " chan=" << chan << " controller.find(dev)==controller.end ? "
<< ((this->controller.find(dev)==this->controller.end()) ? "true" : "false" );
logwrite( function, message.str() );
#endif
retstring="invalid_argument";
return( ERROR );
}
return( NO_ERROR );
}
/***** AstroCam::Interface::extract_dev_chan ********************************/
long Interface::do_abort() {
std::string function = "AstroCam::Interface::do_abort";
std::stringstream message;
// int this_expbuf = this->get_expbuf();
for ( const auto &dev : this->active_devnums ) {
this->exposure_pending( dev, false );
for ( int buf=0; buf < NUM_EXPBUF; ++buf ) this->write_pending( buf, dev, false );
}
this->state_monitor_condition.notify_all();
logwrite( function, "[DEBUG] exposure pending set to false" );
return( NO_ERROR );
}
/***** AstroCam::Interface::do_bin ******************************************/
/**
* @brief set/get binning factor
* @details Since binning is set together with all image parameters, this
* function will call image_size(), and using this function is
* a convenience so that the user can change only binning.
* @param[in] args argument string contains <axis> [<factor>]
* @param[out] retstring reference to string to return error or help
* @return ERROR | NO_ERROR | HELP
*
*/
long Interface::do_bin( std::string args, std::string &retstring ) {
std::string function = "AstroCam::Interface::do_bin";
std::ostringstream message;
long error = NO_ERROR;
// Help
//
if ( args.empty() || args == "?" ) {
retstring = CAMERAD_BIN;
retstring.append( " <axis> [ <binfactor> ]\n" );
retstring.append( " set or get binning factor for the specified axis.\n" );
retstring.append( " This affects all channels.\n" );
retstring.append( " If <binfactor> is omitted then the current binning factor is returned.\n" );
retstring.append( " Specify <axis> from { spat spec }\n" );
retstring.append( " spec = spectral (dispersion) axis\n" );
retstring.append( " spat = spatial (slit) axis\n" );
retstring.append( message.str() );
return HELP;
}
// If no connected devices then nothing to do here
//
if ( this->numdev == 0 ) {
logwrite(function, "ERROR: no connected devices");
retstring="not_connected";
return( ERROR );
}
// Don't make any changes while an exposure is pending.
//
if ( this->exposure_pending() ) {
std::vector<int> pending = this->exposure_pending_list();
message << "ERROR: cannot change binning while exposure is pending for chan";
message << ( pending.size() > 1 ? "s " : " " );
for ( const auto &dev : pending ) message << this->controller.at(dev).channel << " ";
this->camera.async.enqueue_and_log( "CAMERAD", function, message.str() );
retstring="exposure_in_progress";
return ERROR;
}
// Tokenize args to get the axis and possible binning factor. There
// must be either 2 tokens (<axis> <bin> to set) or 1 token (<axis> to get).
//
std::vector<std::string> tokens;
Tokenize( args, tokens, " " );
std::string logical_axis;
int binfactor=-1;
try {
if ( tokens.size() > 0 ) {
logical_axis = tokens.at(0);
if (logical_axis != "spec" && logical_axis != "spat") {
logwrite(function, "ERROR unknown axis \""+logical_axis+"\". expected { spat spec }");
retstring="bad_arguments";
return ERROR;
}
}
if ( tokens.size() > 2 ) {
message << "ERROR: expected <axis> [ <binfactor> ] but received \"" << retstring << "\"";
logwrite( function, message.str() );
retstring="bad_arguments";
return ERROR;
}
if ( tokens.size() == 2 ) {
binfactor = std::stoi( tokens.at(1) );
if ( binfactor < 1 ) {
logwrite( function, "ERROR binfactor " +tokens.at(1)+ " must be greater than 0.");
retstring="invalid_argument";
return ERROR;
}
// Now, since binning applies equally to all devices and the image
// must be resized for binning, set the image size for each device.
// This uses the existing image size parameters and the new binning.
// The requested overscans are sent here, which can be modified by binning.
//
for ( const auto &dev : this->active_devnums ) {
auto pcontroller = this->get_active_controller(dev);
if (!pcontroller) continue;
// determine which physical axis corresponds to the requested logical axis
int physical_axis = logical_axis=="spec" ? pcontroller->spec_physical_axis()
: pcontroller->spat_physical_axis();
// update the binning factor in the info class for this axis
pcontroller->info.binning[physical_axis] = binfactor;
// get the logical coordinates from the class
int spat, spec, osspat, osspec, binspat, binspec;
this->get_logical(pcontroller, spat, spec, osspat, osspec, binspat, binspec);
// when requested axis is spatial and a BOI is defined,
// spat is sum of BOI bands and osspat is removed
if (logical_axis=="spat" && pcontroller->has_boi()) {
error = this->load_boi_pairs(pcontroller, spat);
osspat = 0;
}
if (error==NO_ERROR) error = this->set_image_size(pcontroller,
spat, spec,
osspat, osspec,
binspat, binspec);
if (error != NO_ERROR) break;
}
}
// return binning for the requested logical axis
if (this->numdev>0) {
int dev = this->active_devnums[0];
int physical_axis = (logical_axis=="spec") ? this->controller.at(dev).spec_physical_axis() :
this->controller.at(dev).spat_physical_axis();
message << this->controller.at(dev).info.binning[physical_axis];
if ( error == NO_ERROR ) retstring = message.str();
}
}
catch (const std::exception &e) {
logwrite(function, "ERROR parsing '"+args+"': "+std::string(e.what()));
retstring="invalid_argument";
return ERROR;
}
logwrite( function, message.str() );
return error;
}
/***** AstroCam::Interface::do_bin ******************************************/
/***** AstroCam::Interface::do_connect_controller ***************************/
/**
* @brief opens a connection to the PCI/e device(s)
* @param[in] devices_in optional string containing space-delimited list of devices
* @param[out] retstring reference to string to return error or help
* @return ERROR | NO_ERROR | HELP
*
* Input parameter devices_in defaults to empty string which will attempt to
* connect to all detected devices.
*
* If devices_in is specified (and not empty) then it must contain a space-delimited
* list of device numbers to open. A public vector connected_devnums will hold these device
* numbers. This vector will be updated here to represent only the devices that
* are actually connected.