Re: Using setDefaultSet and clone()

Hi Martin-

martin schweitzer wrote:

In an application here, we recently found that our main FlatField is
storing data as floats rather than doubles.  At first we tried changing
the FlatField to a FieldImpl, however that produced problems with
DirectManipulationRenderer, so another approach was tried.  We then
tried calling setDefaultSet() on our RealTypes, which worked in all
ways except our data cloning now appears to not give us a copy as it
used to.

We were surprised that calling setDefaultSet() appears to change
the behaviour of FieldImpl.clone()

I'm surprised too!  Especially since you are calling copy = true
on your setSamples (the default).

When we run the program below, we get a 'deep copy' when using clone. If we
uncomment the following two lines, we get a 'shallow copy':

           // lat.setDefaultSet(new DoubleSet(lat));
           // lon.setDefaultSet(new DoubleSet(lon));

Any feedback or suggestions will be welcome.

First, I'd suggest that instead of calling the above, you
use the RangeSet argument of the FlatField ctor to change the
type on the fly.  That way you don't change the default set
for lat and lon RealTypes for other uses:

    Field myField1 = new FlatField(func, domain, null, null, new Set[] {
              new DoubleSet(lat), new DoubleSet(lon)}, null);

However, the problem you are seeing stems from the way the
backing DoubleRange is not recreated as it is for the FloatRange.
So, the two FlatFields hold the same backing store array and
when you call setSamples on the second field, you end up modifying
the backing store of the first since the DoubleRange is shared.

I'll need others to help figure out a work around for this and
then we'll get back to you.


Here is the program:

import visad.*;

public class FlatFieldTest
   public static void main(String[] args)
       try {
           RealType lat = RealType.getRealType("Latitude");
           RealType lon = RealType.getRealType("Longitude");
           RealTupleType range = new RealTupleType(lat, lon);
           RealType time = RealType.getRealType("time");
*            // If the following two lines are commented out, then
           // clone works...
           // lat.setDefaultSet(new DoubleSet(lat));
           // lon.setDefaultSet(new DoubleSet(lon));
           double samples[][] = {{1, 2}};
Set domain = new Gridded1DDoubleSet(RealType.getRealType("time"),
samples, 2);
           FunctionType func = new FunctionType(time, range);
           Field myField1 = new FlatField(func, domain);

           double[][] values = new double[2][2];
           values[0][0] = 100;
           values[0][1] = 200;
           values[0][0] = 130;
           values[0][1] = 180;

           Field myField2 = (FieldImpl)((FieldImpl)myField1).clone();

           System.out.println("ff1" + myField1);
           System.out.println("ff1" + myField2);
       } catch (Exception e) {

*Output when the lines are commented out:*
ff1: FlatField
   (time -> (Latitude, Longitude))
(100.0, 0.0), (200.0, 0.0)

ff2: FlatField
   (time -> (Latitude, Longitude))
(130.0, 0.0), (180.0, 0.0)

*Output when the lines are uncommented:*
ff1: FlatField
   (time -> (Latitude, Longitude))
(130.0, 0.0), (180.0, 0.0)

ff2: FlatField
   (time -> (Latitude, Longitude))
(130.0, 0.0), (180.0, 0.0)

Don Murray                               UCAR Unidata Program
dmurray@xxxxxxxxxxxxxxxx                        P.O. Box 3000
(303) 497-8628                              Boulder, CO 80307

To unsubscribe visad, visit: