cdm/src/main/java/ucar/nc2/ncml/AggregationExisting.java
protected void persistRead() {
    if (diskCache2 == null) return;

    String cacheName = getCacheName();
    if (cacheName == null) return;
    if (cacheName.startsWith("file:"))      // LOOK
      cacheName = cacheName.substring(5);

    File cacheFile = diskCache2.getCacheFile(cacheName);
    if (cacheFile == null) throw new IllegalStateException();
    if (!cacheFile.exists())
      return;
    long lastWritten = cacheFile.lastModified();

    if (logger.isDebugEnabled()) logger.debug(" Try to Read cache {} " + cacheFile.getPath());

    Element aggElem;
    try {
      aggElem = ucar.nc2.util.xml.Parse.readRootElement("file:" + cacheFile.getPath());
    } catch (IOException e) {
      if (debugCache) System.out.println(" No cache for " + cacheName + " - " + e.getMessage());
      return;
    }

    String version = aggElem.getAttributeValue("version");
    if ((version == null) || !version.equals("3")) return; // dont read old cache files, recreate

    // use a map to find datasets to avoid O(n**2) searching
    Map<String, Dataset> map = new HashMap<String, Dataset>();
    for (Dataset ds : getDatasets()) {
      map.put(ds.getId(), ds);
    }

    List<Element> ncList = aggElem.getChildren("netcdf", Catalog.ncmlNS);
    for (Element netcdfElemNested : ncList) {
      String id = netcdfElemNested.getAttributeValue("id");
      DatasetOuterDimension dod = (DatasetOuterDimension) map.get(id);

      if (null == dod) {
        // this should mean that the dataset has been deleted. so not a problem
        if (logger.isDebugEnabled()) logger.debug(" have cache but no dataset= {}", id);
        continue;
      }
      if (logger.isDebugEnabled()) logger.debug(" use cache for dataset= {}", id);

      MFile mfile = dod.getMFile();
      if (mfile != null && mfile.getLastModified() > lastWritten) {  // skip datasets that have changed
        if (logger.isDebugEnabled()) logger.debug(" dataset was changed= {}", mfile);
        continue;
      }

      if (dod.ncoord == 0) {
        String ncoordsS = netcdfElemNested.getAttributeValue("ncoords");
        try {
          dod.ncoord = Integer.parseInt(ncoordsS);
          if (logger.isDebugEnabled()) logger.debug(" Read the cache; ncoords = {}", dod.ncoord);
        } catch (NumberFormatException e) {
          logger.error("bad ncoord attribute on dataset=" + id);
        }
      }

      // if (dod.coordValue != null) continue; // allow ncml to override

      List<Element> cacheElemList = netcdfElemNested.getChildren("cache", Catalog.ncmlNS);
      for (Element cacheElemNested : cacheElemList) {
        String varName = cacheElemNested.getAttributeValue("varName");
        CacheVar pv = findCacheVariable(varName);
        if (pv != null) {
          String sdata = cacheElemNested.getText();
          if (sdata.length() == 0) continue;
          if (logger.isDebugEnabled()) logger.debug(" read data for var = " + varName + " size= " + sdata.length());

          //long start = System.nanoTime();
          String[] vals = sdata.split(" ");
          //double took = .001 * .001 * .001 * (System.nanoTime() - start);
          //if (debugPersist) System.out.println("  split took = " + took + " sec; ");

          try {
            //start = System.nanoTime();
            Array data = Array.makeArray(pv.dtype, vals);
            //took = .001 * .001 * .001 * (System.nanoTime() - start);
            //if (debugPersist) System.out.println("  makeArray took = " + took + " sec nelems= "+data.getSize());
            pv.putData(id, data);
            countCacheUse++;

          } catch (Exception e) {
            logger.warn("Error reading cached data ", e);
          }

        } else {
          logger.warn("not a cache var=" + varName);
        }
      }
    }

  }
