Changeset a99a4045 in sasview for src/sas/sascalc
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/dataloader/readers/cansas_reader.py
rbcabf4e ra99a4045 209 209 name = attr.get("name", "") 210 210 type = attr.get("type", "") 211 unit = attr.get("unit", "") 211 212 # Get the element name and set the current names level 212 213 tagname = node.tag.replace(self.base_ns, "") … … 244 245 self.add_intermediate() 245 246 else: 246 if isinstance(self.current_dataset, plottable_2D): 247 # I and Q - 1D data 248 if tagname == 'I' and isinstance(self.current_dataset, 249 plottable_1D): 250 self.current_dataset.yaxis("Intensity", unit) 251 data_list = node.text.split(',') 252 for item in data_list: 253 data_point, unit = self._get_node_value_from_text(node, 254 item) 255 self.current_dataset.y = np.append( 256 self.current_dataset.y, data_point) 257 elif tagname == 'Idev' and isinstance(self.current_dataset, 258 plottable_1D): 259 data_list = node.text.split(',') 260 for item in data_list: 261 data_point, unit = self._get_node_value_from_text(node, 262 item) 263 self.current_dataset.dy = np.append( 264 self.current_dataset.dy, data_point) 265 elif tagname == 'Q': 266 data_list = node.text.split(',') 267 for item in data_list: 268 data_point, unit = self._get_node_value_from_text(node, 269 item) 270 self.current_dataset.x = np.append( 271 self.current_dataset.x, data_point) 272 elif tagname == 'Qdev': 273 data_list = node.text.split(',') 274 for item in data_list: 275 data_point, unit = self._get_node_value_from_text(node, 276 item) 277 self.current_dataset.dx = np.append( 278 self.current_dataset.dx, data_point) 279 elif tagname == 'dQw': 280 data_list = node.text.split(',') 281 for item in data_list: 282 data_point, unit = self._get_node_value_from_text(node, 283 item) 284 self.current_dataset.dqw = np.append( 285 self.current_dataset.dqw, data_point) 286 elif tagname == 'dQl': 287 data_list = node.text.split(',') 288 for item in data_list: 289 data_point, unit = self._get_node_value_from_text(node, 290 item) 291 self.current_dataset.dql = np.append( 292 self.current_dataset.dql, data_point) 293 elif tagname == 'Qmean': 294 pass 295 elif tagname == 'Shadowfactor': 296 pass 297 elif isinstance(self.current_dataset, plottable_2D): 247 298 data_point = node.text 248 299 unit = attr.get('unit', '') 249 300 else: 250 data_point, unit = self._get_node_value(node , tagname)301 data_point, unit = self._get_node_value(node) 251 302 252 303 # If this is a dataset, store the data appropriately … … 258 309 elif tagname == 'SASnote': 259 310 self.current_datainfo.notes.append(data_point) 260 261 # I and Q - 1D data262 elif tagname == 'I' and isinstance(self.current_dataset, plottable_1D):263 self.current_dataset.yaxis("Intensity", unit)264 self.current_dataset.y = np.append(self.current_dataset.y, data_point)265 elif tagname == 'Idev' and isinstance(self.current_dataset, plottable_1D):266 self.current_dataset.dy = np.append(self.current_dataset.dy, data_point)267 elif tagname == 'Q':268 self.current_dataset.xaxis("Q", unit)269 self.current_dataset.x = np.append(self.current_dataset.x, data_point)270 elif tagname == 'Qdev':271 self.current_dataset.dx = np.append(self.current_dataset.dx, data_point)272 elif tagname == 'dQw':273 self.current_dataset.dxw = np.append(self.current_dataset.dxw, data_point)274 elif tagname == 'dQl':275 self.current_dataset.dxl = np.append(self.current_dataset.dxl, data_point)276 elif tagname == 'Qmean':277 pass278 elif tagname == 'Shadowfactor':279 pass280 311 281 312 # I and Qx, Qy - 2D data … … 677 708 return name 678 709 679 def _get_node_value(self, node, tagname): 710 def _get_node_value_from_text(self, node, node_text): 711 """ 712 Get the value of a node and any applicable units 713 714 :param node: The XML node to get the value of 715 :param tagname: The tagname of the node 716 """ 717 units = "" 718 # If the value is a float, compile with units. 719 if self.ns_list.ns_datatype == "float": 720 # If an empty value is given, set as zero. 721 if node_text is None or node_text.isspace() \ 722 or node_text.lower() == "nan": 723 node_text = "0.0" 724 # Convert the value to the base units 725 tag = node.tag.replace(self.base_ns, "") 726 node_text, units = self._unit_conversion(node, tag, node_text) 727 728 # If the value is a timestamp, convert to a datetime object 729 elif self.ns_list.ns_datatype == "timestamp": 730 if node_text is None or node_text.isspace(): 731 pass 732 else: 733 try: 734 node_text = \ 735 datetime.datetime.fromtimestamp(node_text) 736 except ValueError: 737 node_text = None 738 return node_text, units 739 740 def _get_node_value(self, node): 680 741 """ 681 742 Get the value of a node and any applicable units … … 691 752 else: 692 753 node_value = "" 693 694 # If the value is a float, compile with units. 695 if self.ns_list.ns_datatype == "float": 696 # If an empty value is given, set as zero. 697 if node_value is None or node_value.isspace() \ 698 or node_value.lower() == "nan": 699 node_value = "0.0" 700 #Convert the value to the base units 701 node_value, units = self._unit_conversion(node, tagname, node_value) 702 703 # If the value is a timestamp, convert to a datetime object 704 elif self.ns_list.ns_datatype == "timestamp": 705 if node_value is None or node_value.isspace(): 706 pass 707 else: 708 try: 709 node_value = \ 710 datetime.datetime.fromtimestamp(node_value) 711 except ValueError: 712 node_value = None 754 node_value, units = self._get_node_value_from_text(node, node_value) 713 755 return node_value, units 714 756 … … 914 956 self._write_data_2d(datainfo, entry_node) 915 957 else: 916 self._write_data(datainfo, entry_node) 958 if self._check_root(): 959 self._write_data(datainfo, entry_node) 960 else: 961 self._write_data_linearized(datainfo, entry_node) 917 962 # Transmission Spectrum Info 918 963 self._write_trans_spectrum(datainfo, entry_node) … … 1036 1081 self.write_node(point, "dQl", datainfo.dxl[i], 1037 1082 {'unit': datainfo.x_unit}) 1083 1084 def _write_data_linearized(self, datainfo, entry_node): 1085 """ 1086 Writes 1D I and Q data to an XML file is a single Idata element 1087 1088 :param datainfo: The Data1D object the information is coming from 1089 :param entry_node: lxml node ElementTree object to be appended to 1090 """ 1091 node = self.create_element("SASdata") 1092 self.append(node, entry_node) 1093 1094 point = self.create_element("Idata") 1095 node.append(point) 1096 x = ','.join([str(datainfo.x[i]) for i in xrange(len(datainfo.x))]) 1097 self.write_node(point, "Q", x, {'unit': datainfo.x_unit}) 1098 y = ','.join([str(datainfo.y[i]) for i in xrange(len(datainfo.y))]) 1099 self.write_node(point, "I", y, {'unit': datainfo.y_unit}) 1100 if datainfo.dy is not None: 1101 dy = ','.join( 1102 [str(datainfo.dy[i]) for i in xrange(len(datainfo.dy))]) 1103 self.write_node(point, "Idev", dy, {'unit': datainfo.y_unit}) 1104 if datainfo.dx is not None: 1105 dx = ','.join( 1106 [str(datainfo.dx[i]) for i in xrange(len(datainfo.dx))]) 1107 self.write_node(point, "Qdev", dx, {'unit': datainfo.x_unit}) 1108 if datainfo.dxw is not None: 1109 dxw = ','.join( 1110 [str(datainfo.dxw[i]) for i in xrange(len(datainfo.dxw))]) 1111 self.write_node(point, "dQw", dxw, {'unit': datainfo.x_unit}) 1112 if datainfo.dxl is not None: 1113 dxl = ','.join( 1114 [str(datainfo.dxl[i]) for i in xrange(len(datainfo.dxl))]) 1115 self.write_node(point, "dQl", dxl, {'unit': datainfo.x_unit}) 1038 1116 1039 1117 def _write_data_2d(self, datainfo, entry_node): … … 1330 1408 {"unit": item.slit_length_unit}) 1331 1409 1332 1333 1410 def _write_process_notes(self, datainfo, entry_node): 1334 1411 """ … … 1378 1455 self.append(node, entry_node) 1379 1456 1380 def _check_ origin(self, entry_node, doc):1457 def _check_root(self): 1381 1458 """ 1382 1459 Return the document, and the SASentry node associated with … … 1394 1471 mod = mod_name.split("sas/") 1395 1472 mod_name = mod[1] 1396 if mod_name != "sascalc/dataloader/readers/cansas_reader": 1473 return mod_name == "sascalc/dataloader/readers/cansas_reader" 1474 1475 def _check_origin(self, entry_node, doc): 1476 """ 1477 Return the document, and the SASentry node associated with 1478 the data we just wrote. 1479 If the calling function was not the cansas reader, return a minidom 1480 object rather than an lxml object. 1481 1482 :param entry_node: lxml node ElementTree object to be appended to 1483 :param doc: entire xml tree 1484 """ 1485 if not self._check_root(): 1397 1486 string = self.to_string(doc, pretty_print=False) 1398 1487 doc = parseString(string)
Note: See TracChangeset
for help on using the changeset viewer.