visad/src/main/java/ucar/nc2/iosp/grid/GridIndexToNC.java
public void open(GridIndex index, GridTableLookup lookup, int version,
                   NetcdfFile ncfile, CancelTask cancelTask) throws IOException {

    // create the HorizCoord Systems : one for each gds
    List<GridDefRecord> hcsList = index.getHorizCoordSys();
    boolean needGroups = (hcsList.size() > 1);
    for (GridDefRecord gds : hcsList) {
      Group g = null;
      if (needGroups) {
        g = new Group(ncfile, null, gds.getGroupName());
        ncfile.addGroup(null, g);
      }
      // (GridDefRecord gdsIndex, String grid_name, String shape_name, Group g)
      GridHorizCoordSys hcs = makeGridHorizCoordSys(gds, lookup, g);
      hcsHash.put(gds.getParam(GridDefRecord.GDS_KEY), hcs);
    }

    // run through each record
    GridRecord firstRecord = null;
    List<GridRecord> records = index.getGridRecords();

    for (GridRecord gridRecord : records) {
      if (firstRecord == null) {
        firstRecord = gridRecord;
      }

      GridHorizCoordSys hcs =  hcsHash.get(gridRecord.getGridDefRecordId());
      int cdmHash = gridRecord.cdmVariableHash();
      GridVariable pv = hcs.varHash.get(cdmHash);
      if (null == pv) {
        String name = gridRecord.cdmVariableName(lookup, true, true);
        pv = makeGridVariable(indexFilename, name, hcs, lookup);
        hcs.varHash.put(cdmHash, pv);

        // keep track of all products with same parameter name == "simple name"
        String simpleName = gridRecord.getParameterDescription();
        List<GridVariable> plist = hcs.productHash.get(simpleName);
        if (null == plist) {
          plist = new ArrayList<>();
          hcs.productHash.put(simpleName, plist);
        }
        plist.add(pv);

      } /* else if ( lookup instanceof Grib2GridTableLookup ) {
        Grib2GridTableLookup g2lookup = (Grib2GridTableLookup) lookup;
        // check for non interval pv and interval record which needs a interval pv
        if( ! pv.isInterval() && g2lookup.isInterval(gridRecord) ) {
          // make an interval variable
          String interval = name +"_interval";
          pv = (GridVariable) hcs.varHash.get(interval);
          if (null == pv) {
            pv = new GridVariable(interval, hcs, lookup);
            hcs.varHash.put(cdmHash, pv);
            String simpleName = makeVariableName(gridRecord, lookup, false, true); // LOOK may not be a good idea
            List<GridVariable> plist = hcs.productHash.get(simpleName);
            if (null == plist) {
              plist = new ArrayList<GridVariable>();
              hcs.productHash.put(simpleName, plist);
            }
            plist.add(pv);
          }

        } else if ( pv.isInterval() && !g2lookup.isInterval(gridRecord)  ) {
          // make a non-interval variable
          // logger.info( "Non-Interval records for %s%n", pv.getName());  LOOK
            continue;
        }
      } // grid2 */

      pv.addProduct(gridRecord);
    }

    // global CF Conventions
    // Conventions attribute change must be in sync with CDM code
    ncfile.addAttribute(null, new Attribute("Conventions", "CF-1.4"));

    addExtraAttributes(firstRecord, lookup, ncfile);

    // CF Global attributes
    ncfile.addAttribute(null, new Attribute("title", lookup.getTitle()));
    if (lookup.getInstitution() != null)
      ncfile.addAttribute(null, new Attribute("institution", lookup.getInstitution()));
    String source = lookup.getSource();
    if ( source != null && ! source.startsWith( "Unknown"))
      ncfile.addAttribute(null, new Attribute("source", source));
    // String now = formatter.toDateTimeStringISO( Calendar.getInstance().getTime());
    ncfile.addAttribute(null, new Attribute("history", "Direct read of "+ lookup.getGridType() +" into NetCDF-Java 4 API"));
    if ( lookup.getComment() != null)
      ncfile.addAttribute(null, new Attribute("comment", lookup.getComment()));

    // dataset discovery
    //if ( center != null)
    //  ncfile.addAttribute(null, new Attribute("center_name", center));

    // CDM attributes
    ncfile.addAttribute(null, new Attribute(CF.FEATURE_TYPE, FeatureType.GRID.toString()));
    ncfile.addAttribute(null, new Attribute("file_format", lookup.getGridType()));
    // ncfile.addAttribute(null, new Attribute("location", ncfile.getLocation()));
    ncfile.addAttribute(null, new Attribute(_Coordinate.ModelRunDate,
            formatter.toDateTimeStringISO(lookup.getFirstBaseTime())));

    /* if (fmrcCoordSys != null) {
      makeDefinedCoordSys(ncfile, lookup, fmrcCoordSys);
    } else {
      makeDenseCoordSys(ncfile, lookup, cancelTask);
    } */
    makeDenseCoordSys(ncfile, lookup, cancelTask);

    if (GridServiceProvider.debugMissing) {
      try (Formatter f = new Formatter(System.out)) {
        int count = 0;
        Collection<GridHorizCoordSys> hcset = hcsHash.values();
        for (GridHorizCoordSys hcs : hcset) {
          List<GridVariable> gribvars = new ArrayList<>(hcs.varHash.values());
          for (GridVariable gv : gribvars) {
            count += gv.showMissingSummary(f);
          }
        }
        System.out.println(" total missing= " + count);
      }
    }

    if (GridServiceProvider.debugMissingDetails) {
      Formatter f = new Formatter();
      Collection<GridHorizCoordSys> hcset = hcsHash.values();
      for (GridHorizCoordSys hcs : hcset) {
        f.format("******** Horiz Coordinate= %s%n", hcs.getGridName());

        String lastVertDesc = null;
        List<GridVariable> gribvars = new ArrayList<>(hcs.varHash.values());
        Collections.sort(gribvars, new CompareGridVariableByVertName());

        for (GridVariable gv : gribvars) {
          String vertDesc = gv.getVertName();
          if (!vertDesc.equals(lastVertDesc)) {
            f.format("---Vertical Coordinate= %s%n", vertDesc);
            lastVertDesc = vertDesc;
          }
          gv.showMissing(f);
        }
      }
    }

    // clean out stuff we dont need anymore
    //for (GridHorizCoordSys ghcs : hcsHash.values()) {
    //  ghcs.empty();
    //}
  }
