Changeset 1124 for trunk/bindings/python
- Timestamp:
- 14/11/08 21:19:43 (4 years ago)
- Location:
- trunk/bindings/python/nxs
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/bindings/python/nxs/__init__.py
r1122 r1124 11 11 The NeXus file interface requires compiled libraries to read the 12 12 underlying HDF_ files. Binary packages are available for some 13 platforms from the NeXus site. Details of where the nxs package 13 platforms from the NeXus site. Details of where the nxs package 14 14 searches for the libraries are recorded in `nxs.napi`. 15 15 … … 44 44 When converting code to python from other languages you do not 45 45 necessarily want to rewrite the file handling code using the 46 tree view. The `nxs.napi` module provides an interface which 47 more closely follows the NeXus application programming 46 tree view. The `nxs.napi` module provides an interface which 47 more closely follows the NeXus application programming 48 48 interface (NAPI_). 49 49 … … 55 55 from nxs.napi import * 56 56 from nxs.tree import * 57 -
trunk/bindings/python/nxs/napi.py
r1123 r1124 123 123 .. _NAPI: http://www.nexusformat.org/Application_Program_Interface 124 124 """ 125 __all__ = ['UNLIMITED', 'MAXRANK', 'MAXNAMELEN','MAXPATHLEN','NeXus','open'] 125 __all__ = ['UNLIMITED', 'MAXRANK', 'MAXNAMELEN','MAXPATHLEN', 126 'NeXus','NeXusError','open'] 126 127 127 128 import sys, os, numpy, ctypes … … 263 264 return NeXus(filename, mode) 264 265 266 class NeXusError(Exception): 267 """NeXus Error""" 268 pass 269 265 270 class NeXus(object): 266 271 … … 282 287 Raises ValueError if the open mode is invalid. 283 288 284 Raises RuntimeError if the file could not be opened, with the289 Raises NeXusError if the file could not be opened, with the 285 290 filename as part of the error message. 286 291 … … 303 308 else: 304 309 op = 'create' 305 raise RuntimeError, "Could not %s %s"%(op,filename)310 raise NeXusError, "Could not %s %s"%(op,filename) 306 311 self.isopen = True 307 312 … … 324 329 Opens the NeXus file handle if it is not already open. 325 330 326 Raises RuntimeError if the file could not be opened.331 Raises NeXusError if the file could not be opened. 327 332 328 333 Corresponds to NXopen(filename,mode,&handle) … … 335 340 status = nxlib.nxiopen_(self.filename,mode,_ref(self.handle)) 336 341 if status == ERROR: 337 raise RuntimeError, "Could not open %s"%(self.filename)342 raise NeXusError, "Could not open %s"%(self.filename) 338 343 self.path = [] 339 344 … … 344 349 Close the NeXus file associated with handle. 345 350 346 Raises RuntimeError if file could not be closed.351 Raises NeXusError if file could not be closed. 347 352 348 353 Corresponds to NXclose(&handle) … … 352 357 status = nxlib.nxiclose_(_ref(self.handle)) 353 358 if status == ERROR: 354 raise RuntimeError, "Could not close NeXus file %s"%(self.filename)359 raise NeXusError, "Could not close NeXus file %s"%(self.filename) 355 360 self.path = [] 356 361 … … 361 366 Flush all data to the NeXus file. 362 367 363 Raises RuntimeError if this fails.368 Raises NeXusError if this fails. 364 369 365 370 Corresponds to NXflush(&handle) … … 367 372 status = nxlib.nxiflush_(_ref(self.handle)) 368 373 if status == ERROR: 369 raise RuntimeError, "Could not flush NeXus file %s"%(self.filename)374 raise NeXusError, "Could not flush NeXus file %s"%(self.filename) 370 375 371 376 nxlib.nxisetnumberformat_.restype = c_int … … 393 398 Create the group nxclass:name. 394 399 395 Raises RuntimeError if the group could not be created.400 Raises NeXusError if the group could not be created. 396 401 397 402 Corresponds to NXmakegroup(handle, name, nxclass) … … 399 404 status = nxlib.nximakegroup_(self.handle, name, nxclass) 400 405 if status == ERROR: 401 raise RuntimeError,\406 raise NeXusError,\ 402 407 "Could not create %s:%s in %s"%(nxclass,name,self._loc()) 403 408 … … 468 473 Close the currently open group. 469 474 470 Raises RuntimeError if the group could not be closed.475 Raises NeXusError if the group could not be closed. 471 476 472 477 Corresponds to NXclosegroup(handle) … … 476 481 group = self.path.pop() 477 482 if status == ERROR: 478 raise RuntimeError, "Could not close %s:"%(group,self._loc())483 raise NeXusError, "Could not close %s:"%(group,self._loc()) 479 484 480 485 nxlib.nxigetinfo_.restype = c_int … … 506 511 Reset getnextentry to return the first entry in the group. 507 512 508 Raises RuntimeError if this fails.513 Raises NeXusError if this fails. 509 514 510 515 Corresponds to NXinitgroupdir(handle) … … 512 517 status = nxlib.nxiinitgroupdir_(self.handle) 513 518 if status == ERROR: 514 raise RuntimeError, \519 raise NeXusError, \ 515 520 "Could not reset group scan: %s"%(self._loc()) 516 521 … … 521 526 Return the next entry in the group as name,nxclass tuple. 522 527 523 Raises RuntimeError if this fails, or if there is no next entry.528 Raises NeXusError if this fails, or if there is no next entry. 524 529 525 530 Corresponds to NXgetnextentry(handle,name,nxclass,&storage). … … 534 539 status = nxlib.nxigetnextentry_(self.handle,name,nxclass,_ref(storage)) 535 540 if status == ERROR or status == EOD: 536 raise RuntimeError, \541 raise NeXusError, \ 537 542 "Could not get next entry: %s"%(self._loc()) 538 543 ## Note: ignoring storage --- it is useless without dimensions … … 594 599 complex values. 595 600 596 Raises RuntimeError if this fails.601 Raises NeXusError if this fails. 597 602 598 603 Note that this is the recommended way to establish if you have … … 609 614 _ref(storage)) 610 615 if status == ERROR: 611 raise RuntimeError, "Could not get data info: %s"%(self._loc())616 raise NeXusError, "Could not get data info: %s"%(self._loc()) 612 617 shape = shape[:rank.value]+0 613 618 dtype = _pytype_code[storage.value] … … 637 642 Close the currently open data set. 638 643 639 Raises RuntimeError if this fails (e.g., because no644 Raises NeXusError if this fails (e.g., because no 640 645 dataset is open). 641 646 … … 646 651 name = self.path.pop() 647 652 if status == ERROR: 648 raise RuntimeError,\653 raise NeXusError,\ 649 654 "Could not close data %s: %s"%(name,self._loc()) 650 655 … … 807 812 Reset the getnextattr list to the first attribute. 808 813 809 Raises RuntimeError if this fails.814 Raises NeXusError if this fails. 810 815 811 816 Corresponds to NXinitattrdir(handle) … … 813 818 status = nxlib.nxiinitattrdir_(self.handle) 814 819 if status == ERROR: 815 raise RuntimeError, \820 raise NeXusError, \ 816 821 "Could not reset attribute list: %s"%(self._loc()) 817 822 … … 824 829 this number of times. 825 830 826 Raises RuntimeError if this fails.831 Raises NeXusError if this fails. 827 832 828 833 Corresponds to NXgetattrinfo(handl, &n) … … 831 836 status = nxlib.nxigetattrinfo_(self.handle,_ref(n)) 832 837 if status == ERROR: 833 raise RuntimeError, "Could not get attr info: %s"%(self._loc())838 raise NeXusError, "Could not get attr info: %s"%(self._loc()) 834 839 #print "num attrs",n.value 835 840 return n.value … … 845 850 attribute. 846 851 847 Raises RuntimeError if NeXus returns ERROR or EOD.852 Raises NeXusError if NeXus returns ERROR or EOD. 848 853 849 854 Corresponds to NXgetnextattr(handle,name,&length,&storage) … … 859 864 status = nxlib.nxigetnextattr_(self.handle,name,_ref(length),_ref(storage)) 860 865 if status == ERROR or status == EOD: 861 raise RuntimeError, "Could not get next attr: %s"%(self._loc())866 raise NeXusError, "Could not get next attr: %s"%(self._loc()) 862 867 dtype = _pytype_code[storage.value] 863 868 #print "next attr",name.value,length.value,dtype … … 894 899 895 900 Raises TypeError if the value type is incorrect. 896 Raises RuntimeError if the attribute could not be saved.901 Raises NeXusError if the attribute could not be saved. 897 902 898 903 Corresponds to NXputattr(handle,name,data,length,storage) … … 934 939 status = nxlib.nxiputattr_(self.handle,name,data,length,storage) 935 940 if status == ERROR: 936 raise RuntimeError, "Could not write attr %s: %s"%(name,self._loc())941 raise NeXusError, "Could not write attr %s: %s"%(name,self._loc()) 937 942 938 943 def attrs(self): … … 963 968 Return the id of the current group so we can link to it later. 964 969 965 Raises RuntimeError970 Raises NeXusError 966 971 967 972 Corresponds to NXgetgroupID(handle, &ID) … … 970 975 status = nxlib.nxigetgroupid_(self.handle,_ref(ID)) 971 976 if status == ERROR: 972 raise RuntimeError, "Could not link to group: %s"%(self._loc())977 raise NeXusError, "Could not link to group: %s"%(self._loc()) 973 978 return ID 974 979 … … 979 984 Return the id of the current data so we can link to it later. 980 985 981 Raises RuntimeError986 Raises NeXusError 982 987 983 988 Corresponds to NXgetdataID(handle, &ID) … … 986 991 status = nxlib.nxigetdataid_(self.handle,_ref(ID)) 987 992 if status == ERROR: 988 raise RuntimeError, "Could not link to data: %s"%(self._loc())993 raise NeXusError, "Could not link to data: %s"%(self._loc()) 989 994 return ID 990 995 … … 996 1001 open group. 997 1002 998 Raises RuntimeError1003 Raises NeXusError 999 1004 1000 1005 Corresponds to NXmakelink(handle, &ID) … … 1002 1007 status = nxlib.nximakelink_(self.handle,_ref(ID)) 1003 1008 if status == ERROR: 1004 raise RuntimeError, "Could not make link: %s"%(self._loc())1009 raise NeXusError, "Could not make link: %s"%(self._loc()) 1005 1010 1006 1011 nxlib.nximakenamedlink_.restype = c_int … … 1011 1016 open group, but under a different name. 1012 1017 1013 Raises RuntimeError1018 Raises NeXusError 1014 1019 1015 1020 Corresponds to NXmakenamedlink(handle,name,&ID) … … 1017 1022 status = nxlib.nximakenamedlink_(self.handle,name,_ref(ID)) 1018 1023 if status == ERROR: 1019 raise RuntimeError, "Could not make link %s: %s"%(name,self._loc())1024 raise NeXusError, "Could not make link %s: %s"%(name,self._loc()) 1020 1025 1021 1026 nxlib.nxisameid_.restype = c_int … … 1042 1047 perhaps the existence of a 'target' attribute in the current item. 1043 1048 1044 Raises RuntimeError.1049 Raises NeXusError. 1045 1050 1046 1051 Corresponds to NXopensourcegroup(handle) … … 1048 1053 status = nxlib.nxiopensourcegroup_(self.handle) 1049 1054 if status == ERROR: 1050 raise RuntimeError, "Could not open source group: %s"%(self._loc())1055 raise NeXusError, "Could not open source group: %s"%(self._loc()) 1051 1056 1052 1057 def link(self): … … 1086 1091 group is an external link to another file. 1087 1092 1088 Raises RuntimeError if this fails.1093 Raises NeXusError if this fails. 1089 1094 1090 1095 Corresponds to NXinquirefile(&handle,file,len) … … 1093 1098 status = nxlib.nxiinquirefile_(self.handle,filename,maxnamelen) 1094 1099 if status == ERROR: 1095 raise RuntimeError,\1100 raise NeXusError,\ 1096 1101 "Could not determine filename: %s"%(self._loc()) 1097 1102 return filename.value … … 1105 1110 otherwise return None. 1106 1111 1107 Raises RuntimeError if link fails.1112 Raises NeXusError if link fails. 1108 1113 1109 1114 Corresponds to NXisexternalgroup(&handle,name,nxclass,file,len) … … 1111 1116 status = nxlib.nxilinkexternal_(self.handle,name,nxclass,url) 1112 1117 if status == ERROR: 1113 raise RuntimeError,\1118 raise NeXusError,\ 1114 1119 "Could not link %s to %s: %s"%(name,url,self._loc()) 1115 1120 -
trunk/bindings/python/nxs/tree.py
r1122 r1124 126 126 import nxs.napi 127 127 import nxs.unit 128 from nxs.napi import NeXusError 128 129 129 130 … … 403 404 try: 404 405 gid[target] = self.getdataID() 405 except RuntimeError:406 except NeXusError: 406 407 gid[target] = self.getgroupID() 407 408 … … 718 719 """ 719 720 def plot(self, signal, axes, entry, title, **opts): 721 """ 722 Plot the data entry. 723 724 Raises NeXusError if the data cannot be plotted. 725 """ 720 726 import pylab 721 727 pylab.cla() … … 755 761 # No support for higher dimensions yet 756 762 else: 757 raise RuntimeError, "Cannot plot a dataset of rank 3 or greater."763 raise NeXusError, "Cannot plot a dataset of rank 3 or greater." 758 764 @staticmethod 759 765 def show(): … … 798 804 """ 799 805 Plot data contained within the group. 806 807 Raises NeXusError if the data could not be plotted. 800 808 """ 801 809 … … 806 814 break 807 815 else: 808 raise RuntimeError('No plottable signal')816 raise NeXusError('No plottable signal') 809 817 810 818 # Find the associated axes … … 812 820 axes = [getattr(self,a) for a in signal.Aaxes.nxdata.split(':')] 813 821 else: 814 raise RuntimeError('Axes attribute missing from signal')822 raise NeXusError('Axes attribute missing from signal') 815 823 816 824 # Construct title
Note: See TracChangeset
for help on using the changeset viewer.
