visad/src/main/java/ucar/nc2/iosp/vis5d/Vis5DIosp.java
private void makeFile(RandomAccessFile raf, NetcdfFile ncfile, CancelTask cancelTask) throws IOException {


    ncfile.empty();
    int[] sizes = new int[5];
    int[] map_proj = new int[1];
    String[] varnames = new String[MAXVARS];
    String[] varunits = new String[MAXVARS];
    int[] n_levels = new int[MAXVARS];
    int[] vert_sys = new int[1];
    float[] vertargs = new float[MAXVERTARGS];
    double[] times = new double[MAXTIMES];
    float[] projargs = new float[MAXPROJARGS];

    try {
      v5dstruct = V5DStruct.v5d_open(raf, sizes, n_levels, varnames,
              varunits, map_proj, projargs,
              vert_sys, vertargs, times);
    } catch (BadFormException bfe) {
      throw new IOException("Vis5DIosp.makeFile: bad file "
              + bfe.getMessage());
    }

    if (sizes[0] < 1) {
      throw new IOException("Vis5DIosp.makeFile: bad file");
    }

    int nr = sizes[0];
    int nc = sizes[1];
    int nl = sizes[2];
    int ntimes = sizes[3];
    int nvars = sizes[4];
    // System.out.println("nr: "+nr);
    // System.out.println("nc: "+nc);
    // System.out.println("nl: "+nl);
    // System.out.println("ntimes: "+ntimes);
    // System.out.println("nvars: "+nvars);

    Dimension time = new Dimension(TIME, ntimes, true);
    Dimension row = new Dimension(ROW, nr, true);
    Dimension col = new Dimension(COLUMN, nc, true);
    ncfile.addDimension(null, time);
    ncfile.addDimension(null, row);
    ncfile.addDimension(null, col);

    // time
    Variable timeVar = new Variable(ncfile, null, null, TIME);
    timeVar.setDataType(DataType.DOUBLE);
    timeVar.setDimensions(TIME);
    timeVar.addAttribute(
            new Attribute(CDM.UNITS, "seconds since 1900-01-01 00:00:00"));
    timeVar.addAttribute(new Attribute("long_name", TIME));
    timeVar.addAttribute(new Attribute(_Coordinate.AxisType,
            AxisType.Time.toString()));
    Array varArray = new ArrayDouble.D1(ntimes);
    for (int i = 0; i < ntimes; i++) {
      ((ArrayDouble.D1) varArray).set(i, times[i]);
    }
    timeVar.setCachedData(varArray, false);
    ncfile.addVariable(null, timeVar);

    // rows and columns
    Variable rowVar = new Variable(ncfile, null, null, ROW);
    rowVar.setDataType(DataType.INT);
    rowVar.setDimensions(ROW);
    varArray = new ArrayInt.D1(nr, false);
    for (int i = 0; i < nr; i++) {
      ((ArrayInt.D1) varArray).set(i, i);
    }
    rowVar.setCachedData(varArray, false);
    ncfile.addVariable(null, rowVar);

    Variable colVar = new Variable(ncfile, null, null, COLUMN);
    colVar.setDataType(DataType.INT);
    colVar.setDimensions(COLUMN);
    varArray = new ArrayInt.D1(nc, false);
    for (int i = 0; i < nc; i++) {
      ((ArrayInt.D1) varArray).set(i, i);
    }
    colVar.setCachedData(varArray, false);
    ncfile.addVariable(null, colVar);

    // sanity check on levels
    Hashtable<Integer, Object> var_table = new Hashtable<>();
    boolean have3D = false;
    for (int i = 0; i < nvars; i++) {
      int nlevs = n_levels[i];
      if (!have3D && (nlevs > 1)) {
        have3D = true;
      }
      var_table.put(nlevs, new Object());
    }
    int n_var_groups = var_table.size();
    if (n_var_groups > 2) {
      throw new IOException(
              "Vis5DIosp.makeFile: more than two variable groups by n_levels");
    } else if (n_var_groups == 0) {
      throw new IOException(
              "Vis5DIosp.makeFile: number of variable groups == 0");
    }
    Variable vert = null;
    if (have3D) {
      Dimension lev = new Dimension(LEVEL, nl, true);
      ncfile.addDimension(null, lev);
      vert = makeVerticalVariable(vert_sys[0], nl, vertargs);
      if (vert != null) {
        ncfile.addVariable(null, vert);
      }
    }
    varTable = new Hashtable<>();
    String dim3D = TIME + " " + LEVEL + " " + COLUMN + " " + ROW;
    String dim2D = TIME + " " + COLUMN + " " + ROW;
    //String coords3D = TIME + " " + vert.getName() + " " + LAT + " " + LON;
    String coords3D = "unknown";
    if (vert != null) {
      coords3D = TIME + " Height " + LAT + " " + LON;
    }
    String coords2D = TIME + " " + LAT + " " + LON;

    for (int i = 0; i < nvars; i++) {
      Variable v = new Variable(ncfile, null, null, varnames[i]);
      if (n_levels[i] > 1) {
        v.setDimensions(dim3D);
        v.addAttribute(new Attribute(CF.COORDINATES, coords3D));
      } else {
        v.setDimensions(dim2D);
        v.addAttribute(new Attribute(CF.COORDINATES, coords2D));
      }
      v.setDataType(DataType.FLOAT);
      String units = varunits[i].trim();
      if (units.equals("")) {  // see if its in the unitTable
        String key = varnames[i].trim().toLowerCase();
        units = unitTable.get(key);
      }
      if (units != null) {
        v.addAttribute(new Attribute(CDM.UNITS, units));
      }
      // TODO: do two vars with the same name have different values?
      // check agaist duplicat variable names
      if (varTable.get(v) == null) {
        varTable.put(v, i);
        ncfile.addVariable(null, v);
      }
    }
    double[][] proj_args = Set.floatToDouble(new float[][]{
            projargs
    });
    addLatLonVariables(map_proj[0], proj_args[0], nr, nc);
    // Vis5DGridDefRecord gridDef = new Vis5DGridDefRecord(map_proj[0], proj_args[0], nr, nc);
    ncfile.addAttribute(null, new Attribute("Conventions", "CF-1.0"));
    ncfile.finish();
  }
