在netcdf文件中将float转换为byte

时间:2017-07-26 16:25:54

标签: bash netcdf netcdf4 nco cdo-climate

我有一个带有标准浮点字段的大型netcdf文件,该字段仅包含0.0或1.0。我想从命令行将其转换为字节类型以节省一些空间,并且还使得更容易在数组中读取到fortran中的字节类型。

我尝试使用ncap

// Example program
#include <iostream>
#include <string>
#include <typeinfo>

struct spString
{
    template <class... T>
    spString format(T...) { return spString(); }
    const char* c_str() { return nullptr; }

    spString operator+(spString) { return spString(); }
    spString operator+(const char*) { return spString(); }
};

struct debuggable
{
    spString getDebugString() { return spString(); }
};

void fromFloat(spString, float&) {}
void fromInt(spString, int&) {}


template <class T> inline auto from( T v )
    -> decltype( v.getDebugString(), spString() )
{
    return v.getDebugString();
}
template <class T> inline auto from( T v )
    -> decltype( v->getDebugString(), spString() )
{
    spString r;
    r.format( "%u (%s)", (size_t) v, v->getDebugString().c_str() );
    return  r;
}
template <class T> inline spString from( T v )
{
    return spString("(") + typeid(T).name() + " instance)"; 
}
template <> inline spString from( float _v          ) { spString _d;  fromFloat         ( _d, _v ); return _d; }
template <> inline spString from( int _v            ) { spString _d;  fromInt           ( _d, _v ); return _d; }
//other base types

int main()
{
    debuggable x{};

    from(0);
    from(0.f);
    from(x);
}

但它似乎只是将所有字段归零。我在源文件上使用zip_6 netcdf4压缩,我不确定这是否复杂?

更新:我发现ncap2与byte

一起工作
ncap -s 'fire=byte(fire)' CAMS_2003-2017_frp_mask2_africa_zip.nc test.nc

但我不明白为什么两者不同?这可能是一个内存问题,因为在尝试转换为&#34; int&#34;时,ncap和nca​​p2都会因内存分配而失败。而不是&#34;字节&#34;。

1 个答案:

答案 0 :(得分:2)

我找到了两种方法:

cdo -b I8 copy in.nc out.nc 

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