[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Greetings



Hi Scottie,

I've appended a complete working example of using the copyin method
and the DecimateMap to interlace x and y values into a MultiArray
that's twice as big.  For your application, just use the netCDF x
variable (a MultiArray) in p[lace of xma and similarly for yma.  Let
me know if you have questions about this.  I'm CC:ing Glenn in case he
knows an easier way ...

The example is just the small amount of stuff in main.  The rest is
just copied from the other examples to print out the resulting
MultiArray, and can be ignored.

--Russ

package ucar.demo;

import java.io.IOException;
import ucar.multiarray.*;

/**
 * Simple example program demonstrating interlacing two MultiArrays
 * into another MultiArray.
 *
 * @author Russ Rew
 * @version $Id: Interlace.java,v 1.2 1998/05/20 16:18:31 russ Exp $ */
public class Interlace {

    public static void main (String[] args)
        throws java.io.IOException{ // no I/O here, so this won't really happen

        /* Start with two 1-dimensional MultiArrays for x and y values of 
length n */
        double[] xx = new double[] {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 
8.0, 9.0};
        double[] yy = new double[] {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 
8.5, 9.5};
        int nn = xx.length;

        MultiArray xma = new ArrayMultiArray(xx);
        MultiArray yma = new ArrayMultiArray(yy);

        /* Create a new 2*nn element MultiArray for interlaced values. */
        MultiArray pts = new ArrayMultiArray(double.class, new int[] {2 * nn} );

                                // first copyin the x values in every other 
position
        boolean[] xPlaces = new boolean[] {true, false};
        IndexMap evenDMap = new DecimateMap(0, xPlaces);
        MultiArray xpts = new MultiArrayProxy(pts, evenDMap);
        xpts.copyin(new int[] {0}, xma);

                                // then copyin the y values in remaining 
positions
        boolean[] yPlaces = new boolean[] {false, true};
        IndexMap oddDMap = new DecimateMap(0, yPlaces);
        MultiArray ypts = new MultiArrayProxy(pts, oddDMap );
        ypts.copyin(new int[] {0}, yma);
        System.out.println("pts = " + MultiArrayToString(pts));
    }

    public static String
    MultiArrayToString(MultiArray ma) {
        StringBuffer buf = new StringBuffer(
            ((Object)ma).toString() + " (" + 
            ma.getComponentType() + ", " +
            "[" );
        int[] shape = ma.getLengths();
        int rank = ma.getRank();
        for(int i=0; i < rank - 1; i++) {
            buf.append(shape[i] + ", ");
        }
        if (rank > 0) {
            buf.append(shape[rank - 1]);
        }
        try {
            buf.append("])" +
                       MultiArrayToStringHelper(ma, new IndentLevel()));
        } catch (java.io.IOException e) {
            e.printStackTrace();
        }
        return buf.toString();
    }

    /**
     * Maintains indentation level for printing nested structures.
     */
    static class IndentLevel {
        private int level = 0;
        private int indentation;
        private StringBuffer indent;
        private StringBuffer blanks;

        public IndentLevel() {
            this(4);
        }
    
        public IndentLevel(int indentation) {
            if (indentation > 0)
                this.indentation = indentation;
            indent = new StringBuffer();
            blanks = new StringBuffer();
            for (int i=0; i < indentation; i++)
                blanks.append(" ");
        }

        public void incr() {
            level += indentation;
            indent.append(blanks);
        }

        public void decr() {
            level -= indentation;
            indent.setLength(level);
        }
    
        public String getIndent() {
            return indent.toString();
        }
    }

    private static String
    MultiArrayToStringHelper(MultiArray ma, IndentLevel ilev)
        throws java.io.IOException{ // no I/O here, so this won't really happen
        
        final int rank = ma.getRank();
        if (rank == 0) {
            try {
                return ma.get((int[])null).toString();
            } catch (IOException ee) {
            }
        }
        StringBuffer buf = new StringBuffer();
        buf.append("\n" + ilev.getIndent() + "{");
        ilev.incr();
        final int [] dims = ma.getLengths();
        final int last = dims[0];
        for(int ii = 0; ii < last; ii++)
        {
            final MultiArray inner =
                new MultiArrayProxy(ma, new SliceMap(0, ii));
            buf.append(MultiArrayToStringHelper(inner, ilev));
            if(ii != last - 1)
                buf.append(", ");
        }
        ilev.decr();
        if (rank > 1) {
            buf.append("\n" + ilev.getIndent());
        }
        buf.append("}");
        
        return buf.toString();
    }    
}