Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem writing big-endian data to existing file #1802

Open
neishm opened this issue Aug 4, 2020 · 2 comments
Open

Problem writing big-endian data to existing file #1802

neishm opened this issue Aug 4, 2020 · 2 comments
Assignees
Milestone

Comments

@neishm
Copy link

neishm commented Aug 4, 2020

Writing big-endian data into an existing netCDF4 (on a little-endian system) does not seem to properly swap the bytes.

The following example from Unidata/netcdf4-python#1033 (comment) triggers the problem:

#include <netcdf.h>
int main() {
    int i, iret, dimid, varid, ncid;
    int data[10];
    for (i = 0; i < 10; i++)
        data[i] = i;
    iret = nc_create("test.nc", NC_NETCDF4, &ncid);
    iret = nc_def_dim(ncid, "x", 10, &dimid);
    iret = nc_def_var(ncid, "v", NC_INT, 1, &dimid, &varid);
    iret = nc_def_var_endian(ncid, varid, NC_ENDIAN_BIG);
    /*iret = nc_put_var_int(ncid, varid, data);*/
    iret = nc_close(ncid);
    iret = nc_open("test.nc", NC_WRITE, &ncid);
    iret = nc_put_var_int(ncid, varid, data);
    iret = nc_close(ncid);
}

The resulting file has the data:

 v = 0, 16777216, 33554432, 50331648, 67108864, 83886080, 100663296, 
    117440512, 134217728, 150994944 ;

where the values should be 0,1,2,3,4,5,6,7,8,9. This was tested with netcdf version 4.7.3 on an Ubuntu 20.04 system, and also the latest development snapshot (b9bb44f) was tested with the same result.

If the intermediate nc_close and nc_open calls are commented out (i.e. still writing into a new file), then the data is written correctly.

@WardF WardF self-assigned this Aug 27, 2020
@WardF WardF added this to the 4.8.0 milestone Aug 27, 2020
@WardF WardF pinned this issue Aug 30, 2020
@WardF
Copy link
Member

WardF commented Aug 30, 2020

So recreating this, I am relieved that it doesn't appear the issue is storing the data, but rather in how ncdump is printing it. I am investigating further.

@WardF
Copy link
Member

WardF commented Sep 8, 2020

Ok, still sorting through this, making a comment so it does not shuffle down the stack. I'm seeing largely consistent behavior between BE and LE systems, but I have found some differences. I need to sort out the intent (endianness in memory vs. in storage) so I know which behavior is expected. Work is ongoing (on this and so many other things O_o).

@WardF WardF modified the milestones: 4.8.0, 4.8.2 Aug 30, 2021
@WardF WardF modified the milestones: 4.8.2, 4.9.1 Jun 15, 2022
@WardF WardF modified the milestones: 4.9.1, 4.9.2 Feb 13, 2023
@WardF WardF modified the milestones: 4.9.2, 4.9.3 May 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants