class TestNamelist(TestCase): def setup_namelist(self): nlpath = self.datadir.join('namelist.ocean') self.nl = NameList(bytes(nlpath)) def setup_streams(self): sfpath = self.datadir.join('streams.ocean') self.sf = StreamsFile(bytes(sfpath)) def test_open_files(self): self.setup_namelist() self.setup_streams() def test_read_namelist(self): self.setup_namelist() # check accessing generalized function techniques self.assertEqual(self.nl.config_dt, '00:10:00') self.assertEqual(self.nl['config_dt'], '00:10:00') # check cast accessors self.assertEqual(self.nl.getint('config_num_halos'), 3) self.assertApproxEqual(self.nl.getfloat('config_min_thickness'), 1.0) self.assertEqual(self.nl.getbool('config_do_restart'), False) # tests for use of ' and " for string selections self.assertEqual(self.nl.config_test_extra_equals1, 'a = b') self.assertEqual(self.nl.config_test_extra_equals2, 'a = b') def test_read_streamsfile(self): self.setup_streams() # check self.assertEqual(self.sf.read('output', 'type'), 'output') self.assertEqual(self.sf.read('restart', 'output_interval'), '0100_00:00:00') files = self.sf.readpath('output') expectedFiles = [] for date in ['0001-01-01','0001-01-02','0001-02-01','0002-01-01']: expectedFiles.append('{}/output/output.{}_00.00.00.nc' .format(self.sf.streamsdir, date)) self.assertEqual(files, expectedFiles) files = self.sf.readpath('output', startDate='0001-01-03', endDate='0001-12-30') expectedFiles = [] for date in ['0001-01-02','0001-02-01']: expectedFiles.append('{}/output/output.{}_00.00.00.nc' .format(self.sf.streamsdir, date)) self.assertEqual(files, expectedFiles) files = self.sf.readpath('output', startDate='0001-01-03') expectedFiles = [] for date in ['0001-01-02','0001-02-01','0002-01-01']: expectedFiles.append('{}/output/output.{}_00.00.00.nc' .format(self.sf.streamsdir, date)) self.assertEqual(files, expectedFiles) files = self.sf.readpath('output', endDate='0001-12-30') expectedFiles = [] for date in ['0001-01-01','0001-01-02','0001-02-01']: expectedFiles.append('{}/output/output.{}_00.00.00.nc' .format(self.sf.streamsdir, date)) self.assertEqual(files, expectedFiles) files = self.sf.readpath('restart', startDate='0001-01-01', endDate='0001-12-31') expectedFiles = [] for seconds in ['00010','00020']: expectedFiles.append('{}/restarts/restart.0001-01-01_{}.nc' .format(self.sf.streamsdir, seconds)) self.assertEqual(files, expectedFiles) files = self.sf.readpath('mesh') expectedFiles = ['{}/mesh.nc'.format(self.sf.streamsdir)] self.assertEqual(files, expectedFiles) files = self.sf.readpath('mesh', startDate='0001-01-01', endDate='0001-12-31') expectedFiles = ['{}/mesh.nc'.format(self.sf.streamsdir)] self.assertEqual(files, expectedFiles)
class TestNamelist(TestCase): def setup_namelist(self): nlpath = self.datadir.join('namelist.ocean') self.nl = NameList(str(nlpath)) def setup_streams(self): sfpath = self.datadir.join('streams.ocean') self.sf = StreamsFile(str(sfpath)) def test_open_files(self): self.setup_namelist() self.setup_streams() def test_read_namelist(self): self.setup_namelist() # check accessing generalized function techniques self.assertEqual(self.nl.config_dt, '00:10:00') self.assertEqual(self.nl['config_dt'], '00:10:00') # check cast accessors self.assertEqual(self.nl.getint('config_num_halos'), 3) self.assertApproxEqual(self.nl.getfloat('config_min_thickness'), 1.0) self.assertEqual(self.nl.getbool('config_do_restart'), False) # tests for use of ' and " for string selections self.assertEqual(self.nl.config_test_extra_equals1, 'a = b') self.assertEqual(self.nl.config_test_extra_equals2, 'a = b') def test_read_streamsfile(self): self.setup_streams() # check self.assertEqual(self.sf.read('output', 'type'), 'output') self.assertEqual(self.sf.read('restart', 'output_interval'), '0100_00:00:00') files = self.sf.readpath('output') expectedFiles = [] for date in ['0001-01-01', '0001-01-02', '0001-02-01', '0002-01-01']: expectedFiles.append('{}/output/output.{}_00.00.00.nc'.format( self.sf.streamsdir, date)) self.assertEqual(files, expectedFiles) files = self.sf.readpath('output', startDate='0001-01-02', endDate='0001-12-30', calendar='gregorian_noleap') expectedFiles = [] for date in ['0001-01-02', '0001-02-01']: expectedFiles.append('{}/output/output.{}_00.00.00.nc'.format( self.sf.streamsdir, date)) self.assertEqual(files, expectedFiles) files = self.sf.readpath('output', startDate='0001-01-02', calendar='gregorian_noleap') expectedFiles = [] for date in ['0001-01-02', '0001-02-01', '0002-01-01']: expectedFiles.append('{}/output/output.{}_00.00.00.nc'.format( self.sf.streamsdir, date)) self.assertEqual(files, expectedFiles) files = self.sf.readpath('output', endDate='0001-12-30', calendar='gregorian_noleap') expectedFiles = [] for date in ['0001-01-01', '0001-01-02', '0001-02-01']: expectedFiles.append('{}/output/output.{}_00.00.00.nc'.format( self.sf.streamsdir, date)) self.assertEqual(files, expectedFiles) files = self.sf.readpath('restart', startDate='0001-01-01', endDate='0001-12-31', calendar='gregorian_noleap') expectedFiles = [] for seconds in ['00010', '00020']: expectedFiles.append('{}/restarts/restart.0001-01-01_{}.nc'.format( self.sf.streamsdir, seconds)) self.assertEqual(files, expectedFiles) files = self.sf.readpath('mesh') expectedFiles = ['{}/mesh.nc'.format(self.sf.streamsdir)] self.assertEqual(files, expectedFiles) files = self.sf.readpath('mesh', startDate='0001-01-01', endDate='0001-12-31', calendar='gregorian_noleap') expectedFiles = ['{}/mesh.nc'.format(self.sf.streamsdir)] self.assertEqual(files, expectedFiles)
def update_time_bounds_from_file_names(config, section, componentName): # {{{ """ Update the start and end years and dates for time series, climatologies or climate indices based on the years actually available in the list of files. """ # Authors # ------- # Xylar Asay-Davis # read parameters from config file # the run directory contains the restart files runDirectory = build_config_full_path(config, 'input', 'runSubdirectory') # if the history directory exists, use it; if not, fall back on # runDirectory historyDirectory = build_config_full_path( config, 'input', '{}HistorySubdirectory'.format(componentName), defaultPath=runDirectory) errorOnMissing = config.getboolean('input', 'errorOnMissing') namelistFileName = build_config_full_path( config, 'input', '{}NamelistFileName'.format(componentName)) try: namelist = NameList(namelistFileName) except (OSError, IOError): # this component likely doesn't have output in this run return streamsFileName = build_config_full_path( config, 'input', '{}StreamsFileName'.format(componentName)) try: historyStreams = StreamsFile(streamsFileName, streamsdir=historyDirectory) except (OSError, IOError): # this component likely doesn't have output in this run return calendar = namelist.get('config_calendar_type') requestedStartYear = config.getint(section, 'startYear') requestedEndYear = config.get(section, 'endYear') if requestedEndYear == 'end': requestedEndYear = None else: # get it again as an integer requestedEndYear = config.getint(section, 'endYear') startDate = '{:04d}-01-01_00:00:00'.format(requestedStartYear) if requestedEndYear is None: endDate = None else: endDate = '{:04d}-12-31_23:59:59'.format(requestedEndYear) streamName = 'timeSeriesStatsMonthlyOutput' try: inputFiles = historyStreams.readpath(streamName, startDate=startDate, endDate=endDate, calendar=calendar) except ValueError: # this component likely doesn't have output in this run return if len(inputFiles) == 0: raise ValueError('No input files found for stream {} in {} between ' '{} and {}'.format(streamName, componentName, requestedStartYear, requestedEndYear)) years, months = get_files_year_month(sorted(inputFiles), historyStreams, streamName) # search for the start of the first full year firstIndex = 0 while (firstIndex < len(years) and months[firstIndex] != 1): firstIndex += 1 startYear = years[firstIndex] # search for the end of the last full year lastIndex = len(years) - 1 while (lastIndex >= 0 and months[lastIndex] != 12): lastIndex -= 1 endYear = years[lastIndex] if requestedEndYear is None: config.set(section, 'endYear', str(endYear)) requestedEndYear = endYear if startYear != requestedStartYear or endYear != requestedEndYear: if errorOnMissing: raise ValueError( "{} start and/or end year different from requested\n" "requested: {:04d}-{:04d}\n" "actual: {:04d}-{:04d}\n".format(section, requestedStartYear, requestedEndYear, startYear, endYear)) else: print("Warning: {} start and/or end year different from " "requested\n" "requested: {:04d}-{:04d}\n" "actual: {:04d}-{:04d}\n".format(section, requestedStartYear, requestedEndYear, startYear, endYear)) config.set(section, 'startYear', str(startYear)) config.set(section, 'endYear', str(endYear)) startDate = '{:04d}-01-01_00:00:00'.format(startYear) config.set(section, 'startDate', startDate) endDate = '{:04d}-12-31_23:59:59'.format(endYear) config.set(section, 'endDate', endDate)