IngestionTool { static String getDatePath(File sourceFile, String productType, Pattern pattern) throws IllegalArgumentException { int numberOfParenthesis = countChars(pattern.pattern(), '('); if (numberOfParenthesis >= 2) { Matcher matcher = pattern.matcher(sourceFile.getName()); if (! matcher.matches()) { throw new IllegalArgumentException("pattern " + pattern.pattern() + " does not match file name " + sourceFile.getName()); } if (matcher.groupCount() == 3) { return String.format("%s/%s/%s", matcher.group(1), matcher.group(2), matcher.group(3)); } else if (matcher.groupCount() == 2 && matcher.group(1).length() == 4 && matcher.group(2).length() == 3) { Date date = null; try { date = YEAR_DAY_OF_YEAR_FORMAT.parse(matcher.group(1) + matcher.group(2)); } catch (ParseException e) { throw new IllegalArgumentException("file name " + sourceFile.getName() + " does not contain expected year and dayofyear according to pattern " + pattern.pattern() + ": " + e.getMessage()); } return YEAR_MONTH_DAY_FORMAT.format(date); } else if (matcher.groupCount() == 2 && matcher.group(1).length() == 2 && matcher.group(2).length() == 3) { Date date = null; try { date = YEAR2_DAY_OF_YEAR_FORMAT.parse(matcher.group(1) + matcher.group(2)); } catch (ParseException e) { throw new IllegalArgumentException("file name " + sourceFile.getName() + " does not contain expected year and dayofyear according to pattern " + pattern.pattern() + ": " + e.getMessage()); } return YEAR_MONTH_DAY_FORMAT.format(date); } else { throw new IllegalArgumentException("pattern " + pattern.pattern() + " does not contain recognised date in file name " + sourceFile.getName()); } } else if (productType != null && productType.startsWith("MODIS")) { try { return YEAR_MONTH_DAY_FORMAT.format(YEAR_DAY_OF_YEAR_FORMAT.parse(sourceFile.getName().substring(1, 8))); } catch (ParseException e) { throw new IllegalArgumentException("file name " + sourceFile + " does not contain recognised date for MODIS default pattern"); } } else if (productType != null && productType.startsWith("SPOT_VGT")) { try { return YEAR_MONTH_DAY_FORMAT.format(YEAR_DAY_OF_YEAR_FORMAT.parse(sourceFile.getName().substring(10, 18))); } catch (ParseException e) { throw new IllegalArgumentException("file name " + sourceFile.getName() + " does not contain recognised date for SPOT_VGT default pattern"); } } else if (productType != null && productType.startsWith("AVHRR")) { try { return YEAR_MONTH_DAY_FORMAT.format(MONTH_DAY_YEAR2_FORMAT.parse(sourceFile.getName().substring(4, 10))); } catch (ParseException e) { throw new IllegalArgumentException("file name " + sourceFile.getName() + " does not contain recognised date for NOAA AVHRR pattern"); } } else if ("MER_RR__1P".equals(productType) || "MER_FRS_1P".equals(productType)) { return String.format("%s/%s/%s", sourceFile.getName().substring(14, 18), sourceFile.getName().substring(18, 20), sourceFile.getName().substring(20, 22)); } else { File dayDir = sourceFile.getParentFile(); if (dayDir == null || dayDir.getName().length() != 2) { return "."; } File monthDir = dayDir.getParentFile(); if (monthDir == null || monthDir.getName().length() != 2) { return "."; } File yearDir = monthDir.getParentFile(); if (yearDir == null || yearDir.getName().length() != 4) { return "."; } return String.format("%s/%s/%s", yearDir.getName(), monthDir.getName(), dayDir.getName()); } }  static int handleIngestionCommand(CommandLine commandLine, String[] files, FileSystem hdfs); static String expand(Matcher source, String template); static final String DEFAULT_PRODUCT_TYPE; static final String DEFAULT_REVISION; static final DateFormat YEAR_MONTH_DAY_FORMAT; static final DateFormat YEAR_DAY_OF_YEAR_FORMAT; static final DateFormat YEAR2_DAY_OF_YEAR_FORMAT; static final DateFormat MONTH_DAY_YEAR2_FORMAT; static final long MINIMUM_BLOCK_SIZE; static final long MAXIMUM_BLOCK_SIZE; }
@Test public void testPatternDatePath() { assertEquals("2006/07/18", IngestionTool.getDatePath(new File("MER_RR__1POBCM20060718_091715_000001012049_00308_22907_0113.N1"), "", Pattern.compile("..............(....)(..)(..).*"))); }
@Test public void testTypeDatePath() { assertEquals("2006/07/18", IngestionTool.getDatePath(new File("MER_RR__1POBCM20060718_091715_000001012049_00308_22907_0113.N1"), "MER_RR__1P", Pattern.compile(".*"))); }
@Test public void testPattern2DatePath() { assertEquals("2006/07/18", IngestionTool.getDatePath(new File("A2006199xxx.hdf"), "", Pattern.compile(".(....)(...).*"))); }
@Test public void testPattern3DatePath() { assertEquals("2006/07/18", IngestionTool.getDatePath(new File("NSS.GHRR.NM.D06199.S1409.E1554.B2348788.WI.gz"), "", Pattern.compile("NSS\\.GHRR\\...\\.D(..)(...).*"))); }
@Test public void testDirectoryDatePath() { assertEquals("2006/07/18", IngestionTool.getDatePath(new File("/mnt/test/2006/07/18/A2006199xxx.hdf"), "notype", Pattern.compile(".*"))); }
@Test public void testNoPatternDatePath() { assertEquals(".", IngestionTool.getDatePath(new File("A2006199xxx.hdf"), "notype", Pattern.compile(".*"))); }
@Test public void testAvhrrDatePath() { assertEquals("1999/07/01", IngestionTool.getDatePath(new File("ao14070199085656_090012.l1b"), "AVHRR_L1B", Pattern.compile(".*"))); }