def run_task(self): # {{{ """ Performs analysis of ocean heat content (OHC) from time-series output. """ # Authors # ------- # Xylar Asay-Davis, Milena Veneziani, Greg Streletz self.logger.info("\nComputing anomalies...") config = self.config startDate = config.get('timeSeries', 'startDate') endDate = config.get('timeSeries', 'endDate') if config.has_option('timeSeries', 'anomalyRefYear'): anomalyYear = config.getint('timeSeries', 'anomalyRefYear') anomalyRefDate = '{:04d}-01-01_00:00:00'.format(anomalyYear) anomalyEndDate = '{:04d}-12-31_23:59:59'.format(anomalyYear) else: anomalyRefDate = get_simulation_start_time(self.runStreams) anomalyYear = int(anomalyRefDate[0:4]) anomalyEndDate = '{:04d}-12-31_23:59:59'.format(anomalyYear) ds = compute_moving_avg_anomaly_from_start( timeSeriesFileName=self.inputFile, variableList=self.variableList, anomalyStartTime=anomalyRefDate, anomalyEndTime=anomalyEndDate, startDate=startDate, endDate=endDate, calendar=self.calendar, movingAveragePoints=self.movingAveragePoints, alter_dataset=self.alter_dataset) outFileName = self.outFileName if not os.path.isabs(outFileName): baseDirectory = build_config_full_path( config, 'output', 'timeSeriesSubdirectory') outFileName = '{}/{}'.format(baseDirectory, outFileName) write_netcdf(ds, outFileName) # }}}
def get_start_and_end(self): # {{{ """ Get the start and end years and dates for the climatology. This function is provided to allow a custom method for setting the start and end years of the climatology. Returns ------- startYear, endYear : int The start and end years of the climatology """ # Authors # ------- # Xylar Asay-Davis config = self.config if config.has_option('climatology', 'anomalyRefYear'): anomalyRefYear = config.getint('climatology', 'anomalyRefYear') else: # we want the start year of the simulation, which we can't get yet anomalyRefDate = get_simulation_start_time(self.runStreams) anomalyRefYear = int(anomalyRefDate[0:4]) startYear = anomalyRefYear endYear = anomalyRefYear startDate = '{:04d}-01-01_00:00:00'.format(startYear) endDate = '{:04d}-12-31_23:59:59'.format(endYear) config.set('climatology', 'startYear', str(startYear)) config.set('climatology', 'startDate', startDate) config.set('climatology', 'endYear', str(endYear)) config.set('climatology', 'endDate', endDate) return startYear, endYear
def setup_and_check(self): # {{{ ''' Perform steps to set up the analysis and check for errors in the setup. ''' # Authors # ------- # Xylar Asay-Davis # first, call setup_and_check from the base class (AnalysisTask), # which will perform some common setup, including storing: # self.runDirectory , self.historyDirectory, self.plotsDirectory, # self.namelist, self.runStreams, self.historyStreams, # self.calendar super(MpasTimeSeriesTask, self).setup_and_check() config = self.config baseDirectory = build_config_full_path( config, 'output', 'timeSeriesSubdirectory') make_directories(baseDirectory) self.outputFile = '{}/{}.nc'.format(baseDirectory, self.fullTaskName) self.check_analysis_enabled( analysisOptionName='config_am_timeseriesstatsmonthly_enable', raiseException=True) # get a list of timeSeriesStats output files from the streams file, # reading only those that are between the start and end dates startDate = config.get(self.section, 'startDate') endDate = config.get(self.section, 'endDate') streamName = 'timeSeriesStatsMonthlyOutput' self.inputFiles = self.historyStreams.readpath( streamName, startDate=startDate, endDate=endDate, calendar=self.calendar) if len(self.inputFiles) == 0: raise IOError('No files were found in stream {} between {} and ' '{}.'.format(streamName, startDate, endDate)) self.runMessage = '\nComputing MPAS time series from first year ' \ 'plus files:\n' \ ' {} through\n {}'.format( os.path.basename(self.inputFiles[0]), os.path.basename(self.inputFiles[-1])) # Make sure first year of data is included for computing anomalies if config.has_option('timeSeries', 'anomalyRefYear'): anomalyYear = config.getint('timeSeries', 'anomalyRefYear') anomalyStartDate = '{:04d}-01-01_00:00:00'.format(anomalyYear) else: anomalyStartDate = get_simulation_start_time(self.runStreams) anomalyYear = int(anomalyStartDate[0:4]) anomalyEndDate = '{:04d}-12-31_23:59:59'.format(anomalyYear) firstYearInputFiles = self.historyStreams.readpath( streamName, startDate=anomalyStartDate, endDate=anomalyEndDate, calendar=self.calendar) for fileName in firstYearInputFiles: if fileName not in self.inputFiles: self.inputFiles.append(fileName) self.inputFiles = sorted(self.inputFiles) with xr.open_dataset(self.inputFiles[0]) as ds: self.allVariables = list(ds.data_vars.keys())
def setup_and_check(self): # {{{ """ Perform steps to set up the analysis and check for errors in the setup. Raises ------ OSError If files are not present """ # Authors # ------- # Xylar Asay-Davis # first, call setup_and_check from the base class (AnalysisTask), # which will perform some common setup, including storing: # self.runDirectory , self.historyDirectory, self.plotsDirectory, # self.namelist, self.runStreams, self.historyStreams, # self.calendar super(TimeSeriesSeaIce, self).setup_and_check() config = self.config self.startDate = self.config.get('timeSeries', 'startDate') self.endDate = self.config.get('timeSeries', 'endDate') self.variableList = ['timeMonthly_avg_iceAreaCell', 'timeMonthly_avg_iceVolumeCell'] self.mpasTimeSeriesTask.add_variables(variableList=self.variableList) self.inputFile = self.mpasTimeSeriesTask.outputFile if config.get('runs', 'preprocessedReferenceRunName') != 'None': check_path_exists(config.get('seaIcePreprocessedReference', 'baseDirectory')) # get a list of timeSeriesStatsMonthly output files from the streams # file, reading only those that are between the start and end dates streamName = 'timeSeriesStatsMonthlyOutput' self.startDate = config.get('timeSeries', 'startDate') self.endDate = config.get('timeSeries', 'endDate') self.inputFiles = \ self.historyStreams.readpath(streamName, startDate=self.startDate, endDate=self.endDate, calendar=self.calendar) if len(self.inputFiles) == 0: raise IOError('No files were found in stream {} between {} and ' '{}.'.format(streamName, self.startDate, self.endDate)) self.simulationStartTime = get_simulation_start_time(self.runStreams) try: self.restartFileName = self.runStreams.readpath('restart')[0] except ValueError: raise IOError('No MPAS-SeaIce restart file found: need at least ' 'one restart file to perform remapping of ' 'climatologies.') # these are redundant for now. Later cleanup is needed where these # file names are reused in run() self.xmlFileNames = [] polarPlot = config.getboolean('timeSeriesSeaIceAreaVol', 'polarPlot') mainRunName = config.get('runs', 'mainRunName') preprocessedReferenceRunName = \ config.get('runs', 'preprocessedReferenceRunName') compareWithObservations = config.getboolean('timeSeriesSeaIceAreaVol', 'compareWithObservations') polarXMLFileNames = [] if (not compareWithObservations and preprocessedReferenceRunName == 'None'): for variableName in ['iceArea', 'iceVolume']: filePrefix = '{}.{}'.format(mainRunName, variableName) self.xmlFileNames.append('{}/{}.xml'.format( self.plotsDirectory, filePrefix)) polarXMLFileNames.append('{}/{}_polar.xml'.format( self.plotsDirectory, filePrefix)) else: for hemisphere in ['NH', 'SH']: for variableName in ['iceArea', 'iceVolume']: filePrefix = '{}{}_{}'.format(variableName, hemisphere, mainRunName) self.xmlFileNames.append('{}/{}.xml'.format( self.plotsDirectory, filePrefix)) polarXMLFileNames.append('{}/{}_polar.xml'.format( self.plotsDirectory, filePrefix)) if polarPlot: self.xmlFileNames.extend(polarXMLFileNames) return # }}}