private void makeFile(RandomAccessFile raf, NetcdfFile ncfile, CancelTask cancelTask) throws IOException {


    ncfile.empty();
    int[] sizes = new int[5];
    int[] map_proj = new int[1];
    String[] varnames = new String[MAXVARS];
    String[] varunits = new String[MAXVARS];
    int[] n_levels = new int[MAXVARS];
    int[] vert_sys = new int[1];
    float[] vertargs = new float[MAXVERTARGS];
    double[] times = new double[MAXTIMES];
    float[] projargs = new float[MAXPROJARGS];

    try {
      v5dstruct = V5DStruct.v5d_open(raf, sizes, n_levels, varnames,
              varunits, map_proj, projargs,
              vert_sys, vertargs, times);
    } catch (BadFormException bfe) {
      throw new IOException("Vis5DIosp.makeFile: bad file "
              + bfe.getMessage());
    }

    if (sizes[0] < 1) {
      throw new IOException("Vis5DIosp.makeFile: bad file");
    }

    int nr = sizes[0];
    int nc = sizes[1];
    int nl = sizes[2];
    int ntimes = sizes[3];
    int nvars = sizes[4];
    // System.out.println("nr: "+nr);
    // System.out.println("nc: "+nc);
    // System.out.println("nl: "+nl);
    // System.out.println("ntimes: "+ntimes);
    // System.out.println("nvars: "+nvars);

    Dimension time = new Dimension(TIME, ntimes, true);
    Dimension row = new Dimension(ROW, nr, true);
    Dimension col = new Dimension(COLUMN, nc, true);
    ncfile.addDimension(null, time);
    ncfile.addDimension(null, row);
    ncfile.addDimension(null, col);

    // time
    Variable timeVar = new Variable(ncfile, null, null, TIME);
    timeVar.setDataType(DataType.DOUBLE);
    timeVar.setDimensions(TIME);
    timeVar.addAttribute(
            new Attribute(CDM.UNITS, "seconds since 1900-01-01 00:00:00"));
    timeVar.addAttribute(new Attribute("long_name", TIME));
    timeVar.addAttribute(new Attribute(_Coordinate.AxisType,
            AxisType.Time.toString()));
    Array varArray = new ArrayDouble.D1(ntimes);
    for (int i = 0; i < ntimes; i++) {
      ((ArrayDouble.D1) varArray).set(i, times[i]);
    }
    timeVar.setCachedData(varArray, false);
    ncfile.addVariable(null, timeVar);

    // rows and columns
    Variable rowVar = new Variable(ncfile, null, null, ROW);
    rowVar.setDataType(DataType.INT);
    rowVar.setDimensions(ROW);
    varArray = new ArrayInt.D1(nr, false);
    for (int i = 0; i < nr; i++) {
      ((ArrayInt.D1) varArray).set(i, i);
    }
    rowVar.setCachedData(varArray, false);
    ncfile.addVariable(null, rowVar);

    Variable colVar = new Variable(ncfile, null, null, COLUMN);
    colVar.setDataType(DataType.INT);
    colVar.setDimensions(COLUMN);
    varArray = new ArrayInt.D1(nc, false);
    for (int i = 0; i < nc; i++) {
      ((ArrayInt.D1) varArray).set(i, i);
    }
    colVar.setCachedData(varArray, false);
    ncfile.addVariable(null, colVar);

    // sanity check on levels
    Hashtable<Integer, Object> var_table = new Hashtable<>();
    boolean have3D = false;
    for (int i = 0; i < nvars; i++) {
      int nlevs = n_levels[i];
      if (!have3D && (nlevs > 1)) {
        have3D = true;
      }
      var_table.put(nlevs, new Object());
    }
    int n_var_groups = var_table.size();
    if (n_var_groups > 2) {
      throw new IOException(
              "Vis5DIosp.makeFile: more than two variable groups by n_levels");
    } else if (n_var_groups == 0) {
      throw new IOException(
              "Vis5DIosp.makeFile: number of variable groups == 0");
    }
    Variable vert = null;
    if (have3D) {
      Dimension lev = new Dimension(LEVEL, nl, true);
      ncfile.addDimension(null, lev);
      vert = makeVerticalVariable(vert_sys[0], nl, vertargs);
      if (vert != null) {
        ncfile.addVariable(null, vert);
      }
    }
    varTable = new Hashtable<>();
    String dim3D = TIME + " " + LEVEL + " " + COLUMN + " " + ROW;
    String dim2D = TIME + " " + COLUMN + " " + ROW;
    //String coords3D = TIME + " " + vert.getName() + " " + LAT + " " + LON;
    String coords3D = "unknown";
    if (vert != null) {
      coords3D = TIME + " Height " + LAT + " " + LON;
    }
    String coords2D = TIME + " " + LAT + " " + LON;

    for (int i = 0; i < nvars; i++) {
      Variable v = new Variable(ncfile, null, null, varnames[i]);
      if (n_levels[i] > 1) {
        v.setDimensions(dim3D);
        v.addAttribute(new Attribute(CF.COORDINATES, coords3D));
      } else {
        v.setDimensions(dim2D);
        v.addAttribute(new Attribute(CF.COORDINATES, coords2D));
      }
      v.setDataType(DataType.FLOAT);
      String units = varunits[i].trim();
      if (units.equals("")) {  // see if its in the unitTable
        String key = varnames[i].trim().toLowerCase();
        units = unitTable.get(key);
      }
      if (units != null) {
        v.addAttribute(new Attribute(CDM.UNITS, units));
      }
      // TODO: do two vars with the same name have different values?
      // check agaist duplicat variable names
      if (varTable.get(v) == null) {
        varTable.put(v, i);
        ncfile.addVariable(null, v);
      }
    }
    double[][] proj_args = Set.floatToDouble(new float[][]{
            projargs
    });
    addLatLonVariables(map_proj[0], proj_args[0], nr, nc);
    // Vis5DGridDefRecord gridDef = new Vis5DGridDefRecord(map_proj[0], proj_args[0], nr, nc);
    ncfile.addAttribute(null, new Attribute("Conventions", "CF-1.0"));
    ncfile.finish();
  }
Make the netcdf file

@param raf        the RandomAccessFile
@param ncfile     the netCDF file handle
@param cancelTask the cancel task
@throws IOException problem reading the file