Changeset 1116 for trunk/bindings/python


Ignore:
Timestamp:
24/10/08 17:30:01 (4 years ago)
Author:
Paul Kienzle
Message:

improve comments and docs; fix whitespace; fix minor bugs introduced for 2.4 support; Refs #101

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bindings/python/nxs.py

    r1005 r1116  
    1010================ 
    1111 
    12 This wrapper needs the location of the libNeXus precompiled binary. It  
     12This wrapper needs the location of the libNeXus precompiled binary. It 
    1313looks in the following places in order: 
    1414    os.environ['NEXUSLIB']                  - All 
     
    2121    /usr/lib                                - Unix and Darwin 
    2222 
    23 On Windows it looks for libNeXus.dll and libNeXus-0.dll;  
     23On Windows it looks for libNeXus.dll and libNeXus-0.dll; 
    2424NEXUSDIR defaults to r'C:\Program Files\NeXus Data Format' 
    2525On OS X it looks for libNeXus.dylib 
     
    3232because the supporting HDF5 dlls were not available in the usual places. 
    3333 
    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  
     34If you are extracting the nexus library from a bundle at runtime, set 
     35os.environ['NEXUSLIB'] to the path where it is extracted before the 
    3636first import of nxs. 
    3737 
     
    5050  - Adds link() function to return the name of the linked to group, if any 
    5151  - NXmalloc/NXfree are not needed. 
    52    
     52 
    5353Example: 
    5454 
     
    5959  print file.getdata() 
    6060  file.close() 
    61    
     61 
    6262  See nxstest.py for a more complete example. 
    6363 
     
    202202        files = [] 
    203203 
    204     # NEXUSDIR is set by the Windows installer for NeXus 
    205     if 'NEXUSDIR' in os.environ: 
    206         winnxdir = os.environ['NEXUSDIR'] 
    207     else: 
    208         winnxdir =  'C:/Program Files/NeXus Data Format' 
    209  
    210204    # Default names and locations to look for the library are system dependent 
    211205    filedir = os.path.dirname(__file__) 
    212206    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 
    213213        files += [filedir+"/libNeXus.dll", 
    214214                  filedir+"/libNeXus-0.dll", 
     
    245245 
    246246class NeXus(object): 
    247      
     247 
    248248    # Define the interface to the dll 
    249249    lib = _libnexus() 
    250      
     250 
    251251    # ==== File ==== 
    252252    #lib.nxiopen_.restype = c_int 
     
    255255        """ 
    256256        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 
    258266        Raises RuntimeError if the file could not be opened, with the 
    259267        filename as part of the error message. 
    260      
     268 
    261269        Corresponds to NXopen(filename,mode,&handle) 
    262270        """ 
     271        self.isopen = False 
     272 
    263273        # Convert open mode from string to integer and check it is valid 
    264         self.isopen = False 
    265274        if mode in _nxopen_mode: mode = _nxopen_mode[mode] 
    266275        if mode not in _nxopen_mode.values(): 
     
    273282        if status == ERROR: 
    274283            if mode in [ACC_READ, ACC_RDWR]: 
    275               op = 'open' 
     284                op = 'open' 
    276285            else: 
    277               op = 'create' 
     286                op = 'create' 
    278287            raise RuntimeError, "Could not %s %s"%(op,filename) 
    279288        self.isopen = True 
     
    285294        if self.isopen: self.close() 
    286295 
    287      
     296 
    288297    def __str__(self): 
    289298        """ 
     
    291300        """ 
    292301        return "NeXus('%s')"%self.filename 
    293          
     302 
    294303 
    295304    def open(self): 
     
    312321        """ 
    313322        Close the NeXus file associated with handle. 
    314      
     323 
    315324        Raises RuntimeError if file could not be opened. 
    316      
     325 
    317326        Corresponds to NXclose(&handle) 
    318327        """ 
     
    323332                raise RuntimeError, "Could not close NeXus file %s"%(self.filename) 
    324333        self.path = [] 
    325      
     334 
    326335    lib.nxiflush_.restype = c_int 
    327336    lib.nxiflush_.argtypes = [c_void_pp] 
     
    329338        """ 
    330339        Flush all data to the NeXus file. 
    331      
     340 
    332341        Raises RuntimeError if this fails. 
    333      
     342 
    334343        Corresponds to NXflush(&handle) 
    335344        """ 
     
    344353        Set the output format for the numbers of the given type (only 
    345354        applies to XML). 
    346          
     355 
    347356        Raises ValueError if this fails. 
    348          
     357 
    349358        Corresponds to NXsetnumberformat(&handle,type,format) 
    350359        """ 
     
    361370        """ 
    362371        Create the group nxclass:name. 
    363      
     372 
    364373        Raises RuntimeError if the group could not be created. 
    365      
     374 
    366375        Corresponds to NXmakegroup(handle, name, nxclass) 
    367376        """ 
     
    370379            raise RuntimeError,\ 
    371380                "Could not create %s:%s in %s"%(nxclass,name,self._loc()) 
    372      
     381 
    373382    lib.nxiopenpath_.restype = c_int 
    374383    lib.nxiopenpath_.argtypes = [c_void_p, c_char_p] 
     
    377386        Open a particular group '/path/to/group'.  Paths can 
    378387        be relative to the currently open group. 
    379      
     388 
    380389        Raises ValueError. 
    381          
     390 
    382391        Corresponds to NXopenpath(handle, path) 
    383392        """ 
     
    390399        else: 
    391400            self.path = [] 
    392          
    393          
     401 
     402 
    394403    lib.nxiopengrouppath_.restype = c_int 
    395404    lib.nxiopengrouppath_.argtypes = [c_void_p, c_char_p] 
     
    397406        """ 
    398407        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 
    400409        the currently open group. 
    401      
     410 
    402411        Raises ValueError. 
    403          
     412 
    404413        Corresponds to NXopengrouppath(handle, path) 
    405414        """ 
     
    412421        else: 
    413422            self.path = [] 
    414          
    415          
     423 
     424 
    416425    lib.nxiopengroup_.restype = c_int 
    417426    lib.nxiopengroup_.argtypes = [c_void_p, c_char_p, c_char_p] 
     
    419428        """ 
    420429        Open the group nxclass:name. 
    421      
     430 
    422431        Raises ValueError if the group could not be opened. 
    423      
     432 
    424433        Corresponds to NXopengroup(handle, name, nxclass) 
    425434        """ 
     
    430439                "Could not open %s:%s in %s"%(nxclass,name,self._loc()) 
    431440        self.path.append(name) 
    432      
     441 
    433442    lib.nxiclosegroup_.restype = c_int 
    434443    lib.nxiclosegroup_.argtypes = [c_void_p] 
     
    436445        """ 
    437446        Close the currently open group. 
    438      
     447 
    439448        Raises RuntimeError if the group could not be closed. 
    440      
     449 
    441450        Corresponds to NXclosegroup(handle) 
    442451        """ 
     
    456465 
    457466        Raises ValueError if the group could not be opened. 
    458      
     467 
    459468        Corresponds to NXgetgroupinfo(handle) 
    460469        """ 
     
    468477        #print "group info",nxclass.value,name.value,n.value 
    469478        return n.value,path.value,nxclass.value 
    470      
     479 
    471480    lib.nxiinitgroupdir_.restype = c_int 
    472481    lib.nxiinitgroupdir_.argtypes = [c_void_p] 
     
    474483        """ 
    475484        Reset getnextentry to return the first entry in the group. 
    476      
     485 
    477486        Raises RuntimeError if this fails. 
    478      
     487 
    479488        Corresponds to NXinitgroupdir(handle) 
    480489        """ 
     
    483492            raise RuntimeError, \ 
    484493                "Could not reset group scan: %s"%(self._loc()) 
    485      
     494 
    486495    lib.nxigetnextentry_.restype = c_int 
    487496    lib.nxigetnextentry_.argtypes = [c_void_p, c_char_p, c_char_p, c_int_p] 
     
    489498        """ 
    490499        Return the next entry in the group as name,nxclass tuple. 
    491      
     500 
    492501        Raises RuntimeError if this fails, or if there is no next entry. 
    493      
     502 
    494503        Corresponds to NXgetnextentry(handle,name,nxclass,&storage). 
    495      
     504 
    496505        This function doesn't return the storage class for data entries 
    497506        since getinfo returns shape and storage, both of which are required 
     
    502511        storage = c_int(0) 
    503512        status = self.lib.nxigetnextentry_(self.handle,name,nxclass,_ref(storage)) 
    504         if status == ERROR or status ==EOD: 
     513        if status == ERROR or status == EOD: 
    505514            raise RuntimeError, \ 
    506515                "Could not get next entry: %s"%(self._loc()) 
     
    510519        #print "group next",nxclass.value, name.value, storage.value 
    511520        return name.value,nxclass.value 
    512      
     521 
    513522    def entries(self): 
    514523        """ 
    515524        Iterator of entries. 
    516          
     525 
    517526        for name,nxclass in nxs.entries(): 
    518527            process(name,nxclass) 
    519528 
    520529        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 
    524534        This does not correspond to an existing NeXus API function, 
    525535        but instead combines the work of initgroupdir/getnextentry 
     
    527537        """ 
    528538        # 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. 
    530541        n,path,_ = self.getgroupinfo() 
    531542        #print "path",path 
     
    534545        else: 
    535546            path = "/" 
     547 
     548        # Read list of entries 
    536549        self.initgroupdir() 
    537550        L = [] 
    538551        for i in range(n): 
    539552            L.append(self.getnextentry()) 
    540         for i in range(n): 
    541             name,nxclass = L[i] 
     553        for name,nxclass in L: 
    542554            self.openpath(path)  # Reset the file cursor 
    543555            if nxclass == "SDS": 
     
    546558                self.opengroup(name,nxclass) 
    547559            yield name,nxclass 
    548             ##Don't need the following given that openpath resets the cursor 
    549             #if nxclass == "SDS": 
    550             #    self.closedata() 
    551             #else: 
    552             #    self.closegroup() 
    553560 
    554561    # ==== Data ==== 
     
    564571        'float[32|64]' for floating point values.  No support for 
    565572        complex values. 
    566      
     573 
    567574        Raises RuntimeError if this fails. 
    568          
     575 
    569576        Note that this is the recommended way to establish if you have 
    570577        a dataset open. 
    571      
     578 
    572579        Corresponds to NXgetinfo(handle, &rank, dims, &storage), 
    573580        but with storage converted from HDF values to numpy compatible 
     
    591598        """ 
    592599        Open the named data set within the current group. 
    593      
     600 
    594601        Raises ValueError if could not open the dataset. 
    595      
     602 
    596603        Corresponds to NXopendata(handle, name) 
    597604        """ 
     
    601608            raise ValueError, "Could not open data %s: %s"%(name, self._loc()) 
    602609        self.path.append(name) 
    603      
     610 
    604611    lib.nxiclosedata_.restype = c_int 
    605612    lib.nxiclosedata_.argtypes = [c_void_p] 
     
    607614        """ 
    608615        Close the currently open data set. 
    609      
     616 
    610617        Raises RuntimeError if this fails (e.g., because no 
    611618        dataset is open). 
    612      
     619 
    613620        Corresponds to NXclosedata(handle) 
    614621        """ 
     
    619626            raise RuntimeError,\ 
    620627                "Could not close data %s: %s"%(name,self._loc()) 
    621      
     628 
    622629    lib.nximakedata_.restype = c_int 
    623630    lib.nximakedata_.argtypes  = [c_void_p, c_char_p, c_int, c_int, c_int_p] 
    624631    def makedata(self, name, dtype=None, shape=None): 
    625632        """ 
    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 
    627634        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, 
    630637        and use putslab to write individual slabs. 
    631          
     638 
    632639        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) 
    635642        """ 
    636643        # TODO: With keywords for compression and chunks, this can act as 
     
    645652        if status == ERROR: 
    646653            raise ValueError, "Could not create data %s: %s"%(name,self._loc()) 
    647      
     654 
    648655    lib.nxicompmakedata_.restype = c_int 
    649656    lib.nxicompmakedata_.argtypes  = [c_void_p, c_char_p, c_int, c_int, c_int_p, 
    650657                                      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', 
    652659                     chunks=None): 
    653660        """ 
     
    657664        of the compressed chunks in the data file.  There should be one 
    658665        chunk size for each dimension in the data. 
    659          
     666 
    660667        Defaults to mode='lzw' with chunk size set to the length of the 
    661668        fastest varying dimension. 
    662      
     669 
    663670        Raises ValueError if it fails. 
    664      
     671 
    665672        Corresponds to NXmakedata(handle,name,type,rank,dims). 
    666673        """ 
     
    681688            raise ValueError, \ 
    682689                "Could not create compressed data %s: %s"%(name,self._loc()) 
    683      
     690 
    684691    lib.nxigetdata_.restype = c_int 
    685692    lib.nxigetdata_.argtypes = [c_void_p, c_void_p] 
     
    689696        string is returned.  If data is a scalar (1-D numeric array of 
    690697        length 1), a python numeric scalar is returned. 
    691      
     698 
    692699        Raises RuntimeError if this fails. 
    693      
     700 
    694701        Corresponds to NXgetdata(handle, data) 
    695702        """ 
     
    702709        #print "data",ret() 
    703710        return datafn() 
    704      
     711 
    705712    lib.nxigetslab_.restype = c_int 
    706713    lib.nxigetslab_.argtypes = [c_void_p, c_void_p, c_int_p, c_int_p] 
     
    708715        """ 
    709716        Get a slab from the data array. 
    710          
     717 
    711718        Offsets are 0-origin.  Shape can be inferred from the data. 
    712719        Offset and shape must each have one entry per dimension. 
    713          
     720 
    714721        Raises ValueError if this fails. 
    715          
     722 
    716723        Corresponds to NXgetslab(handle,data,offset,shape) 
    717724        """ 
     
    728735            raise ValueError, "Could not read slab: %s"%(self._loc()) 
    729736        return datafn() 
    730       
     737 
    731738    lib.nxiputdata_.restype = c_int 
    732739    lib.nxiputdata_.argtypes = [c_void_p, c_void_p] 
     
    734741        """ 
    735742        Write data into the currently open data block. 
    736      
     743 
    737744        Raises ValueError if this fails. 
    738      
     745 
    739746        Corresponds to NXputdata(handle, data) 
    740747        """ 
     
    744751        if status == ERROR: 
    745752            raise ValueError, "Could not write data: %s"%(self._loc()) 
    746          
     753 
    747754    lib.nxiputslab_.restype = c_int 
    748755    lib.nxiputslab_.argtypes = [c_void_p, c_void_p, c_int_p, c_int_p] 
     
    750757        """ 
    751758        Put a slab into the data array. 
    752          
     759 
    753760        Offsets are 0-origin.  Shape can be inferred from the data. 
    754761        Offset and shape must each have one entry per dimension. 
    755          
     762 
    756763        Raises ValueError if this fails. 
    757          
     764 
    758765        Corresponds to NXputslab(handle,data,offset,shape) 
    759766        """ 
     
    768775        if status == ERROR: 
    769776            raise ValueError, "Could not write slab: %s"%(self._loc()) 
    770          
    771          
    772      
     777 
     778 
     779 
    773780    # ==== Attributes ==== 
    774781    lib.nxiinitattrdir_.restype = c_int 
     
    777784        """ 
    778785        Reset the getnextattr list to the first attribute. 
    779      
     786 
    780787        Raises RuntimeError if this fails. 
    781      
     788 
    782789        Corresponds to NXinitattrdir(handle) 
    783790        """ 
     
    786793            raise RuntimeError, \ 
    787794                "Could not reset attribute list: %s"%(self._loc()) 
    788          
     795 
    789796    lib.nxigetattrinfo_.restype = c_int 
    790797    lib.nxigetattrinfo_.argtypes = [c_void_p, c_int_p] 
     
    794801        group/data object.  Do not call getnextattr() more than 
    795802        this number of times. 
    796      
     803 
    797804        Raises RuntimeError if this fails. 
    798      
     805 
    799806        Corresponds to NXgetattrinfo(handl, &n) 
    800807        """ 
     
    805812        #print "num attrs",n.value 
    806813        return n.value 
    807      
     814 
    808815    lib.nxigetnextattr_.restype = c_int 
    809816    lib.nxigetnextattr_.argtypes = [c_void_p, c_char_p, c_int_p, c_int_p] 
     
    813820        Call getattrinfo to determine the number of attributes before 
    814821        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 
    816823        attribute. 
    817824 
     
    821828        but with storage converted from HDF values to numpy compatible 
    822829        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 
    825832        of bytes required to store the entire attribute. 
    826833        """ 
     
    844851        data type from getnextattr to allocate the appropriate amount of 
    845852        space for the attribute. 
    846      
     853 
    847854        Corresponds to NXgetattr(handle,name,data,&length,&storage) 
    848855        """ 
     
    863870        Saves the named attribute.  The attribute value is a string 
    864871        or a scalar. 
    865      
     872 
    866873        Raises ValueError if the attribute could not be saved. 
    867          
     874 
    868875        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 
    871878        than the number of bytes to write. 
    872879        """ 
     
    905912        if status == ERROR: 
    906913            raise ValueError, "Could not write attr %s: %s"%(name,self._loc()) 
    907      
     914 
    908915    def attrs(self): 
    909916        """ 
    910917        Iterate over attributes. 
    911          
     918 
    912919        for name,value in file.attrs(): 
    913920            process(name,value) 
     
    919926        combines the work of attrinfo/initattrdir/getnextattr/getattr. 
    920927        """ 
     928        self.initattrdir() 
    921929        n = self.getattrinfo() 
    922         self.initattrdir() 
    923930        for i in range(n): 
    924931            name,length,dtype = self.getnextattr() 
    925932            value = self.getattr(name,length,dtype) 
    926933            yield name,value 
    927      
     934 
    928935    # ==== Linking ==== 
    929936    lib.nxigetgroupid_.restype = c_int 
     
    932939        """ 
    933940        Return the id of the current group so we can link to it later. 
    934          
     941 
    935942        Raises RuntimeError 
    936          
     943 
    937944        Corresponds to NXgetgroupID(handle, &ID) 
    938945        """ 
     
    942949            raise RuntimeError, "Could not link to group: %s"%(self._loc()) 
    943950        return ID 
    944      
     951 
    945952    lib.nxigetdataid_.restype = c_int 
    946953    lib.nxigetdataid_.argtypes = [c_void_p, c_NXlink_p] 
     
    948955        """ 
    949956        Return the id of the current data so we can link to it later. 
    950          
     957 
    951958        Raises RuntimeError 
    952          
     959 
    953960        Corresponds to NXgetdataID(handle, &ID) 
    954961        """ 
     
    958965            raise RuntimeError, "Could not link to data: %s"%(self._loc()) 
    959966        return ID 
    960      
     967 
    961968    lib.nximakelink_.restype = c_int 
    962969    lib.nximakelink_.argtypes = [c_void_p, c_NXlink_p] 
    963970    def makelink(self, ID): 
    964971        """ 
    965         Link the previously captured group/data ID into the currently  
     972        Link the previously captured group/data ID into the currently 
    966973        open group. 
    967          
     974 
    968975        Raises RuntimeError 
    969          
     976 
    970977        Corresponds to NXmakelink(handle, &ID) 
    971978        """ 
     
    973980        if status == ERROR: 
    974981            raise RuntimeError, "Could not make link: %s"%(self._loc()) 
    975      
     982 
    976983    lib.nximakenamedlink_.restype = c_int 
    977984    lib.nximakenamedlink_.argtypes = [c_void_p, c_char_p, c_NXlink_p] 
    978985    def makenamedlink(self,name,ID): 
    979986        """ 
    980         Link the previously captured group/data ID into the currently  
     987        Link the previously captured group/data ID into the currently 
    981988        open group, but under a different name. 
    982          
     989 
    983990        Raises RuntimeError 
    984          
     991 
    985992        Corresponds to NXmakenamedlink(handle,name,&ID) 
    986993        """ 
     
    988995        if status == ERROR: 
    989996            raise RuntimeError, "Could not make link %s: %s"%(name,self._loc()) 
    990      
     997 
    991998    lib.nxisameid_.restype = c_int 
    992999    lib.nxisameid_.argtypes = [c_void_p, c_NXlink_p, c_NXlink_p] 
     
    9941001        """ 
    9951002        Return True of ID1 and ID2 point to the same group/data. 
    996          
     1003 
    9971004        This should not raise any errors. 
    998          
     1005 
    9991006        Corresponds to NXsameID(handle,&ID1,&ID2) 
    10001007        """ 
    10011008        status = self.lib.nxisameid_(self.handle, _ref(ID1), _ref(ID2)) 
    10021009        return status == OK 
    1003      
     1010 
    10041011    lib.nxiopensourcegroup_.restype = c_int 
    10051012    lib.nxiopensourcegroup_.argtyps = [c_void_p] 
     
    10081015        If the current node is a linked to another group or data, then 
    10091016        open the group or data that it is linked to. 
    1010          
     1017 
    10111018        Note: it is unclear how can we tell if we are linked, other than 
    10121019        perhaps the existence of a 'target' attribute in the current item. 
    1013          
     1020 
    10141021        Raises RuntimeError 
    1015          
     1022 
    10161023        Corresponds to NXopensourcegroup(handle) 
    10171024        """ 
     
    10221029    def link(self): 
    10231030        """ 
    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 
    10271034        to self. 
    10281035 
     
    10681075 
    10691076    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, 
    10711078                                       c_char_p, c_char_p] 
    10721079    def linkexternal(self, name, nxclass, url): 
    10731080        """ 
    1074         Return the filename for the external link if there is one,  
     1081        Return the filename for the external link if there is one, 
    10751082        otherwise return None. 
    1076          
     1083 
    10771084        Corresponds to NXisexternalgroup(&handle,name,nxclass,file,len) 
    10781085        """ 
     
    10851092 
    10861093    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, 
    10881095                                       c_char_p, c_char_p, c_int] 
    10891096    def isexternalgroup(self, name, nxclass, maxnamelen=MAXPATHLEN): 
    10901097        """ 
    1091         Return the filename for the external link if there is one,  
     1098        Return the filename for the external link if there is one, 
    10921099        otherwise return None. 
    1093          
     1100 
    10941101        Corresponds to NXisexternalgroup(&handle,name,nxclass,file,len) 
    10951102        """ 
     
    11001107            return None 
    11011108        else: 
    1102             url.value 
    1103  
     1109            return url.value 
    11041110 
    11051111    # ==== Utility functions ==== 
     
    11071113        """ 
    11081114        Return file location as string filename:path 
    1109          
     1115 
    11101116        This is an extension to the NeXus API. 
    11111117        """ 
     
    11151121            pathstr = "root" 
    11161122        return "%s(%s)"%(self.filename,pathstr) 
    1117      
     1123 
    11181124    def _poutput(self, dtype, shape): 
    11191125        """ 
     
    11431149        return datafn,pdata,size 
    11441150 
    1145     def _nxany(self,iter): 
    1146         """ 
    1147         replace python 2.5 any() 
    1148         """ 
    1149         for e in iter: 
    1150             if e: 
    1151                 return 1 
    1152             return 0 
    1153      
    11541151    def _pinput(self, data, dtype, shape): 
    11551152        """ 
     
    11731170            input_shape = numpy.array([i for i in data.shape if i != 1]) 
    11741171            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(): 
    11791173                raise ValueError,\ 
    11801174                    "Shape mismatch %s!=%s: %s"%(data.shape,shape,self.filename) 
     
    11821176                raise ValueError,\ 
    11831177                    "Type mismatch %s!=%s: %s"%(dtype,data.dtype,self._loc()) 
    1184      
     1178 
    11851179        if dtype == 'char': 
    11861180            # String: hand it over as usual for strings.  Assumes the string 
     
    11911185            data = numpy.ascontiguousarray(data) 
    11921186            pdata = data.ctypes.data 
    1193              
     1187 
    11941188        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 
    11961235 
    11971236__id__ = "$ID$" 
Note: See TracChangeset for help on using the changeset viewer.