Skip to content

Commit f245c29

Browse files
authored
Merge pull request #222 from aglowacki/master
update load netcdf callback
2 parents 75be7a9 + 6bb7c6a commit f245c29

File tree

2 files changed

+49
-210
lines changed

2 files changed

+49
-210
lines changed

src/io/file/netcdf_io.cpp

Lines changed: 44 additions & 209 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ size_t NetCDF_IO<T_real>::_load_spectra(E_load_type ltype,
104104
size_t detector,
105105
data_struct::Spectra_Line<T_real>* spec_line,
106106
size_t line_size,
107-
data_struct::Spectra<T_real>* spectra)
107+
data_struct::Spectra<T_real>* spectra,
108+
size_t cur_row,
109+
size_t max_rows,
110+
data_struct::IO_Callback_Func_Def<T_real> *callback_fun,
111+
void* user_data)
108112
{
109113
std::lock_guard<std::mutex> lock(_mutex);
110114

@@ -123,6 +127,7 @@ size_t NetCDF_IO<T_real>::_load_spectra(E_load_type ltype,
123127
T_real elapsed_realtime = 0.;
124128
T_real input_counts = 0.;
125129
T_real output_counts = 0.;
130+
data_struct::Spectra<T_real>* callback_spectra = nullptr;
126131

127132
size_t dim2size[NC_MAX_VAR_DIMS] = {0};
128133

@@ -222,11 +227,11 @@ size_t NetCDF_IO<T_real>::_load_spectra(E_load_type ltype,
222227
}
223228

224229
size_t spec_cntr = 0;
225-
if (ltype == E_load_type::LINE || ltype == E_load_type::CALLBACKF)
230+
if (ltype == E_load_type::LINE)
226231
{
227232
spec_cntr = spec_line->size();
228233
}
229-
else if (ltype == E_load_type::INTEGRATED)
234+
else if (ltype == E_load_type::INTEGRATED || ltype == E_load_type::CALLBACKF)
230235
{
231236
spec_cntr = line_size;
232237
}
@@ -237,6 +242,10 @@ size_t NetCDF_IO<T_real>::_load_spectra(E_load_type ltype,
237242
{
238243
(*spec_line)[j].resize(spectra_size); // should be renames to resize
239244
}
245+
else if (ltype == E_load_type::CALLBACKF)
246+
{
247+
callback_spectra = new data_struct::Spectra<T_real>(spectra_size);
248+
}
240249

241250
//read header
242251
if( (retval = _nc_get_vars_real(ncid, varid, start, count, stride, &data_in[0][0][0]) ) != 0)
@@ -264,7 +273,7 @@ size_t NetCDF_IO<T_real>::_load_spectra(E_load_type ltype,
264273
unsigned short i1 = data_in[0][0][ELAPSED_LIVETIME_OFFSET+(detector*8)];
265274
unsigned short i2 = data_in[0][0][ELAPSED_LIVETIME_OFFSET+(detector*8)+1];
266275
unsigned int ii = i1 | i2<<16;
267-
if (ltype == E_load_type::LINE || ltype == E_load_type::CALLBACKF)
276+
if (ltype == E_load_type::LINE)
268277
{
269278
elapsed_livetime = ((float)ii) * 320e-9f; // need to multiply by this value becuase of the way it is saved
270279
if (elapsed_livetime == 0)
@@ -289,12 +298,15 @@ size_t NetCDF_IO<T_real>::_load_spectra(E_load_type ltype,
289298
{
290299
elapsed_livetime += ((float)ii) * 320e-9f;
291300
}
292-
301+
else if(ltype == E_load_type::CALLBACKF && callback_spectra != nullptr)
302+
{
303+
callback_spectra->elapsed_livetime( ((float)ii) * 320e-9f);
304+
}
293305

294306
i1 = data_in[0][0][ELAPSED_REALTIME_OFFSET+(detector*8)];
295307
i2 = data_in[0][0][ELAPSED_REALTIME_OFFSET+(detector*8)+1];
296308
ii = i1 | i2<<16;
297-
if (ltype == E_load_type::LINE || ltype == E_load_type::CALLBACKF)
309+
if (ltype == E_load_type::LINE)
298310
{
299311
elapsed_realtime = ((float)ii) * 320e-9f; // need to multiply by this value becuase of the way it is saved
300312
if (elapsed_realtime == 0)
@@ -319,7 +331,10 @@ size_t NetCDF_IO<T_real>::_load_spectra(E_load_type ltype,
319331
{
320332
elapsed_realtime += ((float)ii) * 320e-9f;
321333
}
322-
334+
else if(ltype == E_load_type::CALLBACKF && callback_spectra != nullptr)
335+
{
336+
callback_spectra->elapsed_realtime(((float)ii) * 320e-9f);
337+
}
323338

324339
i1 = data_in[0][0][INPUT_COUNTS_OFFSET+(detector*8)];
325340
i2 = data_in[0][0][INPUT_COUNTS_OFFSET+(detector*8)+1];
@@ -349,6 +364,11 @@ size_t NetCDF_IO<T_real>::_load_spectra(E_load_type ltype,
349364
{
350365
input_counts += ((float)ii) / elapsed_livetime;
351366
}
367+
else if(ltype == E_load_type::CALLBACKF && callback_fun != nullptr)
368+
{
369+
callback_spectra->input_counts(((float)ii) / elapsed_livetime);
370+
}
371+
352372

353373
i1 = data_in[0][0][OUTPUT_COUNTS_OFFSET+(detector*8)];
354374
i2 = data_in[0][0][OUTPUT_COUNTS_OFFSET+(detector*8)+1];
@@ -378,8 +398,12 @@ size_t NetCDF_IO<T_real>::_load_spectra(E_load_type ltype,
378398
{
379399
output_counts += ((float)ii) / elapsed_realtime;
380400
}
401+
else if(ltype == E_load_type::CALLBACKF && callback_fun != nullptr)
402+
{
403+
callback_spectra->output_counts(((float)ii) / elapsed_realtime);
404+
}
381405

382-
if (ltype == E_load_type::LINE || ltype == E_load_type::CALLBACKF)
406+
if (ltype == E_load_type::LINE)
383407
{
384408
(*spec_line)[j].elapsed_livetime(elapsed_livetime);
385409
(*spec_line)[j].elapsed_realtime(elapsed_realtime);
@@ -413,6 +437,12 @@ size_t NetCDF_IO<T_real>::_load_spectra(E_load_type ltype,
413437
(*spectra)(k) += data_in[0][0][k];
414438
}
415439
}
440+
else if(ltype == E_load_type::CALLBACKF && callback_fun != nullptr)
441+
{
442+
callback_spectra->recalc_elapsed_livetime();
443+
(*callback_fun)(cur_row, j, max_rows, line_size, detector, callback_spectra, user_data);
444+
}
445+
416446
start[2] += spectra_size * (4 - detector);
417447
count[2] = header_size;
418448

@@ -455,15 +485,15 @@ size_t NetCDF_IO<T_real>::_load_spectra(E_load_type ltype,
455485
template<typename T_real>
456486
size_t NetCDF_IO<T_real>::load_spectra_line(std::string path, size_t detector, data_struct::Spectra_Line<T_real>* spec_line)
457487
{
458-
return _load_spectra(E_load_type::LINE, path, detector, spec_line, -1, nullptr);
488+
return _load_spectra(E_load_type::LINE, path, detector, spec_line, -1, nullptr, 0, 0, nullptr, nullptr);
459489
}
460490

461491
//-----------------------------------------------------------------------------
462492

463493
template<typename T_real>
464494
size_t NetCDF_IO<T_real>::load_spectra_line_integrated(std::string path, size_t detector, size_t line_size, data_struct::Spectra<T_real>* spectra)
465495
{
466-
return _load_spectra(E_load_type::INTEGRATED, path, detector, nullptr, line_size, spectra);
496+
return _load_spectra(E_load_type::INTEGRATED, path, detector, nullptr, line_size, spectra, 0, 0, nullptr, nullptr);
467497
}
468498

469499
//-----------------------------------------------------------------------------
@@ -477,207 +507,12 @@ bool NetCDF_IO<T_real>::load_spectra_line_with_callback(std::string path,
477507
data_struct::IO_Callback_Func_Def<T_real> callback_fun,
478508
void* user_data)
479509
{
480-
481-
std::lock_guard<std::mutex> lock(_mutex);
482-
483-
size_t header_size = 256;
484-
int ncid, varid, retval;
485-
size_t start[] = {0, 0, 0};
486-
size_t count[] = {1, 1, header_size};
487-
ptrdiff_t stride[] = {1, 1, 1};
488-
T_real *data_in;
489-
size_t spectra_size;
490-
size_t num_detectors = detector_num_arr.size();
491-
int dataidx = 0;
492-
T_real elapsed_livetime = 0.;
493-
T_real elapsed_realtime = 0.;
494-
T_real input_counts = 0.;
495-
T_real output_counts = 0.;
496-
497-
nc_type rh_type;
498-
int rh_ndims;
499-
int rh_dimids[NC_MAX_VAR_DIMS] = {0};
500-
int rh_natts;
501-
502-
size_t dim2size[NC_MAX_VAR_DIMS] = {0};
503-
504-
if( (retval = nc_open(path.c_str(), NC_NOWRITE, &ncid)) != 0 )
505-
{
506-
logE<< path << " :: " << nc_strerror(retval)<<"\n";
507-
return false;
508-
}
509-
510-
if( (retval = nc_inq_varid(ncid, "array_data", &varid)) != 0)
511-
{
512-
logE<< path << " :: " << nc_strerror(retval)<<"\n";
513-
nc_close(ncid);
514-
return false;
515-
}
516-
517-
if( (retval = nc_inq_var (ncid, varid, 0, &rh_type, &rh_ndims, rh_dimids, &rh_natts) ) != 0)
518-
{
519-
logE<< path << " :: " << nc_strerror(retval)<<"\n";
520-
nc_close(ncid);
521-
return false;
522-
}
523-
524-
for (int i=0; i < rh_ndims; i++)
525-
{
526-
if( (retval = nc_inq_dimlen(ncid, rh_dimids[i], &dim2size[i]) ) != 0)
527-
{
528-
logE<< path << " :: " << nc_strerror(retval)<<"\n";
529-
nc_close(ncid);
530-
return false;
531-
}
532-
}
533-
534-
data_in = new T_real[dim2size[0] * dim2size[1] * dim2size[2]];
535-
count[2] = dim2size[2];
536-
//read in last col sector to get total number of cols
537-
//start[0] = dim2size[0] - 1;
538-
if( (retval = _nc_get_vars_real(ncid, varid, start, count, stride, &data_in[0]) ) != 0)
539-
{
540-
delete[] data_in;
541-
logE<< path << " :: " << nc_strerror(retval)<<"\n";
542-
nc_close(ncid);
543-
return false;
544-
}
545-
546-
if (data_in[0] != 21930 || data_in[1] != -21931)
547-
{
548-
delete[] data_in;
549-
logE<<"NetCDF header not found! Stopping load : "<<path<<"\n";
550-
nc_close(ncid);
551-
return false;
552-
}
553-
554-
//can't read from file because it can change inbetween rows ...
555-
//max_cols = (124 * (dim2size[0] - 1) ) + data_in[0][0][8];
556-
header_size = data_in[2];
557-
//num_cols = data_in[][0][8]; //sum all across the first dim looking at value 8
558-
spectra_size = data_in[20];
559-
560-
start[2] += header_size;
561-
count[2] = header_size + (spectra_size * MAX_NUM_SUPPORTED_DETECOTRS_PER_COL); //only 4 element detector supported per col
562-
563-
//loop through col sectors
564-
for(size_t j = 0; j < max_cols; j++)
510+
if (detector_num_arr.size() > 0)
565511
{
566-
/*
567-
//read header
568-
if( (retval = _nc_get_vars_real(ncid, varid, start, count, stride, &data_in[0][0][0]) ) != 0)
569-
{
570-
logE<< path << " :: " << nc_strerror(retval)<<"\n";
571-
nc_close(ncid);
572-
return false;
573-
}
574-
*/
575-
int midx = start[2];
576-
577-
if (data_in[midx] != 13260 || data_in[midx + 1] != -13261)
578-
{
579-
delete[] data_in;
580-
if(j < max_cols -2)
581-
{
582-
logE<<"NetCDF sub header not found! Stopping load at Col: "<<j<<" path :"<<path<<"\n";
583-
nc_close(ncid);
584-
return false;
585-
}
586-
//last two may not be filled with data
587-
//TODO: send end of row stream_block down pipeline
588-
nc_close(ncid);
589-
return true;
590-
}
591-
592-
for(size_t detector_num : detector_num_arr)
593-
{
594-
595-
if (detector_num > 3)
596-
{
597-
dataidx = 1;
598-
detector_num -= MAX_NUM_SUPPORTED_DETECOTRS_PER_COL;
599-
}
600-
else
601-
{
602-
dataidx = 0;
603-
}
604-
605-
midx = (dataidx * dim2size[2]) + start[2];
606-
607-
unsigned short i1 = data_in[midx+(ELAPSED_LIVETIME_OFFSET+(detector_num*8))];
608-
unsigned short i2 = data_in[midx + (ELAPSED_LIVETIME_OFFSET+(detector_num*8)+1)];
609-
unsigned int ii = i1 | i2<<16;
610-
elapsed_livetime = ((float)ii) * 320e-9f; // need to multiply by this value becuase of the way it is saved
611-
if(elapsed_livetime == 0)
612-
{
613-
if(j < max_cols-2) // usually the last two are missing which spams the log ouput.
614-
{
615-
logW<<"Reading in elapsed lifetime for Col:"<<j<<" is 0. Setting it to 1.0 " << path <<"\n";
616-
elapsed_livetime = 1.0;
617-
}
618-
}
619-
620-
i1 = data_in[midx + (ELAPSED_REALTIME_OFFSET+(detector_num*8))];
621-
i2 = data_in[midx + (ELAPSED_REALTIME_OFFSET+(detector_num*8)+1)];
622-
ii = i1 | i2<<16;
623-
elapsed_realtime = ((float)ii) * 320e-9f; // need to multiply by this value becuase of the way it is saved
624-
if(elapsed_realtime == 0)
625-
{
626-
if(j < max_cols-2) // usually the last two are missing which spams the log ouput.
627-
{
628-
logW<<"Reading in elapsed realtime for Col:"<<j<<" is 0. Setting it to 1.0 "<<path<<"\n";
629-
elapsed_realtime = 1.0;
630-
}
631-
}
632-
633-
i1 = data_in[midx + (INPUT_COUNTS_OFFSET+(detector_num*8))];
634-
i2 = data_in[midx + (INPUT_COUNTS_OFFSET+(detector_num*8)+1)];
635-
ii = i1 | i2<<16;
636-
input_counts = ((float)ii) / elapsed_livetime;
637-
638-
i1 = data_in[midx + (OUTPUT_COUNTS_OFFSET+(detector_num*8))];
639-
i2 = data_in[midx + (OUTPUT_COUNTS_OFFSET+(detector_num*8)+1)];
640-
ii = i1 | i2<<16;
641-
output_counts = ((float)ii) / elapsed_realtime;
642-
643-
data_struct::Spectra<T_real>* spectra = new data_struct::Spectra<T_real>(spectra_size);
644-
645-
spectra->elapsed_livetime(elapsed_livetime);
646-
spectra->elapsed_realtime(elapsed_realtime);
647-
spectra->input_counts(input_counts);
648-
spectra->output_counts(output_counts);
649-
spectra->recalc_elapsed_livetime();
650-
651-
int idx = header_size + (detector_num*spectra_size);
652-
653-
for(size_t k=0; k<spectra_size; k++)
654-
{
655-
(*spectra)[k] = data_in[midx + (idx+k)];
656-
}
657-
658-
callback_fun(row, j, max_rows, max_cols, detector_num, spectra, user_data);
659-
660-
}
661-
662-
start[2] += count[2];
663-
664-
if(start[2] >= dim2size[2])
665-
{
666-
start[0]++;
667-
start[2] = header_size;
668-
}
512+
size_t detector = detector_num_arr[0];
513+
return _load_spectra(E_load_type::CALLBACKF, path, detector, nullptr, max_cols, nullptr, row, max_rows, &callback_fun, nullptr);
669514
}
670-
671-
delete[] data_in;
672-
673-
if((retval = nc_close(ncid)) != 0)
674-
{
675-
logE<< path << " :: " << nc_strerror(retval)<<"\n";
676-
return false;
677-
}
678-
679-
return true;
680-
515+
return false;
681516
}
682517

683518
//-----------------------------------------------------------------------------

src/io/file/netcdf_io.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ class DLL_EXPORT NetCDF_IO
115115
size_t detector,
116116
data_struct::Spectra_Line<T_real>* spec_line,
117117
size_t line_size,
118-
data_struct::Spectra<T_real>* spectra);
118+
data_struct::Spectra<T_real>* spectra,
119+
size_t cur_row,
120+
size_t max_rows,
121+
data_struct::IO_Callback_Func_Def<T_real> *callback_fun,
122+
void* user_data);
119123

120124
static NetCDF_IO *_this_inst;
121125

0 commit comments

Comments
 (0)