Changeset 1120 for trunk/bindings/python/nexus.py
- Timestamp:
- 25/10/08 00:17:16 (4 years ago)
- File:
-
- 1 edited
-
trunk/bindings/python/nexus.py (modified) (8 diffs)
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
Note: See TracChangeset
for help on using the changeset viewer.
