convert all the variables and dimensions from int/float to double in netcdf file

无人久伴 提交于 2021-02-10 22:30:50

问题


I have a netCDF file with the foll. ncdump:

netcdf nc_data {
dimensions:
    lon = 1440 ;
    bounds = 2 ;
    lat = 720 ;
    time = UNLIMITED ; // (6 currently)
variables:
    double lon_bounds(lon, bounds) ;
    double lat_bounds(lat, bounds) ;
    int time(time) ;
        time:units = "year as %Y.%f" ;
        time:calendar = "proleptic_gregorian" ;
        time:long_name = "time" ;
        time:standard_name = "time" ;
        time:axis = "T" ;
    float lat(lat) ;
        lat:units = "degrees_north" ;
        lat:long_name = "latitude" ;
        lat:standard_name = "latitude" ;
        lat:axis = "Y" ;
        lat:bounds = "lat_bounds" ;
    float lon(lon) ;
        lon:units = "degrees_east" ;
        lon:long_name = "longitude" ;
        lon:standard_name = "longitude" ;
        lon:axis = "X" ;
        lon:bounds = "lon_bounds" ;
    float valf(time, lat, lon) ;
        valf: standard_name = “area_fraction”
        valf:missing_value = -9999.f ;
        valf:_fillvalue = -9999.f ;
        valf:long_name = "val fraction" ;
        valf:units = "fraction of grid cell" ;
}

Is there a nco command to convert all the variables and dimensions fro int/float to double?

I found this:

ncap2 -s 'valf=double(valf)' nc_data.nc nc_data.nc

However, this only works one variable at a time and I have ~30 variables in my file


回答1:


ncap2 can do this elegantly, though it requires the current snapshot (that will shortly become 4.6.0). Note that the command you used above does not change the type of the variable in the output file since NCO version 4.5.3, and the current snapshot does allow it to work. (Interim versions of ncap2 require that the output variable have a different name than the input in order to change its type, e.g., val_dbl=double(valf).

Importantly, the current snapshot of ncap2 understands variable lists. So you could create an output file of certain variables with altered names with this script:

@all=get_vars_in();
*sz=@all.size();

for(idx=0;idx<sz;idx++){

  @var_nm=sprint(@all(idx));

  if( *@var_nm.type() == NC_INT || *@var_nm.type() == NC_FLOAT )
   *@var_nm=*@var_nm.double();

}

No renaming of variables is required.



来源:https://stackoverflow.com/questions/36749825/convert-all-the-variables-and-dimensions-from-int-float-to-double-in-netcdf-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!