@Override
    public void parse(InputStream stream, ContentHandler handler, Metadata metadata,
                      ParseContext context) throws IOException, SAXException, TikaException {

        if (!ExternalParser.check("gdalinfo")) {
            return;
        }

        // first set up and run GDAL
        // process the command
        TemporaryResources tmp = new TemporaryResources();
        TikaInputStream tis = TikaInputStream.get(stream, tmp, metadata);

        String runCommand = processCommand(tis);
        String output = execCommand(new String[]{runCommand});

        // now extract the actual metadata params
        // from the GDAL output in the content stream
        // to do this, we need to literally process the output
        // from the invoked command b/c we can't read metadata and
        // output text from the handler in ExternalParser
        // at the same time, so for now, we can't use the
        // ExternalParser to do this and I've had to bring some of
        // that functionality directly into this class
        // TODO: investigate a way to do both using ExternalParser

        extractMetFromOutput(output, metadata);
        applyPatternsToOutput(output, metadata, getPatterns());

        // make the content handler and provide output there
        // now that we have metadata
        processOutput(handler, metadata, output);
    }
@Test
    public void testParseFITS() {
        String fitsFilename = "/test-documents/WFPC2u5780205r_c0fx.fits";

        assumeTrue(canRun());
        // If the exit code is 1 (meaning FITS isn't supported by the installed version of
        // gdalinfo, don't run this test.
        String[] fitsCommand = {"gdalinfo", getResourceAsUrl(fitsFilename).getPath()};
        assumeTrue(ExternalParser.check(fitsCommand, 1));

        String expectedAllgMin = "-7.319537E1";
        String expectedAtodcorr = "COMPLETE";
        String expectedAtodfile = "uref$dbu1405iu.r1h";
        String expectedCalVersion = "                        ";
        String expectedCalibDef = "1466";

        GDALParser parser = new GDALParser();
        InputStream stream = TestGDALParser.class.getResourceAsStream(fitsFilename);
        Metadata met = new Metadata();
        BodyContentHandler handler = new BodyContentHandler();
        try {
            parser.parse(stream, handler, met, new ParseContext());
            assertNotNull(met);
            assertNotNull(met.get("ALLG-MIN"));
            assertEquals(expectedAllgMin, met.get("ALLG-MIN"));
            assertNotNull(met.get("ATODCORR"));
            assertEquals(expectedAtodcorr, met.get("ATODCORR"));
            assertNotNull(met.get("ATODFILE"));
            assertEquals(expectedAtodfile, met.get("ATODFILE"));
            assertNotNull(met.get("CAL_VER"));
            assertEquals(expectedCalVersion, met.get("CAL_VER"));
            assertNotNull(met.get("CALIBDEF"));
            assertEquals(expectedCalibDef, met.get("CALIBDEF"));

        } catch (Exception e) {
            e.printStackTrace();
            fail(e.getMessage());
        }
    }