Search code examples
bashnetcdfnetcdf4ncocdo-climate

converting float to byte in netcdf file


I have a large netcdf file with a standard float field which consists of only 0.0 or 1.0. I would like to convert it to a byte type from the command line to save some space and also make it easier to read in the array to a byte type in fortran.

I tried to use ncap

ncap -s 'fire=byte(fire)' CAMS_2003-2017_frp_mask2_africa_zip.nc test.nc

but it just seems to zero all the fields. I was using zip_6 netcdf4 compression on the source file, I'm not sure if that complicates matters?

UPDATE: I found that ncap2 works with byte

ncap2 -s 'fire=byte(fire)' CAMS_2003-2017_frp_mask2_africa_zip.nc test.nc

But I don't understand why the two differ? It may be a memory issue, as both ncap and ncap2 fail with memory allocation when trying to convert to "int" instead of "byte".


Solution

  • I found two ways of doing this:

    cdo -b I8 copy in.nc out.nc 
    

    or

    ncap2 -s 'fire=byte(fire)' in.nc out.nc