~wbetz/fesslix/stat

« back to all changes in this revision

Viewing changes to src/flxstat.cpp

  • Committer: Wolfgang Betz
  • Date: 2017-07-04 04:35:15 UTC
  • Revision ID: wolfgang.betz@fesslix.org-20170704043515-ylljumpce2b6tfzf
Added object 'stat_cdf_save' and 'stat_cdf_load'

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 */
17
17
 
18
18
 
19
 
#define VERSION_REV 16
20
 
#define VERSION_DATE "2017-06-29"
 
19
#define VERSION_REV 17
 
20
#define VERSION_DATE "2017-07-03"
21
21
 
22
22
#include "flxstat.h"
23
23
#include "flxfunction_ope_calc.h"
24
 
 
 
24
#include <fstream>
25
25
 
26
26
#if defined(_MSC_VER)
27
27
  #include "flxlibstat_export.h"
54
54
    flx_iface.add_obj("stat_cdf_new", new FlxObjReadStat_CDF_new()); 
55
55
    flx_iface.add_obj("stat_cdf_append", new FlxObjReadStat_CDF_append()); 
56
56
    flx_iface.add_obj("stat_cdf_plot", new FlxObjReadStat_CDF_plot()); 
 
57
    flx_iface.add_obj("stat_cdf_save", new FlxObjReadStat_CDF_save()); 
 
58
    flx_iface.add_obj("stat_cdf_load", new FlxObjReadStat_CDF_load()); 
57
59
  // functions
58
60
    flxdata_ref = dataBox;
59
61
}
345
347
  };
346
348
}
347
349
 
 
350
void flx_cdf::save(const std::string& fileS)
 
351
{
 
352
  std::ofstream file(fileS.c_str(), std::ios::out | std::ios::binary);
 
353
  if (file.is_open()==false) {
 
354
    throw FlxException("flx_cdf::save", "File '" + fileS + "' could not be opened.");
 
355
  }
 
356
  file.write((char*)&(list[0]),sizeof(flx_cdf_entry)*list.size());
 
357
  file.close();
 
358
}
 
359
 
 
360
void flx_cdf::load(const std::string& fileS)
 
361
{
 
362
  if (N>0) throw FlxException("flx_cdf::load_01", "A stored data-CDF can only be loaded to an empty data-CDF.");
 
363
  std::ifstream file(fileS.c_str(), std::ios::in | std::ios::binary | std::ios::ate );
 
364
  if (file.is_open()==false || file.good()==false) {
 
365
    throw FlxException("flx_cdf::load_02", "File '" + fileS + "' could not be opened.");
 
366
  }
 
367
  std::streampos size = file.tellg();
 
368
  size_t sizeN = size/sizeof(flx_cdf_entry);
 
369
  if ( (size_t)size != sizeN*sizeof(flx_cdf_entry) ) {
 
370
    throw FlxException("flx_cdf::load_03", "The file '" + fileS + "' seems to be either corrupt or does not contain a data-CDF.");
 
371
  }
 
372
  list.resize(sizeN);
 
373
  file.seekg (0, std::ios::beg);
 
374
  file.read((char*)&(list[0]),size);
 
375
  file.close();
 
376
  // count N
 
377
    size_t Nlist = list.size();
 
378
    for (size_t i=0;i<Nlist;++i) {
 
379
      N += list[i].occ;
 
380
    };
 
381
}
 
382
 
348
383
void FlxObjStat_CDF_new::task()
349
384
{
350
385
  const size_t Nreserve = Nreservef->cast2tulong(false);
453
488
  }
454
489
}
455
490
 
 
491
void FlxObjStat_CDF_save::task()
 
492
{
 
493
  const std::string name = fname->eval_word(true);
 
494
  const std::string fileS = fileF->eval(false);
 
495
  flx_cdf& dcdf = CDFbox.get(name);
 
496
  dcdf.save(fileS);
 
497
  GlobalVar.slog(4) << "stat: stored data-CDF '" << name << "' in file '" << fileS << "'." << std::endl;
 
498
}
 
499
 
 
500
FlxObjBase* FlxObjReadStat_CDF_save::read()
 
501
{
 
502
  FlxString* fname = new FlxString(false,false);
 
503
  FlxString* fileF = NULL;
 
504
  try {
 
505
    reader->getChar('(');
 
506
    fileF = new FlxString(false,false);
 
507
    reader->getChar(')');
 
508
    read_optionalPara(false);
 
509
    return new FlxObjStat_CDF_save(get_doLog(),fname,fileF);
 
510
  } catch (FlxException& e) {
 
511
    FLXMSG("FlxObjReadStat_CDF_save::read",1);
 
512
    delete fname;
 
513
    if (fileF) delete fileF;
 
514
    throw;
 
515
  }
 
516
}
 
517
 
 
518
void FlxObjStat_CDF_load::task()
 
519
{
 
520
  const std::string name = fname->eval_word(true);
 
521
  const std::string fileS = fileF->eval(false);
 
522
  flx_cdf& dcdf = CDFbox.get(name);
 
523
  dcdf.load(fileS);
 
524
  GlobalVar.slog(4) << "stat: loaded data-CDF '" << name << "' from file '" << fileS << "'." << std::endl;
 
525
}
 
526
 
 
527
FlxObjBase* FlxObjReadStat_CDF_load::read()
 
528
{
 
529
  FlxString* fname = new FlxString(false,false);
 
530
  FlxString* fileF = NULL;
 
531
  try {
 
532
    reader->getChar('(');
 
533
    fileF = new FlxString(false,false);
 
534
    reader->getChar(')');
 
535
    read_optionalPara(false);
 
536
    return new FlxObjStat_CDF_load(get_doLog(),fname,fileF);
 
537
  } catch (FlxException& e) {
 
538
    FLXMSG("FlxObjReadStat_CDF_load::read",1);
 
539
    delete fname;
 
540
    if (fileF) delete fileF;
 
541
    throw;
 
542
  }
 
543
}
 
544
 
456
545
 
457
546