コード例 #1
0
    def test_run(self):
        application = cli.App(self.config_file)

        # Create a mask
        self._create_mask_file()

        # Execute
        self._prepare_config_file(number_of_output_files=3)
        application.run()

        # Check that it has created three files
        full_prefix = os.path.join(self.output_dir,
                                   application.config.filename_prefix)
        self.assertTrue(
            os.path.exists(full_prefix + "-2014-04-30-15-00+0200.tif"))
        self.assertTrue(
            os.path.exists(full_prefix + "-2014-04-30-14-00+0200.tif"))
        self.assertTrue(
            os.path.exists(full_prefix + "-2014-04-30-13-00+0200.tif"))

        # Check the timestamp in the last file
        fp = gdal.Open(full_prefix + "-2014-04-30-15-00+0200.tif")
        timestamp = fp.GetMetadata()["TIMESTAMP"]
        fp = None
        self.assertEqual(timestamp, "2014-04-30 15:00+0200")
コード例 #2
0
    def test_delete_obsolete_files(self, m):
        application = cli.App(self.config_file)
        application.run()

        # Create three files
        prefix = application.config.filename_prefix
        filename1 = os.path.join(self.output_dir, "{}-1.tif".format(prefix))
        filename2 = os.path.join(self.output_dir, "{}-2.tif".format(prefix))
        filename3 = os.path.join(self.output_dir, "{}-3.tif".format(prefix))
        Path(filename1).touch()
        Path(filename2).touch()
        Path(filename3).touch()

        # Just to make sure we didn't screw up above, check
        self.assertTrue(os.path.exists(filename1))
        self.assertTrue(os.path.exists(filename2))
        self.assertTrue(os.path.exists(filename3))

        # Execute for number_of_output_files = 2 and check
        application.config.number_of_output_files = 2
        application._delete_obsolete_files()
        self.assertFalse(os.path.exists(filename1))
        self.assertTrue(os.path.exists(filename2))
        self.assertTrue(os.path.exists(filename3))

        # Re-execute; nothing should have changed
        application._delete_obsolete_files()
        self.assertFalse(os.path.exists(filename1))
        self.assertTrue(os.path.exists(filename2))
        self.assertTrue(os.path.exists(filename3))
コード例 #3
0
 def test_no_timezone(self):
     self._remove_timezone_from_file(self.filenames[0])
     self._remove_timezone_from_file(self.filenames[1])
     application = cli.App(self.config_file)
     self._create_mask_file()
     self._prepare_config_file(number_of_output_files=3)
     msg = "{} does not contain Timezone".format(self.filenames[0])
     with self.assertRaisesRegex(click.ClickException, msg):
         application.run()
コード例 #4
0
 def test_missing_files_parameter_raises_error(self):
     with open(self.configfilename, "w") as f:
         f.write(
             textwrap.dedent("""\
                 mask = /etc/hspatial/mask.tif
                 epsg = 2100
                 output_dir = /var/opt/hspatial
                 filename_prefix = rainfall
                 method = idw
                 number_of_output_files = 24
                 """))
     msg = "No option 'files'"
     with self.assertRaisesRegex(click.ClickException, msg):
         cli.App(self.configfilename).run()
コード例 #5
0
 def test_dates_to_calculate_error1(self):
     self._create_mask_file()
     application = cli.App(self.config_file)
     with open(self.filenames[0], "a") as f:
         f.write(
             textwrap.dedent("""\
                 2014-04-30 16:00,23.4,
                 malformed date,24.4,
                 2014-04-30 18:00,25.4,
                 2014-04-30 19:00,25.4,
                 """))
     msg = (r"^Unable to parse date string u?'malformed date' "
            r"\(file " + self.filenames[0] + r", 3 lines from the end\)$")
     with self.assertRaisesRegex(click.ClickException, msg):
         application.run()
