//+********************************************************************** // // File: edf_retriever.cpp // // Project: Fable data to NeXus translation // // Description: code for implementing a retriever of data from edf files. // It is added into NXtranslate program in the form of plugin. // By parsing a XML configuration file of // NXtranslate program using this plugin, data from EDF image // header as well as the image itself can be read and stored // in NeXus file. // // Author(s): Jaroslaw Butanowicz // // Original: April 2006 // //+********************************************************************** #include #include #include #include #include #include #include "edf_retriever.h" #include "edf_reader.h" #include "../node.h" #include "../node_util.h" #include "../string_util.h" #include "../tree.hh" // //delimiter used to separate statements in location parameter. //used in command parser // #define DELIMITER ':' using std::ifstream; using std::invalid_argument; using std::runtime_error; using std::string; using std::cout; using std::endl; using std::vector; /** * Fuction parses location command from .xml configuration file. * Command can be of the form letter or letter:variable_name. * It does all the checkins for correctness of the command. There are * only three letters allowed "A", "I", "S". Only in case of letter "S"(single label), * variable_name parameter can be in the location. * Parameters: input - string location(command) * output - char* key, * Returns: 0 in case of A letter, 1 in case of S letter, * 2 in case of L letter. * In case of any other labels error will occur. */ int command_parser(const string &location, char* key){ int if_label=0; //initialization of flag int size = location.size(); //check size of command string // //location cannot be empty // if(size<=0) throw invalid_argument(" cannot parse empty string "); // //label only of the form A(all paramters from header) S:key(value for the given key is saved) // or I(only image saved) // if(location[0]!='A' && location[0]!='S' && location[0]!='I') throw invalid_argument(" bad location parameter only A , I or S:key are allowed "); // //check syntax for All keys and Image location(only letter A or I allowed) // if(location[0]=='A' || location[0]=='I'){ if(size!=1) throw invalid_argument(" for all only A for image only I "); } // //check syntax for Single key location(syntax-> S:key) // if(location[0]=='S'){ if(size<3 || location[1]!=DELIMITER) throw invalid_argument(" Bad syntax. Correct syntax: S:key "); } // //case for Single key // if(location[0]=='S'){ for(int i=2; i &tr){ char key[256];//variable for holding variable name int label_case = command_parser(location, key);//parse location, and check for case with label // //case for single label // if(label_case==1){ double double_value; string string_value; int dims[1]={0}; edf_reader Edf = edf_reader(infile);//parse edf file and built object with header values string key_string(key);//built string from the name of variable // //go thru the whole double value dictionary and look for key //if key exists read double value, and store in the NeXus file in the place from which it was invoked // if(Edf.double_by_key(key_string, double_value)){ double valueD[1]={double_value}; dims[0] = 1; Node node(key, (void*)valueD, 1, dims, NX_FLOAT64); tr.insert(tr.begin(),node); } else { // //go thru the whole string dictionary and look for key //if key exists read string value, and store in the NeXus file in the place from which it was invoked // if(Edf.string_by_key(key_string, string_value)){ const int size=string_value.size();//check size of string std::vector valueS(size); for(int i=0; i::iterator root, log, data; // //create NXentry root group, in the place where command was invoked, // Node node("empty", "NXentry"); root = tr.insert(tr.end(),node);//insert root to the tree // //create NXlog group for storing header values //group is a sub group of NXentry root group // Node log_node("EDF_log", "NXlog"); log = tr.append_child(root,log_node); // //create NXdata group for storing EDF image //group is a sub group of NXentry root group // Node data_node("EDF_image", "NXdata"); data = tr.append_child(root,data_node); // //get every keys and values from dicitonary of double values // for(int i=1; i valueS(size); // //convert C++ string into C-style string, NXtranslate convention // for(int i=0; i