NOTICE: This version of the NSF Unidata web site (archive.unidata.ucar.edu) is no longer being updated.
Current content can be found at unidata.ucar.edu.

To learn about what's going on, see About the Archive Site.

Re: Units Conversion

Hi Cameron,

> I have field with the function type (time -> (Temperature, Precipatation))
> Temperature is loaded in celcius, and Precipitation is in millimeters.
> I have tried experimenting with adding two fields of this type together and
> have found that the Temperature gets converted to Kelvin.
> Is it possible to convert all the Temperatures in the result field to Celcius
> again? and how do I go about it?

The conversion to Kelvin happens because Units with offsets,
such as Celcius, are dangerous.  For example,

  0 C + 0 C = 0 C != 546 K = 273 K + 273 K

To change your FlatField back to Celcius, you need to
explicitly construct a new FlatField, sort of like:

  FlatField field = ... // this is your old field, with Kelvin

  Unit[][] long_units = field.getRangeUnits();
  Unit[] units = new Unit[long_units.length];
  for (int i=0; i<long_units.length; i++) units[i] = long_units[i][0];
  Unit[] new_units = Unit.copyUnitArray(units);
  new_units[0] = celcius; // your Celcius Unit

  FlatField new_field
    new FlatField((FunctionType) field.getType(),
                  field.getDomainSet(),
                  null, null, // I assume you're not using 
RangeCoordinateSystems
                  field.getRangeSets(),
                  new_units);
  float[][] values = field.getFloats();
  values = Unit.convertTuple(values, units, new_units);
  new_field.setSamples(values);

Let me know if you have any problems with this.

Cheers,
Bill
----------------------------------------------------------
Bill Hibbard, SSEC, 1225 W. Dayton St., Madison, WI  53706
hibbard@xxxxxxxxxxxxxxxxx  608-263-4427  fax: 608-263-6738
http://www.ssec.wisc.edu/~billh/vis.html

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