/** Create the WCS using the definition given in the FITS header. */
  public WCS(Header h) throws TransformationException {

    wcsKeys = new HashMap<String, Object>();

    this.h = h;
    headerNaxis = new int[2];
    if (checkDSS()) {
      headerNaxis[0] = h.getIntValue("NAXIS1");
      headerNaxis[1] = h.getIntValue("NAXIS2");
      if (headerNaxis[0] == 0) {
        headerNaxis[0] = h.getIntValue("XPIXELS");
        headerNaxis[1] = h.getIntValue("YPIXELS");
      }
      doDSSWCS();
      stdWCS = false;

    } else if (checkNeat()) {
      headerNaxis[0] = h.getIntValue("NAXIS1");
      headerNaxis[1] = h.getIntValue("NAXIS2");
      doNeatWCS();
      stdWCS = false;

    } else { // More or less standard FITS WCS

      getAxes();
      if (lonAxis == -1 || latAxis == -1) {
        throw new TransformationException("Unable to find coordinate axes");
      }
      headerNaxis[0] = h.getIntValue("NAXIS" + lonAxis);
      headerNaxis[1] = h.getIntValue("NAXIS" + latAxis);
      extractCoordinateSystem();
      extractProjection();
      extractScaler();
    }
  }
  /**
   * Find which axes are being used for coordinates. Normally this will just be 1 and 2, but
   * occasionally we may be surprised.
   */
  private void getAxes() {

    int naxes = h.getIntValue("NAXIS");

    // The first axes match are assumed to be correct.
    for (int i = 1; i <= naxes; i += 1) {
      String axis = h.getStringValue("CTYPE" + i);
      if (axis != null && axis.length() >= 4) {
        axis = axis.substring(0, 4);
      } else {
        continue;
      }

      if (lonAxis == -1) {
        if (axis.equals("RA--")
            || axis.equals("GLON")
            || axis.equals("ELON")
            || axis.equals("HLON")) {
          lonAxis = i;
        }
      }
      if (latAxis == -1) {
        if (axis.equals("DEC-")
            || axis.equals("GLAT")
            || axis.equals("ELAT")
            || axis.equals("HLAT")) {
          latAxis = i;
        }
      }
    }
    if (lonAxis > -1) {
      wcsKeys.put("CTYPE1", h.getStringValue("CTYPE" + lonAxis));
    }
    if (latAxis > -1) {
      wcsKeys.put("CTYPE2", h.getStringValue("CTYPE" + latAxis));
    }
  }