Uncategorized

Python/C api hdf5 doesnt write all array elements just the first one


I’ve confirmed that the data types in my code match, and they do match. However, when I execute the code, only the first element of the array gets stored in the HDF5 file instead of all the elements. I’ve attempted to resolve this by changing the data types, but I’m still unable to figure out what’s causing this issue. Below is the code I’m using to write to the HDF5 file.

void *write_to_hdf5(void *arg)
{
    PyObject *py_args = (PyObject *)arg;
    PyArrayObject *py_data;
    char *filename;
    char *dataset_name;

    // Parse the arguments tuple
    if (!PyArg_ParseTuple(py_args, "Oss", &py_data, &filename, &dataset_name))
    {
        PyErr_SetString(PyExc_TypeError, "Expected a tuple (data, filename, dataset_name)");
        pthread_exit(NULL);
    }
    
    printf("Received data for writing\n");
    
    printf("Type of py_data: %d\n", PyArray_TYPE(py_data));
    
    printf("Array data: ");
    for (int i = 0; i < PyArray_SIZE(py_data); ++i)
    {
        printf("%lld ", (long long)((long long *)PyArray_DATA(py_data))[i]);
    }
    printf("\n");
    // Open file for writing
    file_id = H5Fopen(filename, H5F_ACC_RDWR, H5P_DEFAULT);
    if (file_id < 0)
    {
        // File doesn't exist, so create it
        file_id = H5Fcreate(filename, H5F_ACC_TRUNC | H5F_ACC_SWMR_WRITE, H5P_DEFAULT, H5P_DEFAULT);
        if (file_id < 0)
        {
            PyErr_SetString(PyExc_RuntimeError, "Failed to create file");
            pthread_exit(NULL);
        }
        printf("Created new file: %s\n", filename);
    }
    
    // Check if dataset exists
    dataset_id = H5Dopen2(file_id, dataset_name, H5P_DEFAULT);
    if (dataset_id < 0)
    {
        // Create dataset if it doesn't exist
        hid_t dataspace = H5Screate(H5S_SCALAR); // Create dataspace
        hid_t datatype = H5T_NATIVE_LLONG;
        dataset_id = H5Dcreate2(file_id, dataset_name, datatype, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
        if (dataset_id < 0)
        {
            PyErr_SetString(PyExc_RuntimeError, "Failed to create dataset");
            H5Sclose(dataspace);
            H5Fclose(file_id);
            pthread_exit(NULL);
        }
        H5Sclose(dataspace);
        printf("Created new dataset: %s\n", dataset_name);
    }
    
    // Write data to the dataset
    hsize_t dims[1];                                                        
    dims[0] = PyArray_DIM(py_data, 0);                                       
    printf("Number of elements in py_data: %lld\n", (long long int)dims[0]); // Debug statement
    
    hid_t dataspace = H5Dget_space(dataset_id);
    printf("Dataspace dimensions before resizing: %lld\n", (long long int)H5Sget_simple_extent_npoints(dataspace)); // Debug statement
    
    H5Sset_extent_simple(dataspace, 1, dims, NULL); // Resize dataspace to match data size
    
    printf("Dataspace dimensions after resizing: %lld\n", (long long int)H5Sget_simple_extent_npoints(dataspace)); 
    
    hid_t datatype = H5T_NATIVE_LLONG;
    
    printf("Writing data to dataset...\n"); // Debug statement
    if (H5Dwrite(dataset_id, datatype, H5S_ALL, dataspace, H5P_DEFAULT, PyArray_DATA(py_data)) < 0)
    {
        PyErr_SetString(PyExc_RuntimeError, "Failed to write data to dataset");
        H5Sclose(dataspace);
        H5Dclose(dataset_id);
        H5Fclose(file_id);
        pthread_exit(NULL);
    }
    printf("Data successfully written to dataset\n"); 
    
    // Close dataset
    H5Sclose(dataspace);
    H5Dclose(dataset_id);
    
    // Close the file
    H5Fclose(file_id);
    
    pthread_exit(NULL);

}

my test code

import h5tome import numpy as np import time
   
   def test_async_write_and_read(): # Sample data to write data_to_write = np.array([1, 2, 3, 4, 5], dtype=np.int64)
print("Data to write:", data_to_write)  
   filename = "test.h5"
   dataset_name = "data"
   
   # Start asynchronous write operation
   h5tome.start_async_write(data_to_write, filename, dataset_name)
   print("Async write operation started")
   test_async_write_and_read()```



checked using HDF view store only 1 array



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *