- Timestamp:
- Jan 27, 2014 3:35:28 PM (11 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 64beda4
- Parents:
- 16bd5ca
- Location:
- src/sans/dataloader/readers
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sans/dataloader/readers/cansas_constants.py
r76cd1ae re090c624 8 8 format = '' 9 9 10 10 11 def __init__(self): 11 12 self.ns = self.CANSAS_NS 12 13 self.format = self.CANSAS_FORMAT 14 15 16 def _iterate_namespace(self, namespace): 17 """ 18 Method to iterate through a cansas constants tree based on a list of 19 names 20 21 :param namespace: A list of names that match the tree structure of 22 cansas_constants 23 """ 24 # The current level to look through in cansas_constants. 25 return_me = currentLevel() 26 return_me.current_level = self.CANSAS_FORMAT.get("SASentry") 27 # Defaults for variable and datatype 28 return_me.ns_variable = "{0}.meta_data[\"{2}\"] = \"{1}\"" 29 return_me.ns_datatype = "content" 30 return_me.ns_optional = True 31 for name in namespace: 32 if name != "SASentry": 33 return_me.current_level = \ 34 return_me.current_level.get("children").get(name, "") 35 if return_me.current_level == "": 36 return_me.current_level = \ 37 return_me.current_level.get("<any>", "") 38 cl_variable = return_me.current_level.get("variable", "") 39 cl_datatype = return_me.current_level.get("storeas", "") 40 cl_units_optional = \ 41 return_me.current_level.get("units_required", "") 42 # Where are how to store the variable for the given namespace 43 # CANSAS_CONSTANTS tree is hierarchical, so is no value, inherit 44 return_me.ns_variable = cl_variable if cl_variable != "" else \ 45 return_me.ns_variable 46 return_me.ns_datatype = cl_datatype if cl_datatype != "" else \ 47 return_me.ns_datatype 48 return_me.ns_optional = cl_units_optional if \ 49 cl_units_optional != return_me.ns_optional \ 50 else return_me.ns_optional 51 return return_me 52 53 13 54 """ 14 55 CANSAS_NS holds the base namespace and the default schema file information … … 24 65 } 25 66 } 67 26 68 27 69 """ … … 697 739 } 698 740 } 699 741 742 class currentLevel: 743 744 current_level = '' 745 ns_variable = '' 746 ns_datatype = '' 747 ns_optional = True 748 749 def __init__(self): 750 self.current_level = '' 751 self.ns_variable = '' 752 self.ns_datatype = "content" 753 self.ns_optional = True -
src/sans/dataloader/readers/cansas_reader.py
r76cd1ae re090c624 40 40 41 41 42 def write_node(doc, parent, name, value, attr={}):43 """44 :param doc: document DOM45 :param parent: parent node46 :param name: tag of the element47 :param value: value of the child text node48 :param attr: attribute dictionary49 50 :return: True if something was appended, otherwise False51 """52 if value is not None:53 node = doc.createElement(name)54 node.appendChild(doc.createTextNode(str(value)))55 for item in attr:56 node.setAttribute(item, attr[item])57 parent.appendChild(node)58 return True59 return False60 61 62 42 def get_content(location, node): 63 43 """ … … 101 81 102 82 103 104 class CANSASError(Exception): 105 """Base class all CANSAS reader exceptions are derived""" 106 pass 107 108 class NotCANSASFileError(CANSASError): 109 def __init__(self): 110 self.value = "This is not a proper CanSAS file." 111 def __str__(self): 112 return repr(self.value) 83 def write_node(doc, parent, name, value, attr={}): 84 """ 85 :param doc: document DOM 86 :param parent: parent node 87 :param name: tag of the element 88 :param value: value of the child text node 89 :param attr: attribute dictionary 90 91 :return: True if something was appended, otherwise False 92 """ 93 if value is not None: 94 node = doc.createElement(name) 95 node.appendChild(doc.createTextNode(str(value))) 96 for item in attr: 97 node.setAttribute(item, attr[item]) 98 parent.appendChild(node) 99 return True 100 return False 101 113 102 114 103 class Reader(): … … 274 263 return name 275 264 276 def _iterate_namespace(self, namespace):277 """278 Method to iterate through a cansas constants tree based on a list of279 names280 281 :param namespace: A list of names that match the tree structure of282 cansas_constants283 """284 # The current level to look through in cansas_constants.285 current_level = CANSAS_FORMAT.get("SASentry")286 # Defaults for variable and datatype287 ns_variable = "{0}.meta_data[\"{2}\"] = \"{1}\""288 ns_datatype = "content"289 ns_optional = True290 for name in namespace:291 if name != "SASentry":292 current_level = current_level.get("children").get(name, "")293 if current_level == "":294 current_level = current_level.get("<any>", "")295 cl_variable = current_level.get("variable", "")296 cl_datatype = current_level.get("storeas", "")297 cl_units_optional = current_level.get("units_required", "")298 # Where are how to store the variable for the given namespace299 # CANSAS_CONSTANTS tree is hierarchical, so is no value, inherit300 ns_variable = cl_variable if cl_variable != "" else ns_variable301 ns_datatype = cl_datatype if cl_datatype != "" else ns_datatype302 ns_optional = cl_units_optional if cl_units_optional != \303 ns_optional else ns_optional304 return current_level, ns_variable, ns_datatype, ns_optional305 306 265 307 266 def _unit_conversion(self, new_current_level, attr, data1d, \ … … 397 356 ns.append(tagname) 398 357 attr = node.attrib 358 children = node.getchildren() 359 save_data1d = data1d 399 360 400 361 # Look for special cases 401 save_data1d = data1d402 362 if tagname == "SASdetector": 403 363 data1d = Detector() … … 420 380 elif tagname == "aperture": 421 381 data1d = Aperture() 422 382 if tagname == "Idata" and children is not None: 383 dql = 0 384 dqw = 0 385 for child in children: 386 tag = child.tag.replace(base_ns, "") 387 if tag == "dQl": 388 dql = 1 389 if tag == "dQw": 390 dqw = 1 391 if dqw == 1 and dql == 0: 392 data1d.dxl = numpy.append(data1d.dxl, 0.0) 393 elif dql == 1 and dqw == 0: 394 data1d.dxw = numpy.append(data1d.dxw, 0.0) 395 423 396 # Get where to store content 424 new_current_level, ns_var, ns_datatype, \ 425 optional = self._iterate_namespace(ns) 397 cs_values = constants._iterate_namespace(ns) 426 398 # If the element is a child element, recurse 427 if node.getchildren() is not None: 428 # Returned value is new Data1D object with all previous and new values in it. 399 if children is not None: 400 # Returned value is new Data1D object with all previous and 401 # new values in it. 429 402 data1d, extras = self._parse_entry(node, ns, data1d, extras) 430 403 … … 437 410 438 411 # If the value is a float, compile with units. 439 if ns_datatype == "float":412 if cs_values.ns_datatype == "float": 440 413 # If an empty value is given, store as zero. 441 414 if node_value is None or node_value.isspace() \ 442 415 or node_value.lower() == "nan": 443 416 node_value = "0.0" 444 node_value, unit = self._unit_conversion(new_current_level,\ 445 attr, data1d, node_value, optional) 417 node_value, unit = self._unit_conversion(\ 418 cs_values.current_level, attr, data1d, \ 419 node_value, cs_values.ns_optional) 446 420 447 # If appending to a dictionary (meta_data | run_name), name sure the key is unique 448 if ns_var == "{0}.meta_data[\"{2}\"] = \"{1}\"": 449 # If we are within a Process, Detector, Collimation or Aperture instance, pull out old data1d 450 tagname = self._create_unique_key(data1d.meta_data, tagname, 0) 421 # If appending to a dictionary (meta_data | run_name) 422 # make sure the key is unique 423 if cs_values.ns_variable == "{0}.meta_data[\"{2}\"] = \"{1}\"": 424 # If we are within a Process, Detector, Collimation or 425 # Aperture instance, pull out old data1d 426 tagname = self._create_unique_key(data1d.meta_data, \ 427 tagname, 0) 451 428 if isinstance(data1d, Data1D) == False: 452 store_me = ns_var.format("data1d", node_value, tagname) 429 store_me = cs_values.ns_variable.format("data1d", \ 430 node_value, tagname) 453 431 extras.append(store_me) 454 ns_var = None 455 if ns_var == "{0}.run_name[\"{2}\"] = \"{1}\"": 456 tagname = self._create_unique_key(data1d.run_name, tagname, 0) 432 cs_values.ns_variable = None 433 if cs_values.ns_variable == "{0}.run_name[\"{2}\"] = \"{1}\"": 434 tagname = self._create_unique_key(data1d.run_name, \ 435 tagname, 0) 457 436 458 437 # Check for Data1D object and any extra commands to save … … 461 440 exec item 462 441 # Don't bother saving empty information unless it is a float 463 if ns_varis not None and node_value is not None and \442 if cs_values.ns_variable is not None and node_value is not None and \ 464 443 node_value.isspace() == False: 465 444 # Format a string and then execute it. 466 store_me = ns_var.format("data1d", node_value, tagname)445 store_me = cs_values.ns_variable.format("data1d", node_value, tagname) 467 446 exec store_me 468 447 # Get attributes and process them … … 470 449 for key in node.keys(): 471 450 try: 472 cansas_attrib = new_current_level.get("attributes").get(key) 451 cansas_attrib = \ 452 cs_values.current_level.get("attributes").get(key) 473 453 attrib_variable = cansas_attrib.get("variable") 474 454 if key == 'unit' and unit != '': … … 525 505 main_node.setAttribute("xmlns:xsi", 526 506 "http://www.w3.org/2001/XMLSchema-instance") 527 main_node.setAttribute("xsi:schemaLocation", 528 "{0} http://svn.smallangles.net/svn/canSAS/1dwg/trunk/cansas1d.xsd".format(ns)) 507 main_node.setAttribute("xsi:schemaLocation", "{0} http://svn.smallangles.net/svn/canSAS/1dwg/trunk/cansas1d.xsd".format(ns)) 529 508 530 509 doc.appendChild(main_node)
Note: See TracChangeset
for help on using the changeset viewer.