bufr/src/main/java/ucar/nc2/iosp/bufr/DataDescriptorTreeConstructor.java
private List<DataDescriptor> replicate(List<DataDescriptor> keys) {
    List<DataDescriptor> tree = new ArrayList<DataDescriptor>();
    Iterator<DataDescriptor> dkIter = keys.iterator();
    while (dkIter.hasNext()) {
      DataDescriptor dk = dkIter.next();
      if (dk.f == 1) {
        dk.subKeys = new ArrayList<DataDescriptor>();
        dk.replication = dk.y; // replication count

        if (dk.replication == 0) { // delayed replication
          root.isVarLength = true; // variable sized data == deferred replication == sequence data

          // the next one is the replication count size : does not count in field count (x)
          DataDescriptor replication = dkIter.next();
          if (replication.y == 0)
            dk.replicationCountSize = 1; // ??
          else if (replication.y == 1)
            dk.replicationCountSize = 8;
          else if (replication.y == 2)
            dk.replicationCountSize = 16;
          else if (replication.y == 11)
            dk.repetitionCountSize = 8;
          else if (replication.y == 12)
            dk.repetitionCountSize = 16;
          else
            log.error("Unknown replication type= "+replication);
        }

        // transfer to the subKey list
        for (int j = 0; j < dk.x && dkIter.hasNext(); j++) {
          dk.subKeys.add( dkIter.next());
        }

        // recurse
        dk.subKeys = replicate(dk.subKeys);

      } else if ((dk.f == 3) && (dk.subKeys != null)) {
        dk.subKeys = replicate( dk.subKeys); // do at all levels
      }

      tree.add(dk);
    }

    return tree;
  }
private List<DataDescriptor> replicate(List<DataDescriptor> keys) {
    List<DataDescriptor> tree = new ArrayList<DataDescriptor>();
    Iterator<DataDescriptor> dkIter = keys.iterator();
    while (dkIter.hasNext()) {
      DataDescriptor dk = dkIter.next();
      if (dk.f == 1) {
        dk.subKeys = new ArrayList<DataDescriptor>();
        dk.replication = dk.y; // replication count

        if (dk.replication == 0) { // delayed replication
          root.isVarLength = true; // variable sized data == deferred replication == sequence data

          // the next one is the replication count size : does not count in field count (x)
          DataDescriptor replication = dkIter.next();
          if (replication.y == 0)
            dk.replicationCountSize = 1; // ??
          else if (replication.y == 1)
            dk.replicationCountSize = 8;
          else if (replication.y == 2)
            dk.replicationCountSize = 16;
          else if (replication.y == 11)
            dk.repetitionCountSize = 8;
          else if (replication.y == 12)
            dk.repetitionCountSize = 16;
          else
            log.error("Unknown replication type= "+replication);
        }

        // transfer to the subKey list
        for (int j = 0; j < dk.x && dkIter.hasNext(); j++) {
          dk.subKeys.add( dkIter.next());
        }

        // recurse
        dk.subKeys = replicate(dk.subKeys);

      } else if ((dk.f == 3) && (dk.subKeys != null)) {
        dk.subKeys = replicate( dk.subKeys); // do at all levels
      }

      tree.add(dk);
    }

    return tree;
  }
private List < DataDescriptor > replicate ( List < DataDescriptor > keys ) { List < DataDescriptor > tree = new ArrayList < DataDescriptor > ( ) ; Iterator < DataDescriptor > dkIter = keys . iterator ( ) ; while ( dkIter . hasNext ( ) ) { DataDescriptor dk = dkIter . next ( ) ; if ( dk . f == 1 ) { dk . subKeys = new ArrayList < DataDescriptor > ( ) ; dk . replication = dk . y ; // replication count
 if ( dk . replication == 0 ) { // delayed replication
 root . isVarLength = true ; // variable sized data == deferred replication == sequence data
 // the next one is the replication count size : does not count in field count (x)
 DataDescriptor replication = dkIter . next ( ) ; if ( replication . y == 0 ) dk . replicationCountSize = 1 ; // ??
 else if ( replication . y == 1 ) dk . replicationCountSize = 8 ; else if ( replication . y == 2 ) dk . replicationCountSize = 16 ; else if ( replication . y == 11 ) dk . repetitionCountSize = 8 ; else if ( replication . y == 12 ) dk . repetitionCountSize = 16 ; else log . error ( "Unknown replication type= " + replication ) ; } // transfer to the subKey list
 for ( int j = 0 ; j < dk . x && dkIter . hasNext ( ) ; j ++ ) { dk . subKeys . add ( dkIter . next ( ) ) ; } // recurse
 dk . subKeys = replicate ( dk . subKeys ) ; } else if ( ( dk . f == 3 ) && ( dk . subKeys != null ) ) { dk . subKeys = replicate ( dk . subKeys ) ; // do at all levels
 } tree . add ( dk ) ; } return tree ; }
look for replication, move replicated items into subtree
look for replication move replicated items into subtree