Skip to content

float32 write not supported in python api #331

@wmles

Description

@wmles

The 3_write_serial.py-example works with float64, and it seems, that float32 is not supported. The traceback from the exception is:

RuntimeError Traceback (most recent call last)
in ()
42 # TODO implement slicing protocol
43 # E[offset[0]:extent[0], offset[1]:extent[1]] = global_data
---> 44 rho.store_chunk(offset, extent, global_data)
45 print("Stored the whole Dataset contents as a single chunk, " +
46 "ready to write content")

RuntimeError: Datatype 'float32' not known in 'store_chunk'!

to reproduce, paste the following modified example in an ipython shell

    import openPMD
    import numpy as np
    
    
    if __name__ == "__main__":
        # user input: size of matrix to write, default 3x3
        size = 3
    
        # matrix dataset to write with values 0...size*size-1
        global_data = np.arange(size*size, dtype=np.float32)
    
        print("Set up a 2D square array ({0}x{1}) that will be written".format(
            size, size))
    
        # open file for writing
        series = openPMD.Series(
            "../samples/3_write_serial_py.h5",
            openPMD.Access_Type.create
        )
    
        print("Created an empty {0} Series".format(series.iteration_encoding))
    
        print(len(series.iterations))
        rho = series.iterations[1]. \
            meshes["rho"][openPMD.Mesh_Record_Component.SCALAR]
    
        datatype = openPMD.Datatype.FLOAT
        # datatype = openPMD.determineDatatype(global_data)
        extent = [size, size]
        dataset = openPMD.Dataset(datatype, extent)
    
        print("Created a Dataset of size {0}x{1} and Datatype {2}".format(
            dataset.extent[0], dataset.extent[1], dataset.dtype))
    
        rho.reset_dataset(dataset)
        print("Set the dataset properties for the scalar field rho in iteration 1")
    
        # writing fails on already open file error
        series.flush()
        print("File structure has been written")
    
        offset = [0, 0]
        # TODO implement slicing protocol
        # E[offset[0]:extent[0], offset[1]:extent[1]] = global_data
        rho.store_chunk(offset, extent, global_data)
        print("Stored the whole Dataset contents as a single chunk, " +
              "ready to write content")
    
        series.flush()
    print("Dataset content has been fully written")

...where I only changed the dtype=np.double to dtype=np.float32 and the openPMD.Datatype.DOUBLE to openPMD.Datatype.FLOAT
Where the np.float32 is equivalent to the aliases np.single, "f", and maybe more.

The numpy documentation makes no distinction between float32 and float64 regarding the compatibility to C types. Might be just an easy to fix bug.

Software Environment:
openpmd-api 0.4.0a installed via conda 4.5.2 on piz-daint (SUSE Linux Enterprise Server 12 SP2) for python 3.6.3 working with h5py 2.7.0 (how do i get hdf5-version? :))

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions