private void ensureOpen() throws IOException {
   if (table == null) {
     table = DataIO.table(new File(file), null);
     rowsIterator = (TableIterator<String[]>) table.rows().iterator();
     /*
      * If tStart is null then the reader try to read all the value in the file, nb time step constant.
      */
     if (tStart == null) {
       String secondTime = null;
       // get the first time in the file.
       if (rowsIterator.hasNext()) {
         String[] row = rowsIterator.next();
         tStart = row[1];
       }
       // get the time of the second row in the file.
       if (rowsIterator.hasNext()) {
         String[] row = rowsIterator.next();
         secondTime = row[1];
       }
       // the dt is equal to the fifference of the time of 2 rows.
       tTimestep =
           formatter.parseDateTime(secondTime).getMinuteOfDay()
               - formatter.parseDateTime(tStart).getMinuteOfDay();
       // close and reopen to read the row.
       rowsIterator.close();
       rowsIterator = (TableIterator<String[]>) table.rows().iterator();
     }
   }
 }
예제 #2
0
  private HashMap<Integer, double[]> readIdData(String path) throws Exception {
    CSTable table = DataIO.table(new File(path), null);
    HashMap<Integer, List<Double>> dataMap = new HashMap<>();
    int columnCount = table.getColumnCount();
    int[] ids = new int[columnCount - 1]; // minus type and timestamp
    for (int i = 2; i <= columnCount; i++) {
      Map<String, String> columnInfo = table.getColumnInfo(i);
      String idStr = columnInfo.get("id");
      int id = Integer.parseInt(idStr);
      ids[i - 2] = id;
    }
    TableIterator<String[]> rowsIterator = (TableIterator<String[]>) table.rows().iterator();
    while (rowsIterator.hasNext()) {
      String[] row = rowsIterator.next();
      for (int i = 2; i < row.length; i++) {
        List<Double> dataList = dataMap.get(ids[i - 2]);
        if (dataList == null) {
          dataList = new ArrayList<>();
          dataMap.put(ids[i - 2], dataList);
        }
        double value = -1;
        if (row[i] == null || row[i].length() == 0) {
          value = JGTConstants.doubleNovalue;
        } else {
          String valueStr = row[i];
          value = Double.parseDouble(valueStr);
        }
        dataList.add(value);
      }
    }

    HashMap<Integer, double[]> outDataMap = new HashMap<>();
    for (Entry<Integer, List<Double>> entry : dataMap.entrySet()) {
      Integer id = entry.getKey();
      List<Double> valueList = entry.getValue();
      double[] values = new double[valueList.size()];
      for (int i = 0; i < values.length; i++) {
        values[i] = valueList.get(i);
      }
      outDataMap.put(id, values);
    }

    return outDataMap;
  }
 @Finalize
 public void close() throws IOException {
   rowsIterator.close();
 }
  /**
   * Get the needed datarow from the table.
   *
   * @param tableRowIterator
   * @return the row that is aligned with the expected timestep.
   * @throws IOException if the expected timestep is < than the current.
   */
  private String[] getExpectedRow(TableIterator<String[]> tableRowIterator, DateTime expectedDT)
      throws IOException {
    while (tableRowIterator.hasNext()) {
      String[] row = tableRowIterator.next();
      DateTime currentTimestamp = formatter.parseDateTime(row[1]);
      if (currentTimestamp.equals(expectedDT)) {
        if (pNum == 1) {
          return row;
        } else {
          String[][] allRows = new String[pNum][];
          allRows[0] = row;
          int rowNum = 1;
          for (int i = 1; i < pNum; i++) {
            if (tableRowIterator.hasNext()) {
              String[] nextRow = tableRowIterator.next();
              allRows[i] = nextRow;
              rowNum++;
            }
          }
          // now aggregate
          String[] aggregatedRow = new String[row.length];
          // date is the one of the first instant
          aggregatedRow[0] = allRows[0][0];
          aggregatedRow[1] = allRows[0][1];
          for (int col = 2; col < allRows[0].length; col++) {

            boolean hasOne = false;
            switch (pAggregation) {
              case 0:
                double sum = 0;
                for (int j = 0; j < rowNum; j++) {
                  String valueStr = allRows[j][col];
                  if (!valueStr.equals(fileNovalue)) {
                    double value = Double.parseDouble(valueStr);
                    sum = sum + value;
                    hasOne = true;
                  }
                }
                if (!hasOne) {
                  sum = doubleNovalue;
                }
                aggregatedRow[col] = String.valueOf(sum);
                break;
              case 1:
                double avg = 0;
                for (int j = 0; j < rowNum; j++) {
                  String valueStr = allRows[j][col];
                  if (!valueStr.equals(fileNovalue)) {
                    double value = Double.parseDouble(valueStr);
                    avg = avg + value;
                    hasOne = true;
                  }
                }
                if (!hasOne) {
                  avg = doubleNovalue;
                } else {
                  avg = avg / pNum;
                }
                aggregatedRow[col] = String.valueOf(avg);
                break;

              default:
                break;
            }
          }
          return aggregatedRow;
        }
      } else if (currentTimestamp.isBefore(expectedDT)) {
        // browse until the instant is found
        continue;
      } else if (currentTimestamp.isAfter(expectedDT)) {
        /*
         * lost the moment, for now throw exception.
         * Could be enhanced in future.
         */
        String message =
            "The data are not aligned with the simulation interval ("
                + currentTimestamp
                + "/"
                + expectedDT
                + "). Check your data file: "
                + file;
        throw new IOException(message);
      }
    }
    return null;
  }
  @Execute
  public void nextRecord() throws IOException {
    ensureOpen();
    if (tCurrent == null) {
      tPrevious = null;
      tCurrent = tStart.trim();
      expectedTimestamp = formatter.parseDateTime(tCurrent);
    } else {

      tPrevious = tCurrent;
      expectedTimestamp = expectedTimestamp.plusMinutes(tTimestep);
      tCurrent = expectedTimestamp.toString(formatter);
    }
    outData = new HashMap<Integer, double[]>();

    int columnCount = table.getColumnCount();
    List<Integer> idList = new ArrayList<Integer>();
    List<Integer> idCountList = new ArrayList<Integer>();
    int count = 0;
    Integer previousIdInteger = null;
    for (int i = 2; i <= columnCount; i++) {
      String id = table.getColumnInfo(i).get(idfield);
      try {
        Integer idInteger = Integer.valueOf(id);
        idList.add(idInteger);
        if (previousIdInteger == null) {
          count++;
        } else {
          if (idInteger.intValue() == previousIdInteger.intValue()) {
            count++;
          } else {
            idCountList.add(count);
            count = 1;
          }
        }
        if (i == columnCount) {
          idCountList.add(count);
        }
        previousIdInteger = idInteger;
      } catch (Exception e) {
        throw new ModelsIllegalargumentException(
            "The id value doesn't seem to be an integer.", this.getClass().getSimpleName(), pm);
      }
    }

    if (rowsIterator.hasNext()) {
      String[] row = getExpectedRow(rowsIterator, expectedTimestamp);

      int idCountIndex = 0;
      for (int i = 2; i < row.length; i++) {
        Integer id = idList.get(i - 2);
        Integer idCount = idCountList.get(idCountIndex);
        double[] values = outData.get(id);
        if (values == null) {
          values = new double[idCount];
          outData.put(id, values);
        }
        for (int j = 0; j < idCount; j++, i++) {
          if (row[i] == null || row[i].length() == 0) {
            values[j] = novalue;
          } else {
            String valueStr = row[i].trim();
            if (valueStr.equals(fileNovalue)) {
              values[j] = novalue;
            } else {
              values[j] = Double.parseDouble(valueStr);
            }
          }
        }
        idCountIndex++;
        i--;
      }
    } else {
      outData = null;
    }

    // time ran out
    if (tEnd != null && tCurrent.equals(tEnd)) {
      doProcess = false;
    }
    // data ran out
    if (!rowsIterator.hasNext()) {
      doProcess = false;
    }
  }