public void open(GridIndex index, GridTableLookup lookup, int version,
                   NetcdfFile ncfile, CancelTask cancelTask) throws IOException {

    // create the HorizCoord Systems : one for each gds
    List<GridDefRecord> hcsList = index.getHorizCoordSys();
    boolean needGroups = (hcsList.size() > 1);
    for (GridDefRecord gds : hcsList) {
      Group g = null;
      if (needGroups) {
        g = new Group(ncfile, null, gds.getGroupName());
        ncfile.addGroup(null, g);
      }
      // (GridDefRecord gdsIndex, String grid_name, String shape_name, Group g)
      GridHorizCoordSys hcs = makeGridHorizCoordSys(gds, lookup, g);
      hcsHash.put(gds.getParam(GridDefRecord.GDS_KEY), hcs);
    }

    // run through each record
    GridRecord firstRecord = null;
    List<GridRecord> records = index.getGridRecords();

    for (GridRecord gridRecord : records) {
      if (firstRecord == null) {
        firstRecord = gridRecord;
      }

      GridHorizCoordSys hcs =  hcsHash.get(gridRecord.getGridDefRecordId());
      int cdmHash = gridRecord.cdmVariableHash();
      GridVariable pv = hcs.varHash.get(cdmHash);
      if (null == pv) {
        String name = gridRecord.cdmVariableName(lookup, true, true);
        pv = makeGridVariable(indexFilename, name, hcs, lookup);
        hcs.varHash.put(cdmHash, pv);

        // keep track of all products with same parameter name == "simple name"
        String simpleName = gridRecord.getParameterDescription();
        List<GridVariable> plist = hcs.productHash.get(simpleName);
        if (null == plist) {
          plist = new ArrayList<>();
          hcs.productHash.put(simpleName, plist);
        }
        plist.add(pv);

      } /* else if ( lookup instanceof Grib2GridTableLookup ) {
        Grib2GridTableLookup g2lookup = (Grib2GridTableLookup) lookup;
        // check for non interval pv and interval record which needs a interval pv
        if( ! pv.isInterval() && g2lookup.isInterval(gridRecord) ) {
          // make an interval variable
          String interval = name +"_interval";
          pv = (GridVariable) hcs.varHash.get(interval);
          if (null == pv) {
            pv = new GridVariable(interval, hcs, lookup);
            hcs.varHash.put(cdmHash, pv);
            String simpleName = makeVariableName(gridRecord, lookup, false, true); // LOOK may not be a good idea
            List<GridVariable> plist = hcs.productHash.get(simpleName);
            if (null == plist) {
              plist = new ArrayList<GridVariable>();
              hcs.productHash.put(simpleName, plist);
            }
            plist.add(pv);
          }

        } else if ( pv.isInterval() && !g2lookup.isInterval(gridRecord)  ) {
          // make a non-interval variable
          // logger.info( "Non-Interval records for %s%n", pv.getName());  LOOK
            continue;
        }
      } // grid2 */

      pv.addProduct(gridRecord);
    }

    // global CF Conventions
    // Conventions attribute change must be in sync with CDM code
    ncfile.addAttribute(null, new Attribute("Conventions", "CF-1.4"));

    addExtraAttributes(firstRecord, lookup, ncfile);

    // CF Global attributes
    ncfile.addAttribute(null, new Attribute("title", lookup.getTitle()));
    if (lookup.getInstitution() != null)
      ncfile.addAttribute(null, new Attribute("institution", lookup.getInstitution()));
    String source = lookup.getSource();
    if ( source != null && ! source.startsWith( "Unknown"))
      ncfile.addAttribute(null, new Attribute("source", source));
    // String now = formatter.toDateTimeStringISO( Calendar.getInstance().getTime());
    ncfile.addAttribute(null, new Attribute("history", "Direct read of "+ lookup.getGridType() +" into NetCDF-Java 4 API"));
    if ( lookup.getComment() != null)
      ncfile.addAttribute(null, new Attribute("comment", lookup.getComment()));

    // dataset discovery
    //if ( center != null)
    //  ncfile.addAttribute(null, new Attribute("center_name", center));

    // CDM attributes
    ncfile.addAttribute(null, new Attribute(CF.FEATURE_TYPE, FeatureType.GRID.toString()));
    ncfile.addAttribute(null, new Attribute("file_format", lookup.getGridType()));
    // ncfile.addAttribute(null, new Attribute("location", ncfile.getLocation()));
    ncfile.addAttribute(null, new Attribute(_Coordinate.ModelRunDate,
            formatter.toDateTimeStringISO(lookup.getFirstBaseTime())));

    /* if (fmrcCoordSys != null) {
      makeDefinedCoordSys(ncfile, lookup, fmrcCoordSys);
    } else {
      makeDenseCoordSys(ncfile, lookup, cancelTask);
    } */
    makeDenseCoordSys(ncfile, lookup, cancelTask);

    if (GridServiceProvider.debugMissing) {
      try (Formatter f = new Formatter(System.out)) {
        int count = 0;
        Collection<GridHorizCoordSys> hcset = hcsHash.values();
        for (GridHorizCoordSys hcs : hcset) {
          List<GridVariable> gribvars = new ArrayList<>(hcs.varHash.values());
          for (GridVariable gv : gribvars) {
            count += gv.showMissingSummary(f);
          }
        }
        System.out.println(" total missing= " + count);
      }
    }

    if (GridServiceProvider.debugMissingDetails) {
      Formatter f = new Formatter();
      Collection<GridHorizCoordSys> hcset = hcsHash.values();
      for (GridHorizCoordSys hcs : hcset) {
        f.format("******** Horiz Coordinate= %s%n", hcs.getGridName());

        String lastVertDesc = null;
        List<GridVariable> gribvars = new ArrayList<>(hcs.varHash.values());
        Collections.sort(gribvars, new CompareGridVariableByVertName());

        for (GridVariable gv : gribvars) {
          String vertDesc = gv.getVertName();
          if (!vertDesc.equals(lastVertDesc)) {
            f.format("---Vertical Coordinate= %s%n", vertDesc);
            lastVertDesc = vertDesc;
          }
          gv.showMissing(f);
        }
      }
    }

    // clean out stuff we dont need anymore
    //for (GridHorizCoordSys ghcs : hcsHash.values()) {
    //  ghcs.empty();
    //}
  }
