[netcdf-java] Mimicking ncks output with Java

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@xxxxxxxx


  • 2008 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdf-java archives: