visad/src/main/java/ucar/nc2/iosp/mcidas/AreaReader.java
public boolean init(String location, NetcdfFile ncfile) throws AreaFileException {

    af = new AreaFile(location);

    //read metadata
    dirBlock = af.getDir();
    ad = af.getAreaDirectory();
    int numElements = ad.getElements();
    int numLines = ad.getLines();
    int numBands = ad.getNumberOfBands();
    bandMap = ad.getBands();
    navBlock = af.getNav();
    Date nomTime = ad.getNominalTime();
    DateFormatter df = new DateFormatter();
    try {
      nav = AREAnav.makeAreaNav(navBlock, af.getAux());
    } catch (McIDASException me) {
      throw new AreaFileException(me.getMessage());
    }
    int sensor = dirBlock[AreaFile.AD_SENSORID];
    String calName = McIDASUtil.intBitsToString(dirBlock[AreaFile.AD_CALTYPE]);
    int calType = getCalType(calName);

    // TODO:  Need to support calibrated data.
    if ((af.getCal() != null)
            && CalibratorFactory.hasCalibrator(sensor)) {
      //System.out.println("can calibrate");
      try {
        calibrator = CalibratorFactory.getCalibrator(sensor, calType, af.getCal());
      } catch (CalibratorException ce) {
        // System.out.println("can't make calibrator");
        calibrator = null;
      }
      //System.out.println("calibrator = " + calibrator);

    }
    calUnit = ad.getCalibrationUnitName();
    calScale = (1.0f / ad.getCalibrationScaleFactor());

    // make the dimensions
    Dimension elements = new Dimension("elements", numElements, true);
    Dimension lines = new Dimension("lines", numLines, true);
    Dimension bands = new Dimension("bands", numBands, true);
    Dimension time = new Dimension("time", 1, true);
    Dimension dirDim = new Dimension("dirSize", AreaFile.AD_DIRSIZE,
            true);
    Dimension navDim = new Dimension("navSize", navBlock.length, true);
    List<Dimension> image = new ArrayList<>();
    image.add(time);
    image.add(bands);
    image.add(lines);
    image.add(elements);
    ncfile.addDimension(null, elements);
    ncfile.addDimension(null, lines);
    ncfile.addDimension(null, bands);
    ncfile.addDimension(null, time);
    ncfile.addDimension(null, dirDim);
    ncfile.addDimension(null, navDim);


    Array varArray;

    // make the variables

    // time
    Variable timeVar = new Variable(ncfile, null, null, "time");
    timeVar.setDataType(DataType.INT);
    timeVar.setDimensions("time");
    timeVar.addAttribute(new Attribute(CDM.UNITS,
            "seconds since "
                    + df.toDateTimeString(nomTime)));
    timeVar.addAttribute(new Attribute("long_name", "time"));
    varArray = new ArrayInt.D1(1, false);
    ((ArrayInt.D1) varArray).set(0, 0);
    timeVar.setCachedData(varArray, false);
    ncfile.addVariable(null, timeVar);


    // lines and elements
    Variable lineVar = new Variable(ncfile, null, null, "lines");
    lineVar.setDataType(DataType.INT);
    lineVar.setDimensions("lines");
    //lineVar.addAttribute(new Attribute(CDM.UNITS, "km"));
    lineVar.addAttribute(new Attribute("standard_name",
            "projection_y_coordinate"));
    varArray = new ArrayInt.D1(numLines, false);
    for (int i = 0; i < numLines; i++) {
      int pos = nav.isFlippedLineCoordinates()
              ? i
              : numLines - i - 1;
      ((ArrayInt.D1) varArray).set(i, pos);
    }
    lineVar.setCachedData(varArray, false);
    ncfile.addVariable(null, lineVar);

    Variable elementVar = new Variable(ncfile, null, null, "elements");
    elementVar.setDataType(DataType.INT);
    elementVar.setDimensions("elements");
    //elementVar.addAttribute(new Attribute(CDM.UNITS, "km"));
    elementVar.addAttribute(new Attribute("standard_name",
            "projection_x_coordinate"));
    varArray = new ArrayInt.D1(numElements, false);
    for (int i = 0; i < numElements; i++) {
      ((ArrayInt.D1) varArray).set(i, i);
    }
    elementVar.setCachedData(varArray, false);
    ncfile.addVariable(null, elementVar);


    // TODO: handle bands and calibrations
    Variable bandVar = new Variable(ncfile, null, null, "bands");
    bandVar.setDataType(DataType.INT);
    bandVar.setDimensions("bands");
    bandVar.addAttribute(new Attribute("long_name",
            "spectral band number"));
    bandVar.addAttribute(new Attribute("axis", "Z"));
    Array bandArray = new ArrayInt.D1(numBands, false);
    for (int i = 0; i < numBands; i++) {
      ((ArrayInt.D1) bandArray).set(i, bandMap[i]);
    }
    bandVar.setCachedData(bandArray, false);
    ncfile.addVariable(null, bandVar);

    // the image
    Variable imageVar = new Variable(ncfile, null, null, "image");
    imageVar.setDataType(DataType.INT);
    imageVar.setDimensions(image);
    setCalTypeAttributes(imageVar, getCalType(calName));
    imageVar.addAttribute(new Attribute(getADDescription(AreaFile.AD_CALTYPE),
            calName));
    imageVar.addAttribute(new Attribute("bands", bandArray));
    imageVar.addAttribute(new Attribute("grid_mapping", "AREAnav"));
    ncfile.addVariable(null, imageVar);


    Variable dirVar = new Variable(ncfile, null, null, "areaDirectory");
    dirVar.setDataType(DataType.INT);
    dirVar.setDimensions("dirSize");
    setAreaDirectoryAttributes(dirVar);
    ArrayInt.D1 dirArray = new ArrayInt.D1(AreaFile.AD_DIRSIZE, false);
    for (int i = 0; i < AreaFile.AD_DIRSIZE; i++) {
      dirArray.set(i, dirBlock[i]);
    }
    dirVar.setCachedData(dirArray, false);
    ncfile.addVariable(null, dirVar);

    Variable navVar = new Variable(ncfile, null, null, "navBlock");
    navVar.setDataType(DataType.INT);
    navVar.setDimensions("navSize");
    setNavBlockAttributes(navVar);
    ArrayInt.D1 navArray = new ArrayInt.D1(navBlock.length, false);
    for (int i = 0; i < navBlock.length; i++) {
      navArray.set(i, navBlock[i]);
    }
    navVar.setCachedData(navArray, false);
    ncfile.addVariable(null, navVar);


    // projection variable
    ProjectionImpl projection = new McIDASAreaProjection(af);
    Variable proj = new Variable(ncfile, null, null, "AREAnav");
    proj.setDataType(DataType.CHAR);
    proj.setDimensions("");

    for (Parameter p : projection.getProjectionParameters()) {
      proj.addAttribute(new Attribute(p));
    }

    // For now, we have to overwrite the parameter versions of thes
    proj.addAttribute(new Attribute("grid_mapping_name", McIDASAreaProjection.GRID_MAPPING_NAME));
        /*
        proj.addAttribute(new Attribute(McIDASAreaProjection.ATTR_AREADIR,
                                        dirArray));
        proj.addAttribute(new Attribute(McIDASAreaProjection.ATTR_NAVBLOCK,
                                        navArray));
        */
    varArray = new ArrayChar.D0();
    ((ArrayChar.D0) varArray).set(' ');
    proj.setCachedData(varArray, false);

    ncfile.addVariable(null, proj);

    // add global attributes
    ncfile.addAttribute(null, new Attribute("Conventions", "CF-1.0"));
    ncfile.addAttribute(null, new Attribute(CF.FEATURE_TYPE, FeatureType.GRID.toString()));
    ncfile.addAttribute(null, new Attribute("nominal_image_time", df.toDateTimeString(nomTime)));

    String encStr = "netCDF encoded on " + df.toDateTimeString(new Date());
    ncfile.addAttribute(null, new Attribute("history", encStr));

    //Lastly, finish the file
    ncfile.finish();
    return true;
  }
