Package ucar.ma2

Class ArrayStructureMA

  • All Implemented Interfaces:
    Iterable<StructureData>

    public class ArrayStructureMA
    extends ArrayStructure
    Concrete implementation of ArrayStructure, data storage is in member arrays, which are converted to StructureData member data on the fly. This defers object creation for efficiency. Use getJavaArrayXXX() and getScalarXXX() data accessors if possible. How to create:
     ArrayStructureMA asma = new ArrayStructureMA(smembers, getShape());
     for (int i = 0; i < orgVariables.size(); i++) {
       Variable v = (Variable) orgVariables.get(i);
       Array data = v.read();
       asma.setMemberArray(v.getName(), data);
     }
     
    How to do Nested Structures:
      Structure {
        float f1;
        short f2(3);
    
        Structure {
          int g1;
          double(2) g2;
          double(3,4) g3;
    
          Structure {
            int h1;
            double(2) h2;
          } nested2(7);
    
        } nested1(12);
      } s(4);
     
    • For f1, you need an ArrayFloat of shape {4}
    • For f2, you need an ArrayShort of shape {4, 3} .
    • For nested1, you need an ArrayStructure of shape {4, 12}. Use an ArrayStructureMA that has 3 members:
      • For g1, you need an ArrayInt of shape (4, 12}
      • For g2, you need an ArrayDouble of shape {4, 12, 2}.
      • For g3, you need an ArrayDouble of shape {4, 12, 3, 4}.
    • For nested2, you need an ArrayStructure of shape {4, 12, 7}. Use an ArrayStructureMA that has 2 members:
      • For h1, you need an ArrayInt of shape (4, 12, 7}
      • For h2, you need an ArrayDouble of shape {4, 12, 7, 2}.
    Example code:
     public void testMA() throws IOException, InvalidRangeException {
       StructureMembers members = new StructureMembers("s");
     
       StructureMembers.Member m = members.addMember("f1", "desc", CDM.UNITS, DataType.FLOAT, new int[] {1});
       Array data = Array.factory(DataType.FLOAT, new int[] {4});
       m.setDataArray(data);
       fill(data);
     
       m = members.addMember("f2", "desc", CDM.UNITS, DataType.SHORT, new int[] {3});
       data = Array.factory(DataType.SHORT, new int[] {4, 3});
       m.setDataArray(data);
       fill(data);
     
       m = members.addMember("nested1", "desc", CDM.UNITS, DataType.STRUCTURE, new int[] {9});
       data = makeNested1(m);
       m.setDataArray(data);
     
       ArrayStructureMA as = new ArrayStructureMA(members, new int[] {4});
       new TestStructureArray().testArrayStructure(as);
     
       // get f2 out of the 3nd "s"
       StructureMembers.Member f2 = as.getStructureMembers().findMember("f2");
       short[] f2data = as.getJavaArrayShort(2, f2);
       assert f2data[0] == 20;
       assert f2data[1] == 21;
       assert f2data[2] == 22;
     
       // get nested1 out of the 3nd "s"
       StructureMembers.Member nested1 = as.getStructureMembers().findMember("nested1");
       ArrayStructure nested1Data = as.getArrayStructure(2, nested1);
     
       // get g1 out of the 7th "nested1"
       StructureMembers.Member g1 = nested1Data.getStructureMembers().findMember("g1");
       int g1data = nested1Data.getScalarInt(6, g1);
       assert g1data == 26;
     
       // get nested2 out of the 7th "nested1"
       StructureMembers.Member nested2 = nested1Data.getStructureMembers().findMember("nested2");
       ArrayStructure nested2Data = nested1Data.getArrayStructure(6, nested2);
     
       // get h1 out of the 4th "nested2"
       StructureMembers.Member h1 = nested2Data.getStructureMembers().findMember("h1");
       int val = nested2Data.getScalarInt(4, h1);
       assert (val == 264);
     }
     
     
     public ArrayStructure makeNested1(StructureMembers.Member parent) throws IOException, InvalidRangeException {
       StructureMembers members = new StructureMembers(parent.getName());
       parent.setStructureMembers(members);
     
       StructureMembers.Member m = members.addMember("g1", "desc", CDM.UNITS, DataType.INT, new int[] {1});
       Array data = Array.factory(DataType.INT, new int[] {4, 9});
       m.setDataArray(data);
       fill(data);
     
       m = members.addMember("g2", "desc", CDM.UNITS, DataType.DOUBLE, new int[] {2});
       data = Array.factory(DataType.DOUBLE, new int[] {4, 9, 2});
       m.setDataArray(data);
       fill(data);
     
       m = members.addMember("g3", "desc", CDM.UNITS, DataType.DOUBLE, new int[] {3, 4});
       data = Array.factory(DataType.DOUBLE, new int[] {4, 9, 3, 4});
       m.setDataArray(data);
       fill(data);
     
       m = members.addMember("nested2", "desc", CDM.UNITS, DataType.STRUCTURE, new int[] {7});
       data = makeNested2(m);
       m.setDataArray(data);
     
       return new ArrayStructureMA(members, new int[] {4, 9});
     }
     
     public ArrayStructure makeNested2(StructureMembers.Member parent) throws IOException, InvalidRangeException {
       StructureMembers members = new StructureMembers(parent.getName());
       parent.setStructureMembers(members);
     
       StructureMembers.Member m = members.addMember("h1", "desc", CDM.UNITS, DataType.INT, new int[] {1});
       Array data = Array.factory(DataType.INT, new int[] {4, 9, 7});
       m.setDataArray(data);
       fill(data);
     
       m = members.addMember("h2", "desc", CDM.UNITS, DataType.DOUBLE, new int[] {2});
       data = Array.factory(DataType.DOUBLE, new int[] {4, 9, 7, 2});
       m.setDataArray(data);
       fill(data);
     
       return new ArrayStructureMA(members, new int[] {4, 9, 7});
     }
     
    See Also:
    Array
    • Constructor Detail

      • ArrayStructureMA

        public ArrayStructureMA​(StructureMembers members,
                                int[] shape)
        Create a new Array of type StructureData and the given members and shape.

        You must set the data Arrays on each of the Members, using setDataObject(). These data Arrays contain the data for that member Variable, for all the StructureData. Therefore it has rank one greater that the Members. The extra dimension must be the outermost (slowest varying) dimension. ie, if some member has shape [3,10], the array would have shape [nrows, 3, 10].

        Parameters:
        members - a description of the structure members
        shape - the shape of the Array.
    • Method Detail

      • factoryMA

        public static ArrayStructureMA factoryMA​(ArrayStructure from)
                                          throws IOException
        Turn any ArrayStructure into a ArrayStructureMA
        Parameters:
        from - copy from here. If from is a ArrayStructureMA, return it.
        Returns:
        equivalent ArrayStructureMA
        Throws:
        IOException - on error reading a sequence
      • setMemberArray

        public void setMemberArray​(String memberName,
                                   Array data)
        Set the data array for this member.
        Parameters:
        memberName - name of member
        data - Array for this member.
      • getArray

        public Array getArray​(int recno,
                              StructureMembers.Member m)
        Description copied from class: ArrayStructure
        Get member data of any type for a specific record as an Array. This may avoid the overhead of creating the StructureData object, but is equivilent to getStructure(recno).getArray( Member m).
        Overrides:
        getArray in class ArrayStructure
        Parameters:
        recno - get data from the recnum-th StructureData of the ArrayStructure. Must be less than getSize();
        m - get data from this StructureMembers.Member.
        Returns:
        Array values.
      • factoryMA

        public static ArrayStructureMA factoryMA​(Structure from,
                                                 int[] shape)
        Create an ArrayStructure for a Structure. Allow nested Structures. Create the data arrays, and an iterator.
        Parameters:
        from - copy from here. If from is a ArrayStructureMA, return it.
        shape - the shape of the ArrayStructure
        Returns:
        equivilent ArrayStructureMA