Program: 19227
Project: 19227
Buggy source:
@Override public void writeOutput(Writer writer) throws IOException {
  if (!hasError) {
    List<String> obsProps=this.handler.getRequestedObservedProperties();
    StringBuilder newString=new StringBuilder();
    Boolean isFirstBlock=true;
    String headerStr="String_Node_Str";
    Map<String,List<Double>> heightMap=new HashMap<String,List<Double>>();
    boolean isProfile=handler.getCDMDataset() instanceof TimeSeriesProfile;
    boolean is3dGrid=this.handler.is3dGrid(this.handler.getCDMDataset().getStationName(0));
    List<String> allObsInHeader=new ArrayList<String>();
    for (int p=0; p < this.handler.getProcedures().length; p++) {
      String keyVals=this.handler.getValueBlockForAllObs(BLOCK_SEPERATOR,DECIMAL_SEPERATOR,TOKEN_SEPERATOR,p);
      for (      String block : keyVals.split(BLOCK_SEPERATOR)) {
        StringBuilder newBlock=new StringBuilder();
        boolean appendedHeader=false;
        Integer stNum=null;
        Integer bin=null;
        String currTime=null;
        String lat=null;
        String lon=null;
        for (        String token : block.split(TOKEN_SEPERATOR)) {
          String[] tokenSplit=token.split("String_Node_Str");
          if (isFirstBlock && appendedHeader) {
            headerStr=headerStr + TOKEN_SEPERATOR;
            appendedHeader=false;
          }
          if (token.contains(baseCDMClass.TIME_STR)) {
            currTime=tokenSplit[1];
            if (isFirstBlock) {
              headerStr=headerStr + tokenSplit[0];
              appendedHeader=true;
            }
          }
 else           if (token.contains(baseCDMClass.STATION_STR)) {
            stNum=Integer.parseInt(tokenSplit[1]);
            if (isFirstBlock) {
              headerStr=headerStr + tokenSplit[0];
              appendedHeader=true;
            }
          }
 else           if (tokenSplit[0].equals("String_Node_Str") || tokenSplit[0].equals("String_Node_Str")) {
            if (isFirstBlock) {
              headerStr=headerStr + tokenSplit[0];
              appendedHeader=true;
            }
            if (tokenSplit[0].equals("String_Node_Str"))             lat=tokenSplit[1];
 else             lon=tokenSplit[1];
          }
 else           if (token.startsWith("String_Node_Str") && (isProfile || is3dGrid)) {
            bin=Integer.valueOf(tokenSplit[1]);
          }
 else {
            if (obsProps.contains(tokenSplit[0]) && tokenSplit.length > 1) {
              String station=this.handler.stationToFieldName(this.handler.getProcedures()[stNum]);
              String stationSensor=station + "String_Node_Str" + tokenSplit[0];
              if (isFirstBlock)               headerStr+="String_Node_Str" + TOKEN_SEPERATOR;
              if ((isProfile || is3dGrid) && !heightMap.containsKey(stationSensor)) {
                if (isProfile) {
                  if (isFirstBlock)                   headerStr=headerStr + "String_Node_Str" + ((TimeSeriesProfile)handler.getCDMDataset()).getHeightAxisUnits()+ "String_Node_Str"+ TOKEN_SEPERATOR;
                  heightMap.put(stationSensor,((TimeSeriesProfile)handler.getCDMDataset()).getProfileHeightsForStation(String.valueOf(stNum)));
                }
 else {
                  Grid grid=((Grid)this.handler.getCDMDataset());
                  heightMap.put(stationSensor,grid.getDepths(tokenSplit[0]));
                  if (isFirstBlock)                   headerStr=headerStr + "String_Node_Str" + ((Grid)this.handler.getCDMDataset()).getDepthUnits(tokenSplit[0])+ "String_Node_Str"+ TOKEN_SEPERATOR;
                }
                appendedHeader=true;
              }
              newBlock.append(currTime).append(TOKEN_SEPERATOR);
              if (lat != null) {
                newBlock.append(lat).append(TOKEN_SEPERATOR);
              }
              if (lon != null) {
                newBlock.append(lon).append(TOKEN_SEPERATOR);
              }
              newBlock.append(stationSensor);
              if (isProfile || is3dGrid) {
                if (bin != null)                 newBlock.append(TOKEN_SEPERATOR).append(heightMap.get(stationSensor).get(bin));
 else {
                  newBlock.append(TOKEN_SEPERATOR).append("String_Node_Str");
                }
              }
              newBlock.append(TOKEN_SEPERATOR).append(tokenSplit[1]).append(BLOCK_SEPERATOR);
              String sensorUnits=this.handler.getUnitsString(tokenSplit[0]);
              if (sensorUnits == null)               sensorUnits="String_Node_Str";
              if (isFirstBlock) {
                headerStr+="String_Node_Str";
              }
              if (!allObsInHeader.contains(tokenSplit[0])) {
                headerStr=this.handler.stationToFieldName(this.handler.getProcedures()[stNum]) + "String_Node_Str" + tokenSplit[0]+ "String_Node_Str"+ this.handler.getVariableStandardName(tokenSplit[0])+ "String_Node_Str"+ sensorUnits+ "String_Node_Str"+ "String_Node_Str"+ headerStr;
                allObsInHeader.add(tokenSplit[0]);
                appendedHeader=true;
              }
              isFirstBlock=false;
            }
          }
        }
        newString.append(newBlock.toString());
      }
      writer.write(headerStr + BLOCK_SEPERATOR + newString.toString());
    }
  }
}

