Ignore:
Timestamp:
25/10/08 00:17:16 (4 years ago)
Author:
Paul Kienzle
Message:

python: make proper package (step 1) Refs #101.

File:
1 edited

Legend:

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

    r1117 r1120  
    33 
    44""" 
    5 High level interface to NeXus files. 
    6  
    7 Unlike the nxs routines which implement the NeXus API directly, the 
    8 nexus routines preload the entire file structure into memory and use 
     5Tree view for NeXus files. 
     6 
     7Unlike the `nxs.napi` routines which implement the NeXus API directly, the 
     8`nxs.tree` routines preload the entire file structure into memory and use 
    99a natural syntax for navigating the data hierarchy.  Large datasets 
    1010are not read until they are needed, and may be read or written one 
     
    1313There are a number of functions which operate on files:: 
    1414 
    15   * tree = read(file)   loads a structure from a file 
    16   * write(file, tree)   saves a structure to a file 
    17   * dir(file)           display the contents of a file 
     15    import nxs 
     16    tree = nxs.read('file.nxs')   # loads a structure from a file 
     17    nxs.write('copy.nxs', tree)   # saves a structure to a file 
     18    nxs.dir('copy.nxs')           # display the contents of a file 
     19 
    1820 
    1921The tree returned from read() has an entry for each group, field and 
     
    110112this with your own definitions for NXgroup(), NXattr(), SDS() and NXlink() 
    111113if you want to change the nature of the tree.  The properties of these 
    112 classes are closely coupled to the behaviour of read/write so refer to 
    113 the source if you need to do this. 
     114classes are closely coupled to the behaviour of readfile/writefile so  
     115refer to the source if you need to do this. 
    114116""" 
    115 __all__ = ['read', 'write', 'dir'] 
     117__all__ = ['read', 'write', 'dir', 'NeXusTree'] 
    116118 
    117119from copy import copy, deepcopy 
    118120import numpy 
    119 import nxs 
    120 import nxsunit 
    121  
    122  
    123 class NeXus(nxs.NeXus): 
     121import nxs.napi 
     122import nxs.unit 
     123 
     124 
     125class NeXusTree(nxs.napi.NeXus): 
    124126    """ 
    125127    Structure-based interface to the NeXus file API. 
    126128 
    127     Usage: 
    128  
    129     file = NeXus(filename, ['r','rw','w']) 
    130     root = file.read() 
    131       - read the structure of the NeXus file.  This returns a NeXus tree. 
    132     file.write(root) 
    133       - write a NeXus tree to the file. 
    134     data = file.readpath(path) 
    135       - read data from a particular path 
     129    Usage:: 
     130 
     131      file = NeXusTree(filename, ['r','rw','w']) 
     132        - open the NeXus file 
     133      root = file.readfile() 
     134        - read the structure of the NeXus file.  This returns a NeXus tree. 
     135      file.writefile(root) 
     136        - write a NeXus tree to the file. 
     137      data = file.readpath(path) 
     138        - read data from a particular path 
    136139 
    137140 
    138141    Example:: 
    139142 
    140       nx = NeXus('REF_L_1346.nxs','r') 
    141       tree = nx.read() 
     143      nx = NeXusTree('REF_L_1346.nxs','r') 
     144      tree = nx.readfile() 
    142145      for entry in tree.NXentry: 
    143146          process(entry) 
    144       copy = NeXus('modified.nxs','w') 
    145       copy.write(tree) 
     147      copy = NeXusTree('modified.nxs','w') 
     148      copy.writefile(tree) 
    146149 
    147150    Note that the large datasets are not loaded immediately.  Instead, the 
     
    152155 
    153156    """ 
    154     def read(self): 
     157    def readfile(self): 
    155158        """ 
    156159        Read the nexus file structure from the file.  Reading of large datasets 
     
    172175        return root 
    173176 
    174     def write(self, tree): 
     177    def writefile(self, tree): 
    175178        """ 
    176179        Write the nexus file structure to the file.  The file is assumed to 
     
    833836    Read a NeXus file, returning a tree of nodes 
    834837    """ 
    835     file = NeXus(filename,mode) 
    836     tree = file.read() 
     838    file = NeXusTree(filename,mode) 
     839    tree = file.readfile() 
    837840    file.close() 
    838841    return tree 
    839842 
    840 def write(filename, tree): 
     843def write(filename, tree, format='w5'): 
    841844    """ 
    842845    Write a NeXus file from a tree of nodes 
    843846    """ 
    844     file = NeXus(filename,'w5') 
    845     file.write(tree) 
     847    file = NeXusTree(filename, format) 
     848    file.writefile(tree) 
    846849 
    847850def dir(file): 
     
    849852    Read and summarize the named nexus file. 
    850853    """ 
    851     tree = read(file) 
     854    tree = readfile(file) 
    852855    tree.nxtree() 
    853856 
     
    888891    import sys 
    889892    demo(sys.argv) 
     893 
Note: See TracChangeset for help on using the changeset viewer.