def test_delete_obsolete_files(self): application = SpatializeApp() application.read_command_line() application.read_configuration() # Create three files prefix = application.config['General']['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)) with open(filename1, 'w'): pass with open(filename2, 'w'): pass with open(filename3, 'w'): pass # 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['General']['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))
def test_dates_to_calculate_error2(self): application = SpatializeApp() application.read_command_line() application.read_configuration() with open(self.filenames[0], 'w') as f: f.write(textwrap.dedent('''\ 2014-04-30 11:00,18.3, 2014-04-30 13:00,20.4, 2014-04-30 14:00,21.4, 2014-04-30 15:00,22.4, ''')) with open(self.filenames[1], 'w') as f: f.write(textwrap.dedent('''\ Time_step=60,0 Timestamp_rounding=0,0 Timestamp_offset=0,0 2014-04-30 11:00,18.3, 2014-04-30 12:00,19.3, 2014-04-30 13:00,20.4, 2014-04-30 14:00,21.4, ''')) error_message = r"^Unable to parse date string " \ r"u?'Timestamp_offset=0' " \ r"\(file " + self.filenames[1] + ", 5 lines from the end\)$" six.assertRaisesRegex(self, iso8601.ParseError, error_message, six.next, application.dates_to_calculate)
def test_execute(self): application = SpatializeApp() application.read_command_line() application.read_configuration() application.setup_logger() # Create some time series data with open(application.files[0], 'a') as f: f.write(textwrap.dedent('''\ 2014-04-30 11:00,18.3, 2014-04-30 13:00,20.4, 2014-04-30 14:00,21.4, 2014-04-30 15:00,22.4, ''')) with open(application.files[1], 'a') as f: f.write(textwrap.dedent('''\ 2014-04-30 11:00,18.3, 2014-04-30 12:00,19.3, 2014-04-30 13:00,20.4, 2014-04-30 14:00,21.4, ''')) # Create a mask mask_filename = os.path.join(self.tempdir, 'mask.tif') mask = gdal.GetDriverByName('GTiff').Create(mask_filename, 640, 480, 1, gdal.GDT_Float32) mask.SetGeoTransform((23, 0.01, 0, 39, 0, -0.01)) mask.SetProjection( 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,' '298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"' ']],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",' '0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG",' '"4326"]]') mask = None # Execute application.config['General']['number_of_output_files'] = 3 application.execute() # Check that it has created three files full_prefix = os.path.join( self.output_dir, application.config['General']['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')
def test_dates_to_calculate(self): application = SpatializeApp() application.read_command_line() application.read_configuration() with open(self.filenames[0], 'w') as f: f.write(textwrap.dedent('''\ 2014-04-30 11:00,18.3, 2014-04-30 13:00,20.4, 2014-04-30 14:00,21.4, 2014-04-30 15:00,22.4, ''')) with open(self.filenames[1], 'w') as f: f.write(textwrap.dedent('''\ Time_step=60,0 Timestamp_rounding=0,0 Timestamp_offset=0,0 2014-04-30 11:00,18.3, 2014-04-30 12:00,19.3, 2014-04-30 13:00,20.4, 2014-04-30 14:00,21.4, ''')) # Check for number_of_output_files=24 dates = [] for d in application.dates_to_calculate: dates.append(d) self.assertEquals(dates, [datetime(2014, 4, 30, 11, 0), datetime(2014, 4, 30, 12, 0), datetime(2014, 4, 30, 13, 0), datetime(2014, 4, 30, 14, 0), datetime(2014, 4, 30, 15, 0)]) # Check for number_of_output_files=2 application.config['General']['number_of_output_files'] = 2 dates = [] for d in application.dates_to_calculate: dates.append(d) self.assertEquals(dates, [datetime(2014, 4, 30, 14, 0), datetime(2014, 4, 30, 15, 0)]) # Check for number_of_output_files=4 application.config['General']['number_of_output_files'] = 4 dates = [] for d in application.dates_to_calculate: dates.append(d) self.assertEquals(dates, [datetime(2014, 4, 30, 12, 0), datetime(2014, 4, 30, 13, 0), datetime(2014, 4, 30, 14, 0), datetime(2014, 4, 30, 15, 0)])
def test_date_fmt(self): application = SpatializeApp() application.read_command_line() application.read_configuration() # Ten-minute with open(application.files[0], 'w') as f: f.write('Time_step=10,0\n\n') with open(application.files[1], 'w') as f: f.write('Time_step=10,0\n\n') self.assertEquals(application.date_fmt, '%Y-%m-%d %H:%M%z') # Hourly with open(application.files[0], 'w') as f: f.write('Time_step=60,0\n\n') with open(application.files[1], 'w') as f: f.write('Time_step=60,0\n\n') self.assertEquals(application.date_fmt, '%Y-%m-%d %H:%M%z') # Daily with open(application.files[0], 'w') as f: f.write('Time_step=1440,0\n\n') with open(application.files[1], 'w') as f: f.write('Time_step=1440,0\n\n') self.assertEquals(application.date_fmt, '%Y-%m-%d') # Monthly with open(application.files[0], 'w') as f: f.write('Time_step=0,1\n\n') with open(application.files[1], 'w') as f: f.write('Time_step=0,1\n\n') self.assertEquals(application.date_fmt, '%Y-%m') # Annual with open(application.files[0], 'w') as f: f.write('Time_step=0,12\n\n') with open(application.files[1], 'w') as f: f.write('Time_step=0,12\n\n') self.assertEquals(application.date_fmt, '%Y') # Inconsistent with open(application.files[0], 'w') as f: f.write('Time_step=10,0\n\n') with open(application.files[1], 'w') as f: f.write('Time_step=60,0\n\n') self.assertRaises(WrongValueError, lambda: application.date_fmt)
def test_dates_to_calculate_error1(self): application = SpatializeApp() application.read_command_line() application.read_configuration() with open(self.filenames[0], 'w') as f: f.write(textwrap.dedent('''\ 2014-04-30 11:00,18.3, malformed date,20.4, 2014-04-30 14:00,21.4, 2014-04-30 15:00,22.4, ''')) with open(self.filenames[1], 'w') as f: f.write(textwrap.dedent('''\ 2014-04-30 11:00,18.3, 2014-04-30 12:00,19.3, 2014-04-30 13:00,20.4, 2014-04-30 14:00,21.4, ''')) error_message = r"^Unable to parse date string u?'malformed date' " \ r"\(file " + self.filenames[0] + ", 3 lines from the end\)$" six.assertRaisesRegex(self, iso8601.ParseError, error_message, six.next, application.dates_to_calculate)