10 #include <ImageStreamIO/ImageStruct.h>
11 #include <ImageStreamIO/ImageStreamIO.h>
13 #include <xrif/xrif.h>
15 #include <mx/ioutils/fileUtils.hpp>
16 #include <mx/improc/eigenCube.hpp>
17 #include <mx/ioutils/fits/fitsFile.hpp>
19 #include <mx/sys/timeUtils.hpp>
21 #include "../../libMagAOX/libMagAOX.hpp"
29 std::this_thread::sleep_for(std::chrono::microseconds(usec));
54 static_cast<void>(signum);
55 static_cast<void>(siginf);
56 static_cast<void>(ucont);
137 config.add(
"dir",
"d",
"dir" , argType::Required,
"",
"dir",
false,
"string",
"The directory to search for files. Can be empty if pull path given in files.");
138 config.add(
"files",
"f",
"files" , argType::Required,
"",
"files",
false,
"vector<string>",
"List of files to use. If dir is not empty, it will be pre-pended to each name.");
139 config.add(
"numFrames",
"N",
"numFrames" , argType::Required,
"",
"numFrames",
false,
"int",
"The number of frames to store in memory. This defines how many different images will be streamed. If 0 (the default), all frames found using dir and files are loaded and stored.");
140 config.add(
"earliest",
"e",
"earliest" , argType::True,
"",
"earliest",
false,
"bool",
"If set or true, then the earliest numFrames in the archive are used. By default (if not set) the latest numFrames are used.");
141 config.add(
"shmimName",
"n",
"shmimName" , argType::Required,
"",
"shmimName",
false,
"string",
"The name of the shared memory buffer to stream to. Default is \"xrif2shmim\"");
142 config.add(
"circBuffLength",
"L",
"circBuffLength" , argType::Required,
"",
"circBuffLength",
false,
"int",
"The length of the shared memory circular buffer. Default is 1.");
144 config.add(
"fps",
"F",
"fps" , argType::Required,
"",
"fps",
false,
"float",
"The rate, in frames per second, at which to stream images. Default is 10 fps.");
150 config(
m_dir,
"dir");
156 config(
m_fps,
"fps");
163 struct sigaction act;
167 act.sa_flags = SA_SIGINFO;
172 if( sigaction(SIGTERM, &act, 0) < 0 )
174 std::cerr <<
" (" << invokedName <<
"): error setting SIGTERM handler: " << strerror(errno) <<
"\n";
179 if( sigaction(SIGQUIT, &act, 0) < 0 )
181 std::cerr <<
" (" << invokedName <<
"): error setting SIGQUIT handler: " << strerror(errno) <<
"\n";
186 if( sigaction(SIGINT, &act, 0) < 0 )
188 std::cerr <<
" (" << invokedName <<
"): error setting SIGINT handler: " << strerror(errno) <<
"\n";
200 m_files = mx::ioutils::getFileNames(
m_dir,
"",
"",
".xrif");
209 for(
size_t n=0; n<
m_files.size(); ++n)
217 std::cerr <<
" (" << invokedName <<
"): No files found.\n";
227 std::cerr <<
" (" << invokedName <<
"): Error allocating xrif.\n";
242 char header[XRIF_HEADER_SIZE];
247 for(
long n=st; n != ed; n += stp)
249 FILE * fp_xrif = fopen(
m_files[n].c_str(),
"rb");
250 size_t nr = fread(header, 1, XRIF_HEADER_SIZE, fp_xrif);
252 if(nr != XRIF_HEADER_SIZE)
254 std::cerr <<
" (" << invokedName <<
"): Error reading header of " <<
m_files[n] <<
"\n";
258 uint32_t header_size;
259 xrif_read_header(
m_xrif, &header_size , header);
271 std::cerr <<
" (" << invokedName <<
"): width mis-match in " <<
m_files[n] <<
"\n";
276 std::cerr <<
" (" << invokedName <<
"): height mis-match in " <<
m_files[n] <<
"\n";
281 std::cerr <<
" (" << invokedName <<
"): data type mismatch in " <<
m_files[n] <<
"\n";
287 std::cerr <<
" (" << invokedName <<
"): Cubes detected in " <<
m_files[n] <<
"\n";
299 nframes +=
m_xrif->frames;
310 std::cerr <<
" (" << invokedName <<
"): exiting.\n";
317 std::cerr <<
" (" << invokedName <<
"): Reading " <<
m_numFrames <<
" frames in " << (ed-st)*stp <<
" file";
337 for(
long n=st; n != ed; n += stp)
341 FILE * fp_xrif = fopen(
m_files[n].c_str(),
"rb");
342 size_t nr = fread(header, 1, XRIF_HEADER_SIZE, fp_xrif);
343 if(nr != XRIF_HEADER_SIZE)
345 std::cerr <<
" (" << invokedName <<
"): Error reading header of " <<
m_files[n] <<
"\n";
350 uint32_t header_size;
351 xrif_read_header(
m_xrif, &header_size , header);
353 xrif_allocate_raw(
m_xrif);
354 xrif_allocate_reordered(
m_xrif);
356 nr = fread(
m_xrif->raw_buffer, 1,
m_xrif->compressed_size, fp_xrif);
361 if(nr !=
m_xrif->compressed_size)
363 std::cerr <<
" (" << invokedName <<
"): Error reading data from " <<
m_files[n] <<
"\n";
375 long ped = tmpc.planes();
378 pst = tmpc.planes()-1;
382 for(
int p = pst; p != ped; p += stp)
384 m_frames.image(findex) = tmpc.image(p);
395 std::cerr <<
" (" << invokedName <<
"): exiting.\n";
412 ImageStreamIO_createIm_gpu(&
m_imageStream,
m_shmimName.c_str(), 3, imsize,
m_dataType, -1, 1, IMAGE_NB_SEMAPHORE, 0, CIRCULAR_BUFFER | ZAXIS_TEMPORAL, 0);
417 uint64_t next_cnt1 = 0;
424 double lastSend = mx::sys::get_curr_time();
461 if(findex >=
m_frames.planes()) findex = 0;
464 double ct = mx::sys::get_curr_time();
465 delta += 0.1 * (ct-lastSend - 1.0/
m_fps);
474 std::cerr <<
" (" << invokedName <<
"): exited normally.\n";
A utility to stream MagaO-X images from xrif compressed archives to an ImageStreamIO stream.
std::vector< std::string > m_files
List of files to use. If dir is not empty, it will be pre-pended to each name.
uint32_t m_height
The height of the image.
uint32_t m_width
The width of the image.
size_t m_typeSize
The size of the type, in bytes. Result of sizeof.
uint8_t m_dataType
The ImageStreamIO type code.
double m_fps
The rate, in frames per second, at which to stream images. Default is 10 fps.
size_t m_numFrames
The number of frames to store in memory. This defines how many different images will be streamed....
bool m_earliest
If true, then the earliest numFrames in the archive are used. By default (if not set) the latest numF...
IMAGE m_imageStream
The ImageStreamIO shared memory buffer.
std::string m_shmimName
The name of the shared memory buffer to stream to. Default is "xrif2shmim".
virtual void setupConfig()
std::string m_dir
The directory to search for files. Can be empty if pull path given in files. If files is empty,...
mx::improc::eigenCube< float > m_frames
virtual void loadConfig()
uint32_t m_circBuffLength
The length of the shared memory circular buffer. Default is 1.
void sigTermHandler(int signum, siginfo_t *siginf, void *ucont)
void microsleep(unsigned usec)
Sleep for a specified period in microseconds.