[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 20031118: netcdf data problem (fwd)



>To: <address@hidden>
>From: "Birgit Heese" <address@hidden>
>Subject: netcdf data problem
>Organization: MIM
>Keywords: 200311171529.hAHFT5Ob010057

Hi Birgit,

> Did you find any solution to the problem when changing a value
> in a netCDF file it grows to the nearest multiple of 4096 with extra zero
> bytes?
> I am still wasting my harddisk space.

I'm still investigating how best to fix the problem, but I have a
workaround that may help until we have a real fix.  If you open the
file with the NC_SHARE flag set, the file doesn't grow to 4096 bytes.

   (void) nc_open("file.nc", NC_WRITE|NC_SHARE, &ncid);

Also, if you know the correct size of the netCDF files that were
written without this flag set, you can shrink them to that size from
4096 bytes, to save space, using the appended "slice" program.  For
example, if you have a file named "file.nc" that is 4096 bytes, but you
know it really should be 1234 bytes and all the extra bytes are merely
nulls added by this netCDF bug, you could use the appended "slice"
program to fix this on a Unix system by invoking the commands:

  $ mv file.nc file.nc.orig
  $ slice 0 1234 file.nc.orig > file.nc

then when you are sure this is OK, remove the original file:

  $ rm file.nc.orig

--Russ

#include <stdio.h>

/* copy argv[2] bytes to stdout from argv[3], starting at byte argv[1] */
main(argc, argv)
     int argc;
     char *argv[];
{
    long i;
    FILE *fp;
    long start;
    long num;
    int c;

    if (argc != 4) {
        fprintf(stderr, "wrong number of arguments: %d\n", argc);
        fprintf(stderr, "usage:\t%s start_byte num_bytes file > out\n",argv[0]);
        return 2;
    }

    i = 0;
    start = strtol(argv[1],(char **) NULL, 10);
    num = strtol(argv[2],(char **) NULL, 10);
    fp = fopen(argv[3], "r");
    if (fp == NULL) {
        
        perror("can't open file");
        return 1;
    }
    fseek(fp, start, 0);
    while ((c = getc(fp)) != EOF && i++ < num) {
        putchar(c);
    }
    return 0;
}