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