Fixed version (ground truth):
@Override public void writeOutput(Writer writer) throws IOException {
  if (!hasError) {
    List<String> obsProps=this.handler.getRequestedObservedProperties();
    StringBuilder newString=new StringBuilder();
    Boolean isFirstBlock=true;
    String headerStr="String_Node_Str";
    Map<String,List<Double>> heightMap=new HashMap<String,List<Double>>();
    boolean isProfile=handler.getCDMDataset() instanceof TimeSeriesProfile;
    boolean is3dGrid=this.handler.is3dGrid(this.handler.getCDMDataset().getStationName(0));
    List<String> allObsInHeader=new ArrayList<String>();
    for (int p=0; p < this.handler.getProcedures().length; p++) {
      String keyVals=this.handler.getValueBlockForAllObs(BLOCK_SEPERATOR,DECIMAL_SEPERATOR,TOKEN_SEPERATOR,p);
      for (      String block : keyVals.split(BLOCK_SEPERATOR)) {
        StringBuilder newBlock=new StringBuilder();
        boolean appendedHeader=false;
        Integer stNum=null;
        Integer bin=null;
        String currTime=null;
        String lat=null;
        String currDepth=null;
        String lon=null;
        for (        String token : block.split(TOKEN_SEPERATOR)) {
          String[] tokenSplit=token.split("String_Node_Str");
          if (isFirstBlock && appendedHeader) {
            headerStr=headerStr + TOKEN_SEPERATOR;
            appendedHeader=false;
          }
          if (token.contains(baseCDMClass.TIME_STR)) {
            currTime=tokenSplit[1];
            if (isFirstBlock) {
              headerStr=headerStr + tokenSplit[0];
              appendedHeader=true;
            }
          }
 else           if (token.contains(baseCDMClass.STATION_STR)) {
            stNum=Integer.parseInt(tokenSplit[1]);
            if (isFirstBlock) {
              headerStr=headerStr + tokenSplit[0];
              appendedHeader=true;
            }
          }
 else           if (tokenSplit[0].equals("String_Node_Str") || tokenSplit[0].equals("String_Node_Str")) {
            if (isFirstBlock) {
              headerStr=headerStr + tokenSplit[0];
              appendedHeader=true;
            }
            if (tokenSplit[0].equals("String_Node_Str"))             lat=tokenSplit[1];
 else             lon=tokenSplit[1];
          }
 else           if (token.startsWith("String_Node_Str") && (isProfile || is3dGrid)) {
            bin=Integer.valueOf(tokenSplit[1]);
          }
 else           if (tokenSplit[0].equals(this.handler.getDepthAxisName())) {
            currDepth=tokenSplit[1];
          }
 else {
            if (obsProps.contains(tokenSplit[0]) && tokenSplit.length > 1) {
              String station="String_Node_Str";
              if (stNum != null)               station=this.handler.stationToFieldName(this.handler.getProcedures()[stNum]) + "String_Node_Str";
              String stationSensor=station + tokenSplit[0];
              if (isFirstBlock) {
                headerStr+="String_Node_Str" + TOKEN_SEPERATOR;
                if (!(isProfile || is3dGrid) && currDepth != null) {
                  headerStr+=this.handler.getDepthAxisName() + "String_Node_Str" + this.handler.getDepthUnits()+ "String_Node_Str"+ TOKEN_SEPERATOR;
                }
              }
              if ((isProfile || is3dGrid) && !heightMap.containsKey(stationSensor)) {
                if (isProfile) {
                  if (isFirstBlock)                   headerStr=headerStr + "String_Node_Str" + ((TimeSeriesProfile)handler.getCDMDataset()).getHeightAxisUnits()+ "String_Node_Str"+ TOKEN_SEPERATOR;
                  heightMap.put(stationSensor,((TimeSeriesProfile)handler.getCDMDataset()).getProfileHeightsForStation(stNum));
                }
 else {
                  Grid grid=((Grid)this.handler.getCDMDataset());
                  heightMap.put(stationSensor,grid.getDepths(tokenSplit[0]));
                  if (isFirstBlock)                   headerStr=headerStr + this.handler.getDepthAxisName() + "String_Node_Str"+ ((Grid)this.handler.getCDMDataset()).getDepthUnits(tokenSplit[0])+ "String_Node_Str"+ TOKEN_SEPERATOR;
                }
                appendedHeader=true;
              }
              newBlock.append(currTime).append(TOKEN_SEPERATOR);
              if (lat != null) {
                newBlock.append(lat).append(TOKEN_SEPERATOR);
              }
              if (lon != null) {
                newBlock.append(lon).append(TOKEN_SEPERATOR);
              }
              newBlock.append(stationSensor);
              if (isProfile || is3dGrid) {
                if (bin != null)                 newBlock.append(TOKEN_SEPERATOR).append(heightMap.get(stationSensor).get(bin));
 else {
                  newBlock.append(TOKEN_SEPERATOR).append("String_Node_Str");
                }
              }
 else               if (currDepth != null) {
                newBlock.append(TOKEN_SEPERATOR).append(currDepth);
              }
              newBlock.append(TOKEN_SEPERATOR).append(tokenSplit[1]).append(BLOCK_SEPERATOR);
              String sensorUnits=this.handler.getUnitsString(tokenSplit[0]);
              if (sensorUnits == null)               sensorUnits="String_Node_Str";
              if (isFirstBlock) {
                headerStr+="String_Node_Str";
              }
              if (!allObsInHeader.contains(tokenSplit[0])) {
                String statName="String_Node_Str";
                if (stNum != null) {
                  statName=this.handler.stationToFieldName(this.handler.getProcedures()[stNum]) + "String_Node_Str";
                }
                headerStr=statName + tokenSplit[0] + "String_Node_Str"+ this.handler.getVariableStandardName(tokenSplit[0])+ "String_Node_Str"+ sensorUnits+ "String_Node_Str"+ "String_Node_Str"+ headerStr;
                allObsInHeader.add(tokenSplit[0]);
                appendedHeader=true;
              }
              isFirstBlock=false;
            }
          }
        }
        newString.append(newBlock.toString());
      }
      writer.write(headerStr + BLOCK_SEPERATOR + newString.toString());
    }
  }
}