Skip to content
Snippets Groups Projects
Commit c8a6bd30 authored by Wuttke, Joachim's avatar Wuttke, Joachim
Browse files

file_in.cpp, rssm.cpp from Sravani1

parent 7670613a
No related branches found
No related tags found
No related merge requests found
......@@ -7,7 +7,6 @@
//! \file file_in.cpp
//! \brief NFileIn: load files.
#include <cstdlib>
#include <cstring>
#include <iostream>
......@@ -15,7 +14,6 @@
#include <string>
#include <vector>
#include <map>
#include <yaml-cpp/yaml.h>
#include <../trivia/string_ops.hpp>
......@@ -33,7 +31,7 @@
#include "file_in.hpp"
using namespace std;
using namespace YAML;
namespace NFileIn {
void Load_08( ifstream& F_in, string flong );
......@@ -42,7 +40,7 @@ namespace NFileIn {
//! Load file in current or legacy format.
void NFileIn::load()
void NFileIn::load(void)
{
string fnames = sask( "Load file(s)" );
vector<string> vflong; // unix names
......@@ -76,10 +74,9 @@ void NFileIn::load()
}
}
//! Load a YAML file in y08 format.
void NFileIn::Load_08( ifstream& FS, string flong )
void NFileIn::Load_08(ifstream& FS,string flong)
{
string lin, key, val, fdir, fshort, fext, res;
double num, vx, vy, dy;
......@@ -89,30 +86,31 @@ void NFileIn::Load_08( ifstream& FS, string flong )
triv::fname_divide( flong, &fdir, &fshort, &fext);
YAML::Parser parser(FS);
YAML::Node doc;
parser.GetNextDocument(doc);
const YAML::Node doc = YAML::Load(FS);
if( doc.Type() != YAML::NodeType::Map )
throw S("document root is not of MAP type");
// read meta
if(!doc.FindValue("Meta"))
//read meta
if(!doc["Meta"])
throw S("no Meta");
try {
if ( doc["Meta"].Type() != YAML::NodeType::Map &&
doc["Meta"].size() > 0)
throw S("Meta is not a MAP type");
if(!doc["Meta"].FindValue("format"))
if(!doc["Meta"]["format"])
throw S("no format in Meta");
string s;
doc["Meta"]["format"] >> s;
s = doc["Meta"]["format"].as<string>();
if ( s != "frida/y08 for yaml1" )
throw S("format is not frida/y08 for yaml1");
} catch(YAML::ParserException& e) {
std::cout << "no Meta" << e.what() << "\n";
} catch(YAML::Exception& e) {
std::cerr << "no Meta" << e.what() << "\n";
}
if(!doc["Meta"].FindValue("type"))
if(!doc["Meta"]["type"])
throw S("no type in Meta");
string type;
doc["Meta"]["type"] >> type;
type=doc["Meta"]["type"].as<string>();
// create output file
POlo fout;
......@@ -130,43 +128,43 @@ void NFileIn::Load_08( ifstream& FS, string flong )
throw "File "+flong+" has invalid type "+type;
// read history
if(!doc.FindValue("History"))
if(!doc["History"])
throw S("no History");
if ( doc["History"].Type() != YAML::NodeType::Sequence &&
doc["History"].size() > 0 )
throw S("History is not a SEQUENCE type");
for (int i = 0; i < doc["History"].size(); i++) {
string str;
doc["History"][i] >> str;
str=doc["History"][i].as<string>();
fout->lDoc.push_back(str);
}
// read coord
if(!doc.FindValue("Coord"))
if(!doc["Coord"])
throw S("no Coord");
if ( doc["Coord"].Type() != YAML::NodeType::Map && doc["Coord"].size() > 0 )
throw S("Coord is not a MAP type");
if( const YAML::Node *pName = doc["Coord"].FindValue("x") ){
(*pName)["name"] >> fout->xco.name;
(*pName)["unit"] >> fout->xco.unit;
if( const YAML::Node& pName = doc["Coord"]["x"] ){
fout->xco.name = pName["name"].as<string>();
fout->xco.unit=pName["unit"].as<string>();
} else
throw S("no x coord");
if( const YAML::Node *pName = doc["Coord"].FindValue("y") ){
(*pName)["name"] >> fout->yco.name;
(*pName)["unit"] >> fout->yco.unit;
if( const YAML::Node& pName = doc["Coord"]["y"] ){
fout->yco.name = pName["name"].as<string>();
fout->yco.unit= pName["unit"].as<string>();
} else
throw S("no y coord");
iz=0;
while(const YAML::Node *pName = doc["Coord"].FindValue("z"+S(iz))){
(*pName)["name"] >> co.name;
(*pName)["unit"] >> co.unit;
while(const YAML::Node& pName = doc["Coord"]["z"+S(iz)]){
co.name=pName["name"].as<string>();
co.unit=pName["unit"].as<string>();
fout->ZCo.push_back( co );
++iz;
}
// read Param
if(!doc.FindValue("Param"))
if(!doc["Param"])
throw S("no Param");
if ( doc["Param"].Type() != YAML::NodeType::Sequence &&
doc["Param"].size() > 0)
......@@ -174,9 +172,10 @@ void NFileIn::Load_08( ifstream& FS, string flong )
for( int iParam = 0; iParam < (doc["Param"].size()); iParam++ ){
if ( doc["Param"][iParam].Type() != YAML::NodeType::Map )
throw "Param " + S(iParam) + " is not a MAP type";
doc["Param"][iParam]["name"] >> co.name;
doc["Param"][iParam]["unit"] >> co.unit;
doc["Param"][iParam]["value"] >> val;
co.name=doc["Param"][iParam]["name"].as<string>();
co.unit=doc["Param"][iParam]["unit"].as<string>();
val=doc["Param"][iParam]["value"].as<string>();
if( !triv::any2dbl( val, &num ) )
throw "param(" + co.name + "): invalid value " + val;
fout->RPar.push_back( CParam( co, num ) );
......@@ -184,17 +183,17 @@ void NFileIn::Load_08( ifstream& FS, string flong )
// for curve, read formula
if( !isdata ) {
const YAML::Node *pName = doc.FindValue("Formula");
const YAML::Node& pName = doc["Formula"];
if( !pName )
throw S("DEFICIENT FILE: no formula");
string expr;
*pName >> expr;
//string expr;
string expr=pName.as<string>();
fc->parse_function( expr );
fc->curve_set_defaults();
}
//start to read Tables
if(!doc.FindValue("Tables"))
if(!doc["Tables"])
throw S("no Tables");
if ( doc["Tables"].Type() != YAML::NodeType::Sequence &&
doc["Tables"].size() > 0 )
......@@ -204,7 +203,7 @@ void NFileIn::Load_08( ifstream& FS, string flong )
throw "Tables " + S(iTable) + " is not a MAP type";
vector<RObj> z;
for(iz=0; iz<fout->ZCo.size(); ++iz ){
doc["Tables"][iTable]["z" + S(iz)] >> val;
val= doc["Tables"][iTable]["z" + S(iz)].as<string>() ;
if( !triv::any2dbl( val, &num ) )
throw "z" + S(iz) + ": invalid value " + val;
z.push_back( RObjDbl( new CObjDbl( num ) ) );
......@@ -213,11 +212,9 @@ void NFileIn::Load_08( ifstream& FS, string flong )
if( isdata ) {
PSpec sout( new CSpec );
sout->z = z;
YAML::Iterator itxy=doc["Tables"][iTable].begin();
string xytag;
itxy.first() >> xytag;
if ( xytag == "xy" ) {
itxy.second() >> val;
if ( doc["Tables"][iTable]["xy"] ) {
val=doc["Tables"][iTable]["xy"].as<string>();
while( val != "" ) {
triv::string_extract_line( val, &lin, &res );
if( sscanf( lin.c_str(), "%lg %lg\n", &vx, &vy )!=2 )
......@@ -225,8 +222,9 @@ void NFileIn::Load_08( ifstream& FS, string flong )
sout->push_xy( vx, vy );
val = res;
};
} else if ( xytag == "xyd" ) {
itxy.second() >> val;
} else if ( doc["Tables"][iTable]["xyd"] ) {
//itxy.second() >> val;
val=doc["Tables"][iTable]["xyd"].as<string>();
while( val != "" ) {
triv::string_extract_line( val, &lin, &res );
if( sscanf( lin.c_str(),
......@@ -238,11 +236,11 @@ void NFileIn::Load_08( ifstream& FS, string flong )
} else
throw "invalid xytag " + xytag;
fout->V.push_back( sout );
} else {
} else {
PCurve cout( new CCurve );
cout->z = z;
for( int ip=0; ip<fc->nP; ++ip ){
doc["Tables"][iTable]["p" + S(ip)] >> val;
val=doc["Tables"][iTable]["p" + S(ip)].as<string>();
if( !triv::any2dbl( val, &num ) )
throw "p" + S(ip) + ": invalid value " + val;
cout->P.push_back( num );
......
......@@ -43,7 +43,6 @@ class CRawfileSpheres {
//! Read raw data file, store contents as class variables
//void CRawfileSpheres::RdRawYam( FILE *F_in )
void CRawfileSpheres::RdRawYam( ifstream& F_in )
{
string key, val, bla, blub;
......@@ -56,21 +55,21 @@ void CRawfileSpheres::RdRawYam( ifstream& F_in )
daq_time_step=0;
YAML::Parser parser(F_in);
YAML::Node doc;
const YAML::Node *arr;
parser.GetNextDocument(doc);
//YAML::Parser parser(F_in);
const YAML::Node& doc = YAML::Load(F_in);
// read Meta:
if(!doc.FindValue("Meta"))
if(!doc["Meta"])
throw S("no Meta");
try {
YAML::NodeType::value Metatype = doc["Meta"].Type();
if ( Metatype != YAML::NodeType::Map && doc["Meta"].size() > 0)
throw S("DATA BUG: Meta is not a MAP");
if(!doc["Meta"].FindValue("format"))
if(!doc["Meta"]["format"])
throw S("DATA BUG: no format in Meta");
doc["Meta"]["format"] >> val;
string val;
val=doc["Meta"]["format"].as<string>();
if( strncmp( val.c_str(), "acq5.2 for yaml1", 16 ) )
throw "UNEXPECTED DATA: format: " + val + " instead of acq5.2";
} catch( exception& e) {
......@@ -81,12 +80,12 @@ void CRawfileSpheres::RdRawYam( ifstream& F_in )
// read Shortpar:
try{
if(!doc.FindValue("Shortpar"))
if(!doc["Shortpar"])
throw S("DATA BUG: no Shortpar");
for(auto it=doc["Shortpar"].begin(); it!=doc["Shortpar"].end(); ++it) {
it.first() >> key;
it.second() >> val;
if ( key=="incremental" ){
key = it->first.as<string>();
val=it->second.as<string>();
if ( key=="incremental" ){
if ( val=="true" )
incremental = true;
else if ( val=="false" )
......@@ -115,7 +114,7 @@ void CRawfileSpheres::RdRawYam( ifstream& F_in )
// read EnergyHistograms:
try {
if( !doc.FindValue("EnergyHistograms") )
if( !doc["EnergyHistograms"] )
throw S("DATA BUG: no EnergyHistograms");
if ( doc["EnergyHistograms"].Type() != YAML::NodeType::Sequence )
throw S("DATA BUG: EnergyHistograms is not a SEQUENCE");
......@@ -124,30 +123,34 @@ void CRawfileSpheres::RdRawYam( ifstream& F_in )
if ( doc["EnergyHistograms"][iEne].Type() !=
YAML::NodeType::Sequence )
throw "DATA BUG: EnergyHistogram[" + S(iEne) + "] is not a SEQUENCE";
doc["EnergyHistograms"][iEne][0] >> val;
val=doc["EnergyHistograms"][iEne][0].as<string>();
if( !triv::any2dbl( val, &num ) )
throw "E-Hist: invalid x-value " + val;
for( int i=0; i<4; ++i )
rawdata[i].push_back( num );
for( int i=0; i<4; ++i ){ // counts
arr = &(doc["EnergyHistograms"][iEne][i+1]);
if( arr->Type() != YAML::NodeType::Sequence )
// inline Node& Node::operator=(const T& rhs);
const YAML::Node arr = doc["EnergyHistograms"][iEne][i+1];
if( arr.Type() != YAML::NodeType::Sequence )
throw "DATA BUG: EnergyHistogram[" + S(iEne) + "][" +
S(i+1) + "] is not a SEQUENCE";
for( int j=0; j<ndet; ++j ) {
(*arr)[j] >> val;
val=(arr)[j].as<string>();
//(*arr)[j] >> val;
if( !triv::any2dbl( val, &num ) )
throw "E-Hist: invalid count " + val;
rawdata[i].push_back( num );
}
}
for( int i=0; i<4; ++i ){ // tsteps
arr = &(doc["EnergyHistograms"][iEne][i+1+4]);
if( arr->Type() != YAML::NodeType::Sequence )
const YAML::Node arr = (doc["EnergyHistograms"][iEne][i+1+4]);
if( arr.Type() != YAML::NodeType::Sequence )
throw "DATA BUG: EnergyHistogram[" + S(iEne) + "][" +
S(i+1+4) + "] is not a SEQUENCE";
for( int j=0; j<ndet; ++j ) {
(*arr)[j] >> val;
val=(arr)[j].as<string>();
if( !triv::any2dbl( val, &num ) )
throw "E-Hist: invalid tstep " + val;
rawdata[i].push_back( num );
......@@ -160,7 +163,7 @@ void CRawfileSpheres::RdRawYam( ifstream& F_in )
// read ChopperHistograms:
try {
if(!doc.FindValue("ChopperHistograms"))
if(!doc["ChopperHistograms"])
throw S("no ChopperHistograms");
if ( doc["ChopperHistograms"].Type() != YAML::NodeType::Sequence )
throw S("DATA BUG: ChopperHistograms is not a SEQUENCE");
......@@ -169,30 +172,30 @@ void CRawfileSpheres::RdRawYam( ifstream& F_in )
if ( doc["ChopperHistograms"][iCho].Type()
!= YAML::NodeType::Sequence )
throw "DATA BUG: ChopperHistograms " + S(iCho) + " is not a SEQUENCE";
doc["ChopperHistograms"][iCho][0] >> val;
val=doc["ChopperHistograms"][iCho][0].as<string>();
if( !triv::any2dbl( val, &num ) )
throw "DATA BUG: C-Hist: invalid x-value " + val;
for( int i=4; i<6; ++i )
rawdata[i].push_back( num );
for( int i=4; i<6; ++i ){ // 2 cnts lines
arr = &(doc["ChopperHistograms"][iCho][i-4+1]);
if( arr->Type() != YAML::NodeType::Sequence )
const YAML::Node arr = (doc["ChopperHistograms"][iCho][i-4+1]);
if( arr.Type() != YAML::NodeType::Sequence )
throw "DATA BUG: ChopperHistogram[" + S(iCho) + "][" +
S(i-4+1) + "] is not a SEQUENCE";
for( int j=0; j<ndet; ++j ) {
(*arr)[j] >> val;
val=(arr)[j].as<string>();
if( !triv::any2dbl( val, &num ) )
throw "E-Hist: invalid count " + val;
rawdata[i].push_back( num );
}
}
for( int i=4; i<6; ++i ){ // 2 time lines
arr = &(doc["ChopperHistograms"][iCho][i-4+1+2]);
if( arr->Type() != YAML::NodeType::Sequence )
const YAML::Node arr = (doc["ChopperHistograms"][iCho][i-4+1+2]);
if( arr.Type() != YAML::NodeType::Sequence )
throw "DATA BUG: ChopperHistogram[" + S(iCho) + "][" +
S(i-4+1+2) + "] is not a SEQUENCE";
for( int j=0; j<ndet; ++j ) {
(*arr)[j] >> val;
val=(arr)[j].as<string>();
if( !triv::any2dbl( val, &num ) )
throw "E-Hist: invalid count " + val;
rawdata[i].push_back( num );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment