Changeset 1120 for trunk/bindings/python


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

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

Location:
trunk/bindings/python
Files:
1 added
4 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 
  • trunk/bindings/python/nxs.py

    r1116 r1120  
    66Wrapper for the NeXus shared library. 
    77 
     8Use this interface when converting code from other languages which 
     9do not support the natural view of the hierarchy. 
    810 
    911Library Location 
     
    1113 
    1214This wrapper needs the location of the libNeXus precompiled binary. It 
    13 looks in the following places in order: 
     15looks in the following places in order:: 
     16 
    1417    os.environ['NEXUSLIB']                  - All 
    1518    directory containing nxs.py             - All 
     
    2124    /usr/lib                                - Unix and Darwin 
    2225 
    23 On Windows it looks for libNeXus.dll and libNeXus-0.dll; 
    24 NEXUSDIR defaults to r'C:\Program Files\NeXus Data Format' 
     26On Windows it looks for one of libNeXus.dll or libNeXus-0.dll. 
    2527On OS X it looks for libNeXus.dylib 
    2628On Unix it looks for libNeXus.so 
     29NEXUSDIR defaults to r'C:\Program Files\NeXus Data Format'. 
    2730PREFIX defaults to /usr/local, but is replaced by the value of 
    2831--prefix during configure. 
     
    3639first import of nxs. 
    3740 
     41Example 
     42======= 
     43 
     44  import nxs 
     45  file = nxs.open('filename.nxs','rw') 
     46  file.opengroup('entry1') 
     47  file.opendata('definition') 
     48  print file.getdata() 
     49  file.close() 
     50 
     51  See nxstest.py for a more complete example. 
     52 
    3853Interface 
    3954========= 
    4055 
    41 Full documentation of the NeXus API is available at nexusformat.org. 
    42  
    43 This wrapper differs from napi in several respects: 
     56When converting code to python from other languages you do not 
     57necessarily want to redo the file handling code.  The nxs 
     58provides an interface which more closely follows the 
     59NeXus application programming interface (NAPI_). 
     60 
     61This wrapper differs from NAPI in several respects:: 
     62 
    4463  - Data values are loaded/stored directly from numpy arrays. 
    4564  - Return codes are turned into exceptions. 
     
    5170  - NXmalloc/NXfree are not needed. 
    5271 
    53 Example: 
    54  
    55   import nxs 
    56   file = nxs.open('filename.nxs','rw') 
    57   file.opengroup('entry1') 
    58   file.opendata('definition') 
    59   print file.getdata() 
    60   file.close() 
    61  
    62   See nxstest.py for a more complete example. 
    63  
    64 File open modes can be constants or strings: 
     72File open modes can be constants or strings:: 
    6573 
    6674  nxs.ACC_READ      'r' 
     
    7179  nxs.ACC_CREATEXML 'wx' 
    7280 
    73 Dimension constants: 
     81Dimension constants:: 
    7482 
    7583  nxs.UNLIMITED  - for the extensible data dimension 
    7684  nxs.MAXRANK    - for the number of possible dimensions 
    7785 
    78 Data types are strings corresponding to the numpy data types: 
     86Data types are strings corresponding to the numpy data types:: 
    7987 
    8088  'float32' 'float64' 
     
    8290  'uint8' 'uint16' 'uint32' 'uint64' 
    8391 
    84   Use 'char' for strings.  You can use the numpy dtype attribute for the 
    85   data type. 
     92  Use 'char' for string data.   
     93 
     94You can use the numpy A.dtype attribute for the type of array A. 
    8695 
    8796Dimensions are lists of integers or numpy arrays.  You can use the 
    88 numpy shape attribute for the dimensions. 
    89  
    90 Compression codes are: 
     97numpy A.shape attribute for the dimensions of array A. 
     98 
     99Compression codes are:: 
    91100 
    92101 'none' 'lzw' 'rle' 'huffman' 
     
    94103  As of this writing NeXus only supports 'none' and 'lzw'. 
    95104 
    96 Miscellaneous constants: 
     105Miscellaneous constants:: 
    97106 
    98107  nxs.MAXNAMELEN  - names must be shorter than this 
     
    112121   - if I remove the open/close call in the wrapper it doesn't leak. 
    113122 
     123.. _NAPI:  http://www.nexusformat.org/Application_Program_Interface 
    114124""" 
     125__all__ = ['MAXNAMELEN','MAXPATHLEN','NeXus','open'] 
     126 
    115127import sys, os, numpy, ctypes 
    116128 
  • trunk/bindings/python/nxsunit.py

    r1117 r1120  
    1919Usage example: 
    2020 
    21     u = nxsunit.Converter('mili*metre')  # Units stored in mm 
     21    import nxs.unit 
     22    u = nxs.unit.Converter('mili*metre')  # Units stored in mm 
    2223    v = u(3000,'m')  # Convert the value 3000 mm into meters 
    2324 
     
    3031    units = [for attr,value in file.attrs() if attr == 'units'] 
    3132    # 3. set up the converter (assumes that units actually exists) 
    32     u = nxsunit.Converter(units[0]) 
     33    u = nxs.unit.Converter(units[0]) 
    3334    # 4. read the data and convert to the correct units 
    3435    v = u(file.read(),'radians') 
     
    4344getting the dimension from the units as we are currently doing. 
    4445""" 
     46 
     47__all__ = ['Converter'] 
    4548 
    4649# TODO: Add udunits to NAPI rather than reimplementing it in python 
  • trunk/bindings/python/setup.py

    r976 r1120  
    77      description='Python Bindings to libNeXus', 
    88      author='Paul Kienzle', 
    9       py_modules=['nxs'], 
     9      packages = ['nxs'], 
     10      package_dir = {'nxs': ''}, 
    1011      ) 
Note: See TracChangeset for help on using the changeset viewer.