public void open ( GridIndex index , GridTableLookup lookup , int version , NetcdfFile ncfile , CancelTask cancelTask ) throws IOException { // create the HorizCoord Systems : one for each gds List < GridDefRecord > hcsList = index . getHorizCoordSys ( ) ; boolean needGroups = ( hcsList . size ( ) > 1 ) ; for ( GridDefRecord gds : hcsList ) { Group g = null ; if ( needGroups ) { g = new Group ( ncfile , null , gds . getGroupName ( ) ) ; ncfile . addGroup ( null , g ) ; } // (GridDefRecord gdsIndex, String grid_name, String shape_name, Group g) GridHorizCoordSys hcs = makeGridHorizCoordSys ( gds , lookup , g ) ; hcsHash . put ( gds . getParam ( GridDefRecord . GDS_KEY ) , hcs ) ; } // run through each record GridRecord firstRecord = null ; List < GridRecord > records = index . getGridRecords ( ) ; for ( GridRecord gridRecord : records ) { if ( firstRecord == null ) { firstRecord = gridRecord ; } GridHorizCoordSys hcs = hcsHash . get ( gridRecord . getGridDefRecordId ( ) ) ; int cdmHash = gridRecord . cdmVariableHash ( ) ; GridVariable pv = hcs . varHash . get ( cdmHash ) ; if ( null == pv ) { String name = gridRecord . cdmVariableName ( lookup , true , true ) ; pv = makeGridVariable ( indexFilename , name , hcs , lookup ) ; hcs . varHash . put ( cdmHash , pv ) ; // keep track of all products with same parameter name == "simple name" String simpleName = gridRecord . getParameterDescription ( ) ; List < GridVariable > plist = hcs . productHash . get ( simpleName ) ; if ( null == plist ) { plist = new ArrayList <> ( ) ; hcs . productHash . put ( simpleName , plist ) ; } plist . add ( pv ) ; } /* else if ( lookup instanceof Grib2GridTableLookup ) {
        Grib2GridTableLookup g2lookup = (Grib2GridTableLookup) lookup;
        // check for non interval pv and interval record which needs a interval pv
        if( ! pv.isInterval() && g2lookup.isInterval(gridRecord) ) {
          // make an interval variable
          String interval = name +"_interval";
          pv = (GridVariable) hcs.varHash.get(interval);
          if (null == pv) {
            pv = new GridVariable(interval, hcs, lookup);
            hcs.varHash.put(cdmHash, pv);
            String simpleName = makeVariableName(gridRecord, lookup, false, true); // LOOK may not be a good idea
            List<GridVariable> plist = hcs.productHash.get(simpleName);
            if (null == plist) {
              plist = new ArrayList<GridVariable>();
              hcs.productHash.put(simpleName, plist);
            }
            plist.add(pv);
          }

        } else if ( pv.isInterval() && !g2lookup.isInterval(gridRecord)  ) {
          // make a non-interval variable
          // logger.info( "Non-Interval records for %s%n", pv.getName());  LOOK
            continue;
        }
      } // grid2 */ pv . addProduct ( gridRecord ) ; } // global CF Conventions // Conventions attribute change must be in sync with CDM code ncfile . addAttribute ( null , new Attribute ( "Conventions" , "CF-1.4" ) ) ; addExtraAttributes ( firstRecord , lookup , ncfile ) ; // CF Global attributes ncfile . addAttribute ( null , new Attribute ( "title" , lookup . getTitle ( ) ) ) ; if ( lookup . getInstitution ( ) != null ) ncfile . addAttribute ( null , new Attribute ( "institution" , lookup . getInstitution ( ) ) ) ; String source = lookup . getSource ( ) ; if ( source != null && ! source . startsWith ( "Unknown" ) ) ncfile . addAttribute ( null , new Attribute ( "source" , source ) ) ; // String now = formatter.toDateTimeStringISO( Calendar.getInstance().getTime()); ncfile . addAttribute ( null , new Attribute ( "history" , "Direct read of " + lookup . getGridType ( ) + " into NetCDF-Java 4 API" ) ) ; if ( lookup . getComment ( ) != null ) ncfile . addAttribute ( null , new Attribute ( "comment" , lookup . getComment ( ) ) ) ; // dataset discovery //if ( center != null) //  ncfile.addAttribute(null, new Attribute("center_name", center)); // CDM attributes ncfile . addAttribute ( null , new Attribute ( CF . FEATURE_TYPE , FeatureType . GRID . toString ( ) ) ) ; ncfile . addAttribute ( null , new Attribute ( "file_format" , lookup . getGridType ( ) ) ) ; // ncfile.addAttribute(null, new Attribute("location", ncfile.getLocation())); ncfile . addAttribute ( null , new Attribute ( _Coordinate . ModelRunDate , formatter . toDateTimeStringISO ( lookup . getFirstBaseTime ( ) ) ) ) ; /* if (fmrcCoordSys != null) {
      makeDefinedCoordSys(ncfile, lookup, fmrcCoordSys);
    } else {
      makeDenseCoordSys(ncfile, lookup, cancelTask);
    } */ makeDenseCoordSys ( ncfile , lookup , cancelTask ) ; if ( GridServiceProvider . debugMissing ) { try ( Formatter f = new Formatter ( System . out ) ) { int count = 0 ; Collection < GridHorizCoordSys > hcset = hcsHash . values ( ) ; for ( GridHorizCoordSys hcs : hcset ) { List < GridVariable > gribvars = new ArrayList <> ( hcs . varHash . values ( ) ) ; for ( GridVariable gv : gribvars ) { count += gv . showMissingSummary ( f ) ; } } System . out . println ( " total missing= " + count ) ; } } if ( GridServiceProvider . debugMissingDetails ) { Formatter f = new Formatter ( ) ; Collection < GridHorizCoordSys > hcset = hcsHash . values ( ) ; for ( GridHorizCoordSys hcs : hcset ) { f . format ( "******** Horiz Coordinate= %s%n" , hcs . getGridName ( ) ) ; String lastVertDesc = null ; List < GridVariable > gribvars = new ArrayList <> ( hcs . varHash . values ( ) ) ; Collections . sort ( gribvars , new CompareGridVariableByVertName ( ) ) ; for ( GridVariable gv : gribvars ) { String vertDesc = gv . getVertName ( ) ; if ( ! vertDesc . equals ( lastVertDesc ) ) { f . format ( "---Vertical Coordinate= %s%n" , vertDesc ) ; lastVertDesc = vertDesc ; } gv . showMissing ( f ) ; } } } // clean out stuff we dont need anymore //for (GridHorizCoordSys ghcs : hcsHash.values()) { //  ghcs.empty(); //} }
Fill in the netCDF file

@param index        grid index
@param lookup       lookup table
@param version      version of data
@param ncfile       netCDF file to fill in
@param cancelTask   cancel task
@throws IOException Problem reading from the file
Fill in the netCDF file