Re: Using setDefaultSet and clone()

Hi Martin-

I've got a fix for this which will be in the next VisAD
release.  If you are building VisAD from source, you can add
the following line in the FlatField.clone() method:

         clone.DoubleRange = new double[TupleDimension][];

just before:

         clone.packValues(unpackValues(true), false);

so it looks like:

.....
    synchronized(DoubleRange) {
      if (!MissingFlag) {
        try {
          clone.DoubleRange = new double[TupleDimension][];
          clone.packValues(unpackValues(true), false);
        }
        catch (VisADException ex) {
          throw new RuntimeException(ex.toString());
        }
.....

Thanks for finding this bug!

Don


Don Murray wrote:
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.

Don
Regards,
Martin

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;
           myField1.setSamples(values);
           values[0][0] = 130;
           values[0][1] = 180;

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

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

*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
http://www.unidata.ucar.edu/staff/donm
*************************************************************


==============================================================================
To unsubscribe visad, visit:
http://www.unidata.ucar.edu/mailing-list-delete-form.html
==============================================================================