~ubuntu-branches/debian/stretch/adios/stretch

« back to all changes in this revision

Viewing changes to wrappers/numpy/adios_mpi.pyx

  • Committer: Package Import Robot
  • Author(s): Alastair McKinstry
  • Date: 2014-06-16 23:06:38 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20140616230638-5a0z7ylxx8i0edrg
Tags: 1.7.0-1
* New upstream release.
* Add adios.pc pkgconfig file. adios_config now uses this.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
 ADIOS is freely available under the terms of the BSD license described
 
3
 in the COPYING file in the top level directory of this source distribution.
 
4
 
 
5
 Copyright (c) 2008 - 2009.  UT-BATTELLE, LLC. All rights reserved.
 
6
"""
 
7
"""
 
8
 This is a cython file. To generate a CPP file, use the following command:
 
9
 $ cython --cplus adios.pyx
 
10
"""
 
11
 
 
12
import numpy as np
 
13
cimport numpy as np
 
14
 
 
15
import mpi4py.MPI as MPI 
 
16
cimport mpi4py.MPI as MPI
 
17
 
 
18
import cython
 
19
cimport cython
 
20
 
 
21
## ====================
 
22
## ADIOS Exported Functions
 
23
## ====================
 
24
 
 
25
from libc.stdint cimport uint32_t, int64_t, uint64_t
 
26
from libc.stdlib cimport malloc, free
 
27
 
 
28
cdef extern from "adios_types.h":
 
29
    ctypedef enum ADIOS_DATATYPES:
 
30
        adios_unknown
 
31
        adios_byte
 
32
        adios_short
 
33
        adios_integer
 
34
        adios_long
 
35
        adios_unsigned_byte
 
36
        adios_unsigned_short
 
37
        adios_unsigned_integer
 
38
        adios_unsigned_long
 
39
        adios_real
 
40
        adios_double
 
41
        adios_long_double
 
42
        adios_string
 
43
        adios_complex
 
44
        adios_double_complex
 
45
 
 
46
    ctypedef enum ADIOS_BUFFER_ALLOC_WHEN:
 
47
        ADIOS_BUFFER_ALLOC_UNKNOWN
 
48
        ADIOS_BUFFER_ALLOC_NOW
 
49
        ADIOS_BUFFER_ALLOC_LATER
 
50
 
 
51
    ctypedef enum ADIOS_FLAG:
 
52
        pass
 
53
 
 
54
cdef extern from "adios.h":
 
55
    ctypedef struct MPI_Comm:
 
56
        pass
 
57
 
 
58
    ctypedef char* const_char_ptr "const char*"
 
59
 
 
60
    cdef int adios_init (char * config, MPI_Comm)
 
61
    
 
62
    cdef int adios_finalize (int mype)
 
63
    
 
64
    cdef int adios_open (int64_t * fd,
 
65
                         char * group_name,
 
66
                         char * name, 
 
67
                         char * mode,
 
68
                         MPI_Comm comm)
 
69
    
 
70
    cdef int adios_group_size (int64_t fd_p,
 
71
                               uint64_t data_size,
 
72
                               uint64_t * total_size)
 
73
    
 
74
    cdef int adios_write (int64_t fd_p,
 
75
                          char * name,
 
76
                          void * var)
 
77
    
 
78
    cdef int adios_read (int64_t fd_p,
 
79
                         char * name,
 
80
                         void * buffer,
 
81
                         uint64_t buffer_size)
 
82
 
 
83
    cdef int adios_close(int64_t fd_p)
 
84
    
 
85
    cdef int adios_init_noxml (MPI_Comm)
 
86
    
 
87
    cdef int adios_allocate_buffer (ADIOS_BUFFER_ALLOC_WHEN when,
 
88
                                    uint64_t buffer_size)
 
89
    
 
90
    cdef int adios_declare_group (int64_t * id,
 
91
                                  char * name,
 
92
                                  char * time_index,
 
93
                                  ADIOS_FLAG stats)
 
94
    
 
95
    cdef int adios_define_var (int64_t group_id,
 
96
                               char * name,
 
97
                               char * path,
 
98
                               ADIOS_DATATYPES type,
 
99
                               char * dimensions,
 
100
                               char * global_dimensions,
 
101
                               char * local_offsets)
 
102
    
 
103
    cdef int adios_define_attribute (int64_t group,
 
104
                                     char * name,
 
105
                                     char * path,
 
106
                                     ADIOS_DATATYPES type,
 
107
                                     char * value,
 
108
                                     char * var)
 
109
    
 
110
    cdef int adios_select_method (int64_t group,
 
111
                                  char * method,
 
112
                                  char * parameters,
 
113
                                  char * base_path)
 
114
 
 
115
cdef extern from "adios_selection.h":
 
116
    ctypedef enum ADIOS_SELECTION_TYPE:
 
117
        ADIOS_SELECTION_BOUNDINGBOX
 
118
        ADIOS_SELECTION_POINTS
 
119
        ADIOS_SELECTION_WRITEBLOCK
 
120
        ADIOS_SELECTION_AUTO
 
121
 
 
122
    ctypedef struct ADIOS_SELECTION_BOUNDINGBOX_STRUCT:
 
123
        int       ndim
 
124
        uint64_t *start
 
125
        uint64_t *count
 
126
 
 
127
    ctypedef struct ADIOS_SELECTION_POINTS_STRUCT:
 
128
        pass
 
129
 
 
130
    ctypedef struct ADIOS_SELECTION_WRITEBLOCK_STRUCT:
 
131
        pass
 
132
    
 
133
    ctypedef struct ADIOS_SELECTION_AUTO_STRUCT:
 
134
        pass
 
135
 
 
136
    cdef union ADIOS_SELECTION_UNION:
 
137
        ADIOS_SELECTION_BOUNDINGBOX_STRUCT bb
 
138
        ADIOS_SELECTION_POINTS_STRUCT points
 
139
        ADIOS_SELECTION_WRITEBLOCK_STRUCT block
 
140
        ADIOS_SELECTION_AUTO_STRUCT autosel
 
141
 
 
142
    ctypedef struct ADIOS_SELECTION:
 
143
        ADIOS_SELECTION_TYPE    type
 
144
        ADIOS_SELECTION_UNION   u
 
145
 
 
146
    cdef ADIOS_SELECTION * adios_selection_boundingbox (int ndim,
 
147
                                                        const uint64_t *start,
 
148
                                                        const uint64_t *count)
 
149
 
 
150
cdef extern from "adios_read.h":
 
151
    ctypedef enum ADIOS_READ_METHOD:
 
152
        ADIOS_READ_METHOD_BP
 
153
        ADIOS_READ_METHOD_BP_AGGREGATE
 
154
        pass
 
155
 
 
156
    ctypedef enum ADIOS_LOCKMODE:
 
157
        ADIOS_LOCKMODE_NONE
 
158
        ADIOS_LOCKMODE_CURRENT
 
159
        ADIOS_LOCKMODE_ALL
 
160
 
 
161
    ctypedef struct ADIOS_FILE:
 
162
        uint64_t fh               
 
163
        int      nvars            
 
164
        char     ** var_namelist  
 
165
        int      nattrs           
 
166
        char     ** attr_namelist 
 
167
        int      nmeshes          
 
168
        char     ** mesh_namelist 
 
169
        int      current_step     
 
170
        int      last_step        
 
171
        char     *path            
 
172
        int      endianness       
 
173
        int      version          
 
174
        uint64_t file_size
 
175
 
 
176
    ctypedef struct ADIOS_VARINFO:
 
177
        int        varid
 
178
        ADIOS_DATATYPES type  
 
179
        int        ndim       
 
180
        uint64_t * dims       
 
181
        int        nsteps     
 
182
        void     * value      
 
183
        int      * nblocks    
 
184
        int        sum_nblocks
 
185
 
 
186
    cdef int adios_read_init_method (ADIOS_READ_METHOD method, 
 
187
                                     MPI_Comm comm, 
 
188
                                     char * parameters)
 
189
    cdef int adios_read_finalize_method(ADIOS_READ_METHOD method)
 
190
    cdef ADIOS_FILE * adios_read_open (const char * fname, 
 
191
                                       ADIOS_READ_METHOD method, 
 
192
                                       MPI_Comm comm, 
 
193
                                       ADIOS_LOCKMODE lock_mode,
 
194
                                       float timeout_sec)
 
195
    cdef ADIOS_FILE * adios_read_open_file (const char * fname, 
 
196
                                            ADIOS_READ_METHOD method, 
 
197
                                            MPI_Comm comm)
 
198
    cdef int adios_read_close (ADIOS_FILE *fp)
 
199
    cdef int adios_advance_step (ADIOS_FILE *fp, int last, float timeout_sec)
 
200
    cdef void adios_release_step (ADIOS_FILE *fp)
 
201
    cdef ADIOS_VARINFO * adios_inq_var (ADIOS_FILE *fp, const char * varname)
 
202
    cdef ADIOS_VARINFO * adios_inq_var_byid (ADIOS_FILE *fp, int varid)
 
203
    cdef void adios_free_varinfo (ADIOS_VARINFO *cp)
 
204
    cdef int adios_schedule_read (const ADIOS_FILE * fp,
 
205
                                  const ADIOS_SELECTION * sel,
 
206
                                  const char * varname,
 
207
                                  int from_steps,
 
208
                                  int nsteps,
 
209
                                  void * data)
 
210
    cdef int adios_schedule_read_byid (const ADIOS_FILE * fp, 
 
211
                                       const ADIOS_SELECTION * sel,
 
212
                                       int varid,
 
213
                                       int from_steps,
 
214
                                       int nsteps,
 
215
                                       void * data)
 
216
    cdef int adios_perform_reads (const ADIOS_FILE *fp, int blocking)
 
217
 
 
218
 
 
219
## ====================
 
220
## ADIOS Enum (public)
 
221
## ====================
 
222
 
 
223
class DATATYPE:
 
224
    unknown = -1
 
225
    byte = 0
 
226
    short = 1
 
227
    integer = 2
 
228
    long = 4
 
229
    unsigned_byte = 50
 
230
    unsigned_short = 51
 
231
    unsigned_integer = 52
 
232
    unsigned_long = 54
 
233
    real = 5
 
234
    double = 6
 
235
    long_double = 7
 
236
    string = 9
 
237
    complex = 10
 
238
    double_complex = 11
 
239
 
 
240
class FLAG:
 
241
    UNKNOWN = 0
 
242
    YES = 1
 
243
    NO = 2
 
244
    
 
245
class BUFFER_ALLOC_WHEN:
 
246
    UNKNOWN = 0
 
247
    NOW = 1
 
248
    LATER = 2
 
249
    
 
250
## ====================
 
251
## ADIOS Write API
 
252
## ====================
 
253
 
 
254
cpdef init(char * config, MPI.Comm comm = MPI.COMM_WORLD):
 
255
    return adios_init(config, comm.ob_mpi)
 
256
 
 
257
cpdef int64_t open(char * group_name,
 
258
                   char * name,
 
259
                   char * mode,
 
260
                   MPI.Comm comm = MPI.COMM_WORLD):
 
261
    cdef int64_t fd
 
262
    cdef int result
 
263
    result = adios_open(&fd, group_name, name, mode, comm.ob_mpi)
 
264
    return fd
 
265
 
 
266
cpdef int64_t set_group_size(int64_t fd_p, uint64_t data_size):
 
267
    cdef uint64_t total_size
 
268
    cdef int result
 
269
    result = adios_group_size(fd_p, data_size, &total_size)
 
270
    return total_size
 
271
 
 
272
cpdef int write (int64_t fd_p, char * name, np.ndarray val):
 
273
    cdef np.ndarray val_
 
274
    if val.flags.contiguous:
 
275
        val_ = val
 
276
    else:
 
277
        val_ = np.array(val, copy=True)
 
278
 
 
279
    return adios_write (fd_p, name, <void *> val_.data)
 
280
 
 
281
cpdef int write_int (int64_t fd_p, char * name, int val):
 
282
    return adios_write (fd_p, name, &val)
 
283
 
 
284
cpdef int write_long (int64_t fd_p, char * name, long val):
 
285
    return adios_write (fd_p, name, &val)
 
286
 
 
287
cpdef int write_float (int64_t fd_p, char * name, float val):
 
288
    return adios_write (fd_p, name, &val)
 
289
 
 
290
cpdef int read(int64_t fd_p, char * name, np.ndarray val):
 
291
    assert val.flags.contiguous, 'Only contiguous arrays are supported.'
 
292
    print "Reading ... ", val.itemsize * val.size, "(bytes)"
 
293
    return adios_read(fd_p, name, <void *> val.data, val.itemsize * val.size)
 
294
 
 
295
cpdef int close(int64_t fd_p):
 
296
    return adios_close(fd_p)
 
297
 
 
298
cpdef finalize(int mype = 0):
 
299
    return adios_finalize(mype)
 
300
 
 
301
## ====================
 
302
## ADIOS No-XML API
 
303
## ====================
 
304
cpdef int init_noxml(MPI.Comm comm = MPI.COMM_WORLD):
 
305
    return adios_init_noxml(comm.ob_mpi)
 
306
 
 
307
cpdef int allocate_buffer(int when,
 
308
                          uint64_t buffer_size):
 
309
    return adios_allocate_buffer(<ADIOS_BUFFER_ALLOC_WHEN> when,
 
310
                                 buffer_size)
 
311
 
 
312
cpdef int64_t declare_group(char * name,
 
313
                            char * time_index,
 
314
                            int stats):
 
315
    cdef int64_t id = 0
 
316
    adios_declare_group (&id,
 
317
                         name,
 
318
                         time_index,
 
319
                         <ADIOS_FLAG> stats)
 
320
    return id
 
321
 
 
322
cpdef int define_var(int64_t group_id,
 
323
                     char * name,
 
324
                     char * path,
 
325
                     int type,
 
326
                     char * dimensions,
 
327
                     char * global_dimensions,
 
328
                     char * local_offsets):
 
329
    return adios_define_var(group_id,
 
330
                            name, path,
 
331
                            <ADIOS_DATATYPES> type,
 
332
                            dimensions,
 
333
                            global_dimensions,
 
334
                            local_offsets)
 
335
 
 
336
cpdef int define_attribute (int64_t group,
 
337
                            char * name,
 
338
                            char * path,
 
339
                            int type,
 
340
                            char * value,
 
341
                            char * var):
 
342
    return adios_define_attribute (group,
 
343
                                   name,
 
344
                                   path,
 
345
                                   <ADIOS_DATATYPES> type,
 
346
                                   value,
 
347
                                   var)
 
348
 
 
349
cpdef int select_method (int64_t group,
 
350
                         char * method,
 
351
                         char * parameters,
 
352
                         char * base_path):
 
353
    return adios_select_method (group,
 
354
                                method,
 
355
                                parameters,
 
356
                                base_path)
 
357
 
 
358
 
 
359
## ====================
 
360
## ADIOS Read API (V2)
 
361
## ====================
 
362
 
 
363
cdef type adios2nptype(ADIOS_DATATYPES t):
 
364
    cdef type ntype = None
 
365
    if t == adios_byte:
 
366
        ntype = np.int8
 
367
    elif t == adios_short:
 
368
        ntype = np.int16
 
369
    elif t == adios_integer:
 
370
        ntype = np.int32
 
371
    elif t == adios_long:
 
372
        ntype = np.int64
 
373
    elif t == adios_unsigned_byte:
 
374
        ntype = np.uint8
 
375
    elif t == adios_unsigned_short:
 
376
        ntype = np.uint16
 
377
    elif t == adios_unsigned_integer:
 
378
        ntype = np.uint32
 
379
    elif t == adios_unsigned_long:
 
380
        ntype = np.uint64
 
381
    elif t == adios_real:
 
382
        ntype = np.float32
 
383
    elif t == adios_double:
 
384
        ntype = np.float64
 
385
    elif t == adios_long_double:
 
386
        ntype = np.float128
 
387
    elif t == adios_complex:
 
388
        ntype = np.complex64
 
389
    elif t == adios_double_complex:
 
390
        ntype = np.complex128
 
391
    else:
 
392
        ntype = None
 
393
 
 
394
    return ntype
 
395
 
 
396
cdef printfile(ADIOS_FILE * f):
 
397
    print '%15s : %lu' % ('fh', f.fh)
 
398
    print '%15s : %d' % ('nvars', f.nvars)
 
399
    print '%15s : %s' % ('var_namelist', [f.var_namelist[i] for i in range(f.nvars)])
 
400
    print '%15s : %d' % ('nattrs', f.nattrs)
 
401
    print '%15s : %s' % ('attr_namelist', [f.attr_namelist[i] for i in range(f.nattrs)])
 
402
    print '%15s : %d' % ('current_step', f.current_step)       
 
403
    print '%15s : %d' % ('last_step', f.last_step)       
 
404
    print '%15s : %s' % ('path', f.path)
 
405
    print '%15s : %d' % ('endianness', f.endianness)       
 
406
    print '%15s : %d' % ('version', f.version)       
 
407
    print '%15s : %lu' % ('file_size', f.file_size)
 
408
 
 
409
cdef printvar(ADIOS_VARINFO * v):
 
410
    print '%15s : %d' % ('varid', v.varid)
 
411
    print '%15s : %s' % ('type', adios2nptype(v.type))
 
412
    print '%15s : %d' % ('ndim', v.ndim)
 
413
    print '%15s : %s' % ('dims', [v.dims[i] for i in range(v.ndim)])
 
414
    print '%15s : %d' % ('nsteps', v.nsteps)
 
415
 
 
416
## ====================
 
417
## ADIOS Class Definitions for Read
 
418
## ====================
 
419
 
 
420
""" Call adios_read_init_method """
 
421
cpdef read_init(ADIOS_READ_METHOD method = ADIOS_READ_METHOD_BP,
 
422
                MPI.Comm comm = MPI.COMM_WORLD,
 
423
                char * parameters = ""):
 
424
    return adios_read_init_method (method, comm.ob_mpi, parameters)
 
425
 
 
426
 
 
427
""" Call adios_read_finalize_method """
 
428
cpdef read_finalize(ADIOS_READ_METHOD method = ADIOS_READ_METHOD_BP):
 
429
    return adios_read_finalize_method (method)
 
430
 
 
431
""" Python class for ADIOS_FILE structure """
 
432
cdef class file:
 
433
    """ Private Memeber """
 
434
    cpdef ADIOS_FILE * fp
 
435
 
 
436
    """ Public Memeber """
 
437
    cpdef public bytes name
 
438
    cpdef public int nvars
 
439
    cpdef public int nattrs
 
440
    cpdef public int current_step
 
441
    cpdef public int last_step
 
442
    cpdef public int endianness
 
443
    cpdef public int version
 
444
    cpdef public int file_size
 
445
    
 
446
    cpdef public dict var
 
447
    cpdef public dict attr
 
448
 
 
449
    """ Initialization. Call adios_read_open and populate public members """
 
450
    def __init__(self, char * fname,
 
451
                 ADIOS_READ_METHOD method = ADIOS_READ_METHOD_BP,
 
452
                 MPI.Comm comm = MPI.COMM_WORLD):
 
453
        self.fp = NULL
 
454
        self.var = {}
 
455
        self.attr = {}
 
456
 
 
457
        self.fp = adios_read_open_file(fname, method, comm.ob_mpi)
 
458
        assert self.fp != NULL, 'Not an open file'
 
459
 
 
460
        self.name = fname.split('/')[-1]  ## basename
 
461
        self.nvars = self.fp.nvars
 
462
        self.nattrs = self.fp.nattrs
 
463
        self.current_step = self.fp.current_step
 
464
        self.last_step = self.fp.last_step
 
465
        self.endianness = self.fp.endianness  
 
466
        self.version = self.fp.version     
 
467
        self.file_size = self.fp.file_size   
 
468
    
 
469
        for varname in [self.fp.var_namelist[i] for i in range(self.nvars)]:
 
470
            self.var[varname] = var(self, varname)
 
471
 
 
472
    def __del__(self):
 
473
            self.close()
 
474
 
 
475
    """ Call adios_read_close """
 
476
    cpdef close(self):
 
477
        assert self.fp != NULL, 'Not an open file'
 
478
        adios_read_close(self.fp)
 
479
        self.fp = NULL
 
480
 
 
481
    """ Print self """
 
482
    cpdef printself(self):
 
483
        assert self.fp != NULL, 'Not an open file'
 
484
        print '=== AdiosFile ==='
 
485
        print '%15s : %lu' % ('fp', <unsigned long> self.fp)
 
486
        printfile(self.fp)
 
487
 
 
488
""" Python class for ADIOS_VARINFO structure """
 
489
cdef class var:
 
490
    """ Private Memeber """
 
491
    cdef file file
 
492
    cdef ADIOS_VARINFO * vp
 
493
 
 
494
    """ Public Memeber """
 
495
    cpdef public bytes name
 
496
    cpdef public int varid
 
497
    cpdef public type type
 
498
    cpdef public int ndim
 
499
    cpdef public tuple dims
 
500
    cpdef public int nsteps
 
501
 
 
502
    """ Initialization. Call adios_inq_var and populate public members """
 
503
    def __init__(self, file file, char * name):
 
504
        self.file = file
 
505
        self.vp = NULL
 
506
 
 
507
        assert self.file.fp != NULL, 'Not an open file'
 
508
        self.vp = adios_inq_var(self.file.fp, name)
 
509
        assert self.vp != NULL, 'Not a valid var'
 
510
 
 
511
        self.name = name
 
512
        self.varid = self.vp.varid                
 
513
        self.type = adios2nptype(self.vp.type)
 
514
        self.ndim = self.vp.ndim                 
 
515
        self.dims = tuple([self.vp.dims[i] for i in range(self.vp.ndim)])
 
516
        self.nsteps = self.vp.nsteps
 
517
        
 
518
    def __del__(self):
 
519
        self.close()
 
520
 
 
521
    """ Call adios_free_varinfo """
 
522
    cpdef close(self):
 
523
        assert self.vp != NULL, 'Not an open var'
 
524
        adios_free_varinfo(self.vp)
 
525
        self.vp = NULL
 
526
 
 
527
    """ Call adios_schedule_read and adios_perform_reads """
 
528
    cpdef read(self, tuple offset = (), tuple count = (), from_steps = 0, nsteps = 1):
 
529
        assert self.type is not None, 'Data type is not supported yet'
 
530
        assert from_steps + nsteps <= self.nsteps, 'Step index is out of range'
 
531
        
 
532
        cdef list lshape = [self.vp.dims[i] for i in range(self.vp.ndim)]
 
533
        cdef np.ndarray npshape = np.array(lshape, dtype=np.int64)
 
534
        
 
535
        cdef np.ndarray npoffset
 
536
        if len(offset) == 0:
 
537
            npoffset = npshape.copy()
 
538
            npoffset.fill(0)
 
539
        else:
 
540
            npoffset = np.array(offset, dtype=np.int64)
 
541
        
 
542
        cdef np.ndarray npcount
 
543
        if len(count) == 0:
 
544
            npcount = npshape - npoffset
 
545
        else:
 
546
            npcount = np.array(count, dtype=np.int64)
 
547
 
 
548
        assert npshape.ndim == npoffset.ndim, 'Offset dimension mismatch'
 
549
        assert npshape.ndim == npcount.ndim, 'Count dimension mismatch.'
 
550
        assert (npshape - npoffset >= npcount).all(), 'Count is larger than shape.'
 
551
 
 
552
        shape = list(npcount)
 
553
        if (nsteps > 1):
 
554
            shape.insert(0, nsteps)
 
555
        cdef np.ndarray var = np.zeros(shape, dtype=self.type)
 
556
 
 
557
        cdef ADIOS_SELECTION * sel
 
558
        sel = adios_selection_boundingbox (self.vp.ndim, <uint64_t *> npoffset.data, <uint64_t *> npcount.data)
 
559
        
 
560
        adios_schedule_read_byid (self.file.fp, sel, self.vp.varid, from_steps, nsteps, <void *> var.data)
 
561
        adios_perform_reads(self.file.fp, 1)
 
562
 
 
563
        return var
 
564
 
 
565
    """ Print self """
 
566
    cpdef printself(self):
 
567
        assert self.vp != NULL, 'Not an open variable'
 
568
        print '=== AdiosVariable ==='
 
569
        print '%15s : %lu' % ('vp', <unsigned long> self.vp)
 
570
        print '%15s : %lu' % ('fp', <unsigned long> self.file.fp)
 
571
        printvar(self.vp)
 
572
 
 
573
## ====================
 
574
## ADIOS Global functions
 
575
## ====================
 
576
 
 
577
""" Read data in a BP file and return as a numpy array """
 
578
def readvar(fname, varname):
 
579
    f = file(fname, comm=MPI.COMM_SELF)
 
580
    if not f.var.has_key(varname):
 
581
        print "No valid variable"
 
582
        return
 
583
 
 
584
    v = f.var[varname]
 
585
    return v.read(from_steps=0, nsteps=v.nsteps)
 
586
 
 
587
""" List attributes of a BP file """
 
588
def bpls(fname):
 
589
    f = file(fname, comm=MPI.COMM_SELF)
 
590
    return {'nvars': f.nvars,
 
591
            'nattrs': f.nattrs,
 
592
            'vars': tuple([ k for k in f.var.iterkeys() ]),
 
593
            'attrs': tuple([ k for k in f.attr.iterkeys() ]),
 
594
            'time_steps': (f.current_step, f.last_step),
 
595
            'file_size': f.file_size}
 
596