[visad] sampling error bugs

Hi,

I'm trying to use error estimation in VisAD. I'd still prefer to use FlatFields but the implemented error code won't give me an error for each data value. Instead, I am using a FieldImpl with a range of RealTuple[]. Internally, there are Real-s that will estimate errors for binary and unary operations. In the process of doing this, I've run into a few bugs. I've also proposed a couple of bug fixes.

1) Tuple.adjustSamplingError():
When resampleDouble() calls adjustSamplingError() on my RealTuple, it delegates to Tuple's adjustSamplingError() which creates a new Tuple. But since the range type is RealTupleType, the Tuple constructor requires that a RealTuple be constructed instead of a Tuple. I fixed this by overriding adjustSamplingError() in RealTuple:

  public Data adjustSamplingError(Data error, int error_mode)
         throws VisADException, RemoteException {
      if (isMissing() || error == null || error.isMissing()) return this;
      int n = getDimension();
      Real[] newComponents = new Real[n];
      for (int i=0; i<n; i++) {
          Real errorComponent = (Real) ((RealTuple) error).getComponent(i);
          newComponents[i] = (Real)
              getComponent(i).adjustSamplingError(errorComponent, error_mode);
      }
return new RealTuple(newComponents); }

2) Real.adjustSamplingError()
When calling adjustSamplingError() on a Real with no (i.e. null) ErrorEstimate, the method returns itself (with no error) instead of adding the contribution of the "error" argument. I modified the routine to use an error of 0 if the ErrorEstimate is null:

  public Data adjustSamplingError(Data error, int error_mode)
  throws VisADException, RemoteException {
      if (isMissing() || error == null || error.isMissing()) return this;
      int n = getDimension();
      Real[] newComponents = new Real[n];
      for (int i=0; i<n; i++) { >   public Data adjustSamplingError(Data error, 
int error_mode)
         throws VisADException, RemoteException {
    if (isMissing() || error == null || error.isMissing()) return this;
    double a = ((Real) error).getValue();
    double b = (Error == null) ? 0 : Error.getErrorValue();
    double e = (error_mode == INDEPENDENT) ? Math.sqrt(a * a + b * b) :
                                             Math.abs(a) + Math.abs(b);
    return new Real((RealType) Type, Value, unit,
                    new ErrorEstimate(Value, e, unit));
  }

3) FieldImpl.resampleDouble()
When estimating errors with WEIGHTED_AVERAGE sampling, the variable "coefs" is redefined as a new double array within a loop (near line 3272). It is not initialized but the code tries to access it in the next loop iteration with unhappy results. I didn't try to follow the logic to make a fix.

So, is it worth going down this path? It appears that the error estimation is not fully implemented, not to mention that the ErrorEstimate comments for unary and binary operation error estimates say that they are, "actually, more of a WAG than an estimate. These formulas are a bit of a hack and suggestions for improvements are welcome." As I've mentioned before, I'd like to see FlatField keep an error estimate for each range value and propagate them through the math and resampling operations. I figured if Real-s can do it, why not FlatFields. Is anyone using visad ErrorEstimate-s or have any thoughts on doing this?

Thanks,
Doug