コード例 #6
0
 def test_missing_epsg_parameter_raises_error(self):
     with open(self.configfilename, "w") as f:
         f.write(
             textwrap.dedent("""\
                 mask = /etc/hspatial/mask.tif
                 output_dir = /var/opt/hspatial
                 filename_prefix = rainfall
                 number_of_output_files = 24
                 method = idw
                 files = /var/opt/timeseries/inputfile1.hts
                         /var/opt/timeseries/inputfile2.hts
                         /var/opt/timeseries/inputfile3.hts
                 """))
     msg = "No option 'epsg'"
     with self.assertRaisesRegex(click.ClickException, msg):
         cli.App(self.configfilename).run()
コード例 #7
0
 def test_correct_configuration_executes(self, m):
     with open(self.configfilename, "w") as f:
         f.write(
             textwrap.dedent("""\
                 mask = /etc/hspatial/mask.tif
                 epsg = 2100
                 output_dir = /var/opt/hspatial
                 filename_prefix = rainfall
                 method = idw
                 number_of_output_files = 24
                 files = /var/opt/timeseries/inputfile1.hts
                         /var/opt/timeseries/inputfile2.hts
                         /var/opt/timeseries/inputfile3.hts
                 """))
     cli.App(self.configfilename).run()
     m.assert_called_once_with()
コード例 #8
0
    def test_dates_to_calculate(self, *args):
        application = cli.App(self.config_file)
        application.run()
        tzinfo = TzinfoFromString("+0200")

        # Check for number_of_output_files=24
        dates = []
        for d in application._dates_to_calculate:
            dates.append(d)
        self.assertEqual(
            dates,
            [
                dt.datetime(2014, 4, 30, 11, 0, tzinfo=tzinfo),
                dt.datetime(2014, 4, 30, 12, 0, tzinfo=tzinfo),
                dt.datetime(2014, 4, 30, 13, 0, tzinfo=tzinfo),
                dt.datetime(2014, 4, 30, 14, 0, tzinfo=tzinfo),
                dt.datetime(2014, 4, 30, 15, 0, tzinfo=tzinfo),
            ],
        )

        # Check for number_of_output_files=2
        application.config.number_of_output_files = 2
        dates = []
        for d in application._dates_to_calculate:
            dates.append(d)
        self.assertEqual(
            dates,
            [
                dt.datetime(2014, 4, 30, 14, 0, tzinfo=tzinfo),
                dt.datetime(2014, 4, 30, 15, 0, tzinfo=tzinfo),
            ],
        )

        # Check for number_of_output_files=4
        application.config.number_of_output_files = 4
        dates = []
        for d in application._dates_to_calculate:
            dates.append(d)
        self.assertEqual(
            dates,
            [
                dt.datetime(2014, 4, 30, 12, 0, tzinfo=tzinfo),
                dt.datetime(2014, 4, 30, 13, 0, tzinfo=tzinfo),
                dt.datetime(2014, 4, 30, 14, 0, tzinfo=tzinfo),
                dt.datetime(2014, 4, 30, 15, 0, tzinfo=tzinfo),
            ],
        )
コード例 #9
0
    def test_date_fmt(self, m):
        application = cli.App(self.config_file)
        application.run()

        # Ten-minute
        with open(self.filenames[0], "w") as f:
            f.write("Time_step=10min\n\n")
        with open(self.filenames[1], "w") as f:
            f.write("Time_step=10min\n\n")
        self.assertEqual(application._date_fmt, "%Y-%m-%d %H:%M%z")

        # Hourly
        with open(self.filenames[0], "w") as f:
            f.write("Time_step=H\n\n")
        with open(self.filenames[1], "w") as f:
            f.write("Time_step=H\n\n")
        self.assertEqual(application._date_fmt, "%Y-%m-%d %H:%M%z")

        # Daily
        with open(self.filenames[0], "w") as f:
            f.write("Time_step=D\n\n")
        with open(self.filenames[1], "w") as f:
            f.write("Time_step=D\n\n")
        self.assertEqual(application._date_fmt, "%Y-%m-%d")

        # Monthly
        with open(self.filenames[0], "w") as f:
            f.write("Time_step=M\n\n")
        with open(self.filenames[1], "w") as f:
            f.write("Time_step=M\n\n")
        self.assertEqual(application._date_fmt, "%Y-%m")

        # Annual
        with open(self.filenames[0], "w") as f:
            f.write("Time_step=Y\n\n")
        with open(self.filenames[1], "w") as f:
            f.write("Time_step=Y\n\n")
        self.assertEqual(application._date_fmt, "%Y")

        # Inconsistent
        with open(self.filenames[0], "w") as f:
            f.write("Time_step=10min\n\n")
        with open(self.filenames[1], "w") as f:
            f.write("Time_step=60min\n\n")
        with self.assertRaises(click.ClickException):
            application._date_fmt
