visad/src/main/java/ucar/nc2/iosp/vis5d/Vis5DIosp.java
private void addLatLonVariables(int map_proj, double[] proj_args, int nr,
                                  int nc)
          throws IOException {
    //Vis5DGridDefRecord.printProjArgs(map_proj, proj_args);
    Vis5DGridDefRecord vgd = new Vis5DGridDefRecord(map_proj, proj_args, nr, nc);
    GridHorizCoordSys ghc = new GridHorizCoordSys(vgd, new Vis5DLookup(), null);

    Vis5DCoordinateSystem coord_sys;
    try {
      coord_sys = new Vis5DCoordinateSystem(map_proj, proj_args, nr,
              nc);

      Variable lat = new Variable(ncfile, null, null, LAT);
      lat.setDimensions(COLUMN + " " + ROW);
      lat.setDataType(DataType.DOUBLE);
      lat.addAttribute(new Attribute("long_name", "latitude"));
      lat.addAttribute(new Attribute(CDM.UNITS, CDM.LAT_UNITS));
      lat.addAttribute(new Attribute(CF.STANDARD_NAME, "latitude"));
      lat.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lat.toString()));
      ncfile.addVariable(null, lat);

      Variable lon = new Variable(ncfile, null, null, LON);
      lon.setDimensions(COLUMN + " " + ROW);
      lon.setDataType(DataType.DOUBLE);
      lon.addAttribute(new Attribute(CDM.UNITS, CDM.LON_UNITS));
      lon.addAttribute(new Attribute("long_name", "longitude"));
      lon.addAttribute(new Attribute(CF.STANDARD_NAME, "longitude"));
      lon.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lon.toString()));
      ncfile.addVariable(null, lon);

      int[] shape = new int[]{nc, nr};
      Array latArray = Array.factory(DataType.DOUBLE, shape);
      Array lonArray = Array.factory(DataType.DOUBLE, shape);
      double[][] rowcol = new double[2][nr * nc];
      for (int x = 0; x < nc; x++) {
        for (int y = 0; y < nr; y++) {
          int index = x * nr + y;
          rowcol[0][index] = y;
          rowcol[1][index] = x;
        }
      }
      double[][] latlon = coord_sys.toReference(rowcol);
      Index latIndex = latArray.getIndex();
      Index lonIndex = lonArray.getIndex();
            /*
            for (int y = 0; y < nr; y++) {
                for (int x = 0; x < nc; x++) {
                    int index = y * nc + x;
            */
      for (int x = 0; x < nc; x++) {
        for (int y = 0; y < nr; y++) {
          int index = x * nr + y;
                    /*
                    latArray.setDouble(latIndex.set(x, y), latlon[0][index]);
                    lonArray.setDouble(lonIndex.set(x, y), latlon[1][index]);
                    */
          latArray.setDouble(index, latlon[0][index]);
          lonArray.setDouble(index, latlon[1][index]);
        }
      }
      lat.setCachedData(latArray, false);
      lon.setCachedData(lonArray, false);
    } catch (VisADException ve) {
      throw new IOException("Vis5DIosp.addLatLon: " + ve.getMessage());
    }
  }
private void addLatLonVariables(int map_proj, double[] proj_args, int nr,
                                  int nc)
          throws IOException {
    //Vis5DGridDefRecord.printProjArgs(map_proj, proj_args);
    Vis5DGridDefRecord vgd = new Vis5DGridDefRecord(map_proj, proj_args, nr, nc);
    GridHorizCoordSys ghc = new GridHorizCoordSys(vgd, new Vis5DLookup(), null);

    Vis5DCoordinateSystem coord_sys;
    try {
      coord_sys = new Vis5DCoordinateSystem(map_proj, proj_args, nr,
              nc);

      Variable lat = new Variable(ncfile, null, null, LAT);
      lat.setDimensions(COLUMN + " " + ROW);
      lat.setDataType(DataType.DOUBLE);
      lat.addAttribute(new Attribute("long_name", "latitude"));
      lat.addAttribute(new Attribute(CDM.UNITS, CDM.LAT_UNITS));
      lat.addAttribute(new Attribute(CF.STANDARD_NAME, "latitude"));
      lat.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lat.toString()));
      ncfile.addVariable(null, lat);

      Variable lon = new Variable(ncfile, null, null, LON);
      lon.setDimensions(COLUMN + " " + ROW);
      lon.setDataType(DataType.DOUBLE);
      lon.addAttribute(new Attribute(CDM.UNITS, CDM.LON_UNITS));
      lon.addAttribute(new Attribute("long_name", "longitude"));
      lon.addAttribute(new Attribute(CF.STANDARD_NAME, "longitude"));
      lon.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lon.toString()));
      ncfile.addVariable(null, lon);

      int[] shape = new int[]{nc, nr};
      Array latArray = Array.factory(DataType.DOUBLE, shape);
      Array lonArray = Array.factory(DataType.DOUBLE, shape);
      double[][] rowcol = new double[2][nr * nc];
      for (int x = 0; x < nc; x++) {
        for (int y = 0; y < nr; y++) {
          int index = x * nr + y;
          rowcol[0][index] = y;
          rowcol[1][index] = x;
        }
      }
      double[][] latlon = coord_sys.toReference(rowcol);
      Index latIndex = latArray.getIndex();
      Index lonIndex = lonArray.getIndex();
            /*
            for (int y = 0; y < nr; y++) {
                for (int x = 0; x < nc; x++) {
                    int index = y * nc + x;
            */
      for (int x = 0; x < nc; x++) {
        for (int y = 0; y < nr; y++) {
          int index = x * nr + y;
                    /*
                    latArray.setDouble(latIndex.set(x, y), latlon[0][index]);
                    lonArray.setDouble(lonIndex.set(x, y), latlon[1][index]);
                    */
          latArray.setDouble(index, latlon[0][index]);
          lonArray.setDouble(index, latlon[1][index]);
        }
      }
      lat.setCachedData(latArray, false);
      lon.setCachedData(lonArray, false);
    } catch (VisADException ve) {
      throw new IOException("Vis5DIosp.addLatLon: " + ve.getMessage());
    }
  }
Add lat/lon variables to the file

@param map_proj  the map projection identifier
@param proj_args the projection args
@param nr        number of rows
@param nc        number of columns
@throws IOException Problem making the projection