Changeset 1116 for trunk/bindings/python
- Timestamp:
- 24/10/08 17:30:01 (4 years ago)
- File:
-
- 1 edited
-
trunk/bindings/python/nxs.py (modified) (78 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/bindings/python/nxs.py
r1005 r1116 10 10 ================ 11 11 12 This wrapper needs the location of the libNeXus precompiled binary. It 12 This wrapper needs the location of the libNeXus precompiled binary. It 13 13 looks in the following places in order: 14 14 os.environ['NEXUSLIB'] - All … … 21 21 /usr/lib - Unix and Darwin 22 22 23 On Windows it looks for libNeXus.dll and libNeXus-0.dll; 23 On Windows it looks for libNeXus.dll and libNeXus-0.dll; 24 24 NEXUSDIR defaults to r'C:\Program Files\NeXus Data Format' 25 25 On OS X it looks for libNeXus.dylib … … 32 32 because the supporting HDF5 dlls were not available in the usual places. 33 33 34 If you are extracting the nexus library from a bundle at runtime, set 35 os.environ['NEXUSLIB'] to the path where it is extracted before the 34 If you are extracting the nexus library from a bundle at runtime, set 35 os.environ['NEXUSLIB'] to the path where it is extracted before the 36 36 first import of nxs. 37 37 … … 50 50 - Adds link() function to return the name of the linked to group, if any 51 51 - NXmalloc/NXfree are not needed. 52 52 53 53 Example: 54 54 … … 59 59 print file.getdata() 60 60 file.close() 61 61 62 62 See nxstest.py for a more complete example. 63 63 … … 202 202 files = [] 203 203 204 # NEXUSDIR is set by the Windows installer for NeXus205 if 'NEXUSDIR' in os.environ:206 winnxdir = os.environ['NEXUSDIR']207 else:208 winnxdir = 'C:/Program Files/NeXus Data Format'209 210 204 # Default names and locations to look for the library are system dependent 211 205 filedir = os.path.dirname(__file__) 212 206 if sys.platform in ('win32','cygwin'): 207 # NEXUSDIR is set by the Windows installer for NeXus 208 if 'NEXUSDIR' in os.environ: 209 winnxdir = os.environ['NEXUSDIR'] 210 else: 211 winnxdir = 'C:/Program Files/NeXus Data Format' 212 213 213 files += [filedir+"/libNeXus.dll", 214 214 filedir+"/libNeXus-0.dll", … … 245 245 246 246 class NeXus(object): 247 247 248 248 # Define the interface to the dll 249 249 lib = _libnexus() 250 250 251 251 # ==== File ==== 252 252 #lib.nxiopen_.restype = c_int … … 255 255 """ 256 256 Open the NeXus file returning a handle. 257 257 258 mode can be one of the following: 259 nxs.ACC_READ 'r' 260 nxs.ACC_RDWR 'rw' 261 nxs.ACC_CREATE 'w' 262 nxs.ACC_CREATE4 'w4' 263 nxs.ACC_CREATE5 'w5' 264 nxs.ACC_CREATEXML 'wx' 265 258 266 Raises RuntimeError if the file could not be opened, with the 259 267 filename as part of the error message. 260 268 261 269 Corresponds to NXopen(filename,mode,&handle) 262 270 """ 271 self.isopen = False 272 263 273 # Convert open mode from string to integer and check it is valid 264 self.isopen = False265 274 if mode in _nxopen_mode: mode = _nxopen_mode[mode] 266 275 if mode not in _nxopen_mode.values(): … … 273 282 if status == ERROR: 274 283 if mode in [ACC_READ, ACC_RDWR]: 275 op = 'open'284 op = 'open' 276 285 else: 277 op = 'create'286 op = 'create' 278 287 raise RuntimeError, "Could not %s %s"%(op,filename) 279 288 self.isopen = True … … 285 294 if self.isopen: self.close() 286 295 287 296 288 297 def __str__(self): 289 298 """ … … 291 300 """ 292 301 return "NeXus('%s')"%self.filename 293 302 294 303 295 304 def open(self): … … 312 321 """ 313 322 Close the NeXus file associated with handle. 314 323 315 324 Raises RuntimeError if file could not be opened. 316 325 317 326 Corresponds to NXclose(&handle) 318 327 """ … … 323 332 raise RuntimeError, "Could not close NeXus file %s"%(self.filename) 324 333 self.path = [] 325 334 326 335 lib.nxiflush_.restype = c_int 327 336 lib.nxiflush_.argtypes = [c_void_pp] … … 329 338 """ 330 339 Flush all data to the NeXus file. 331 340 332 341 Raises RuntimeError if this fails. 333 342 334 343 Corresponds to NXflush(&handle) 335 344 """ … … 344 353 Set the output format for the numbers of the given type (only 345 354 applies to XML). 346 355 347 356 Raises ValueError if this fails. 348 357 349 358 Corresponds to NXsetnumberformat(&handle,type,format) 350 359 """ … … 361 370 """ 362 371 Create the group nxclass:name. 363 372 364 373 Raises RuntimeError if the group could not be created. 365 374 366 375 Corresponds to NXmakegroup(handle, name, nxclass) 367 376 """ … … 370 379 raise RuntimeError,\ 371 380 "Could not create %s:%s in %s"%(nxclass,name,self._loc()) 372 381 373 382 lib.nxiopenpath_.restype = c_int 374 383 lib.nxiopenpath_.argtypes = [c_void_p, c_char_p] … … 377 386 Open a particular group '/path/to/group'. Paths can 378 387 be relative to the currently open group. 379 388 380 389 Raises ValueError. 381 390 382 391 Corresponds to NXopenpath(handle, path) 383 392 """ … … 390 399 else: 391 400 self.path = [] 392 393 401 402 394 403 lib.nxiopengrouppath_.restype = c_int 395 404 lib.nxiopengrouppath_.argtypes = [c_void_p, c_char_p] … … 397 406 """ 398 407 Open a particular group '/path/to/group', or the dataset containing 399 the group if the path refers to a dataset. Paths can be relative to 408 the group if the path refers to a dataset. Paths can be relative to 400 409 the currently open group. 401 410 402 411 Raises ValueError. 403 412 404 413 Corresponds to NXopengrouppath(handle, path) 405 414 """ … … 412 421 else: 413 422 self.path = [] 414 415 423 424 416 425 lib.nxiopengroup_.restype = c_int 417 426 lib.nxiopengroup_.argtypes = [c_void_p, c_char_p, c_char_p] … … 419 428 """ 420 429 Open the group nxclass:name. 421 430 422 431 Raises ValueError if the group could not be opened. 423 432 424 433 Corresponds to NXopengroup(handle, name, nxclass) 425 434 """ … … 430 439 "Could not open %s:%s in %s"%(nxclass,name,self._loc()) 431 440 self.path.append(name) 432 441 433 442 lib.nxiclosegroup_.restype = c_int 434 443 lib.nxiclosegroup_.argtypes = [c_void_p] … … 436 445 """ 437 446 Close the currently open group. 438 447 439 448 Raises RuntimeError if the group could not be closed. 440 449 441 450 Corresponds to NXclosegroup(handle) 442 451 """ … … 456 465 457 466 Raises ValueError if the group could not be opened. 458 467 459 468 Corresponds to NXgetgroupinfo(handle) 460 469 """ … … 468 477 #print "group info",nxclass.value,name.value,n.value 469 478 return n.value,path.value,nxclass.value 470 479 471 480 lib.nxiinitgroupdir_.restype = c_int 472 481 lib.nxiinitgroupdir_.argtypes = [c_void_p] … … 474 483 """ 475 484 Reset getnextentry to return the first entry in the group. 476 485 477 486 Raises RuntimeError if this fails. 478 487 479 488 Corresponds to NXinitgroupdir(handle) 480 489 """ … … 483 492 raise RuntimeError, \ 484 493 "Could not reset group scan: %s"%(self._loc()) 485 494 486 495 lib.nxigetnextentry_.restype = c_int 487 496 lib.nxigetnextentry_.argtypes = [c_void_p, c_char_p, c_char_p, c_int_p] … … 489 498 """ 490 499 Return the next entry in the group as name,nxclass tuple. 491 500 492 501 Raises RuntimeError if this fails, or if there is no next entry. 493 502 494 503 Corresponds to NXgetnextentry(handle,name,nxclass,&storage). 495 504 496 505 This function doesn't return the storage class for data entries 497 506 since getinfo returns shape and storage, both of which are required … … 502 511 storage = c_int(0) 503 512 status = self.lib.nxigetnextentry_(self.handle,name,nxclass,_ref(storage)) 504 if status == ERROR or status == EOD:513 if status == ERROR or status == EOD: 505 514 raise RuntimeError, \ 506 515 "Could not get next entry: %s"%(self._loc()) … … 510 519 #print "group next",nxclass.value, name.value, storage.value 511 520 return name.value,nxclass.value 512 521 513 522 def entries(self): 514 523 """ 515 524 Iterator of entries. 516 525 517 526 for name,nxclass in nxs.entries(): 518 527 process(name,nxclass) 519 528 520 529 This automatically opens the corresponding group/data for you, 521 and closes it when you are done. Do not rely on any paths 522 remaining open between calls to process. 523 530 and closes it when you are done. Do not rely on any paths 531 remaining open between entries as we restore the current 532 path each time. 533 524 534 This does not correspond to an existing NeXus API function, 525 535 but instead combines the work of initgroupdir/getnextentry … … 527 537 """ 528 538 # To preserve the semantics we must read in the whole list 529 # first, then process the entries one by one. 539 # first, then process the entries one by one. Keep track 540 # of the path so we can restore it between entries. 530 541 n,path,_ = self.getgroupinfo() 531 542 #print "path",path … … 534 545 else: 535 546 path = "/" 547 548 # Read list of entries 536 549 self.initgroupdir() 537 550 L = [] 538 551 for i in range(n): 539 552 L.append(self.getnextentry()) 540 for i in range(n): 541 name,nxclass = L[i] 553 for name,nxclass in L: 542 554 self.openpath(path) # Reset the file cursor 543 555 if nxclass == "SDS": … … 546 558 self.opengroup(name,nxclass) 547 559 yield name,nxclass 548 ##Don't need the following given that openpath resets the cursor549 #if nxclass == "SDS":550 # self.closedata()551 #else:552 # self.closegroup()553 560 554 561 # ==== Data ==== … … 564 571 'float[32|64]' for floating point values. No support for 565 572 complex values. 566 573 567 574 Raises RuntimeError if this fails. 568 575 569 576 Note that this is the recommended way to establish if you have 570 577 a dataset open. 571 578 572 579 Corresponds to NXgetinfo(handle, &rank, dims, &storage), 573 580 but with storage converted from HDF values to numpy compatible … … 591 598 """ 592 599 Open the named data set within the current group. 593 600 594 601 Raises ValueError if could not open the dataset. 595 602 596 603 Corresponds to NXopendata(handle, name) 597 604 """ … … 601 608 raise ValueError, "Could not open data %s: %s"%(name, self._loc()) 602 609 self.path.append(name) 603 610 604 611 lib.nxiclosedata_.restype = c_int 605 612 lib.nxiclosedata_.argtypes = [c_void_p] … … 607 614 """ 608 615 Close the currently open data set. 609 616 610 617 Raises RuntimeError if this fails (e.g., because no 611 618 dataset is open). 612 619 613 620 Corresponds to NXclosedata(handle) 614 621 """ … … 619 626 raise RuntimeError,\ 620 627 "Could not close data %s: %s"%(name,self._loc()) 621 628 622 629 lib.nximakedata_.restype = c_int 623 630 lib.nximakedata_.argtypes = [c_void_p, c_char_p, c_int, c_int, c_int_p] 624 631 def makedata(self, name, dtype=None, shape=None): 625 632 """ 626 Create a data element of the given type and shape. See getinfo 633 Create a data element of the given type and shape. See getinfo 627 634 for details on types. This does not open the data for writing. 628 629 Set the first dimension to nxs.UNLIMITED, for extensible data sets, 635 636 Set the first dimension to nxs.UNLIMITED, for extensible data sets, 630 637 and use putslab to write individual slabs. 631 638 632 639 Raises ValueError if it fails. 633 634 Corresponds to NXmakedata(handle,name,type,rank,dims) 640 641 Corresponds to NXmakedata(handle,name,type,rank,dims) 635 642 """ 636 643 # TODO: With keywords for compression and chunks, this can act as … … 645 652 if status == ERROR: 646 653 raise ValueError, "Could not create data %s: %s"%(name,self._loc()) 647 654 648 655 lib.nxicompmakedata_.restype = c_int 649 656 lib.nxicompmakedata_.argtypes = [c_void_p, c_char_p, c_int, c_int, c_int_p, 650 657 c_int, c_int_p] 651 def compmakedata(self, name, dtype=None, shape=None, mode='lzw', 658 def compmakedata(self, name, dtype=None, shape=None, mode='lzw', 652 659 chunks=None): 653 660 """ … … 657 664 of the compressed chunks in the data file. There should be one 658 665 chunk size for each dimension in the data. 659 666 660 667 Defaults to mode='lzw' with chunk size set to the length of the 661 668 fastest varying dimension. 662 669 663 670 Raises ValueError if it fails. 664 671 665 672 Corresponds to NXmakedata(handle,name,type,rank,dims). 666 673 """ … … 681 688 raise ValueError, \ 682 689 "Could not create compressed data %s: %s"%(name,self._loc()) 683 690 684 691 lib.nxigetdata_.restype = c_int 685 692 lib.nxigetdata_.argtypes = [c_void_p, c_void_p] … … 689 696 string is returned. If data is a scalar (1-D numeric array of 690 697 length 1), a python numeric scalar is returned. 691 698 692 699 Raises RuntimeError if this fails. 693 700 694 701 Corresponds to NXgetdata(handle, data) 695 702 """ … … 702 709 #print "data",ret() 703 710 return datafn() 704 711 705 712 lib.nxigetslab_.restype = c_int 706 713 lib.nxigetslab_.argtypes = [c_void_p, c_void_p, c_int_p, c_int_p] … … 708 715 """ 709 716 Get a slab from the data array. 710 717 711 718 Offsets are 0-origin. Shape can be inferred from the data. 712 719 Offset and shape must each have one entry per dimension. 713 720 714 721 Raises ValueError if this fails. 715 722 716 723 Corresponds to NXgetslab(handle,data,offset,shape) 717 724 """ … … 728 735 raise ValueError, "Could not read slab: %s"%(self._loc()) 729 736 return datafn() 730 737 731 738 lib.nxiputdata_.restype = c_int 732 739 lib.nxiputdata_.argtypes = [c_void_p, c_void_p] … … 734 741 """ 735 742 Write data into the currently open data block. 736 743 737 744 Raises ValueError if this fails. 738 745 739 746 Corresponds to NXputdata(handle, data) 740 747 """ … … 744 751 if status == ERROR: 745 752 raise ValueError, "Could not write data: %s"%(self._loc()) 746 753 747 754 lib.nxiputslab_.restype = c_int 748 755 lib.nxiputslab_.argtypes = [c_void_p, c_void_p, c_int_p, c_int_p] … … 750 757 """ 751 758 Put a slab into the data array. 752 759 753 760 Offsets are 0-origin. Shape can be inferred from the data. 754 761 Offset and shape must each have one entry per dimension. 755 762 756 763 Raises ValueError if this fails. 757 764 758 765 Corresponds to NXputslab(handle,data,offset,shape) 759 766 """ … … 768 775 if status == ERROR: 769 776 raise ValueError, "Could not write slab: %s"%(self._loc()) 770 771 772 777 778 779 773 780 # ==== Attributes ==== 774 781 lib.nxiinitattrdir_.restype = c_int … … 777 784 """ 778 785 Reset the getnextattr list to the first attribute. 779 786 780 787 Raises RuntimeError if this fails. 781 788 782 789 Corresponds to NXinitattrdir(handle) 783 790 """ … … 786 793 raise RuntimeError, \ 787 794 "Could not reset attribute list: %s"%(self._loc()) 788 795 789 796 lib.nxigetattrinfo_.restype = c_int 790 797 lib.nxigetattrinfo_.argtypes = [c_void_p, c_int_p] … … 794 801 group/data object. Do not call getnextattr() more than 795 802 this number of times. 796 803 797 804 Raises RuntimeError if this fails. 798 805 799 806 Corresponds to NXgetattrinfo(handl, &n) 800 807 """ … … 805 812 #print "num attrs",n.value 806 813 return n.value 807 814 808 815 lib.nxigetnextattr_.restype = c_int 809 816 lib.nxigetnextattr_.argtypes = [c_void_p, c_char_p, c_int_p, c_int_p] … … 813 820 Call getattrinfo to determine the number of attributes before 814 821 calling getnextattr. Data type is returned as a string. See 815 getinfo for details. Length is the number of elements in the 822 getinfo for details. Length is the number of elements in the 816 823 attribute. 817 824 … … 821 828 but with storage converted from HDF values to numpy compatible 822 829 strings. 823 824 Note: NeXus API documentation seems to say that length is the number 830 831 Note: NeXus API documentation seems to say that length is the number 825 832 of bytes required to store the entire attribute. 826 833 """ … … 844 851 data type from getnextattr to allocate the appropriate amount of 845 852 space for the attribute. 846 853 847 854 Corresponds to NXgetattr(handle,name,data,&length,&storage) 848 855 """ … … 863 870 Saves the named attribute. The attribute value is a string 864 871 or a scalar. 865 872 866 873 Raises ValueError if the attribute could not be saved. 867 874 868 875 Corresponds to NXputattr(handle,name,data,length,storage) 869 870 Note length is the number of elements to write rather 876 877 Note length is the number of elements to write rather 871 878 than the number of bytes to write. 872 879 """ … … 905 912 if status == ERROR: 906 913 raise ValueError, "Could not write attr %s: %s"%(name,self._loc()) 907 914 908 915 def attrs(self): 909 916 """ 910 917 Iterate over attributes. 911 918 912 919 for name,value in file.attrs(): 913 920 process(name,value) … … 919 926 combines the work of attrinfo/initattrdir/getnextattr/getattr. 920 927 """ 928 self.initattrdir() 921 929 n = self.getattrinfo() 922 self.initattrdir()923 930 for i in range(n): 924 931 name,length,dtype = self.getnextattr() 925 932 value = self.getattr(name,length,dtype) 926 933 yield name,value 927 934 928 935 # ==== Linking ==== 929 936 lib.nxigetgroupid_.restype = c_int … … 932 939 """ 933 940 Return the id of the current group so we can link to it later. 934 941 935 942 Raises RuntimeError 936 943 937 944 Corresponds to NXgetgroupID(handle, &ID) 938 945 """ … … 942 949 raise RuntimeError, "Could not link to group: %s"%(self._loc()) 943 950 return ID 944 951 945 952 lib.nxigetdataid_.restype = c_int 946 953 lib.nxigetdataid_.argtypes = [c_void_p, c_NXlink_p] … … 948 955 """ 949 956 Return the id of the current data so we can link to it later. 950 957 951 958 Raises RuntimeError 952 959 953 960 Corresponds to NXgetdataID(handle, &ID) 954 961 """ … … 958 965 raise RuntimeError, "Could not link to data: %s"%(self._loc()) 959 966 return ID 960 967 961 968 lib.nximakelink_.restype = c_int 962 969 lib.nximakelink_.argtypes = [c_void_p, c_NXlink_p] 963 970 def makelink(self, ID): 964 971 """ 965 Link the previously captured group/data ID into the currently 972 Link the previously captured group/data ID into the currently 966 973 open group. 967 974 968 975 Raises RuntimeError 969 976 970 977 Corresponds to NXmakelink(handle, &ID) 971 978 """ … … 973 980 if status == ERROR: 974 981 raise RuntimeError, "Could not make link: %s"%(self._loc()) 975 982 976 983 lib.nximakenamedlink_.restype = c_int 977 984 lib.nximakenamedlink_.argtypes = [c_void_p, c_char_p, c_NXlink_p] 978 985 def makenamedlink(self,name,ID): 979 986 """ 980 Link the previously captured group/data ID into the currently 987 Link the previously captured group/data ID into the currently 981 988 open group, but under a different name. 982 989 983 990 Raises RuntimeError 984 991 985 992 Corresponds to NXmakenamedlink(handle,name,&ID) 986 993 """ … … 988 995 if status == ERROR: 989 996 raise RuntimeError, "Could not make link %s: %s"%(name,self._loc()) 990 997 991 998 lib.nxisameid_.restype = c_int 992 999 lib.nxisameid_.argtypes = [c_void_p, c_NXlink_p, c_NXlink_p] … … 994 1001 """ 995 1002 Return True of ID1 and ID2 point to the same group/data. 996 1003 997 1004 This should not raise any errors. 998 1005 999 1006 Corresponds to NXsameID(handle,&ID1,&ID2) 1000 1007 """ 1001 1008 status = self.lib.nxisameid_(self.handle, _ref(ID1), _ref(ID2)) 1002 1009 return status == OK 1003 1010 1004 1011 lib.nxiopensourcegroup_.restype = c_int 1005 1012 lib.nxiopensourcegroup_.argtyps = [c_void_p] … … 1008 1015 If the current node is a linked to another group or data, then 1009 1016 open the group or data that it is linked to. 1010 1017 1011 1018 Note: it is unclear how can we tell if we are linked, other than 1012 1019 perhaps the existence of a 'target' attribute in the current item. 1013 1020 1014 1021 Raises RuntimeError 1015 1022 1016 1023 Corresponds to NXopensourcegroup(handle) 1017 1024 """ … … 1022 1029 def link(self): 1023 1030 """ 1024 Returns the item which the current item links to, or None if the 1025 current item is not linked. This is equivalent to scanning the 1026 attributes for target and returning it if target is not equal 1031 Returns the item which the current item links to, or None if the 1032 current item is not linked. This is equivalent to scanning the 1033 attributes for target and returning it if target is not equal 1027 1034 to self. 1028 1035 … … 1068 1075 1069 1076 lib.nxilinkexternal_.restype = c_int 1070 lib.nxilinkexternal_.argtyps = [c_void_p, c_char_p, 1077 lib.nxilinkexternal_.argtyps = [c_void_p, c_char_p, 1071 1078 c_char_p, c_char_p] 1072 1079 def linkexternal(self, name, nxclass, url): 1073 1080 """ 1074 Return the filename for the external link if there is one, 1081 Return the filename for the external link if there is one, 1075 1082 otherwise return None. 1076 1083 1077 1084 Corresponds to NXisexternalgroup(&handle,name,nxclass,file,len) 1078 1085 """ … … 1085 1092 1086 1093 lib.nxiisexternalgroup_.restype = c_int 1087 lib.nxiisexternalgroup_.argtyps = [c_void_p, c_char_p, 1094 lib.nxiisexternalgroup_.argtyps = [c_void_p, c_char_p, 1088 1095 c_char_p, c_char_p, c_int] 1089 1096 def isexternalgroup(self, name, nxclass, maxnamelen=MAXPATHLEN): 1090 1097 """ 1091 Return the filename for the external link if there is one, 1098 Return the filename for the external link if there is one, 1092 1099 otherwise return None. 1093 1100 1094 1101 Corresponds to NXisexternalgroup(&handle,name,nxclass,file,len) 1095 1102 """ … … 1100 1107 return None 1101 1108 else: 1102 url.value 1103 1109 return url.value 1104 1110 1105 1111 # ==== Utility functions ==== … … 1107 1113 """ 1108 1114 Return file location as string filename:path 1109 1115 1110 1116 This is an extension to the NeXus API. 1111 1117 """ … … 1115 1121 pathstr = "root" 1116 1122 return "%s(%s)"%(self.filename,pathstr) 1117 1123 1118 1124 def _poutput(self, dtype, shape): 1119 1125 """ … … 1143 1149 return datafn,pdata,size 1144 1150 1145 def _nxany(self,iter):1146 """1147 replace python 2.5 any()1148 """1149 for e in iter:1150 if e:1151 return 11152 return 01153 1154 1151 def _pinput(self, data, dtype, shape): 1155 1152 """ … … 1173 1170 input_shape = numpy.array([i for i in data.shape if i != 1]) 1174 1171 target_shape = numpy.array([i for i in shape if i != 1]) 1175 print input_shape 1176 print target_shape 1177 # if len(input_shape) != len(target_shape) or any(input_shape != target_shape): 1178 if len(input_shape) != len(target_shape) or self._nxany(input_shape != target_shape): 1172 if len(input_shape) != len(target_shape) or (input_shape != target_shape).any(): 1179 1173 raise ValueError,\ 1180 1174 "Shape mismatch %s!=%s: %s"%(data.shape,shape,self.filename) … … 1182 1176 raise ValueError,\ 1183 1177 "Type mismatch %s!=%s: %s"%(dtype,data.dtype,self._loc()) 1184 1178 1185 1179 if dtype == 'char': 1186 1180 # String: hand it over as usual for strings. Assumes the string … … 1191 1185 data = numpy.ascontiguousarray(data) 1192 1186 pdata = data.ctypes.data 1193 1187 1194 1188 return data,pdata 1195 1189 1190 def show(self, path=None, indent=0): 1191 """ 1192 Print the structure of a NeXus file from the current node. 1193 1194 TODO: Break this into a tree walker and a visitor. 1195 """ 1196 oldpath = "/"+"/".join(self.path) 1197 if not path: path = oldpath 1198 self.openpath(path) 1199 1200 print "=== File",self.inquirefile(),path 1201 self._show(indent=indent) 1202 self.openpath(oldpath) 1203 1204 def _show(self, indent=0): 1205 """ 1206 Print the structure of a NeXus file from the current node. 1207 1208 TODO: Break this into a tree walker and a visitor. 1209 """ 1210 prefix = ' '*indent 1211 link = self.link() 1212 if link: 1213 print "%(prefix)s-> %(link)s" % locals() 1214 return 1215 for attr,value in self.attrs(): 1216 print "%(prefix)s@%(attr)s: %(value)s" % locals() 1217 for name,nxclass in self.entries(): 1218 if nxclass == "SDS": 1219 shape,dtype = self.getinfo() 1220 dims = "x".join([str(x) for x in shape]) 1221 print "%(prefix)s%(name)s %(dtype)s %(dims)s" % locals() 1222 link = self.link() 1223 if link: 1224 print " %(prefix)s-> %(link)s" % locals() 1225 else: 1226 for attr,value in self.attrs(): 1227 print " %(prefix)s@%(attr)s: %(value)s" % locals() 1228 if numpy.prod(shape) < 8: 1229 value = self.getdata() 1230 print " %s%s"%(prefix,str(value)) 1231 else: 1232 print "%(prefix)s%(name)s %(nxclass)s" % locals() 1233 self._show(indent=indent+2) 1234 1196 1235 1197 1236 __id__ = "$ID$"
Note: See TracChangeset
for help on using the changeset viewer.
