def _get_last_dates(self, filename, n): """ Assuming specified file contains a time series, scan it from the bottom and return the list of the n last dates (may be less than n if the time series is too small). 'filename' is used in error messages. """ # Get the time zone with open(filename) as fp: for line in fp: if line.startswith("Timezone") or (line and line[0] in "0123456789"): break if not line.startswith("Timezone"): raise click.ClickException( "{} does not contain Timezone".format(filename)) zonestr = line.partition("=")[2].strip() timezone = TzinfoFromString(zonestr) result = [] previous_line_was_empty = False with ropen(filename) as fp: for i, line in enumerate(fp): if i >= n: break line = line.strip() # Ignore empty lines if not line: previous_line_was_empty = True continue # Is the line in the form of an ini file configuration line? items = line.split("=") if len(items) and ( "," not in items[0]) and previous_line_was_empty: break # Yes; we reached the start of the file previous_line_was_empty = False datestring = line.split(",")[0] try: result.insert( 0, iso8601.parse_date(datestring, default_timezone=timezone)) except iso8601.ParseError as e: raise iso8601.ParseError( str(e) + " (file {}, {} lines from the end)".format( filename, i + 1)) return result
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), ], )
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), ], )
def _datetime64_to_aware_datetime(self, adatetime): result = adatetime.to_pydatetime() if self.timezone: result = result.replace(tzinfo=TzinfoFromString(self.timezone)) return result
def test_h_integrate(self): output_filename_prefix = os.path.join(self.tempdir, "test") result_filename = output_filename_prefix + "-2014-04-22-13-00+0200.tif" hspatial.h_integrate( mask=self.mask, stations_layer=self.stations_layer, date=dt.datetime(2014, 4, 22, 13, 0, tzinfo=TzinfoFromString("EET (+0200)")), output_filename_prefix=output_filename_prefix, date_fmt="%Y-%m-%d %H:%M%z", funct=hspatial.idw, kwargs={}, ) f = gdal.Open(result_filename) result = f.GetRasterBand(1).ReadAsArray() nodatavalue = f.GetRasterBand(1).GetNoDataValue() expected_result = np.array([ [1.5088, 1.6064, nodatavalue, 1.7237], [1.3828, 1.6671, 1.7336, 1.7662], [0.5400, 2.4000, 1.7954, 1.7504], ]) np.testing.assert_almost_equal(result, expected_result, decimal=4) self.assertEqual(f.GetMetadataItem("UNIT"), "microchips") f = None # Wait long enough to make sure that, if we write to a file, its # modification time will be distinguishable from the modification time # of any file that has been written to so far (how long we need to wait # depends on the file system, so we use a test file). mtime_test_file = os.path.join(self.tempdir, "test_mtime") with open(mtime_test_file, "w") as f: f.write("hello, world") reference_mtime = os.path.getmtime(mtime_test_file) while os.path.getmtime(mtime_test_file) - reference_mtime < 0.001: sleep(0.001) with open(mtime_test_file, "w") as f: f.write("hello, world") # Try re-calculating the output; the output file should not be touched # at all. result_mtime = os.path.getmtime(result_filename) hspatial.h_integrate( mask=self.mask, stations_layer=self.stations_layer, date=dt.datetime(2014, 4, 22, 13, 0, tzinfo=TzinfoFromString("EET (+0200)")), output_filename_prefix=output_filename_prefix, date_fmt="%Y-%m-%d %H:%M%z", funct=hspatial.idw, kwargs={}, ) self.assertEqual(os.path.getmtime(result_filename), result_mtime) # Now change one of the input files so that it contains new data that # can be used in the same calculation, and try recalculating. This time # the file should be recalculated. with open(self.filenames[3], "w") as f: f.write("Timezone=EET (UTC+0200)\n" "Location=19.66480 0.15560 2100\n" # GGRS87=17000 17000 "\n" "2014-04-22 12:50,9.70,\n" "2014-04-22 13:00,4.70,\n" "2014-04-22 13:10,7.63,\n") hspatial.h_integrate( mask=self.mask, stations_layer=self.stations_layer, date=dt.datetime(2014, 4, 22, 13, 0, tzinfo=TzinfoFromString("EET (+0200)")), output_filename_prefix=output_filename_prefix, date_fmt="%Y-%m-%d %H:%M%z", funct=hspatial.idw, kwargs={}, ) self.assertGreater(os.path.getmtime(result_filename), result_mtime + 0.0009) f = gdal.Open(result_filename) result = f.GetRasterBand(1).ReadAsArray() nodatavalue = f.GetRasterBand(1).GetNoDataValue() expected_result = np.array([ [2.6736, 3.1053, nodatavalue, 2.5166], [2.3569, 3.5775, 2.9512, 2.3596], [0.5400, 2.4000, 2.5377, 2.3779], ]) np.testing.assert_almost_equal(result, expected_result, decimal=4) f = None
def test_simple(self): atzinfo = TzinfoFromString("+0130") self.assertEqual(atzinfo.offset, dt.timedelta(hours=1, minutes=30))
def test_zero(self): atzinfo = TzinfoFromString("DUMMY (UTC-0000)") self.assertEqual(atzinfo.offset, dt.timedelta(hours=0, minutes=0))
def test_negative(self): atzinfo = TzinfoFromString("DUMMY (UTC-0420)") self.assertEqual(atzinfo.offset, -dt.timedelta(hours=4, minutes=20))
def test_brackets_with_utc(self): atzinfo = TzinfoFromString("DUMMY (UTC+0350)") self.assertEqual(atzinfo.offset, dt.timedelta(hours=3, minutes=50))
def test_brackets(self): atzinfo = TzinfoFromString("DUMMY (+0240)") self.assertEqual(atzinfo.offset, dt.timedelta(hours=2, minutes=40))