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();
     }
   }
 }
Example #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;
  }
  @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;
    }
  }