コード例 #10
0
 def test_wrong_number_of_output_files_parameter_raises_error(self):
     with open(self.configfilename, "w") as f:
         f.write(
             textwrap.dedent("""\
                 mask = /etc/hspatial/mask.tif
                 epsg = 2100
                 output_dir = /var/opt/hspatial
                 filename_prefix = rainfall
                 method = idw
                 number_of_output_files = hello
                 files = /var/opt/timeseries/inputfile1.hts
                         /var/opt/timeseries/inputfile2.hts
                         /var/opt/timeseries/inputfile3.hts
                 """))
     msg = 'Option "number_of_output_files" must be an integer'
     with self.assertRaisesRegex(click.ClickException, msg):
         cli.App(self.configfilename).run()
コード例 #11
0
 def test_nonexistent_log_level_raises_error(self):
     with open(self.configfilename, "w") as f:
         f.write(
             textwrap.dedent("""\
                 loglevel = HELLO
                 mask = /etc/hspatial/mask.tif
                 epsg = 2100
                 output_dir = /var/opt/hspatial
                 filename_prefix = rainfall
                 number_of_output_files = 24
                 method = idw
                 files = /var/opt/timeseries/inputfile1.hts
                         /var/opt/timeseries/inputfile2.hts
                         /var/opt/timeseries/inputfile3.hts
                 """))
     msg = "loglevel must be one of ERROR, WARNING, INFO, DEBUG"
     with self.assertRaisesRegex(click.ClickException, msg):
         cli.App(self.configfilename).run()
コード例 #12
0
 def test_creates_log_file(self, *args):
     logfilename = os.path.join(self.tempdir, "hspatial.log")
     with open(self.configfilename, "w") as f:
         f.write(
             textwrap.dedent("""\
                 logfile = {}
                 mask = /etc/hspatial/mask.tif
                 epsg = 2100
                 output_dir = /var/opt/hspatial
                 filename_prefix = rainfall
                 method = idw
                 number_of_output_files = 24
                 files = /var/opt/timeseries/inputfile1.hts
                         /var/opt/timeseries/inputfile2.hts
                         /var/opt/timeseries/inputfile3.hts
                 """.format(logfilename)))
     cli.App(self.configfilename).run()
     self.assertTrue(os.path.exists(logfilename))
コード例 #13
0
 def test_get_last_dates(self):
     application = cli.App(self.config_file)
     tzinfo = TzinfoFromString("+0200")
     self.assertEqual(
         application._get_last_dates(self.filenames[0], 2),
         [
             dt.datetime(2014, 4, 30, 14, 0, tzinfo=tzinfo),
             dt.datetime(2014, 4, 30, 15, 0, tzinfo=tzinfo),
         ],
     )
     self.assertEqual(
         application._get_last_dates(self.filenames[0], 20),
         [
             dt.datetime(2014, 4, 30, 11, 0, tzinfo=tzinfo),
             dt.datetime(2014, 4, 30, 13, 0, tzinfo=tzinfo),
             dt.datetime(2014, 4, 30, 14, 0, tzinfo=tzinfo),
             dt.datetime(2014, 4, 30, 15, 0, tzinfo=tzinfo),
         ],
     )