protected void persistRead() {
    if (diskCache2 == null) return;

    String cacheName = getCacheName();
    if (cacheName == null) return;
    if (cacheName.startsWith("file:"))      // LOOK
      cacheName = cacheName.substring(5);

    File cacheFile = diskCache2.getCacheFile(cacheName);
    if (cacheFile == null) throw new IllegalStateException();
    if (!cacheFile.exists())
      return;
    long lastWritten = cacheFile.lastModified();

    if (logger.isDebugEnabled()) logger.debug(" Try to Read cache {} " + cacheFile.getPath());

    Element aggElem;
    try {
      aggElem = ucar.nc2.util.xml.Parse.readRootElement("file:" + cacheFile.getPath());
    } catch (IOException e) {
      if (debugCache) System.out.println(" No cache for " + cacheName + " - " + e.getMessage());
      return;
    }

    String version = aggElem.getAttributeValue("version");
    if ((version == null) || !version.equals("3")) return; // dont read old cache files, recreate

    // use a map to find datasets to avoid O(n**2) searching
    Map<String, Dataset> map = new HashMap<String, Dataset>();
    for (Dataset ds : getDatasets()) {
      map.put(ds.getId(), ds);
    }

    List<Element> ncList = aggElem.getChildren("netcdf", Catalog.ncmlNS);
    for (Element netcdfElemNested : ncList) {
      String id = netcdfElemNested.getAttributeValue("id");
      DatasetOuterDimension dod = (DatasetOuterDimension) map.get(id);

      if (null == dod) {
        // this should mean that the dataset has been deleted. so not a problem
        if (logger.isDebugEnabled()) logger.debug(" have cache but no dataset= {}", id);
        continue;
      }
      if (logger.isDebugEnabled()) logger.debug(" use cache for dataset= {}", id);

      MFile mfile = dod.getMFile();
      if (mfile != null && mfile.getLastModified() > lastWritten) {  // skip datasets that have changed
        if (logger.isDebugEnabled()) logger.debug(" dataset was changed= {}", mfile);
        continue;
      }

      if (dod.ncoord == 0) {
        String ncoordsS = netcdfElemNested.getAttributeValue("ncoords");
        try {
          dod.ncoord = Integer.parseInt(ncoordsS);
          if (logger.isDebugEnabled()) logger.debug(" Read the cache; ncoords = {}", dod.ncoord);
        } catch (NumberFormatException e) {
          logger.error("bad ncoord attribute on dataset=" + id);
        }
      }

      // if (dod.coordValue != null) continue; // allow ncml to override

      List<Element> cacheElemList = netcdfElemNested.getChildren("cache", Catalog.ncmlNS);
      for (Element cacheElemNested : cacheElemList) {
        String varName = cacheElemNested.getAttributeValue("varName");
        CacheVar pv = findCacheVariable(varName);
        if (pv != null) {
          String sdata = cacheElemNested.getText();
          if (sdata.length() == 0) continue;
          if (logger.isDebugEnabled()) logger.debug(" read data for var = " + varName + " size= " + sdata.length());

          //long start = System.nanoTime();
          String[] vals = sdata.split(" ");
          //double took = .001 * .001 * .001 * (System.nanoTime() - start);
          //if (debugPersist) System.out.println("  split took = " + took + " sec; ");

          try {
            //start = System.nanoTime();
            Array data = Array.makeArray(pv.dtype, vals);
            //took = .001 * .001 * .001 * (System.nanoTime() - start);
            //if (debugPersist) System.out.println("  makeArray took = " + took + " sec nelems= "+data.getSize());
            pv.putData(id, data);
            countCacheUse++;

          } catch (Exception e) {
            logger.warn("Error reading cached data ", e);
          }

        } else {
          logger.warn("not a cache var=" + varName);
        }
      }
    }

  }
