[netcdf-java] Mimicking ncks output with Java

Kyle Wilcox Kyle.Wilcox at noaa.gov
Thu Mar 20 14:50:40 MDT 2008


Ran into issues with exacting the NC_FLOAT variables output by ncks with 
Java floats.  Thought I would share my experience. If anyone has a 
better way of doing this, please do share.

The issue stems from the fact that Java stores float variables in binary 
and some values (like 0.2) can't be represented in binary.  ncks always 
returns floats with 6 decimal places.

ncks -s '%f' -v VARIABLE NETCDF_FILE

I got an equivalent output in Java doing the following:

import java.math.BigDecimal;
...
float var = ... // (Code to get variable into a Java float excluded)
BigDecimal big_var = new BigDecimal(var);
String ncks_formated_var = big_var.setScale(6,6).toString();

-- 

  Kyle Wilcox
  NOAA Chesapeake Bay Office
  410 Severn Avenue
  Suite 107A
  Annapolis, MD 21403
  office: (410) 295-3151
  Kyle.Wilcox at noaa.gov


More information about the netcdf-java mailing list