public boolean init(String location, NetcdfFile ncfile) throws AreaFileException {

    af = new AreaFile(location);

    //read metadata
    dirBlock = af.getDir();
    ad = af.getAreaDirectory();
    int numElements = ad.getElements();
    int numLines = ad.getLines();
    int numBands = ad.getNumberOfBands();
    bandMap = ad.getBands();
    navBlock = af.getNav();
    Date nomTime = ad.getNominalTime();
    DateFormatter df = new DateFormatter();
    try {
      nav = AREAnav.makeAreaNav(navBlock, af.getAux());
    } catch (McIDASException me) {
      throw new AreaFileException(me.getMessage());
    }
    int sensor = dirBlock[AreaFile.AD_SENSORID];
    String calName = McIDASUtil.intBitsToString(dirBlock[AreaFile.AD_CALTYPE]);
    int calType = getCalType(calName);

    // TODO:  Need to support calibrated data.
    if ((af.getCal() != null)
            && CalibratorFactory.hasCalibrator(sensor)) {
      //System.out.println("can calibrate");
      try {
        calibrator = CalibratorFactory.getCalibrator(sensor, calType, af.getCal());
      } catch (CalibratorException ce) {
        // System.out.println("can't make calibrator");
        calibrator = null;
      }
      //System.out.println("calibrator = " + calibrator);

    }
    calUnit = ad.getCalibrationUnitName();
    calScale = (1.0f / ad.getCalibrationScaleFactor());

    // make the dimensions
    Dimension elements = new Dimension("elements", numElements, true);
    Dimension lines = new Dimension("lines", numLines, true);
    Dimension bands = new Dimension("bands", numBands, true);
    Dimension time = new Dimension("time", 1, true);
    Dimension dirDim = new Dimension("dirSize", AreaFile.AD_DIRSIZE,
            true);
    Dimension navDim = new Dimension("navSize", navBlock.length, true);
    List<Dimension> image = new ArrayList<>();
    image.add(time);
    image.add(bands);
    image.add(lines);
    image.add(elements);
    ncfile.addDimension(null, elements);
    ncfile.addDimension(null, lines);
    ncfile.addDimension(null, bands);
    ncfile.addDimension(null, time);
    ncfile.addDimension(null, dirDim);
    ncfile.addDimension(null, navDim);


    Array varArray;

    // make the variables

    // time
    Variable timeVar = new Variable(ncfile, null, null, "time");
    timeVar.setDataType(DataType.INT);
    timeVar.setDimensions("time");
    timeVar.addAttribute(new Attribute(CDM.UNITS,
            "seconds since "
                    + df.toDateTimeString(nomTime)));
    timeVar.addAttribute(new Attribute("long_name", "time"));
    varArray = new ArrayInt.D1(1, false);
    ((ArrayInt.D1) varArray).set(0, 0);
    timeVar.setCachedData(varArray, false);
    ncfile.addVariable(null, timeVar);


    // lines and elements
    Variable lineVar = new Variable(ncfile, null, null, "lines");
    lineVar.setDataType(DataType.INT);
    lineVar.setDimensions("lines");
    //lineVar.addAttribute(new Attribute(CDM.UNITS, "km"));
    lineVar.addAttribute(new Attribute("standard_name",
            "projection_y_coordinate"));
    varArray = new ArrayInt.D1(numLines, false);
    for (int i = 0; i < numLines; i++) {
      int pos = nav.isFlippedLineCoordinates()
              ? i
              : numLines - i - 1;
      ((ArrayInt.D1) varArray).set(i, pos);
    }
    lineVar.setCachedData(varArray, false);
    ncfile.addVariable(null, lineVar);

    Variable elementVar = new Variable(ncfile, null, null, "elements");
    elementVar.setDataType(DataType.INT);
    elementVar.setDimensions("elements");
    //elementVar.addAttribute(new Attribute(CDM.UNITS, "km"));
    elementVar.addAttribute(new Attribute("standard_name",
            "projection_x_coordinate"));
    varArray = new ArrayInt.D1(numElements, false);
    for (int i = 0; i < numElements; i++) {
      ((ArrayInt.D1) varArray).set(i, i);
    }
    elementVar.setCachedData(varArray, false);
    ncfile.addVariable(null, elementVar);


    // TODO: handle bands and calibrations
    Variable bandVar = new Variable(ncfile, null, null, "bands");
    bandVar.setDataType(DataType.INT);
    bandVar.setDimensions("bands");
    bandVar.addAttribute(new Attribute("long_name",
            "spectral band number"));
    bandVar.addAttribute(new Attribute("axis", "Z"));
    Array bandArray = new ArrayInt.D1(numBands, false);
    for (int i = 0; i < numBands; i++) {
      ((ArrayInt.D1) bandArray).set(i, bandMap[i]);
    }
    bandVar.setCachedData(bandArray, false);
    ncfile.addVariable(null, bandVar);

    // the image
    Variable imageVar = new Variable(ncfile, null, null, "image");
    imageVar.setDataType(DataType.INT);
    imageVar.setDimensions(image);
    setCalTypeAttributes(imageVar, getCalType(calName));
    imageVar.addAttribute(new Attribute(getADDescription(AreaFile.AD_CALTYPE),
            calName));
    imageVar.addAttribute(new Attribute("bands", bandArray));
    imageVar.addAttribute(new Attribute("grid_mapping", "AREAnav"));
    ncfile.addVariable(null, imageVar);


    Variable dirVar = new Variable(ncfile, null, null, "areaDirectory");
    dirVar.setDataType(DataType.INT);
    dirVar.setDimensions("dirSize");
    setAreaDirectoryAttributes(dirVar);
    ArrayInt.D1 dirArray = new ArrayInt.D1(AreaFile.AD_DIRSIZE, false);
    for (int i = 0; i < AreaFile.AD_DIRSIZE; i++) {
      dirArray.set(i, dirBlock[i]);
    }
    dirVar.setCachedData(dirArray, false);
    ncfile.addVariable(null, dirVar);

    Variable navVar = new Variable(ncfile, null, null, "navBlock");
    navVar.setDataType(DataType.INT);
    navVar.setDimensions("navSize");
    setNavBlockAttributes(navVar);
    ArrayInt.D1 navArray = new ArrayInt.D1(navBlock.length, false);
    for (int i = 0; i < navBlock.length; i++) {
      navArray.set(i, navBlock[i]);
    }
    navVar.setCachedData(navArray, false);
    ncfile.addVariable(null, navVar);


    // projection variable
    ProjectionImpl projection = new McIDASAreaProjection(af);
    Variable proj = new Variable(ncfile, null, null, "AREAnav");
    proj.setDataType(DataType.CHAR);
    proj.setDimensions("");

    for (Parameter p : projection.getProjectionParameters()) {
      proj.addAttribute(new Attribute(p));
    }

    // For now, we have to overwrite the parameter versions of thes
    proj.addAttribute(new Attribute("grid_mapping_name", McIDASAreaProjection.GRID_MAPPING_NAME));
        /*
        proj.addAttribute(new Attribute(McIDASAreaProjection.ATTR_AREADIR,
                                        dirArray));
        proj.addAttribute(new Attribute(McIDASAreaProjection.ATTR_NAVBLOCK,
                                        navArray));
        */
    varArray = new ArrayChar.D0();
    ((ArrayChar.D0) varArray).set(' ');
    proj.setCachedData(varArray, false);

    ncfile.addVariable(null, proj);

    // add global attributes
    ncfile.addAttribute(null, new Attribute("Conventions", "CF-1.0"));
    ncfile.addAttribute(null, new Attribute(CF.FEATURE_TYPE, FeatureType.GRID.toString()));
    ncfile.addAttribute(null, new Attribute("nominal_image_time", df.toDateTimeString(nomTime)));

    String encStr = "netCDF encoded on " + df.toDateTimeString(new Date());
    ncfile.addAttribute(null, new Attribute("history", encStr));

    //Lastly, finish the file
    ncfile.finish();
    return true;
  }
initialize; note that the file is reopened here

@param location    the AREA file to open
@param ncfile the netCDF file to fill out
@return true if successful
@throws AreaFileException problem opening the area file