| 1 |
//+********************************************************************** |
| 2 |
// |
| 3 |
// File: edf_retriever.cpp |
| 4 |
// |
| 5 |
// Project: Fable data to NeXus translation |
| 6 |
// |
| 7 |
// Description: code for implementing a retriever of data from edf files. |
| 8 |
// It is added into NXtranslate program in the form of plugin. |
| 9 |
// By parsing a XML configuration file of |
| 10 |
// NXtranslate program using this plugin, data from EDF image |
| 11 |
// header as well as the image itself can be read and stored |
| 12 |
// in NeXus file. |
| 13 |
// |
| 14 |
// Author(s): Jaroslaw Butanowicz |
| 15 |
// |
| 16 |
// Original: April 2006 |
| 17 |
// |
| 18 |
//+********************************************************************** |
| 19 |
|
| 20 |
#include <iostream> |
| 21 |
#include <fstream> |
| 22 |
#include <stdexcept> |
| 23 |
#include <string> |
| 24 |
#include <vector> |
| 25 |
#include <stdio.h> |
| 26 |
#include "edf_retriever.h" |
| 27 |
#include "edf_reader.h" |
| 28 |
#include "../node.h" |
| 29 |
#include "../node_util.h" |
| 30 |
#include "../string_util.h" |
| 31 |
#include "../tree.hh" |
| 32 |
// |
| 33 |
//delimiter used to separate statements in location parameter. |
| 34 |
//used in command parser |
| 35 |
// |
| 36 |
#define DELIMITER ':' |
| 37 |
|
| 38 |
using std::ifstream; |
| 39 |
using std::invalid_argument; |
| 40 |
using std::runtime_error; |
| 41 |
using std::string; |
| 42 |
using std::cout; |
| 43 |
using std::endl; |
| 44 |
using std::vector; |
| 45 |
|
| 46 |
/** |
| 47 |
* Fuction parses location command from .xml configuration file. |
| 48 |
* Command can be of the form letter or letter:variable_name. |
| 49 |
* It does all the checkins for correctness of the command. There are |
| 50 |
* only three letters allowed "A", "I", "S". Only in case of letter "S"(single label), |
| 51 |
* variable_name parameter can be in the location. |
| 52 |
* Parameters: input - string location(command) |
| 53 |
* output - char* key, |
| 54 |
* Returns: 0 in case of A letter, 1 in case of S letter, |
| 55 |
* 2 in case of L letter. |
| 56 |
* In case of any other labels error will occur. |
| 57 |
*/ |
| 58 |
int command_parser(const string &location, char* key){ |
| 59 |
|
| 60 |
int if_label=0; //initialization of flag |
| 61 |
int size = location.size(); //check size of command string |
| 62 |
// |
| 63 |
//location cannot be empty |
| 64 |
// |
| 65 |
if(size<=0) |
| 66 |
throw invalid_argument(" cannot parse empty string "); |
| 67 |
// |
| 68 |
//label only of the form A(all paramters from header) S:key(value for the given key is saved) |
| 69 |
// or I(only image saved) |
| 70 |
// |
| 71 |
if(location[0]!='A' && location[0]!='S' && location[0]!='I') |
| 72 |
throw invalid_argument(" bad location parameter only A , I or S:key are allowed "); |
| 73 |
// |
| 74 |
//check syntax for All keys and Image location(only letter A or I allowed) |
| 75 |
// |
| 76 |
if(location[0]=='A' || location[0]=='I'){ |
| 77 |
if(size!=1) |
| 78 |
throw invalid_argument(" for all only A for image only I "); |
| 79 |
} |
| 80 |
// |
| 81 |
//check syntax for Single key location(syntax-> S:key) |
| 82 |
// |
| 83 |
if(location[0]=='S'){ |
| 84 |
if(size<3 || location[1]!=DELIMITER) |
| 85 |
throw invalid_argument(" Bad syntax. Correct syntax: S:key "); |
| 86 |
} |
| 87 |
// |
| 88 |
//case for Single key |
| 89 |
// |
| 90 |
if(location[0]=='S'){ |
| 91 |
for(int i=2; i<size; i++){ |
| 92 |
key[i-2]=location[i];//subscribe key |
| 93 |
} |
| 94 |
key[size-2]='\0'; |
| 95 |
if_label=1; |
| 96 |
} |
| 97 |
// |
| 98 |
//case for Image |
| 99 |
// |
| 100 |
if(location[0]=='I'){ |
| 101 |
if_label=2; |
| 102 |
} |
| 103 |
return if_label; //returns 2 for Image, 1 for Single key, 0 All |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
/** |
| 110 |
* The factory will call the constructor with a string. The string |
| 111 |
* specifies where to locate the data (e.g. a filename), but |
| 112 |
* interpreting the string is left up to the implementing code. |
| 113 |
*/ |
| 114 |
EdfRetriever::EdfRetriever(const string &str): source(str){ |
| 115 |
|
| 116 |
// |
| 117 |
// open the file |
| 118 |
// |
| 119 |
infile.open(source.c_str()); |
| 120 |
// |
| 121 |
// check that open was successful |
| 122 |
// |
| 123 |
if(!infile.is_open()) |
| 124 |
throw invalid_argument("Could not open file: "+source); |
| 125 |
|
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Destructor is closing the file opened for reading. |
| 130 |
*/ |
| 131 |
EdfRetriever::~EdfRetriever(){ |
| 132 |
// |
| 133 |
// close the file |
| 134 |
// |
| 135 |
if(infile) |
| 136 |
infile.close(); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* This is the method for retrieving data from a file. The whole |
| 141 |
* tree will be written to the new file immediately after being |
| 142 |
* called. Interpreting the string is made by command_parser method. |
| 143 |
*/ |
| 144 |
void EdfRetriever::getData(const string &location, tree<Node> &tr){ |
| 145 |
|
| 146 |
char key[256];//variable for holding variable name |
| 147 |
int label_case = command_parser(location, key);//parse location, and check for case with label |
| 148 |
// |
| 149 |
//case for single label |
| 150 |
// |
| 151 |
if(label_case==1){ |
| 152 |
double double_value; |
| 153 |
string string_value; |
| 154 |
int dims[1]={0}; |
| 155 |
edf_reader Edf = edf_reader(infile);//parse edf file and built object with header values |
| 156 |
string key_string(key);//built string from the name of variable |
| 157 |
// |
| 158 |
//go thru the whole double value dictionary and look for key |
| 159 |
//if key exists read double value, and store in the NeXus file in the place from which it was invoked |
| 160 |
// |
| 161 |
if(Edf.double_by_key(key_string, double_value)){ |
| 162 |
|
| 163 |
double valueD[1]={double_value}; |
| 164 |
dims[0] = 1; |
| 165 |
Node node(key, (void*)valueD, 1, dims, NX_FLOAT64); |
| 166 |
tr.insert(tr.begin(),node); |
| 167 |
} |
| 168 |
else { |
| 169 |
|
| 170 |
// |
| 171 |
//go thru the whole string dictionary and look for key |
| 172 |
//if key exists read string value, and store in the NeXus file in the place from which it was invoked |
| 173 |
// |
| 174 |
if(Edf.string_by_key(key_string, string_value)){ |
| 175 |
|
| 176 |
const int size=string_value.size();//check size of string |
| 177 |
std::vector<char> valueS(size); |
| 178 |
for(int i=0; i<size; i++){ |
| 179 |
valueS[i]=string_value[i];//translate C++ string into C-style string, NXtranslate convetion |
| 180 |
} |
| 181 |
dims[0]=size; |
| 182 |
Node node(key, (void*)&(valueS[0]), 1, dims, NX_CHAR); |
| 183 |
tr.insert(tr.begin(),node); |
| 184 |
} |
| 185 |
// |
| 186 |
//key does not exist in any of both dictionaries, store empty string |
| 187 |
// |
| 188 |
else{ |
| 189 |
|
| 190 |
cout<<" Key "<<key_string<<" does not exist in the header. Empty string is saved.\n"; |
| 191 |
char empty[1]; |
| 192 |
Node node(key, (void*)empty, 1, dims, NX_CHAR); |
| 193 |
tr.insert(tr.begin(),node); |
| 194 |
|
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
// |
| 199 |
//case of single Image(I letter) |
| 200 |
// |
| 201 |
else if(label_case==2){ |
| 202 |
// |
| 203 |
//read EDF file and create object with header values and image |
| 204 |
// |
| 205 |
edf_reader Edf = edf_reader(infile); |
| 206 |
// |
| 207 |
//check the size of image |
| 208 |
// |
| 209 |
int image_dim[1]={Edf.get_image_size()}; |
| 210 |
// |
| 211 |
//store image in the place of the NeXus hierarchy from the command was invoked in .xml file |
| 212 |
// |
| 213 |
Node node_data("Image", (void*)Edf.get_image(), 1, image_dim, NX_INT8); |
| 214 |
tr.insert(tr.end(),node_data); |
| 215 |
|
| 216 |
} |
| 217 |
// |
| 218 |
//case for storing every keys from header with an image |
| 219 |
// |
| 220 |
else{ |
| 221 |
|
| 222 |
double valueD[1]; |
| 223 |
double double_value; |
| 224 |
string string_value; |
| 225 |
int dims1[1]={1}; |
| 226 |
// |
| 227 |
//create object with header values and Image |
| 228 |
// |
| 229 |
edf_reader Edf = edf_reader(infile); |
| 230 |
string key; |
| 231 |
// |
| 232 |
//get size of both dictionaries |
| 233 |
// |
| 234 |
int double_map_size = Edf.size_map_double(); |
| 235 |
int string_map_size = Edf.size_map_string(); |
| 236 |
// |
| 237 |
//iterators used for creation of hierarchical subtree |
| 238 |
// |
| 239 |
tree<Node>::iterator root, log, data; |
| 240 |
// |
| 241 |
//create NXentry root group, in the place where command was invoked, |
| 242 |
// |
| 243 |
Node node("empty", "NXentry"); |
| 244 |
root = tr.insert(tr.end(),node);//insert root to the tree |
| 245 |
// |
| 246 |
//create NXlog group for storing header values |
| 247 |
//group is a sub group of NXentry root group |
| 248 |
// |
| 249 |
Node log_node("EDF_log", "NXlog"); |
| 250 |
log = tr.append_child(root,log_node); |
| 251 |
// |
| 252 |
//create NXdata group for storing EDF image |
| 253 |
//group is a sub group of NXentry root group |
| 254 |
// |
| 255 |
Node data_node("EDF_image", "NXdata"); |
| 256 |
data = tr.append_child(root,data_node); |
| 257 |
// |
| 258 |
//get every keys and values from dicitonary of double values |
| 259 |
// |
| 260 |
for(int i=1; i<double_map_size+1; i++){ |
| 261 |
key=Edf.get_key_double(i, double_value); |
| 262 |
valueD[0]=double_value; |
| 263 |
// |
| 264 |
//store pairs key-double value in NXlog group |
| 265 |
// |
| 266 |
Node node_child(key, (void*)valueD, 1, dims1, NX_FLOAT64); |
| 267 |
tr.append_child(log,node_child); |
| 268 |
} |
| 269 |
// |
| 270 |
//get every keys and values from dicitonary of string values |
| 271 |
// |
| 272 |
for(int i=1; i<string_map_size+1; i++){ |
| 273 |
key=Edf.get_key_string(i, string_value); |
| 274 |
int size=string_value.size(); |
| 275 |
dims1[0]=size; |
| 276 |
std::vector<char> valueS(size); |
| 277 |
// |
| 278 |
//convert C++ string into C-style string, NXtranslate convention |
| 279 |
// |
| 280 |
for(int i=0; i<size; i++){ |
| 281 |
valueS[i]=string_value[i]; |
| 282 |
} |
| 283 |
// |
| 284 |
//store pairs key-string value in NXlog group |
| 285 |
// |
| 286 |
Node node_child(key, (void*)&(valueS[0]), 1, dims1, NX_CHAR); |
| 287 |
tr.append_child(log,node_child); |
| 288 |
} |
| 289 |
// |
| 290 |
//prepare image size |
| 291 |
// |
| 292 |
int image_dim[1]={Edf.get_image_size()}; |
| 293 |
// |
| 294 |
//store edf-image in NXdata group |
| 295 |
// |
| 296 |
Node node_data("Image", (void*)Edf.get_image(), 1, image_dim, NX_INT8); |
| 297 |
tr.append_child(data,node_data); |
| 298 |
} |
| 299 |
} |
| 300 |
// |
| 301 |
//indicate mime type |
| 302 |
// |
| 303 |
const string EdfRetriever::MIME_TYPE("edf"); |
| 304 |
|
| 305 |
string EdfRetriever::toString() const{ |
| 306 |
return "["+MIME_TYPE+"] "+source; |
| 307 |
} |
| 308 |
|