Changes in / [e5cbbce:2cda197] in sasview
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/dataloader/readers/cansas_reader.py
r7432acb r7f0c93f 197 197 """ 198 198 199 if not self._is_call_local() and not recurse:200 self.reset_state()201 self.add_data_set()202 self.names.append("SASentry")203 self.parent_class = "SASentry"204 199 self._check_for_empty_data() 200 self._initialize_new_data_set(dom) 201 202 self.names.append("SASentry") 203 self.parent_class = "SASentry" 204 205 205 self.base_ns = "{0}{1}{2}".format("{", \ 206 206 CANSAS_NS.get(self.cansas_version).get("ns"), "}") 207 207 208 self.ns_list = CONSTANTS.iterate_namespace(self.names) 209 208 210 # Go through each child in the parent element 209 for node in dom: 210 attr = node.attrib 211 name = attr.get("name", "") 212 type = attr.get("type", "") 213 # Get the element name and set the current names level 214 tagname = node.tag.replace(self.base_ns, "") 215 tagname_original = tagname 216 # Skip this iteration when loading in save state information 217 if tagname == "fitting_plug_in" or tagname == "pr_inversion" or tagname == "invariant": 211 for sasNode in dom: 212 # Get the element name and set the current name's level 213 currentTagName = sasNode.tag.replace(self.base_ns, "") 214 # As this is the most likely tag to examine, lets put it first! 215 if currentTagName == "SASdata": 216 # Are there multiple entries here? 217 if len(sasNode) <= 1: 218 multipleEntries = False 219 else: 220 multipleEntries = True 221 222 for setupNode in sasNode[0]: 223 if setupNode.text is not None: 224 # Iterating through the tags in the unit node, getting their tag name and respective unit 225 setupTagName = setupNode.tag.replace(self.base_ns, "") 226 units = setupNode.attrib.get("unit", "") 227 228 # Creating our data array first, if there's only one dataNode we will handle this... 229 startArray = np.fromstring(setupNode.text, dtype=float, sep=",") 230 231 if multipleEntries == True: 232 setupArray = np.zeros((len(sasNode), len(startArray))) 233 setupArray[0] = startArray 234 else: 235 setupArray = startArray 236 237 # Now put this into the relevant location 238 if setupTagName == "I": 239 self.current_dataset.yaxis("Intensity", units) 240 self.current_dataset.y = setupArray 241 elif setupTagName == "Q": 242 self.current_dataset.xaxis("Q", units) 243 self.current_dataset.x = setupArray 244 245 elif setupTagName == "Idev": 246 self.current_dataset.dy = setupArray 247 elif setupTagName == "Qdev": 248 self.current_dataset.dx = setupArray 249 250 elif setupTagName == "Qx": 251 self.current_dataset.xaxis("Qx", units) 252 self.current_dataset.qx_data = setupArray 253 elif setupTagName == "Qy": 254 self.current_dataset.yaxis("Qy", units) 255 self.current_dataset.qy_data = setupArray 256 elif setupTagName == "Qxdev": 257 self.current_dataset.xaxis("Qxdev", units) 258 self.current_dataset.dqx_data = setupArray 259 elif setupTagName == "Qydev": 260 self.current_dataset.yaxis("Qydev", units) 261 self.current_dataset.dqy_data = setupArray 262 elif setupTagName == "dQw": 263 self.current_dataset.dxw = setupArray 264 elif setupTagName == "dQl": 265 self.current_dataset.dxl = setupArray 266 267 elif setupTagName == "Mask": 268 self.current_dataset.mask = np.ndarray.astype(setupArray, dtype=bool) 269 elif setupTagName == "Sesans": 270 self.current_datainfo.isSesans = bool(setupNode.text) 271 272 elif setupTagName == "yacceptance": 273 self.current_datainfo.sample.yacceptance = (setupNode.text, units) 274 elif setupTagName == "zacceptance": 275 self.current_datainfo.sample.zacceptance = (setupNode.text, units) 276 elif setupTagName == "Qmean": 277 pass 278 elif setupTagName == "Shadowfactor": 279 pass 280 281 # If there's more data present, let's deal with that too 282 for loopIter in range(1, len(sasNode)): 283 for dataNode in sasNode[loopIter]: 284 if dataNode.text is not None: 285 # Iterating through the tags in the unit node, getting their tag name and respective unit 286 dataTagName = dataNode.tag.replace(self.base_ns, "") 287 # Creating our data array first 288 dataArray = np.fromstring(dataNode.text, dtype=float, sep=",") 289 290 if dataTagName == "I": 291 self.current_dataset.y[loopIter] = dataArray 292 elif dataTagName == "Q": 293 self.current_dataset.x[loopIter] = dataArray 294 elif dataTagName == "Idev": 295 self.current_dataset.dy[loopIter] = dataArray 296 elif dataTagName == "Qdev": 297 self.current_dataset.dx[loopIter] = dataArray 298 elif dataTagName == "Qx": 299 self.current_dataset.qx_data[loopIter] = dataArray 300 elif dataTagName == "Qy": 301 self.current_dataset.qy_data[loopIter] = dataArray 302 elif dataTagName == "Qxdev": 303 self.current_dataset.dqx_data[loopIter] = dataArray 304 elif dataTagName == "Qydev": 305 self.current_dataset.dqy_data[loopIter] = dataArray 306 elif dataTagName == "dQw": 307 self.current_dataset.dxw[loopIter] = dataArray 308 elif dataTagName == "dQl": 309 self.current_dataset.dxl[loopIter] = dataArray 310 311 self._check_for_empty_resolution() 312 self.data.append(self.current_dataset) 313 314 # If it's not data, let's check for other tags starting with skippable ones... 315 elif currentTagName == "fitting_plug_in" or currentTagName == "pr_inversion" or currentTagName == "invariant": 218 316 continue 219 317 220 # Get where to store content 221 self.names.append(tagname_original) 222 self.ns_list = CONSTANTS.iterate_namespace(self.names) 223 # If the element is a child element, recurse 224 if len(node.getchildren()) > 0: 225 self.parent_class = tagname_original 226 if tagname == 'SASdata': 227 self._initialize_new_data_set(node) 228 if isinstance(self.current_dataset, plottable_2D): 229 x_bins = attr.get("x_bins", "") 230 y_bins = attr.get("y_bins", "") 231 if x_bins is not "" and y_bins is not "": 232 self.current_dataset.shape = (x_bins, y_bins) 318 # If we'e dealing with a title node then extract the text of the node and put it in the right place 319 elif currentTagName == "Title": 320 self.current_datainfo.title = sasNode.text 321 322 323 # If we'e dealing with a run node then extract the name and text of the node and put it in the right place 324 elif currentTagName == "Run": 325 self.current_datainfo.run_name[sasNode.text] = sasNode.attrib.get("name", "") 326 self.current_datainfo.run.append(sasNode.text) 327 328 # If we'e dealing with a sample node 329 elif currentTagName == "SASsample": 330 for sampleNode in sasNode: 331 # Get the variables 332 sampleTagName = sampleNode.tag.replace(self.base_ns, "") 333 sampleUnits = sampleNode.attrib.get("unit", "") 334 sampleData = sampleNode.text 335 336 # Populate it via if switching 337 if sampleTagName == "ID": 338 self.current_datainfo.sample.ID = sampleData 339 elif sampleTagName == "Title": 340 self.current_datainfo.sample.name = sampleData 341 elif sampleTagName == "thickness": 342 self.current_datainfo.sample.thickness = sampleData 343 self.current_datainfo.sample.thickness_unit = sampleUnits 344 elif sampleTagName == "transmission": 345 self.current_datainfo.sample.transmission = sampleData 346 elif sampleTagName == "temperature": 347 self.current_datainfo.sample.temperature = sampleData 348 self.current_datainfo.sample.temperature_unit = sampleUnits 349 elif sampleTagName == "details": 350 self.current_datainfo.sample.details.append(sampleData) 351 352 # Extract the positional data 353 elif sampleTagName == "position": 354 for positionNode in sampleNode: 355 positionTagName = positionNode.tag.replace(self.base_ns, "") 356 positionUnits = positionNode.attrib.get("unit", "") 357 positionData = positionNode.text 358 359 # Extract specific tags 360 if positionTagName == "x": 361 self.current_datainfo.sample.position.x = positionData 362 self.current_datainfo.sample.position_unit = positionUnits 363 elif positionTagName == "y": 364 self.current_datainfo.sample.position.y = positionData 365 self.current_datainfo.sample.position_unit = positionUnits 366 elif positionTagName == "z": 367 self.current_datainfo.sample.position.z = positionData 368 self.current_datainfo.sample.position_unit = positionUnits 369 370 # Extract the orientation data 371 elif sampleTagName == "orientation": 372 for orientationNode in sampleNode: 373 orientationTagName = orientationNode.tag.replace(self.base_ns, "") 374 orientationUnits = orientationNode.attrib.get("unit", "") 375 orientationData = orientationNode.text 376 377 # Extract specific tags 378 if orientationTagName == "roll": 379 self.current_datainfo.sample.orientation.x = orientationData 380 self.current_datainfo.sample.orientation_unit = orientationUnits 381 elif orientationTagName == "pitch": 382 self.current_datainfo.sample.orientation.y = orientationData 383 self.current_datainfo.sample.orientation_unit = orientationUnits 384 elif orientationTagName == "yaw": 385 self.current_datainfo.sample.orientation.z = orientationData 386 self.current_datainfo.sample.orientation_unit = orientationUnits 387 388 # If we're dealing with an instrument node 389 elif currentTagName == "SASinstrument": 390 for instrumentNode in sasNode: 391 instrumentTagName = instrumentNode.tag.replace(self.base_ns, "") 392 instrumentUnits = instrumentNode.attrib.get("unit", "") 393 instrumentData = instrumentNode.text 394 395 # Extract the source name 396 if instrumentTagName == "SASsource": 397 self.name = instrumentNode.attrib.get("name", "") 398 399 for sourceNode in instrumentNode: 400 sourceTagName = sourceNode.tag.replace(self.base_ns, "") 401 sourceUnits = sourceNode.attrib.get("unit", "") 402 sourceData = sourceNode.text 403 404 ## Source Information 405 if sourceTagName == "wavelength": 406 self.current_datainfo.source.wavelength = sourceData 407 self.current_datainfo.source.wavelength_unit = sourceUnits 408 elif sourceTagName == "wavelength_min": 409 self.current_datainfo.source.wavelength_min = sourceData 410 self.current_datainfo.source.wavelength_min_unit = sourceUnits 411 elif sourceTagName == "wavelength_max": 412 self.current_datainfo.source.wavelength_max = sourceData 413 self.current_datainfo.source.wavelength_max_unit = sourceUnits 414 elif sourceTagName == "wavelength_spread": 415 self.current_datainfo.source.wavelength_spread = sourceData 416 self.current_datainfo.source.wavelength_spread_unit = sourceUnits 417 elif sourceTagName == "radiation": 418 self.current_datainfo.source.radiation = sourceData 419 elif sourceTagName == "beam_shape": 420 self.current_datainfo.source.beam_shape = sourceData 421 422 elif sourceTagName == "beam_size": 423 for beamNode in sourceNode: 424 beamTagName = beamNode.tag.replace(self.base_ns, "") 425 beamUnits = beamNode.attrib.get("unit", "") 426 beamData = beamNode.text 427 428 if beamTagName == "x": 429 self.current_datainfo.source.beam_size.x = beamData 430 self.current_datainfo.source.beam_size_unit = beamUnits 431 elif beamTagName == "y": 432 self.current_datainfo.source.beam_size.y = beamData 433 self.current_datainfo.source.beam_size_unit = beamUnits 434 435 elif sourceTagName == "pixel_size": 436 for pixelNode in sourceNode: 437 pixelTagName = pixelNode.tag.replace(self.base_ns, "") 438 pixelUnits = pixelNode.attrib.get("unit", "") 439 pixelData = pixelNode.text 440 441 if pixelTagName == "z": 442 self.current_datainfo.source.data_point.z = pixelData 443 self.current_datainfo.source.beam_size_unit = pixelUnits 444 445 # Extract the collimation 446 elif instrumentTagName == "SAScollimation": 447 self.collimation.name = instrumentNode.attrib.get("name", "") 448 449 for collimationNode in instrumentNode: 450 collimationTagName = collimationNode.tag.replace(self.base_ns, "") 451 collimationUnits = collimationNode.attrib.get("unit", "") 452 collimationData = collimationNode.text 453 454 if collimationTagName == "length": 455 self.collimation.length = collimationData 456 self.collimation.length_unit = collimationUnits 457 elif collimationTagName == "name": 458 self.collimation.name = collimationData 459 460 elif collimationTagName == "aperture": 461 for apertureNode in collimationNode: 462 apertureTagName = apertureNode.tag.replace(self.base_ns, "") 463 apertureUnits = apertureNode.attrib.get("unit", "") 464 apertureData = apertureNode.text 465 466 if apertureTagName == "distance": 467 self.aperture.distance = apertureData 468 self.aperture.distance_unit = apertureUnits 469 470 elif collimationTagName == "size": 471 for sizeNode in collimationNode: 472 sizeTagName = sizeNode.tag.replace(self.base_ns, "") 473 sizeUnits = sizeNode.attrib.get("unit", "") 474 sizeData = sizeNode.text 475 476 if sizeTagName == "x": 477 self.aperture.size.x = sizeData 478 self.collimation.size_unit = sizeUnits 479 elif sizeTagName == "y": 480 self.aperture.size.y = sizeData 481 self.collimation.size_unit = sizeUnits 482 elif sizeTagName == "z": 483 self.aperture.size.z = sizeData 484 self.collimation.size_unit = sizeUnits 485 486 self.current_datainfo.collimation.append(self.collimation) 487 self.collimation = Collimation() 488 489 # Extract the detector 490 elif instrumentTagName == "SASdetector": 491 self.name = instrumentNode.attrib.get("name", "") 492 493 for detectorNode in instrumentNode: 494 detectorTagName = detectorNode.tag.replace(self.base_ns, "") 495 detectorUnits = detectorNode.attrib.get("unit", "") 496 detectorData = detectorNode.text 497 498 if detectorTagName == "name": 499 self.detector.name = detectorData 500 elif detectorTagName == "SDD": 501 self.detector.distance = detectorData 502 self.detector.distance_unit = detectorUnits 503 elif detectorTagName == "slit_length": 504 self.detector.slit_length = detectorData 505 self.detector.slit_length_unit = detectorUnits 506 507 elif detectorTagName == "offset": 508 for offsetNode in detectorNode: 509 offsetTagName = offsetNode.tag.replace(self.base_ns, "") 510 offsetUnits = offsetNode.attrib.get("unit", "") 511 offsetData = offsetNode.text 512 513 if offsetTagName == "x": 514 self.detector.offset.x = offsetData 515 self.detector.offset_unit = offsetUnits 516 elif offsetTagName == "y": 517 self.detector.offset.y = offsetData 518 self.detector.offset_unit = offsetUnits 519 elif offsetTagName == "z": 520 self.detector.offset.z = offsetData 521 self.detector.offset_unit = offsetUnits 522 523 elif detectorTagName == "beam_center": 524 for beamCenterNode in detectorNode: 525 beamCenterTagName = beamCenterNode.tag.replace(self.base_ns, "") 526 beamCenterUnits = beamCenterNode.attrib.get("unit", "") 527 beamCenterData = beamCenterNode.text 528 529 if beamCenterTagName == "x": 530 self.detector.beam_center.x = beamCenterData 531 self.detector.beam_center_unit = beamCenterUnits 532 elif beamCenterTagName == "y": 533 self.detector.beam_center.y = beamCenterData 534 self.detector.beam_center_unit = beamCenterUnits 535 elif beamCenterTagName == "z": 536 self.detector.beam_center.z = beamCenterData 537 self.detector.beam_center_unit = beamCenterUnits 538 539 elif detectorTagName == "pixel_size": 540 for pixelSizeNode in detectorNode: 541 pixelSizeTagName = pixelSizeNode.tag.replace(self.base_ns, "") 542 pixelSizeUnits = pixelSizeNode.attrib.get("unit", "") 543 pixelSizeData = pixelSizeNode.text 544 545 if pixelSizeTagName == "x": 546 self.detector.pixel_size.x = pixelSizeData 547 self.detector.pixel_size_unit = pixelSizeUnits 548 elif pixelSizeTagName == "y": 549 self.detector.pixel_size.y = pixelSizeData 550 self.detector.pixel_size_unit = pixelSizeUnits 551 elif pixelSizeTagName == "z": 552 self.detector.pixel_size.z = pixelSizeData 553 self.detector.pixel_size_unit = pixelSizeUnits 554 555 elif detectorTagName == "orientation": 556 for orientationNode in detectorNode: 557 orientationTagName = orientationNode.tag.replace(self.base_ns, "") 558 orientationUnits = orientationNode.attrib.get("unit", "") 559 orientationData = orientationNode.text 560 561 if orientationTagName == "roll": 562 self.detector.orientation.x = orientationData 563 self.detector.orientation_unit = orientationUnits 564 elif orientationTagName == "pitch": 565 self.detector.orientation.y = orientationData 566 self.detector.orientation_unit = orientationUnits 567 elif orientationTagName == "yaw": 568 self.detector.orientation.z = orientationData 569 self.detector.orientation_unit = orientationUnits 570 571 self.current_datainfo.detector.append(self.detector) 572 self.detector = Detector() 573 574 ## If we'e dealing with a process node 575 elif currentTagName == "SASprocess": 576 for processNode in sasNode: 577 processTagName = processNode.tag.replace(self.base_ns, "") 578 units = processNode.attrib.get("unit", "") 579 580 if processTagName == "name": 581 self.process.name = processNode.text 582 elif processTagName == "description": 583 self.process.description = processNode.text 584 elif processTagName == "date": 585 try: 586 self.process.date = datetime.datetime.fromtimestamp(processNode.text) 587 except: 588 self.process.date = processNode.text 589 elif processTagName == "term": 590 dic = {} 591 dic["name"] = processNode.attrib.get("name", "") 592 dic["value"] = processNode.text 593 dic["unit"] = processNode.attrib.get("unit", "") 594 self.process.term.append(dic) 595 596 self.current_datainfo.process.append(self.process) 597 self.process = Process() 598 599 # If we're dealing with a process note node 600 elif currentTagName == "SASprocessnote": 601 for processNoteNode in sasNode: 602 self.process.notes.append(processNoteNode.text) 603 604 # If we're dealing with a sas note node 605 elif currentTagName == "SASnote": 606 for noteNode in sasNode: 607 self.current_datainfo.notes.append(noteNode.text) 608 609 # If we're dealing with a transmission data node 610 elif currentTagName == "Tdata": 611 # Are there multiple entries here? 612 if len(sasNode) <= 1: 613 multipleEntries == False 614 else: 615 multipleEntries == True 616 617 for setupNode in sasNode[0]: 618 if setupNode.text is not None: 619 # Iterating through the tags in the unit node, getting their tag name and respective unit 620 setupTagName = setupNode.tag.replace(self.base_ns, "") 621 transmissionDataUnits = setupNode.attrib.get("unit", "") 622 623 # Creating our data array first, if there's only one dataNode we will handle this... 624 startArray = np.fromstring(setupNode.text, dtype=float, sep=",") 625 626 if multipleEntries == True: 627 setupArray = np.zeros((len(sasNode), len(startArray))) 628 setupArray[0] = startArray 233 629 else: 234 self.current_dataset.shape = () 235 # Recursion step to access data within the group 236 self._parse_entry(node, True) 237 if tagname == "SASsample": 238 self.current_datainfo.sample.name = name 239 elif tagname == "beam_size": 240 self.current_datainfo.source.beam_size_name = name 241 elif tagname == "SAScollimation": 242 self.collimation.name = name 243 elif tagname == "aperture": 244 self.aperture.name = name 245 self.aperture.type = type 246 self.add_intermediate() 630 setupArray = startArray 631 632 ## Transmission Spectrum 633 if setupTagName == "T": 634 self.transspectrum.transmission = setupArray 635 self.transspectrum.transmission_unit = transmissionDataUnits 636 elif setupTagName == "Tdev": 637 self.transspectrum.transmission_deviation = setupArray 638 self.transspectrum.transmission_deviation_unit = transmissionDataUnits 639 elif setupTagName == "Lambda": 640 self.transspectrum.wavelength = setupArray 641 self.transspectrum.wavelength_unit = transmissionDataUnits 642 643 # If there's more data present, let's deal with that too 644 for loopIter in range(1, len(sasNode)): 645 for dataNode in sasNode[loopIter]: 646 if dataNode.text is not None: 647 dataTagName = dataNode.tag.replace(self.base_ns, "") 648 dataArray = np.fromstring(dataNode.text, dtype=float, sep=",") 649 650 if dataTagName == "T": 651 self.transspectrum.transmission[loopIter] = dataArray 652 elif dataTagName == "Tdev": 653 self.transspectrum.transmission_deviation[loopIter] = dataArray 654 elif dataTagName == "Lambda": 655 self.transspectrum.wavelength[loopIter] = dataArray 656 657 self.current_datainfo.trans_spectrum.append(self.transspectrum) 658 self.transspectrum = TransmissionSpectrum() 659 660 661 ## Everything else goes in meta_data 247 662 else: 248 if isinstance(self.current_dataset, plottable_2D): 249 data_point = node.text 250 unit = attr.get('unit', '') 251 else: 252 data_point, unit = self._get_node_value(node, tagname) 253 254 # If this is a dataset, store the data appropriately 255 if tagname == 'Run': 256 self.current_datainfo.run_name[data_point] = name 257 self.current_datainfo.run.append(data_point) 258 elif tagname == 'Title': 259 self.current_datainfo.title = data_point 260 elif tagname == 'SASnote': 261 self.current_datainfo.notes.append(data_point) 262 263 # I and Q - 1D data 264 elif tagname == 'I' and isinstance(self.current_dataset, plottable_1D): 265 unit_list = unit.split("|") 266 if len(unit_list) > 1: 267 self.current_dataset.yaxis(unit_list[0].strip(), 268 unit_list[1].strip()) 269 else: 270 self.current_dataset.yaxis("Intensity", unit) 271 self.current_dataset.y = np.append(self.current_dataset.y, data_point) 272 elif tagname == 'Idev' and isinstance(self.current_dataset, plottable_1D): 273 self.current_dataset.dy = np.append(self.current_dataset.dy, data_point) 274 elif tagname == 'Q': 275 unit_list = unit.split("|") 276 if len(unit_list) > 1: 277 self.current_dataset.xaxis(unit_list[0].strip(), 278 unit_list[1].strip()) 279 else: 280 self.current_dataset.xaxis("Q", unit) 281 self.current_dataset.x = np.append(self.current_dataset.x, data_point) 282 elif tagname == 'Qdev': 283 self.current_dataset.dx = np.append(self.current_dataset.dx, data_point) 284 elif tagname == 'dQw': 285 self.current_dataset.dxw = np.append(self.current_dataset.dxw, data_point) 286 elif tagname == 'dQl': 287 self.current_dataset.dxl = np.append(self.current_dataset.dxl, data_point) 288 elif tagname == 'Qmean': 289 pass 290 elif tagname == 'Shadowfactor': 291 pass 292 elif tagname == 'Sesans': 293 self.current_datainfo.isSesans = bool(data_point) 294 elif tagname == 'yacceptance': 295 self.current_datainfo.sample.yacceptance = (data_point, unit) 296 elif tagname == 'zacceptance': 297 self.current_datainfo.sample.zacceptance = (data_point, unit) 298 299 # I and Qx, Qy - 2D data 300 elif tagname == 'I' and isinstance(self.current_dataset, plottable_2D): 301 self.current_dataset.yaxis("Intensity", unit) 302 self.current_dataset.data = np.fromstring(data_point, dtype=float, sep=",") 303 elif tagname == 'Idev' and isinstance(self.current_dataset, plottable_2D): 304 self.current_dataset.err_data = np.fromstring(data_point, dtype=float, sep=",") 305 elif tagname == 'Qx': 306 self.current_dataset.xaxis("Qx", unit) 307 self.current_dataset.qx_data = np.fromstring(data_point, dtype=float, sep=",") 308 elif tagname == 'Qy': 309 self.current_dataset.yaxis("Qy", unit) 310 self.current_dataset.qy_data = np.fromstring(data_point, dtype=float, sep=",") 311 elif tagname == 'Qxdev': 312 self.current_dataset.xaxis("Qxdev", unit) 313 self.current_dataset.dqx_data = np.fromstring(data_point, dtype=float, sep=",") 314 elif tagname == 'Qydev': 315 self.current_dataset.yaxis("Qydev", unit) 316 self.current_dataset.dqy_data = np.fromstring(data_point, dtype=float, sep=",") 317 elif tagname == 'Mask': 318 inter = [item == "1" for item in data_point.split(",")] 319 self.current_dataset.mask = np.asarray(inter, dtype=bool) 320 321 # Sample Information 322 elif tagname == 'ID' and self.parent_class == 'SASsample': 323 self.current_datainfo.sample.ID = data_point 324 elif tagname == 'Title' and self.parent_class == 'SASsample': 325 self.current_datainfo.sample.name = data_point 326 elif tagname == 'thickness' and self.parent_class == 'SASsample': 327 self.current_datainfo.sample.thickness = data_point 328 self.current_datainfo.sample.thickness_unit = unit 329 elif tagname == 'transmission' and self.parent_class == 'SASsample': 330 self.current_datainfo.sample.transmission = data_point 331 elif tagname == 'temperature' and self.parent_class == 'SASsample': 332 self.current_datainfo.sample.temperature = data_point 333 self.current_datainfo.sample.temperature_unit = unit 334 elif tagname == 'details' and self.parent_class == 'SASsample': 335 self.current_datainfo.sample.details.append(data_point) 336 elif tagname == 'x' and self.parent_class == 'position': 337 self.current_datainfo.sample.position.x = data_point 338 self.current_datainfo.sample.position_unit = unit 339 elif tagname == 'y' and self.parent_class == 'position': 340 self.current_datainfo.sample.position.y = data_point 341 self.current_datainfo.sample.position_unit = unit 342 elif tagname == 'z' and self.parent_class == 'position': 343 self.current_datainfo.sample.position.z = data_point 344 self.current_datainfo.sample.position_unit = unit 345 elif tagname == 'roll' and self.parent_class == 'orientation' and 'SASsample' in self.names: 346 self.current_datainfo.sample.orientation.x = data_point 347 self.current_datainfo.sample.orientation_unit = unit 348 elif tagname == 'pitch' and self.parent_class == 'orientation' and 'SASsample' in self.names: 349 self.current_datainfo.sample.orientation.y = data_point 350 self.current_datainfo.sample.orientation_unit = unit 351 elif tagname == 'yaw' and self.parent_class == 'orientation' and 'SASsample' in self.names: 352 self.current_datainfo.sample.orientation.z = data_point 353 self.current_datainfo.sample.orientation_unit = unit 354 355 # Instrumental Information 356 elif tagname == 'name' and self.parent_class == 'SASinstrument': 357 self.current_datainfo.instrument = data_point 358 # Detector Information 359 elif tagname == 'name' and self.parent_class == 'SASdetector': 360 self.detector.name = data_point 361 elif tagname == 'SDD' and self.parent_class == 'SASdetector': 362 self.detector.distance = data_point 363 self.detector.distance_unit = unit 364 elif tagname == 'slit_length' and self.parent_class == 'SASdetector': 365 self.detector.slit_length = data_point 366 self.detector.slit_length_unit = unit 367 elif tagname == 'x' and self.parent_class == 'offset': 368 self.detector.offset.x = data_point 369 self.detector.offset_unit = unit 370 elif tagname == 'y' and self.parent_class == 'offset': 371 self.detector.offset.y = data_point 372 self.detector.offset_unit = unit 373 elif tagname == 'z' and self.parent_class == 'offset': 374 self.detector.offset.z = data_point 375 self.detector.offset_unit = unit 376 elif tagname == 'x' and self.parent_class == 'beam_center': 377 self.detector.beam_center.x = data_point 378 self.detector.beam_center_unit = unit 379 elif tagname == 'y' and self.parent_class == 'beam_center': 380 self.detector.beam_center.y = data_point 381 self.detector.beam_center_unit = unit 382 elif tagname == 'z' and self.parent_class == 'beam_center': 383 self.detector.beam_center.z = data_point 384 self.detector.beam_center_unit = unit 385 elif tagname == 'x' and self.parent_class == 'pixel_size': 386 self.detector.pixel_size.x = data_point 387 self.detector.pixel_size_unit = unit 388 elif tagname == 'y' and self.parent_class == 'pixel_size': 389 self.detector.pixel_size.y = data_point 390 self.detector.pixel_size_unit = unit 391 elif tagname == 'z' and self.parent_class == 'pixel_size': 392 self.detector.pixel_size.z = data_point 393 self.detector.pixel_size_unit = unit 394 elif tagname == 'roll' and self.parent_class == 'orientation' and 'SASdetector' in self.names: 395 self.detector.orientation.x = data_point 396 self.detector.orientation_unit = unit 397 elif tagname == 'pitch' and self.parent_class == 'orientation' and 'SASdetector' in self.names: 398 self.detector.orientation.y = data_point 399 self.detector.orientation_unit = unit 400 elif tagname == 'yaw' and self.parent_class == 'orientation' and 'SASdetector' in self.names: 401 self.detector.orientation.z = data_point 402 self.detector.orientation_unit = unit 403 # Collimation and Aperture 404 elif tagname == 'length' and self.parent_class == 'SAScollimation': 405 self.collimation.length = data_point 406 self.collimation.length_unit = unit 407 elif tagname == 'name' and self.parent_class == 'SAScollimation': 408 self.collimation.name = data_point 409 elif tagname == 'distance' and self.parent_class == 'aperture': 410 self.aperture.distance = data_point 411 self.aperture.distance_unit = unit 412 elif tagname == 'x' and self.parent_class == 'size': 413 self.aperture.size.x = data_point 414 self.collimation.size_unit = unit 415 elif tagname == 'y' and self.parent_class == 'size': 416 self.aperture.size.y = data_point 417 self.collimation.size_unit = unit 418 elif tagname == 'z' and self.parent_class == 'size': 419 self.aperture.size.z = data_point 420 self.collimation.size_unit = unit 421 422 # Process Information 423 elif tagname == 'name' and self.parent_class == 'SASprocess': 424 self.process.name = data_point 425 elif tagname == 'description' and self.parent_class == 'SASprocess': 426 self.process.description = data_point 427 elif tagname == 'date' and self.parent_class == 'SASprocess': 428 try: 429 self.process.date = datetime.datetime.fromtimestamp(data_point) 430 except: 431 self.process.date = data_point 432 elif tagname == 'SASprocessnote': 433 self.process.notes.append(data_point) 434 elif tagname == 'term' and self.parent_class == 'SASprocess': 435 unit = attr.get("unit", "") 436 dic = {} 437 dic["name"] = name 438 dic["value"] = data_point 439 dic["unit"] = unit 440 self.process.term.append(dic) 441 442 # Transmission Spectrum 443 elif tagname == 'T' and self.parent_class == 'Tdata': 444 self.transspectrum.transmission = np.append(self.transspectrum.transmission, data_point) 445 self.transspectrum.transmission_unit = unit 446 elif tagname == 'Tdev' and self.parent_class == 'Tdata': 447 self.transspectrum.transmission_deviation = np.append(self.transspectrum.transmission_deviation, data_point) 448 self.transspectrum.transmission_deviation_unit = unit 449 elif tagname == 'Lambda' and self.parent_class == 'Tdata': 450 self.transspectrum.wavelength = np.append(self.transspectrum.wavelength, data_point) 451 self.transspectrum.wavelength_unit = unit 452 453 # Source Information 454 elif tagname == 'wavelength' and (self.parent_class == 'SASsource' or self.parent_class == 'SASData'): 455 self.current_datainfo.source.wavelength = data_point 456 self.current_datainfo.source.wavelength_unit = unit 457 elif tagname == 'wavelength_min' and self.parent_class == 'SASsource': 458 self.current_datainfo.source.wavelength_min = data_point 459 self.current_datainfo.source.wavelength_min_unit = unit 460 elif tagname == 'wavelength_max' and self.parent_class == 'SASsource': 461 self.current_datainfo.source.wavelength_max = data_point 462 self.current_datainfo.source.wavelength_max_unit = unit 463 elif tagname == 'wavelength_spread' and self.parent_class == 'SASsource': 464 self.current_datainfo.source.wavelength_spread = data_point 465 self.current_datainfo.source.wavelength_spread_unit = unit 466 elif tagname == 'x' and self.parent_class == 'beam_size': 467 self.current_datainfo.source.beam_size.x = data_point 468 self.current_datainfo.source.beam_size_unit = unit 469 elif tagname == 'y' and self.parent_class == 'beam_size': 470 self.current_datainfo.source.beam_size.y = data_point 471 self.current_datainfo.source.beam_size_unit = unit 472 elif tagname == 'z' and self.parent_class == 'pixel_size': 473 self.current_datainfo.source.data_point.z = data_point 474 self.current_datainfo.source.beam_size_unit = unit 475 elif tagname == 'radiation' and self.parent_class == 'SASsource': 476 self.current_datainfo.source.radiation = data_point 477 elif tagname == 'beam_shape' and self.parent_class == 'SASsource': 478 self.current_datainfo.source.beam_shape = data_point 479 480 # Everything else goes in meta_data 481 else: 482 new_key = self._create_unique_key(self.current_datainfo.meta_data, tagname) 483 self.current_datainfo.meta_data[new_key] = data_point 484 485 self.names.remove(tagname_original) 486 length = 0 487 if len(self.names) > 1: 488 length = len(self.names) - 1 489 self.parent_class = self.names[length] 490 if not self._is_call_local() and not recurse: 491 self.frm = "" 492 self.add_data_set() 493 empty = None 494 return self.output[0], empty 495 663 new_key = self._create_unique_key(self.current_datainfo.meta_data, currentTagName) 664 self.current_datainfo.meta_data[new_key] = sasNode.text 665 666 self.add_intermediate() 667 668 # As before in the code, I guess in case we have to return a tuple for some reason... 669 return self.output, None 496 670 497 671 def _is_call_local(self): … … 695 869 return name 696 870 697 def _get_node_value(self, node, tagname): 871 def _get_node_value_from_text(self, node, node_text): 872 """ 873 Get the value of a node and any applicable units 874 875 :param node: The XML node to get the value of 876 :param tagname: The tagname of the node 877 """ 878 units = "" 879 # If the value is a float, compile with units. 880 if self.ns_list.ns_datatype == "float": 881 # If an empty value is given, set as zero. 882 if node_text is None or node_text.isspace() \ 883 or node_text.lower() == "nan": 884 node_text = "0.0" 885 # Convert the value to the base units 886 tag = node.tag.replace(self.base_ns, "") 887 node_text, units = self._unit_conversion(node, tag, node_text) 888 889 # If the value is a timestamp, convert to a datetime object 890 elif self.ns_list.ns_datatype == "timestamp": 891 if node_text is None or node_text.isspace(): 892 pass 893 else: 894 try: 895 node_text = \ 896 datetime.datetime.fromtimestamp(node_text) 897 except ValueError: 898 node_text = None 899 return node_text, units 900 901 def _get_node_value(self, node): 698 902 """ 699 903 Get the value of a node and any applicable units … … 709 913 else: 710 914 node_value = "" 711 712 # If the value is a float, compile with units. 713 if self.ns_list.ns_datatype == "float": 714 # If an empty value is given, set as zero. 715 if node_value is None or node_value.isspace() \ 716 or node_value.lower() == "nan": 717 node_value = "0.0" 718 #Convert the value to the base units 719 node_value, units = self._unit_conversion(node, tagname, node_value) 720 721 # If the value is a timestamp, convert to a datetime object 722 elif self.ns_list.ns_datatype == "timestamp": 723 if node_value is None or node_value.isspace(): 724 pass 725 else: 726 try: 727 node_value = \ 728 datetime.datetime.fromtimestamp(node_value) 729 except ValueError: 730 node_value = None 915 node_value, units = self._get_node_value_from_text(node, node_value) 731 916 return node_value, units 732 917 … … 932 1117 self._write_data_2d(datainfo, entry_node) 933 1118 else: 934 self._write_data(datainfo, entry_node) 1119 if self._check_root(): 1120 self._write_data(datainfo, entry_node) 1121 else: 1122 self._write_data_linearized(datainfo, entry_node) 935 1123 # Transmission Spectrum Info 936 1124 # TODO: fix the writer to linearize all data, including T_spectrum … … 998 1186 url = "http://www.cansas.org/formats/1.1/" 999 1187 else: 1000 url = "http:// svn.smallangles.net/svn/canSAS/1dwg/trunk/"1188 url = "http://www.cansas.org/formats/1.0/" 1001 1189 schema_location = "{0} {1}cansas1d.xsd".format(n_s, url) 1002 1190 attrib = {"{" + xsi + "}schemaLocation" : schema_location, … … 1358 1546 {"unit": item.slit_length_unit}) 1359 1547 1360 1361 1548 def _write_process_notes(self, datainfo, entry_node): 1362 1549 """ … … 1406 1593 self.append(node, entry_node) 1407 1594 1408 def _check_ origin(self, entry_node, doc):1595 def _check_root(self): 1409 1596 """ 1410 1597 Return the document, and the SASentry node associated with … … 1417 1604 """ 1418 1605 if not self.frm: 1419 self.frm = inspect.stack()[ 1]1606 self.frm = inspect.stack()[2] 1420 1607 mod_name = self.frm[1].replace("\\", "/").replace(".pyc", "") 1421 1608 mod_name = mod_name.replace(".py", "") 1422 1609 mod = mod_name.split("sas/") 1423 1610 mod_name = mod[1] 1424 if mod_name != "sascalc/dataloader/readers/cansas_reader": 1611 return mod_name == "sascalc/dataloader/readers/cansas_reader" 1612 1613 def _check_origin(self, entry_node, doc): 1614 """ 1615 Return the document, and the SASentry node associated with 1616 the data we just wrote. 1617 If the calling function was not the cansas reader, return a minidom 1618 object rather than an lxml object. 1619 1620 :param entry_node: lxml node ElementTree object to be appended to 1621 :param doc: entire xml tree 1622 """ 1623 if not self._check_root(): 1425 1624 string = self.to_string(doc, pretty_print=False) 1426 1625 doc = parseString(string)
Note: See TracChangeset
for help on using the changeset viewer.