protected void persistRead ( ) { if ( diskCache2 == null ) return ; String cacheName = getCacheName ( ) ; if ( cacheName == null ) return ; if ( cacheName . startsWith ( "file:" ) ) // LOOK
 cacheName = cacheName . substring ( 5 ) ; File cacheFile = diskCache2 . getCacheFile ( cacheName ) ; if ( cacheFile == null ) throw new IllegalStateException ( ) ; if ( ! cacheFile . exists ( ) ) return ; long lastWritten = cacheFile . lastModified ( ) ; if ( logger . isDebugEnabled ( ) ) logger . debug ( " Try to Read cache {} " + cacheFile . getPath ( ) ) ; Element aggElem ; try { aggElem = ucar . nc2 . util . xml . Parse . readRootElement ( "file:" + cacheFile . getPath ( ) ) ; } catch ( IOException e ) { if ( debugCache ) System . out . println ( " No cache for " + cacheName + " - " + e . getMessage ( ) ) ; return ; } String version = aggElem . getAttributeValue ( "version" ) ; if ( ( version == null ) || ! version . equals ( "3" ) ) return ; // dont read old cache files, recreate
 // use a map to find datasets to avoid O(n**2) searching
 Map < String , Dataset > map = new HashMap < String , Dataset > ( ) ; for ( Dataset ds : getDatasets ( ) ) { map . put ( ds . getId ( ) , ds ) ; } List < Element > ncList = aggElem . getChildren ( "netcdf" , Catalog . ncmlNS ) ; for ( Element netcdfElemNested : ncList ) { String id = netcdfElemNested . getAttributeValue ( "id" ) ; DatasetOuterDimension dod = ( DatasetOuterDimension ) map . get ( id ) ; if ( null == dod ) { // this should mean that the dataset has been deleted. so not a problem
 if ( logger . isDebugEnabled ( ) ) logger . debug ( " have cache but no dataset= {}" , id ) ; continue ; } if ( logger . isDebugEnabled ( ) ) logger . debug ( " use cache for dataset= {}" , id ) ; MFile mfile = dod . getMFile ( ) ; if ( mfile != null && mfile . getLastModified ( ) > lastWritten ) { // skip datasets that have changed
 if ( logger . isDebugEnabled ( ) ) logger . debug ( " dataset was changed= {}" , mfile ) ; continue ; } if ( dod . ncoord == 0 ) { String ncoordsS = netcdfElemNested . getAttributeValue ( "ncoords" ) ; try { dod . ncoord = Integer . parseInt ( ncoordsS ) ; if ( logger . isDebugEnabled ( ) ) logger . debug ( " Read the cache; ncoords = {}" , dod . ncoord ) ; } catch ( NumberFormatException e ) { logger . error ( "bad ncoord attribute on dataset=" + id ) ; } } // if (dod.coordValue != null) continue; // allow ncml to override
 List < Element > cacheElemList = netcdfElemNested . getChildren ( "cache" , Catalog . ncmlNS ) ; for ( Element cacheElemNested : cacheElemList ) { String varName = cacheElemNested . getAttributeValue ( "varName" ) ; CacheVar pv = findCacheVariable ( varName ) ; if ( pv != null ) { String sdata = cacheElemNested . getText ( ) ; if ( sdata . length ( ) == 0 ) continue ; if ( logger . isDebugEnabled ( ) ) logger . debug ( " read data for var = " + varName + " size= " + sdata . length ( ) ) ; //long start = System.nanoTime();
 String [ ] vals = sdata . split ( " " ) ; //double took = .001 * .001 * .001 * (System.nanoTime() - start);
 //if (debugPersist) System.out.println("  split took = " + took + " sec; ");
 try { //start = System.nanoTime();
 Array data = Array . makeArray ( pv . dtype , vals ) ; //took = .001 * .001 * .001 * (System.nanoTime() - start);
 //if (debugPersist) System.out.println("  makeArray took = " + took + " sec nelems= "+data.getSize());
 pv . putData ( id , data ) ; countCacheUse ++ ; } catch ( Exception e ) { logger . warn ( "Error reading cached data " , e ) ; } } else { logger . warn ( "not a cache var=" + varName ) ; } } } }
read info from the persistent XML file, if it exists
read info from the persistent XML file if it exists