// read in the index, create if it doesnt exist or is out of date
 public static Grib1TimePartition factory(
     TimePartitionCollection tpc, CollectionManager.Force force, Formatter f) throws IOException {
   Grib1TimePartitionBuilder builder =
       new Grib1TimePartitionBuilder(tpc.getCollectionName(), new File(tpc.getRoot()), tpc);
   builder.readOrCreateIndex(force, f);
   return builder.tp;
 }
 // called by tdm
 public static boolean update(TimePartitionCollection tpc, Formatter f) throws IOException {
   Grib1TimePartitionBuilder builder =
       new Grib1TimePartitionBuilder(tpc.getCollectionName(), new File(tpc.getRoot()), tpc);
   if (!builder.needsUpdate()) return false;
   builder.readOrCreateIndex(CollectionManager.Force.always, f);
   builder.gc.close();
   return true;
 }
  private boolean createPartitionedIndex(Formatter f) throws IOException {
    long start = System.currentTimeMillis();

    // create partitions based on TimePartitionCollections object
    for (CollectionManager dcm : tpc.makePartitions()) {
      tp.addPartition(dcm);
    }

    List<TimePartition.Partition> bad = new ArrayList<TimePartition.Partition>();
    for (TimePartition.Partition dc : tp.getPartitions()) {
      try {
        dc.makeGribCollection(f); // ensure collection has been read successfully
        if (trace) f.format(" Open partition %s%n", dc.getDcm().getCollectionName());
      } catch (Throwable t) {
        logger.error(" Failed to open partition " + dc.getName(), t);
        f.format(" FAIL on partition %s (remove) %n", dc.getDcm().getCollectionName());
        bad.add(dc); // LOOK may be a file leak ?
      }
    }

    // remove ones that failed
    for (TimePartition.Partition p : bad) tp.removePartition(p);

    // choose the "canonical" partition, aka prototype
    int n = tp.getPartitions().size();
    if (n == 0) {
      logger.error(" Nothing in this partition = " + tp.getName());
      f.format(" FAIL Partition empty collection = %s%n", tp.getName());
      return false;
    }
    int idx = tpc.getProtoIndex(n);
    TimePartition.Partition canon = tp.getPartitions().get(idx);
    f.format(" Using canonical partition %s%n", canon.getDcm().getCollectionName());

    // check consistency across vert and ens coords
    if (!checkPartitions(canon, f)) {
      logger.error(
          " Partition check failed, index not written on {} message = {}",
          tp.getName(),
          f.toString());
      f.format(" FAIL Partition check collection = %s%n", tp.getName());
      return false;
    }

    // make the time coordinates, place results into canon
    createPartitionedTimeCoordinates(canon, f);

    // ready to write the index file
    writeIndex(canon, f);

    // close open gc's
    tp.cleanup();

    long took = System.currentTimeMillis() - start;
    f.format(" CreatePartitionedIndex took %d msecs%n", took);
    return true;
  }
  /**
   * write new index if needed
   *
   * @param tpc use this collection
   * @param force force index
   * @param f put status messagess here
   * @return true if index was written
   * @throws IOException on error
   */
  public static boolean writeIndexFile(
      TimePartitionCollection tpc, CollectionManager.Force force, Formatter f) throws IOException {
    Grib1TimePartitionBuilder builder = null;
    try {
      builder =
          new Grib1TimePartitionBuilder(tpc.getCollectionName(), new File(tpc.getRoot()), tpc);
      return builder.readOrCreateIndex(force, f);

    } finally {
      if ((builder != null) && (builder.tp != null)) builder.tp.close();
    }
  }
 private Grib1TimePartitionBuilder(String name, File directory, TimePartitionCollection tpc) {
   FeatureCollectionConfig.GribConfig config =
       (tpc == null)
           ? null
           : (FeatureCollectionConfig.GribConfig)
               tpc.getAuxInfo(FeatureCollectionConfig.AUX_GRIB_CONFIG);
   this.tp = new Grib1TimePartition(name, directory, config);
   this.gc = tp;
   this.tpc = tpc;
 }
 private boolean needsUpdate(long collectionLastModified, Formatter f) throws IOException {
   CollectionManager.ChangeChecker cc = Grib1Index.getChangeChecker();
   for (CollectionManager dcm :
       tpc.makePartitions()) { // LOOK not really right, since we dont know if these files are the
     // same as in the index
     File idxFile = GribCollection.getIndexFile(dcm);
     if (!idxFile.exists()) return true;
     if (collectionLastModified < idxFile.lastModified()) return true;
     for (MFile mfile : dcm.getFiles()) {
       if (cc.hasChangedSince(mfile, idxFile.lastModified())) return true;
     }
   }
   return false;
 }