Changeset 1120 for trunk/bindings/python
- Timestamp:
- 25/10/08 00:17:16 (4 years ago)
- Location:
- trunk/bindings/python
- Files:
-
- 1 added
- 4 edited
-
__init__.py (added)
-
nexus.py (modified) (8 diffs)
-
nxs.py (modified) (9 diffs)
-
nxsunit.py (modified) (3 diffs)
-
setup.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/bindings/python/nexus.py
r1117 r1120 3 3 4 4 """ 5 High level interface toNeXus files.6 7 Unlike the nxsroutines which implement the NeXus API directly, the8 nexusroutines preload the entire file structure into memory and use5 Tree view for NeXus files. 6 7 Unlike the `nxs.napi` routines which implement the NeXus API directly, the 8 `nxs.tree` routines preload the entire file structure into memory and use 9 9 a natural syntax for navigating the data hierarchy. Large datasets 10 10 are not read until they are needed, and may be read or written one … … 13 13 There are a number of functions which operate on files:: 14 14 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 18 20 19 21 The tree returned from read() has an entry for each group, field and … … 110 112 this with your own definitions for NXgroup(), NXattr(), SDS() and NXlink() 111 113 if 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 to113 the source if you need to do this.114 classes are closely coupled to the behaviour of readfile/writefile so 115 refer to the source if you need to do this. 114 116 """ 115 __all__ = ['read', 'write', 'dir' ]117 __all__ = ['read', 'write', 'dir', 'NeXusTree'] 116 118 117 119 from copy import copy, deepcopy 118 120 import numpy 119 import nxs 120 import nxs unit121 122 123 class NeXus (nxs.NeXus):121 import nxs.napi 122 import nxs.unit 123 124 125 class NeXusTree(nxs.napi.NeXus): 124 126 """ 125 127 Structure-based interface to the NeXus file API. 126 128 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 136 139 137 140 138 141 Example:: 139 142 140 nx = NeXus ('REF_L_1346.nxs','r')141 tree = nx.read ()143 nx = NeXusTree('REF_L_1346.nxs','r') 144 tree = nx.readfile() 142 145 for entry in tree.NXentry: 143 146 process(entry) 144 copy = NeXus ('modified.nxs','w')145 copy.write (tree)147 copy = NeXusTree('modified.nxs','w') 148 copy.writefile(tree) 146 149 147 150 Note that the large datasets are not loaded immediately. Instead, the … … 152 155 153 156 """ 154 def read (self):157 def readfile(self): 155 158 """ 156 159 Read the nexus file structure from the file. Reading of large datasets … … 172 175 return root 173 176 174 def write (self, tree):177 def writefile(self, tree): 175 178 """ 176 179 Write the nexus file structure to the file. The file is assumed to … … 833 836 Read a NeXus file, returning a tree of nodes 834 837 """ 835 file = NeXus (filename,mode)836 tree = file.read ()838 file = NeXusTree(filename,mode) 839 tree = file.readfile() 837 840 file.close() 838 841 return tree 839 842 840 def write(filename, tree ):843 def write(filename, tree, format='w5'): 841 844 """ 842 845 Write a NeXus file from a tree of nodes 843 846 """ 844 file = NeXus (filename,'w5')845 file.write (tree)847 file = NeXusTree(filename, format) 848 file.writefile(tree) 846 849 847 850 def dir(file): … … 849 852 Read and summarize the named nexus file. 850 853 """ 851 tree = read (file)854 tree = readfile(file) 852 855 tree.nxtree() 853 856 … … 888 891 import sys 889 892 demo(sys.argv) 893 -
trunk/bindings/python/nxs.py
r1116 r1120 6 6 Wrapper for the NeXus shared library. 7 7 8 Use this interface when converting code from other languages which 9 do not support the natural view of the hierarchy. 8 10 9 11 Library Location … … 11 13 12 14 This wrapper needs the location of the libNeXus precompiled binary. It 13 looks in the following places in order: 15 looks in the following places in order:: 16 14 17 os.environ['NEXUSLIB'] - All 15 18 directory containing nxs.py - All … … 21 24 /usr/lib - Unix and Darwin 22 25 23 On Windows it looks for libNeXus.dll and libNeXus-0.dll; 24 NEXUSDIR defaults to r'C:\Program Files\NeXus Data Format' 26 On Windows it looks for one of libNeXus.dll or libNeXus-0.dll. 25 27 On OS X it looks for libNeXus.dylib 26 28 On Unix it looks for libNeXus.so 29 NEXUSDIR defaults to r'C:\Program Files\NeXus Data Format'. 27 30 PREFIX defaults to /usr/local, but is replaced by the value of 28 31 --prefix during configure. … … 36 39 first import of nxs. 37 40 41 Example 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 38 53 Interface 39 54 ========= 40 55 41 Full documentation of the NeXus API is available at nexusformat.org. 42 43 This wrapper differs from napi in several respects: 56 When converting code to python from other languages you do not 57 necessarily want to redo the file handling code. The nxs 58 provides an interface which more closely follows the 59 NeXus application programming interface (NAPI_). 60 61 This wrapper differs from NAPI in several respects:: 62 44 63 - Data values are loaded/stored directly from numpy arrays. 45 64 - Return codes are turned into exceptions. … … 51 70 - NXmalloc/NXfree are not needed. 52 71 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: 72 File open modes can be constants or strings:: 65 73 66 74 nxs.ACC_READ 'r' … … 71 79 nxs.ACC_CREATEXML 'wx' 72 80 73 Dimension constants: 81 Dimension constants:: 74 82 75 83 nxs.UNLIMITED - for the extensible data dimension 76 84 nxs.MAXRANK - for the number of possible dimensions 77 85 78 Data types are strings corresponding to the numpy data types: 86 Data types are strings corresponding to the numpy data types:: 79 87 80 88 'float32' 'float64' … … 82 90 'uint8' 'uint16' 'uint32' 'uint64' 83 91 84 Use 'char' for strings. You can use the numpy dtype attribute for the 85 data type. 92 Use 'char' for string data. 93 94 You can use the numpy A.dtype attribute for the type of array A. 86 95 87 96 Dimensions are lists of integers or numpy arrays. You can use the 88 numpy shape attribute for the dimensions.89 90 Compression codes are: 97 numpy A.shape attribute for the dimensions of array A. 98 99 Compression codes are:: 91 100 92 101 'none' 'lzw' 'rle' 'huffman' … … 94 103 As of this writing NeXus only supports 'none' and 'lzw'. 95 104 96 Miscellaneous constants: 105 Miscellaneous constants:: 97 106 98 107 nxs.MAXNAMELEN - names must be shorter than this … … 112 121 - if I remove the open/close call in the wrapper it doesn't leak. 113 122 123 .. _NAPI: http://www.nexusformat.org/Application_Program_Interface 114 124 """ 125 __all__ = ['MAXNAMELEN','MAXPATHLEN','NeXus','open'] 126 115 127 import sys, os, numpy, ctypes 116 128 -
trunk/bindings/python/nxsunit.py
r1117 r1120 19 19 Usage example: 20 20 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 22 23 v = u(3000,'m') # Convert the value 3000 mm into meters 23 24 … … 30 31 units = [for attr,value in file.attrs() if attr == 'units'] 31 32 # 3. set up the converter (assumes that units actually exists) 32 u = nxs unit.Converter(units[0])33 u = nxs.unit.Converter(units[0]) 33 34 # 4. read the data and convert to the correct units 34 35 v = u(file.read(),'radians') … … 43 44 getting the dimension from the units as we are currently doing. 44 45 """ 46 47 __all__ = ['Converter'] 45 48 46 49 # TODO: Add udunits to NAPI rather than reimplementing it in python -
trunk/bindings/python/setup.py
r976 r1120 7 7 description='Python Bindings to libNeXus', 8 8 author='Paul Kienzle', 9 py_modules=['nxs'], 9 packages = ['nxs'], 10 package_dir = {'nxs': ''}, 10 11 )
Note: See TracChangeset
for help on using the changeset viewer.
