[8780e9a] | 1 | |
---|
| 2 | |
---|
[0997158f] | 3 | ############################################################################ |
---|
| 4 | #This software was developed by the University of Tennessee as part of the |
---|
| 5 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
| 6 | #project funded by the US National Science Foundation. |
---|
| 7 | #If you use DANSE applications to do scientific research that leads to |
---|
| 8 | #publication, we ask that you acknowledge the use of the software with the |
---|
| 9 | #following sentence: |
---|
| 10 | #This work benefited from DANSE software developed under NSF award DMR-0520547. |
---|
| 11 | #copyright 2008,2009 University of Tennessee |
---|
| 12 | ############################################################################# |
---|
| 13 | |
---|
[579ba85] | 14 | # Known issue: reader not compatible with multiple SASdata entries |
---|
| 15 | # within a single SASentry. Will raise a runtime error. |
---|
[8780e9a] | 16 | |
---|
[a7a5886] | 17 | #TODO: check that all vectors are written only if they have at |
---|
| 18 | # least one non-empty value |
---|
| 19 | #TODO: Writing only allows one SASentry per file. |
---|
| 20 | # Would be best to allow multiple entries. |
---|
[8780e9a] | 21 | #TODO: Store error list |
---|
| 22 | #TODO: Allow for additional meta data for each section |
---|
[a7a5886] | 23 | #TODO: Notes need to be implemented. They can be any XML |
---|
| 24 | # structure in version 1.0 |
---|
[8780e9a] | 25 | # Process notes have the same problem. |
---|
[e390933] | 26 | #TODO: Unit conversion is not complete (temperature units are missing) |
---|
[8780e9a] | 27 | |
---|
| 28 | |
---|
| 29 | import logging |
---|
| 30 | import numpy |
---|
[a7a5886] | 31 | import os |
---|
| 32 | import sys |
---|
| 33 | from DataLoader.data_info import Data1D |
---|
| 34 | from DataLoader.data_info import Collimation |
---|
| 35 | from DataLoader.data_info import Detector |
---|
| 36 | from DataLoader.data_info import Process |
---|
| 37 | from DataLoader.data_info import Aperture |
---|
[b0d0723] | 38 | from lxml import etree |
---|
| 39 | import xml.dom.minidom |
---|
[8780e9a] | 40 | |
---|
[b39c817] | 41 | has_converter = True |
---|
| 42 | try: |
---|
| 43 | from data_util.nxsunit import Converter |
---|
| 44 | except: |
---|
| 45 | has_converter = False |
---|
| 46 | |
---|
[b0d0723] | 47 | CANSAS_NS = "cansas1d/1.0" |
---|
| 48 | |
---|
[4c00964] | 49 | def write_node(doc, parent, name, value, attr={}): |
---|
| 50 | """ |
---|
[0997158f] | 51 | :param doc: document DOM |
---|
| 52 | :param parent: parent node |
---|
| 53 | :param name: tag of the element |
---|
| 54 | :param value: value of the child text node |
---|
| 55 | :param attr: attribute dictionary |
---|
| 56 | |
---|
| 57 | :return: True if something was appended, otherwise False |
---|
[4c00964] | 58 | """ |
---|
| 59 | if value is not None: |
---|
| 60 | node = doc.createElement(name) |
---|
| 61 | node.appendChild(doc.createTextNode(str(value))) |
---|
| 62 | for item in attr: |
---|
| 63 | node.setAttribute(item, attr[item]) |
---|
| 64 | parent.appendChild(node) |
---|
| 65 | return True |
---|
| 66 | return False |
---|
| 67 | |
---|
[8780e9a] | 68 | def get_content(location, node): |
---|
| 69 | """ |
---|
[0997158f] | 70 | Get the first instance of the content of a xpath location. |
---|
| 71 | |
---|
| 72 | :param location: xpath location |
---|
| 73 | :param node: node to start at |
---|
| 74 | |
---|
| 75 | :return: Element, or None |
---|
[8780e9a] | 76 | """ |
---|
[b0d0723] | 77 | nodes = node.xpath(location, namespaces={'ns': CANSAS_NS}) |
---|
| 78 | |
---|
[8780e9a] | 79 | if len(nodes)>0: |
---|
[b0d0723] | 80 | return nodes[0] |
---|
| 81 | else: |
---|
| 82 | return None |
---|
[8780e9a] | 83 | |
---|
| 84 | def get_float(location, node): |
---|
| 85 | """ |
---|
[0997158f] | 86 | Get the content of a node as a float |
---|
| 87 | |
---|
| 88 | :param location: xpath location |
---|
| 89 | :param node: node to start at |
---|
[8780e9a] | 90 | """ |
---|
[b0d0723] | 91 | nodes = node.xpath(location, namespaces={'ns': CANSAS_NS}) |
---|
| 92 | |
---|
[8780e9a] | 93 | value = None |
---|
| 94 | attr = {} |
---|
[a7a5886] | 95 | if len(nodes) > 0: |
---|
[8780e9a] | 96 | try: |
---|
[b0d0723] | 97 | value = float(nodes[0].text) |
---|
[8780e9a] | 98 | except: |
---|
| 99 | # Could not pass, skip and return None |
---|
[a7a5886] | 100 | msg = "cansas_reader.get_float: could not " |
---|
| 101 | msg += " convert '%s' to float" % nodes[0].text |
---|
| 102 | logging.error(msg) |
---|
[b0d0723] | 103 | if nodes[0].get('unit') is not None: |
---|
| 104 | attr['unit'] = nodes[0].get('unit') |
---|
[8780e9a] | 105 | return value, attr |
---|
| 106 | |
---|
[b39c817] | 107 | |
---|
[8780e9a] | 108 | class Reader: |
---|
| 109 | """ |
---|
[0997158f] | 110 | Class to load cansas 1D XML files |
---|
| 111 | |
---|
| 112 | :Dependencies: |
---|
| 113 | The CanSas reader requires PyXML 0.8.4 or later. |
---|
[8780e9a] | 114 | """ |
---|
| 115 | ## CanSAS version |
---|
| 116 | version = '1.0' |
---|
| 117 | ## File type |
---|
[28caa03] | 118 | type_name = "CanSAS 1D" |
---|
| 119 | ## Wildcards |
---|
[7406040] | 120 | type = ["CanSAS 1D files (*.xml)|*.xml", |
---|
| 121 | "CanSAS 1D AVE files (*.AVEx)|*.AVEx", |
---|
| 122 | "CanSAS 1D AVE files (*.ABSx)|*.ABSx"] |
---|
| 123 | |
---|
[8780e9a] | 124 | ## List of allowed extensions |
---|
[a7a5886] | 125 | ext = ['.xml', '.XML','.avex', '.AVEx', '.absx', 'ABSx'] |
---|
[8780e9a] | 126 | |
---|
[fe78c7b] | 127 | def __init__(self): |
---|
| 128 | ## List of errors |
---|
| 129 | self.errors = [] |
---|
| 130 | |
---|
[8780e9a] | 131 | def read(self, path): |
---|
| 132 | """ |
---|
[0997158f] | 133 | Load data file |
---|
| 134 | |
---|
| 135 | :param path: file path |
---|
| 136 | |
---|
| 137 | :return: Data1D object if a single SASentry was found, |
---|
| 138 | or a list of Data1D objects if multiple entries were found, |
---|
| 139 | or None of nothing was found |
---|
| 140 | |
---|
| 141 | :raise RuntimeError: when the file can't be opened |
---|
| 142 | :raise ValueError: when the length of the data vectors are inconsistent |
---|
[8780e9a] | 143 | """ |
---|
| 144 | output = [] |
---|
| 145 | if os.path.isfile(path): |
---|
| 146 | basename = os.path.basename(path) |
---|
| 147 | root, extension = os.path.splitext(basename) |
---|
| 148 | if extension.lower() in self.ext: |
---|
| 149 | |
---|
[b0d0723] | 150 | tree = etree.parse(path, parser=etree.ETCompatXMLParser()) |
---|
[8780e9a] | 151 | # Check the format version number |
---|
[a7a5886] | 152 | # Specifying the namespace will take care of the file |
---|
| 153 | # format version |
---|
[b0d0723] | 154 | root = tree.getroot() |
---|
| 155 | |
---|
[a7a5886] | 156 | entry_list = root.xpath('/ns:SASroot/ns:SASentry', |
---|
| 157 | namespaces={'ns': CANSAS_NS}) |
---|
[8780e9a] | 158 | |
---|
| 159 | for entry in entry_list: |
---|
[fe78c7b] | 160 | self.errors = [] |
---|
[8780e9a] | 161 | sas_entry = self._parse_entry(entry) |
---|
| 162 | sas_entry.filename = basename |
---|
[fe78c7b] | 163 | |
---|
| 164 | # Store loading process information |
---|
| 165 | sas_entry.errors = self.errors |
---|
| 166 | sas_entry.meta_data['loader'] = self.type_name |
---|
[8780e9a] | 167 | output.append(sas_entry) |
---|
| 168 | |
---|
| 169 | else: |
---|
| 170 | raise RuntimeError, "%s is not a file" % path |
---|
| 171 | # Return output consistent with the loader's api |
---|
[a01c743] | 172 | if len(output) == 0: |
---|
| 173 | #cannot return none when it cannot read |
---|
| 174 | #return None |
---|
| 175 | raise RuntimeError, "%s cannot be read \n" % path |
---|
| 176 | elif len(output) == 1: |
---|
[8780e9a] | 177 | return output[0] |
---|
| 178 | else: |
---|
| 179 | return output |
---|
| 180 | |
---|
| 181 | def _parse_entry(self, dom): |
---|
| 182 | """ |
---|
[0997158f] | 183 | Parse a SASentry |
---|
| 184 | |
---|
| 185 | :param node: SASentry node |
---|
| 186 | |
---|
| 187 | :return: Data1D object |
---|
[8780e9a] | 188 | """ |
---|
| 189 | x = numpy.zeros(0) |
---|
| 190 | y = numpy.zeros(0) |
---|
| 191 | |
---|
| 192 | data_info = Data1D(x, y) |
---|
| 193 | |
---|
[b0d0723] | 194 | # Look up title |
---|
[fe78c7b] | 195 | self._store_content('ns:Title', dom, 'title', data_info) |
---|
[b0d0723] | 196 | |
---|
[579ba85] | 197 | # Look up run number |
---|
[b0d0723] | 198 | nodes = dom.xpath('ns:Run', namespaces={'ns': CANSAS_NS}) |
---|
[579ba85] | 199 | for item in nodes: |
---|
[b0d0723] | 200 | if item.text is not None: |
---|
| 201 | value = item.text.strip() |
---|
| 202 | if len(value) > 0: |
---|
| 203 | data_info.run.append(value) |
---|
| 204 | if item.get('name') is not None: |
---|
| 205 | data_info.run_name[value] = item.get('name') |
---|
[579ba85] | 206 | |
---|
[8780e9a] | 207 | # Look up instrument name |
---|
[a7a5886] | 208 | self._store_content('ns:SASinstrument/ns:name', dom, 'instrument', |
---|
| 209 | data_info) |
---|
[8780e9a] | 210 | |
---|
[b0d0723] | 211 | # Notes |
---|
| 212 | note_list = dom.xpath('ns:SASnote', namespaces={'ns': CANSAS_NS}) |
---|
[8780e9a] | 213 | for note in note_list: |
---|
| 214 | try: |
---|
[b0d0723] | 215 | if note.text is not None: |
---|
| 216 | note_value = note.text.strip() |
---|
| 217 | if len(note_value) > 0: |
---|
| 218 | data_info.notes.append(note_value) |
---|
[8780e9a] | 219 | except: |
---|
[a7a5886] | 220 | err_mess = "cansas_reader.read: error processing" |
---|
| 221 | err_mess += " entry notes\n %s" % sys.exc_value |
---|
[fe78c7b] | 222 | self.errors.append(err_mess) |
---|
| 223 | logging.error(err_mess) |
---|
[8780e9a] | 224 | |
---|
| 225 | # Sample info ################### |
---|
[b0d0723] | 226 | entry = get_content('ns:SASsample', dom) |
---|
| 227 | if entry is not None: |
---|
| 228 | data_info.sample.name = entry.get('name') |
---|
[579ba85] | 229 | |
---|
[fe78c7b] | 230 | self._store_content('ns:SASsample/ns:ID', |
---|
[8780e9a] | 231 | dom, 'ID', data_info.sample) |
---|
[fe78c7b] | 232 | self._store_float('ns:SASsample/ns:thickness', |
---|
[8780e9a] | 233 | dom, 'thickness', data_info.sample) |
---|
[fe78c7b] | 234 | self._store_float('ns:SASsample/ns:transmission', |
---|
[8780e9a] | 235 | dom, 'transmission', data_info.sample) |
---|
[fe78c7b] | 236 | self._store_float('ns:SASsample/ns:temperature', |
---|
[8780e9a] | 237 | dom, 'temperature', data_info.sample) |
---|
[b0d0723] | 238 | |
---|
[a7a5886] | 239 | nodes = dom.xpath('ns:SASsample/ns:details', |
---|
| 240 | namespaces={'ns': CANSAS_NS}) |
---|
[8780e9a] | 241 | for item in nodes: |
---|
| 242 | try: |
---|
[b0d0723] | 243 | if item.text is not None: |
---|
| 244 | detail_value = item.text.strip() |
---|
| 245 | if len(detail_value) > 0: |
---|
| 246 | data_info.sample.details.append(detail_value) |
---|
[8780e9a] | 247 | except: |
---|
[a7a5886] | 248 | err_mess = "cansas_reader.read: error processing " |
---|
| 249 | err_mess += " sample details\n %s" % sys.exc_value |
---|
[fe78c7b] | 250 | self.errors.append(err_mess) |
---|
| 251 | logging.error(err_mess) |
---|
[8780e9a] | 252 | |
---|
| 253 | # Position (as a vector) |
---|
[fe78c7b] | 254 | self._store_float('ns:SASsample/ns:position/ns:x', |
---|
[8780e9a] | 255 | dom, 'position.x', data_info.sample) |
---|
[fe78c7b] | 256 | self._store_float('ns:SASsample/ns:position/ns:y', |
---|
[8780e9a] | 257 | dom, 'position.y', data_info.sample) |
---|
[fe78c7b] | 258 | self._store_float('ns:SASsample/ns:position/ns:z', |
---|
[8780e9a] | 259 | dom, 'position.z', data_info.sample) |
---|
| 260 | |
---|
| 261 | # Orientation (as a vector) |
---|
[fe78c7b] | 262 | self._store_float('ns:SASsample/ns:orientation/ns:roll', |
---|
[8780e9a] | 263 | dom, 'orientation.x', data_info.sample) |
---|
[fe78c7b] | 264 | self._store_float('ns:SASsample/ns:orientation/ns:pitch', |
---|
[8780e9a] | 265 | dom, 'orientation.y', data_info.sample) |
---|
[fe78c7b] | 266 | self._store_float('ns:SASsample/ns:orientation/ns:yaw', |
---|
[8780e9a] | 267 | dom, 'orientation.z', data_info.sample) |
---|
| 268 | |
---|
| 269 | # Source info ################### |
---|
[b0d0723] | 270 | entry = get_content('ns:SASinstrument/ns:SASsource', dom) |
---|
| 271 | if entry is not None: |
---|
| 272 | data_info.source.name = entry.get('name') |
---|
[4c00964] | 273 | |
---|
[fe78c7b] | 274 | self._store_content('ns:SASinstrument/ns:SASsource/ns:radiation', |
---|
[8780e9a] | 275 | dom, 'radiation', data_info.source) |
---|
[fe78c7b] | 276 | self._store_content('ns:SASinstrument/ns:SASsource/ns:beam_shape', |
---|
[8780e9a] | 277 | dom, 'beam_shape', data_info.source) |
---|
[fe78c7b] | 278 | self._store_float('ns:SASinstrument/ns:SASsource/ns:wavelength', |
---|
[8780e9a] | 279 | dom, 'wavelength', data_info.source) |
---|
[fe78c7b] | 280 | self._store_float('ns:SASinstrument/ns:SASsource/ns:wavelength_min', |
---|
[8780e9a] | 281 | dom, 'wavelength_min', data_info.source) |
---|
[fe78c7b] | 282 | self._store_float('ns:SASinstrument/ns:SASsource/ns:wavelength_max', |
---|
[8780e9a] | 283 | dom, 'wavelength_max', data_info.source) |
---|
[fe78c7b] | 284 | self._store_float('ns:SASinstrument/ns:SASsource/ns:wavelength_spread', |
---|
[8780e9a] | 285 | dom, 'wavelength_spread', data_info.source) |
---|
| 286 | |
---|
[579ba85] | 287 | # Beam size (as a vector) |
---|
[b0d0723] | 288 | entry = get_content('ns:SASinstrument/ns:SASsource/ns:beam_size', dom) |
---|
| 289 | if entry is not None: |
---|
| 290 | data_info.source.beam_size_name = entry.get('name') |
---|
[579ba85] | 291 | |
---|
[fe78c7b] | 292 | self._store_float('ns:SASinstrument/ns:SASsource/ns:beam_size/ns:x', |
---|
[8780e9a] | 293 | dom, 'beam_size.x', data_info.source) |
---|
[fe78c7b] | 294 | self._store_float('ns:SASinstrument/ns:SASsource/ns:beam_size/ns:y', |
---|
[8780e9a] | 295 | dom, 'beam_size.y', data_info.source) |
---|
[fe78c7b] | 296 | self._store_float('ns:SASinstrument/ns:SASsource/ns:beam_size/ns:z', |
---|
[8780e9a] | 297 | dom, 'beam_size.z', data_info.source) |
---|
| 298 | |
---|
| 299 | # Collimation info ################### |
---|
[a7a5886] | 300 | nodes = dom.xpath('ns:SASinstrument/ns:SAScollimation', |
---|
| 301 | namespaces={'ns': CANSAS_NS}) |
---|
[8780e9a] | 302 | for item in nodes: |
---|
| 303 | collim = Collimation() |
---|
[b0d0723] | 304 | if item.get('name') is not None: |
---|
| 305 | collim.name = item.get('name') |
---|
[fe78c7b] | 306 | self._store_float('ns:length', item, 'length', collim) |
---|
[8780e9a] | 307 | |
---|
| 308 | # Look for apertures |
---|
[b0d0723] | 309 | apert_list = item.xpath('ns:aperture', namespaces={'ns': CANSAS_NS}) |
---|
[8780e9a] | 310 | for apert in apert_list: |
---|
[d6513cd] | 311 | aperture = Aperture() |
---|
[4c00964] | 312 | |
---|
| 313 | # Get the name and type of the aperture |
---|
[b0d0723] | 314 | aperture.name = apert.get('name') |
---|
| 315 | aperture.type = apert.get('type') |
---|
[4c00964] | 316 | |
---|
[fe78c7b] | 317 | self._store_float('ns:distance', apert, 'distance', aperture) |
---|
[579ba85] | 318 | |
---|
[b0d0723] | 319 | entry = get_content('ns:size', apert) |
---|
| 320 | if entry is not None: |
---|
| 321 | aperture.size_name = entry.get('name') |
---|
[579ba85] | 322 | |
---|
[fe78c7b] | 323 | self._store_float('ns:size/ns:x', apert, 'size.x', aperture) |
---|
| 324 | self._store_float('ns:size/ns:y', apert, 'size.y', aperture) |
---|
| 325 | self._store_float('ns:size/ns:z', apert, 'size.z', aperture) |
---|
[8780e9a] | 326 | |
---|
| 327 | collim.aperture.append(aperture) |
---|
| 328 | |
---|
| 329 | data_info.collimation.append(collim) |
---|
| 330 | |
---|
| 331 | # Detector info ###################### |
---|
[a7a5886] | 332 | nodes = dom.xpath('ns:SASinstrument/ns:SASdetector', |
---|
| 333 | namespaces={'ns': CANSAS_NS}) |
---|
[8780e9a] | 334 | for item in nodes: |
---|
| 335 | |
---|
| 336 | detector = Detector() |
---|
| 337 | |
---|
[fe78c7b] | 338 | self._store_content('ns:name', item, 'name', detector) |
---|
| 339 | self._store_float('ns:SDD', item, 'distance', detector) |
---|
[8780e9a] | 340 | |
---|
| 341 | # Detector offset (as a vector) |
---|
[fe78c7b] | 342 | self._store_float('ns:offset/ns:x', item, 'offset.x', detector) |
---|
| 343 | self._store_float('ns:offset/ns:y', item, 'offset.y', detector) |
---|
| 344 | self._store_float('ns:offset/ns:z', item, 'offset.z', detector) |
---|
[8780e9a] | 345 | |
---|
| 346 | # Detector orientation (as a vector) |
---|
[a7a5886] | 347 | self._store_float('ns:orientation/ns:roll', item, 'orientation.x', |
---|
| 348 | detector) |
---|
| 349 | self._store_float('ns:orientation/ns:pitch', item, 'orientation.y', |
---|
| 350 | detector) |
---|
| 351 | self._store_float('ns:orientation/ns:yaw', item, 'orientation.z', |
---|
| 352 | detector) |
---|
[8780e9a] | 353 | |
---|
| 354 | # Beam center (as a vector) |
---|
[a7a5886] | 355 | self._store_float('ns:beam_center/ns:x', item, 'beam_center.x', |
---|
| 356 | detector) |
---|
| 357 | self._store_float('ns:beam_center/ns:y', item, 'beam_center.y', |
---|
| 358 | detector) |
---|
| 359 | self._store_float('ns:beam_center/ns:z', item, 'beam_center.z', |
---|
| 360 | detector) |
---|
[8780e9a] | 361 | |
---|
| 362 | # Pixel size (as a vector) |
---|
[a7a5886] | 363 | self._store_float('ns:pixel_size/ns:x', item, 'pixel_size.x', |
---|
| 364 | detector) |
---|
| 365 | self._store_float('ns:pixel_size/ns:y', item, 'pixel_size.y', |
---|
| 366 | detector) |
---|
| 367 | self._store_float('ns:pixel_size/ns:z', item, 'pixel_size.z', |
---|
| 368 | detector) |
---|
[8780e9a] | 369 | |
---|
[fe78c7b] | 370 | self._store_float('ns:slit_length', item, 'slit_length', detector) |
---|
[8780e9a] | 371 | |
---|
| 372 | data_info.detector.append(detector) |
---|
| 373 | |
---|
| 374 | # Processes info ###################### |
---|
[b0d0723] | 375 | nodes = dom.xpath('ns:SASprocess', namespaces={'ns': CANSAS_NS}) |
---|
[8780e9a] | 376 | for item in nodes: |
---|
| 377 | process = Process() |
---|
[fe78c7b] | 378 | self._store_content('ns:name', item, 'name', process) |
---|
| 379 | self._store_content('ns:date', item, 'date', process) |
---|
| 380 | self._store_content('ns:description', item, 'description', process) |
---|
[8780e9a] | 381 | |
---|
[b0d0723] | 382 | term_list = item.xpath('ns:term', namespaces={'ns': CANSAS_NS}) |
---|
[8780e9a] | 383 | for term in term_list: |
---|
| 384 | try: |
---|
[b0d0723] | 385 | term_attr = {} |
---|
| 386 | for attr in term.keys(): |
---|
| 387 | term_attr[attr] = term.get(attr).strip() |
---|
| 388 | if term.text is not None: |
---|
| 389 | term_attr['value'] = term.text.strip() |
---|
[8780e9a] | 390 | process.term.append(term_attr) |
---|
| 391 | except: |
---|
[a7a5886] | 392 | err_mess = "cansas_reader.read: error processing " |
---|
| 393 | err_mess += " process term\n %s" % sys.exc_value |
---|
[fe78c7b] | 394 | self.errors.append(err_mess) |
---|
| 395 | logging.error(err_mess) |
---|
[8780e9a] | 396 | |
---|
[a7a5886] | 397 | note_list = item.xpath('ns:SASprocessnote', |
---|
| 398 | namespaces={'ns': CANSAS_NS}) |
---|
[8780e9a] | 399 | for note in note_list: |
---|
[b0d0723] | 400 | if note.text is not None: |
---|
| 401 | process.notes.append(note.text.strip()) |
---|
[8780e9a] | 402 | |
---|
| 403 | data_info.process.append(process) |
---|
| 404 | |
---|
| 405 | |
---|
| 406 | # Data info ###################### |
---|
[b0d0723] | 407 | nodes = dom.xpath('ns:SASdata', namespaces={'ns': CANSAS_NS}) |
---|
[a7a5886] | 408 | if len(nodes) > 1: |
---|
| 409 | msg = "CanSAS reader is not compatible with multiple" |
---|
| 410 | msg += " SASdata entries" |
---|
| 411 | raise RuntimeError, msg |
---|
[579ba85] | 412 | |
---|
[b0d0723] | 413 | nodes = dom.xpath('ns:SASdata/ns:Idata', namespaces={'ns': CANSAS_NS}) |
---|
| 414 | |
---|
[8780e9a] | 415 | x = numpy.zeros(0) |
---|
| 416 | y = numpy.zeros(0) |
---|
| 417 | dx = numpy.zeros(0) |
---|
| 418 | dy = numpy.zeros(0) |
---|
[d00f8ff] | 419 | dxw = numpy.zeros(0) |
---|
| 420 | dxl = numpy.zeros(0) |
---|
[8780e9a] | 421 | |
---|
| 422 | for item in nodes: |
---|
[b0d0723] | 423 | _x, attr = get_float('ns:Q', item) |
---|
| 424 | _dx, attr_d = get_float('ns:Qdev', item) |
---|
| 425 | _dxl, attr_l = get_float('ns:dQl', item) |
---|
| 426 | _dxw, attr_w = get_float('ns:dQw', item) |
---|
[8780e9a] | 427 | if _dx == None: |
---|
| 428 | _dx = 0.0 |
---|
[d00f8ff] | 429 | if _dxl == None: |
---|
| 430 | _dxl = 0.0 |
---|
| 431 | if _dxw == None: |
---|
| 432 | _dxw = 0.0 |
---|
[8780e9a] | 433 | |
---|
[a7a5886] | 434 | if attr.has_key('unit') and \ |
---|
| 435 | attr['unit'].lower() != data_info.x_unit.lower(): |
---|
[e390933] | 436 | if has_converter==True: |
---|
| 437 | try: |
---|
| 438 | data_conv_q = Converter(attr['unit']) |
---|
| 439 | _x = data_conv_q(_x, units=data_info.x_unit) |
---|
| 440 | except: |
---|
[a7a5886] | 441 | msg = "CanSAS reader: could not convert " |
---|
| 442 | msg += "Q unit [%s]; " |
---|
| 443 | msg += "expecting [%s]\n %s" % (attr['unit'], |
---|
| 444 | data_info.x_unit, sys.exc_value) |
---|
| 445 | raise ValueError, msg |
---|
| 446 | |
---|
[e390933] | 447 | else: |
---|
[a7a5886] | 448 | msg = "CanSAS reader: unrecognized Q unit [%s]; " |
---|
| 449 | msg += "expecting [%s]" % (attr['unit'], data_info.x_unit) |
---|
| 450 | raise ValueError, msg |
---|
| 451 | |
---|
[d00f8ff] | 452 | # Error in Q |
---|
[a7a5886] | 453 | if attr_d.has_key('unit') and \ |
---|
| 454 | attr_d['unit'].lower() != data_info.x_unit.lower(): |
---|
[e390933] | 455 | if has_converter==True: |
---|
| 456 | try: |
---|
| 457 | data_conv_q = Converter(attr_d['unit']) |
---|
| 458 | _dx = data_conv_q(_dx, units=data_info.x_unit) |
---|
| 459 | except: |
---|
[a7a5886] | 460 | msg = "CanSAS reader: could not convert dQ unit [%s];" |
---|
| 461 | msg += " expecting " |
---|
| 462 | msg += "[%s]\n %s" % (attr['unit'], |
---|
| 463 | data_info.x_unit, sys.exc_value) |
---|
| 464 | raise ValueError, msg |
---|
| 465 | |
---|
[e390933] | 466 | else: |
---|
[a7a5886] | 467 | msg = "CanSAS reader: unrecognized dQ unit [%s]; " |
---|
| 468 | msg += "expecting [%s]" % (attr['unit'], data_info.x_unit) |
---|
| 469 | raise ValueError, msg |
---|
| 470 | |
---|
[d00f8ff] | 471 | # Slit length |
---|
[a7a5886] | 472 | if attr_l.has_key('unit') and \ |
---|
| 473 | attr_l['unit'].lower() != data_info.x_unit.lower(): |
---|
| 474 | if has_converter == True: |
---|
[d00f8ff] | 475 | try: |
---|
| 476 | data_conv_q = Converter(attr_l['unit']) |
---|
| 477 | _dxl = data_conv_q(_dxl, units=data_info.x_unit) |
---|
| 478 | except: |
---|
[a7a5886] | 479 | msg = "CanSAS reader: could not convert dQl unit [%s];" |
---|
| 480 | msg += " expecting [%s]\n %s" % (attr['unit'], |
---|
| 481 | data_info.x_unit, sys.exc_value) |
---|
| 482 | raise ValueError, msg |
---|
| 483 | |
---|
[d00f8ff] | 484 | else: |
---|
[a7a5886] | 485 | msg = "CanSAS reader: unrecognized dQl unit [%s];" |
---|
| 486 | msg += " expecting [%s]" % (attr['unit'], data_info.x_unit) |
---|
| 487 | raise ValueError, msg |
---|
| 488 | |
---|
[d00f8ff] | 489 | # Slit width |
---|
[a7a5886] | 490 | if attr_w.has_key('unit') and \ |
---|
| 491 | attr_w['unit'].lower() != data_info.x_unit.lower(): |
---|
| 492 | if has_converter == True: |
---|
[d00f8ff] | 493 | try: |
---|
| 494 | data_conv_q = Converter(attr_w['unit']) |
---|
| 495 | _dxw = data_conv_q(_dxw, units=data_info.x_unit) |
---|
| 496 | except: |
---|
[a7a5886] | 497 | msg = "CanSAS reader: could not convert dQw unit [%s];" |
---|
| 498 | msg += " expecting [%s]\n %s" % (attr['unit'], |
---|
| 499 | data_info.x_unit, sys.exc_value) |
---|
| 500 | raise ValueError, msg |
---|
| 501 | |
---|
[d00f8ff] | 502 | else: |
---|
[a7a5886] | 503 | msg = "CanSAS reader: unrecognized dQw unit [%s];" |
---|
| 504 | msg += " expecting [%s]" % (attr['unit'], data_info.x_unit) |
---|
| 505 | raise ValueError, msg |
---|
[b0d0723] | 506 | _y, attr = get_float('ns:I', item) |
---|
| 507 | _dy, attr_d = get_float('ns:Idev', item) |
---|
[8780e9a] | 508 | if _dy == None: |
---|
| 509 | _dy = 0.0 |
---|
[a7a5886] | 510 | if attr.has_key('unit') and \ |
---|
| 511 | attr['unit'].lower() != data_info.y_unit.lower(): |
---|
[e390933] | 512 | if has_converter==True: |
---|
| 513 | try: |
---|
| 514 | data_conv_i = Converter(attr['unit']) |
---|
| 515 | _y = data_conv_i(_y, units=data_info.y_unit) |
---|
| 516 | except: |
---|
[fd224fd] | 517 | if attr['unit'].lower() == 'count': |
---|
| 518 | pass |
---|
| 519 | else: |
---|
| 520 | msg = "CanSAS reader: could not" |
---|
| 521 | msg += " convert I(q) unit [%s];" % str(attr['unit']) |
---|
| 522 | msg += " expecting [%s]\n" % str(data_info.y_unit) |
---|
| 523 | msg += " %s" % str(sys.exc_value) |
---|
| 524 | raise ValueError, msg |
---|
[e390933] | 525 | else: |
---|
[a7a5886] | 526 | msg = "CanSAS reader: unrecognized I(q) unit [%s];" |
---|
| 527 | msg += " expecting [%s]" % (attr['unit'], data_info.y_unit) |
---|
| 528 | raise ValueError, msg |
---|
| 529 | |
---|
| 530 | if attr_d.has_key('unit') and \ |
---|
| 531 | attr_d['unit'].lower() != data_info.y_unit.lower(): |
---|
[e390933] | 532 | if has_converter==True: |
---|
| 533 | try: |
---|
| 534 | data_conv_i = Converter(attr_d['unit']) |
---|
| 535 | _dy = data_conv_i(_dy, units=data_info.y_unit) |
---|
| 536 | except: |
---|
[fd224fd] | 537 | if attr_d['unit'].lower() == 'count': |
---|
| 538 | pass |
---|
| 539 | else: |
---|
| 540 | msg = "CanSAS reader: could not convert dI(q) unit " |
---|
| 541 | msg += "[%s]; expecting [%s]\n %s" % (attr_d['unit'], |
---|
| 542 | data_info.y_unit, sys.exc_value) |
---|
| 543 | raise ValueError, msg |
---|
[e390933] | 544 | else: |
---|
[a7a5886] | 545 | msg = "CanSAS reader: unrecognized dI(q) unit [%s]; " |
---|
| 546 | msg += "expecting [%s]" % (attr_d['unit'], data_info.y_unit) |
---|
| 547 | raise ValueError, msg |
---|
[8780e9a] | 548 | |
---|
| 549 | if _x is not None and _y is not None: |
---|
| 550 | x = numpy.append(x, _x) |
---|
[579ba85] | 551 | y = numpy.append(y, _y) |
---|
| 552 | dx = numpy.append(dx, _dx) |
---|
| 553 | dy = numpy.append(dy, _dy) |
---|
[d00f8ff] | 554 | dxl = numpy.append(dxl, _dxl) |
---|
| 555 | dxw = numpy.append(dxw, _dxw) |
---|
| 556 | |
---|
[8780e9a] | 557 | data_info.x = x |
---|
| 558 | data_info.y = y |
---|
| 559 | data_info.dx = dx |
---|
| 560 | data_info.dy = dy |
---|
[d00f8ff] | 561 | data_info.dxl = dxl |
---|
| 562 | data_info.dxw = dxw |
---|
[d6513cd] | 563 | |
---|
| 564 | data_conv_q = None |
---|
| 565 | data_conv_i = None |
---|
| 566 | |
---|
[ca10d8e] | 567 | if has_converter == True and data_info.x_unit != '1/A': |
---|
| 568 | data_conv_q = Converter('1/A') |
---|
[d6513cd] | 569 | # Test it |
---|
| 570 | data_conv_q(1.0, output.Q_unit) |
---|
| 571 | |
---|
[ca10d8e] | 572 | if has_converter == True and data_info.y_unit != '1/cm': |
---|
| 573 | data_conv_i = Converter('1/cm') |
---|
[d6513cd] | 574 | # Test it |
---|
[e390933] | 575 | data_conv_i(1.0, output.I_unit) |
---|
| 576 | |
---|
[99d1af6] | 577 | if data_conv_q is not None: |
---|
[d6513cd] | 578 | data_info.xaxis("\\rm{Q}", data_info.x_unit) |
---|
[99d1af6] | 579 | else: |
---|
| 580 | data_info.xaxis("\\rm{Q}", 'A^{-1}') |
---|
| 581 | if data_conv_i is not None: |
---|
[0e2aa40] | 582 | data_info.yaxis("\\rm{Intensity}", data_info.y_unit) |
---|
[99d1af6] | 583 | else: |
---|
[0e2aa40] | 584 | data_info.yaxis("\\rm{Intensity}","cm^{-1}") |
---|
[99d1af6] | 585 | |
---|
[8780e9a] | 586 | return data_info |
---|
| 587 | |
---|
[b3de3a45] | 588 | def _to_xml_doc(self, datainfo): |
---|
[4c00964] | 589 | """ |
---|
[0997158f] | 590 | Create an XML document to contain the content of a Data1D |
---|
| 591 | |
---|
| 592 | :param datainfo: Data1D object |
---|
[4c00964] | 593 | """ |
---|
| 594 | |
---|
[7d8094b] | 595 | if not issubclass(datainfo.__class__, Data1D): |
---|
[4c00964] | 596 | raise RuntimeError, "The cansas writer expects a Data1D instance" |
---|
| 597 | |
---|
| 598 | doc = xml.dom.minidom.Document() |
---|
| 599 | main_node = doc.createElement("SASroot") |
---|
[fee780b] | 600 | main_node.setAttribute("version", self.version) |
---|
| 601 | main_node.setAttribute("xmlns", "cansas1d/%s" % self.version) |
---|
[a7a5886] | 602 | main_node.setAttribute("xmlns:xsi", |
---|
| 603 | "http://www.w3.org/2001/XMLSchema-instance") |
---|
| 604 | main_node.setAttribute("xsi:schemaLocation", |
---|
| 605 | "cansas1d/%s http://svn.smallangles.net/svn/canSAS/1dwg/trunk/cansas1d.xsd" % self.version) |
---|
[fee780b] | 606 | |
---|
[4c00964] | 607 | doc.appendChild(main_node) |
---|
| 608 | |
---|
| 609 | entry_node = doc.createElement("SASentry") |
---|
| 610 | main_node.appendChild(entry_node) |
---|
| 611 | |
---|
[579ba85] | 612 | write_node(doc, entry_node, "Title", datainfo.title) |
---|
| 613 | for item in datainfo.run: |
---|
| 614 | runname = {} |
---|
[a7a5886] | 615 | if datainfo.run_name.has_key(item) and \ |
---|
| 616 | len(str(datainfo.run_name[item]))>1: |
---|
[579ba85] | 617 | runname = {'name': datainfo.run_name[item] } |
---|
| 618 | write_node(doc, entry_node, "Run", item, runname) |
---|
[4c00964] | 619 | |
---|
| 620 | # Data info |
---|
| 621 | node = doc.createElement("SASdata") |
---|
| 622 | entry_node.appendChild(node) |
---|
| 623 | |
---|
[579ba85] | 624 | for i in range(len(datainfo.x)): |
---|
| 625 | pt = doc.createElement("Idata") |
---|
| 626 | node.appendChild(pt) |
---|
| 627 | write_node(doc, pt, "Q", datainfo.x[i], {'unit':datainfo.x_unit}) |
---|
| 628 | if len(datainfo.y)>=i: |
---|
[a7a5886] | 629 | write_node(doc, pt, "I", datainfo.y[i], |
---|
| 630 | {'unit':datainfo.y_unit}) |
---|
[0d8642c9] | 631 | if datainfo.dx != None and len(datainfo.dx) >= i: |
---|
[a7a5886] | 632 | write_node(doc, pt, "Qdev", datainfo.dx[i], |
---|
| 633 | {'unit':datainfo.x_unit}) |
---|
[0d8642c9] | 634 | if datainfo.dxl != None and len(datainfo.dxl) >= i: |
---|
| 635 | write_node(doc, pt, "dQl", datainfo.dxl[i], |
---|
| 636 | {'unit':datainfo.x_unit}) |
---|
| 637 | if datainfo.dxw != None and len(datainfo.dxw) >= i: |
---|
| 638 | write_node(doc, pt, "dQw", datainfo.dxw[i], |
---|
| 639 | {'unit':datainfo.x_unit}) |
---|
| 640 | if datainfo.dy != None and len(datainfo.dy) >= i: |
---|
[a7a5886] | 641 | write_node(doc, pt, "Idev", datainfo.dy[i], |
---|
| 642 | {'unit':datainfo.y_unit}) |
---|
[579ba85] | 643 | |
---|
| 644 | |
---|
[4c00964] | 645 | # Sample info |
---|
| 646 | sample = doc.createElement("SASsample") |
---|
[579ba85] | 647 | if datainfo.sample.name is not None: |
---|
| 648 | sample.setAttribute("name", str(datainfo.sample.name)) |
---|
[4c00964] | 649 | entry_node.appendChild(sample) |
---|
[579ba85] | 650 | write_node(doc, sample, "ID", str(datainfo.sample.ID)) |
---|
[a7a5886] | 651 | write_node(doc, sample, "thickness", datainfo.sample.thickness, |
---|
| 652 | {"unit":datainfo.sample.thickness_unit}) |
---|
[4c00964] | 653 | write_node(doc, sample, "transmission", datainfo.sample.transmission) |
---|
[a7a5886] | 654 | write_node(doc, sample, "temperature", datainfo.sample.temperature, |
---|
| 655 | {"unit":datainfo.sample.temperature_unit}) |
---|
[4c00964] | 656 | |
---|
| 657 | for item in datainfo.sample.details: |
---|
| 658 | write_node(doc, sample, "details", item) |
---|
| 659 | |
---|
| 660 | pos = doc.createElement("position") |
---|
[a7a5886] | 661 | written = write_node(doc, pos, "x", datainfo.sample.position.x, |
---|
| 662 | {"unit":datainfo.sample.position_unit}) |
---|
| 663 | written = written | write_node(doc, pos, "y", |
---|
| 664 | datainfo.sample.position.y, |
---|
| 665 | {"unit":datainfo.sample.position_unit}) |
---|
| 666 | written = written | write_node(doc, pos, "z", |
---|
| 667 | datainfo.sample.position.z, |
---|
| 668 | {"unit":datainfo.sample.position_unit}) |
---|
[4c00964] | 669 | if written == True: |
---|
| 670 | sample.appendChild(pos) |
---|
| 671 | |
---|
| 672 | ori = doc.createElement("orientation") |
---|
[a7a5886] | 673 | written = write_node(doc, ori, "roll", |
---|
| 674 | datainfo.sample.orientation.x, |
---|
| 675 | {"unit":datainfo.sample.orientation_unit}) |
---|
| 676 | written = written | write_node(doc, ori, "pitch", |
---|
| 677 | datainfo.sample.orientation.y, |
---|
| 678 | {"unit":datainfo.sample.orientation_unit}) |
---|
| 679 | written = written | write_node(doc, ori, "yaw", |
---|
| 680 | datainfo.sample.orientation.z, |
---|
| 681 | {"unit":datainfo.sample.orientation_unit}) |
---|
[4c00964] | 682 | if written == True: |
---|
| 683 | sample.appendChild(ori) |
---|
| 684 | |
---|
| 685 | # Instrument info |
---|
| 686 | instr = doc.createElement("SASinstrument") |
---|
| 687 | entry_node.appendChild(instr) |
---|
| 688 | |
---|
| 689 | write_node(doc, instr, "name", datainfo.instrument) |
---|
| 690 | |
---|
| 691 | # Source |
---|
| 692 | source = doc.createElement("SASsource") |
---|
[579ba85] | 693 | if datainfo.source.name is not None: |
---|
| 694 | source.setAttribute("name", str(datainfo.source.name)) |
---|
[4c00964] | 695 | instr.appendChild(source) |
---|
| 696 | |
---|
| 697 | write_node(doc, source, "radiation", datainfo.source.radiation) |
---|
| 698 | write_node(doc, source, "beam_shape", datainfo.source.beam_shape) |
---|
[579ba85] | 699 | size = doc.createElement("beam_size") |
---|
| 700 | if datainfo.source.beam_size_name is not None: |
---|
| 701 | size.setAttribute("name", str(datainfo.source.beam_size_name)) |
---|
[a7a5886] | 702 | written = write_node(doc, size, "x", datainfo.source.beam_size.x, |
---|
| 703 | {"unit":datainfo.source.beam_size_unit}) |
---|
| 704 | written = written | write_node(doc, size, "y", |
---|
| 705 | datainfo.source.beam_size.y, |
---|
| 706 | {"unit":datainfo.source.beam_size_unit}) |
---|
| 707 | written = written | write_node(doc, size, "z", |
---|
| 708 | datainfo.source.beam_size.z, |
---|
| 709 | {"unit":datainfo.source.beam_size_unit}) |
---|
[579ba85] | 710 | if written == True: |
---|
| 711 | source.appendChild(size) |
---|
| 712 | |
---|
[a7a5886] | 713 | write_node(doc, source, "wavelength", |
---|
| 714 | datainfo.source.wavelength, |
---|
| 715 | {"unit":datainfo.source.wavelength_unit}) |
---|
| 716 | write_node(doc, source, "wavelength_min", |
---|
| 717 | datainfo.source.wavelength_min, |
---|
| 718 | {"unit":datainfo.source.wavelength_min_unit}) |
---|
| 719 | write_node(doc, source, "wavelength_max", |
---|
| 720 | datainfo.source.wavelength_max, |
---|
| 721 | {"unit":datainfo.source.wavelength_max_unit}) |
---|
| 722 | write_node(doc, source, "wavelength_spread", |
---|
| 723 | datainfo.source.wavelength_spread, |
---|
| 724 | {"unit":datainfo.source.wavelength_spread_unit}) |
---|
[4c00964] | 725 | |
---|
| 726 | # Collimation |
---|
| 727 | for item in datainfo.collimation: |
---|
| 728 | coll = doc.createElement("SAScollimation") |
---|
[579ba85] | 729 | if item.name is not None: |
---|
| 730 | coll.setAttribute("name", str(item.name)) |
---|
[4c00964] | 731 | instr.appendChild(coll) |
---|
| 732 | |
---|
[a7a5886] | 733 | write_node(doc, coll, "length", item.length, |
---|
| 734 | {"unit":item.length_unit}) |
---|
[4c00964] | 735 | |
---|
| 736 | for apert in item.aperture: |
---|
[579ba85] | 737 | ap = doc.createElement("aperture") |
---|
| 738 | if apert.name is not None: |
---|
| 739 | ap.setAttribute("name", str(apert.name)) |
---|
| 740 | if apert.type is not None: |
---|
| 741 | ap.setAttribute("type", str(apert.type)) |
---|
| 742 | coll.appendChild(ap) |
---|
[4c00964] | 743 | |
---|
[a7a5886] | 744 | write_node(doc, ap, "distance", apert.distance, |
---|
| 745 | {"unit":apert.distance_unit}) |
---|
[4c00964] | 746 | |
---|
| 747 | size = doc.createElement("size") |
---|
[579ba85] | 748 | if apert.size_name is not None: |
---|
| 749 | size.setAttribute("name", str(apert.size_name)) |
---|
[a7a5886] | 750 | written = write_node(doc, size, "x", apert.size.x, |
---|
| 751 | {"unit":apert.size_unit}) |
---|
| 752 | written = written | write_node(doc, size, "y", apert.size.y, |
---|
| 753 | {"unit":apert.size_unit}) |
---|
| 754 | written = written | write_node(doc, size, "z", apert.size.z, |
---|
| 755 | {"unit":apert.size_unit}) |
---|
[579ba85] | 756 | if written == True: |
---|
| 757 | ap.appendChild(size) |
---|
[4c00964] | 758 | |
---|
| 759 | # Detectors |
---|
| 760 | for item in datainfo.detector: |
---|
| 761 | det = doc.createElement("SASdetector") |
---|
[579ba85] | 762 | written = write_node(doc, det, "name", item.name) |
---|
[a7a5886] | 763 | written = written | write_node(doc, det, "SDD", item.distance, |
---|
| 764 | {"unit":item.distance_unit}) |
---|
| 765 | written = written | write_node(doc, det, "slit_length", |
---|
| 766 | item.slit_length, |
---|
| 767 | {"unit":item.slit_length_unit}) |
---|
[579ba85] | 768 | if written == True: |
---|
| 769 | instr.appendChild(det) |
---|
[4c00964] | 770 | |
---|
| 771 | off = doc.createElement("offset") |
---|
[a7a5886] | 772 | written = write_node(doc, off, "x", item.offset.x, |
---|
| 773 | {"unit":item.offset_unit}) |
---|
| 774 | written = written | write_node(doc, off, "y", item.offset.y, |
---|
| 775 | {"unit":item.offset_unit}) |
---|
| 776 | written = written | write_node(doc, off, "z", item.offset.z, |
---|
| 777 | {"unit":item.offset_unit}) |
---|
[579ba85] | 778 | if written == True: |
---|
| 779 | det.appendChild(off) |
---|
[4c00964] | 780 | |
---|
| 781 | center = doc.createElement("beam_center") |
---|
[a7a5886] | 782 | written = write_node(doc, center, "x", item.beam_center.x, |
---|
| 783 | {"unit":item.beam_center_unit}) |
---|
| 784 | written = written | write_node(doc, center, "y", |
---|
| 785 | item.beam_center.y, |
---|
| 786 | {"unit":item.beam_center_unit}) |
---|
| 787 | written = written | write_node(doc, center, "z", |
---|
| 788 | item.beam_center.z, |
---|
| 789 | {"unit":item.beam_center_unit}) |
---|
[579ba85] | 790 | if written == True: |
---|
| 791 | det.appendChild(center) |
---|
| 792 | |
---|
[4c00964] | 793 | pix = doc.createElement("pixel_size") |
---|
[a7a5886] | 794 | written = write_node(doc, pix, "x", item.pixel_size.x, |
---|
| 795 | {"unit":item.pixel_size_unit}) |
---|
| 796 | written = written | write_node(doc, pix, "y", item.pixel_size.y, |
---|
| 797 | {"unit":item.pixel_size_unit}) |
---|
| 798 | written = written | write_node(doc, pix, "z", item.pixel_size.z, |
---|
| 799 | {"unit":item.pixel_size_unit}) |
---|
[579ba85] | 800 | if written == True: |
---|
| 801 | det.appendChild(pix) |
---|
| 802 | |
---|
| 803 | ori = doc.createElement("orientation") |
---|
[a7a5886] | 804 | written = write_node(doc, ori, "roll", item.orientation.x, |
---|
| 805 | {"unit":item.orientation_unit}) |
---|
| 806 | written = written | write_node(doc, ori, "pitch", |
---|
| 807 | item.orientation.y, |
---|
| 808 | {"unit":item.orientation_unit}) |
---|
| 809 | written = written | write_node(doc, ori, "yaw", |
---|
| 810 | item.orientation.z, |
---|
| 811 | {"unit":item.orientation_unit}) |
---|
[579ba85] | 812 | if written == True: |
---|
| 813 | det.appendChild(ori) |
---|
| 814 | |
---|
[4c00964] | 815 | |
---|
[579ba85] | 816 | # Processes info |
---|
[4c00964] | 817 | for item in datainfo.process: |
---|
| 818 | node = doc.createElement("SASprocess") |
---|
| 819 | entry_node.appendChild(node) |
---|
| 820 | |
---|
[579ba85] | 821 | write_node(doc, node, "name", item.name) |
---|
| 822 | write_node(doc, node, "date", item.date) |
---|
| 823 | write_node(doc, node, "description", item.description) |
---|
| 824 | for term in item.term: |
---|
| 825 | value = term['value'] |
---|
| 826 | del term['value'] |
---|
| 827 | write_node(doc, node, "term", value, term) |
---|
| 828 | for note in item.notes: |
---|
| 829 | write_node(doc, node, "SASprocessnote", note) |
---|
[4c00964] | 830 | |
---|
[b3de3a45] | 831 | # Return the document, and the SASentry node associated with |
---|
| 832 | # the data we just wrote |
---|
| 833 | return doc, entry_node |
---|
| 834 | |
---|
| 835 | def write(self, filename, datainfo): |
---|
| 836 | """ |
---|
[0997158f] | 837 | Write the content of a Data1D as a CanSAS XML file |
---|
| 838 | |
---|
| 839 | :param filename: name of the file to write |
---|
| 840 | :param datainfo: Data1D object |
---|
[b3de3a45] | 841 | """ |
---|
| 842 | # Create XML document |
---|
| 843 | doc, sasentry = self._to_xml_doc(datainfo) |
---|
[4c00964] | 844 | # Write the file |
---|
| 845 | fd = open(filename, 'w') |
---|
| 846 | fd.write(doc.toprettyxml()) |
---|
| 847 | fd.close() |
---|
| 848 | |
---|
[fe78c7b] | 849 | def _store_float(self, location, node, variable, storage, optional=True): |
---|
| 850 | """ |
---|
[0997158f] | 851 | Get the content of a xpath location and store |
---|
| 852 | the result. Check that the units are compatible |
---|
| 853 | with the destination. The value is expected to |
---|
| 854 | be a float. |
---|
| 855 | |
---|
| 856 | The xpath location might or might not exist. |
---|
| 857 | If it does not exist, nothing is done |
---|
| 858 | |
---|
| 859 | :param location: xpath location to fetch |
---|
| 860 | :param node: node to read the data from |
---|
| 861 | :param variable: name of the data member to store it in [string] |
---|
| 862 | :param storage: data object that has the 'variable' data member |
---|
[a7a5886] | 863 | :param optional: if True, no exception will be raised |
---|
| 864 | if unit conversion can't be done |
---|
[0997158f] | 865 | |
---|
| 866 | :raise ValueError: raised when the units are not recognized |
---|
[fe78c7b] | 867 | """ |
---|
| 868 | entry = get_content(location, node) |
---|
| 869 | try: |
---|
| 870 | value = float(entry.text) |
---|
| 871 | except: |
---|
| 872 | value = None |
---|
| 873 | |
---|
| 874 | if value is not None: |
---|
| 875 | # If the entry has units, check to see that they are |
---|
| 876 | # compatible with what we currently have in the data object |
---|
| 877 | units = entry.get('unit') |
---|
| 878 | if units is not None: |
---|
| 879 | toks = variable.split('.') |
---|
| 880 | exec "local_unit = storage.%s_unit" % toks[0] |
---|
| 881 | if units.lower()!=local_unit.lower(): |
---|
| 882 | if has_converter==True: |
---|
| 883 | try: |
---|
| 884 | conv = Converter(units) |
---|
[a7a5886] | 885 | exec "storage.%s = %g" % (variable, |
---|
| 886 | conv(value, units=local_unit)) |
---|
[fe78c7b] | 887 | except: |
---|
[a7a5886] | 888 | err_mess = "CanSAS reader: could not convert" |
---|
| 889 | err_mess += " %s unit [%s]; expecting [%s]\n %s" \ |
---|
[fe78c7b] | 890 | % (variable, units, local_unit, sys.exc_value) |
---|
| 891 | self.errors.append(err_mess) |
---|
| 892 | if optional: |
---|
| 893 | logging.info(err_mess) |
---|
| 894 | else: |
---|
| 895 | raise ValueError, err_mess |
---|
| 896 | else: |
---|
[a7a5886] | 897 | err_mess = "CanSAS reader: unrecognized %s unit [%s];" |
---|
| 898 | err_mess += " expecting [%s]" % (variable, |
---|
| 899 | units, local_unit) |
---|
[fe78c7b] | 900 | self.errors.append(err_mess) |
---|
| 901 | if optional: |
---|
| 902 | logging.info(err_mess) |
---|
| 903 | else: |
---|
| 904 | raise ValueError, err_mess |
---|
| 905 | else: |
---|
| 906 | exec "storage.%s = value" % variable |
---|
| 907 | else: |
---|
| 908 | exec "storage.%s = value" % variable |
---|
| 909 | |
---|
| 910 | def _store_content(self, location, node, variable, storage): |
---|
| 911 | """ |
---|
[0997158f] | 912 | Get the content of a xpath location and store |
---|
| 913 | the result. The value is treated as a string. |
---|
| 914 | |
---|
| 915 | The xpath location might or might not exist. |
---|
| 916 | If it does not exist, nothing is done |
---|
| 917 | |
---|
| 918 | :param location: xpath location to fetch |
---|
| 919 | :param node: node to read the data from |
---|
| 920 | :param variable: name of the data member to store it in [string] |
---|
| 921 | :param storage: data object that has the 'variable' data member |
---|
| 922 | |
---|
| 923 | :return: return a list of errors |
---|
[fe78c7b] | 924 | """ |
---|
| 925 | entry = get_content(location, node) |
---|
| 926 | if entry is not None and entry.text is not None: |
---|
| 927 | exec "storage.%s = entry.text.strip()" % variable |
---|
| 928 | |
---|
| 929 | |
---|
| 930 | |
---|
[8780e9a] | 931 | if __name__ == "__main__": |
---|
| 932 | logging.basicConfig(level=logging.ERROR, |
---|
| 933 | format='%(asctime)s %(levelname)s %(message)s', |
---|
| 934 | filename='cansas_reader.log', |
---|
| 935 | filemode='w') |
---|
| 936 | reader = Reader() |
---|
| 937 | print reader.read("../test/cansas1d.xml") |
---|
[b0d0723] | 938 | #print reader.read("../test/latex_smeared.xml") |
---|
[8780e9a] | 939 | |
---|
| 940 | |
---|
| 941 | |
---|