<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def write_flows_to_gssha_time_series_xys(self, path_to_output_file, series_name, series_id, river_index=None, river_id=None, date_search_start=None, date_search_end=None, daily=False, filter_mode="mean"):
 """ Write out RAPID output to GSSHA WMS time series xys file. Parameters path_to_output_file: str Path to the output xys file. series_name: str The name for the series. series_id: int The ID to give the series. river_index: :obj:`datetime.datetime`, optional This is the index of the river in the file you want the streamflow for. river_id: :obj:`datetime.datetime`, optional This is the river ID that you want the streamflow for. date_search_start: :obj:`datetime.datetime`, optional This is a datetime object with the date of the minimum date for starting. date_search_end: :obj:`datetime.datetime`, optional This is a datetime object with the date of the maximum date for ending. daily: bool, optional If True and the file is CF-Compliant, write out daily flows. filter_mode: str, optional You can get the daily average "mean" or the maximum "max". Defauls is "mean". Example writing entire time series to file: .. code:: python from RAPIDpy import RAPIDDataset river_id = 3624735 path_to_rapid_qout = '/path/to/Qout.nc' with RAPIDDataset(path_to_rapid_qout) as qout_nc: qout_nc.write_flows_to_gssha_time_series_xys( '/timeseries/Qout_{0}.xys'.format(river_id), series_name="RAPID_TO_GSSHA_{0}".format(river_id), series_id=34, river_id=river_id) Example writing entire time series as daily average to file: .. code:: python from RAPIDpy import RAPIDDataset river_id = 3624735 path_to_rapid_qout = '/path/to/Qout.nc' with RAPIDDataset(path_to_rapid_qout) as qout_nc: # NOTE: Getting the river index is not necessary # this is just an example of how to use this river_index = qout_nc.get_river_index(river_id) # if file is CF compliant, you can write out daily average qout_nc.write_flows_to_gssha_time_series_xys( '/timeseries/Qout_daily.xys', series_name="RAPID_TO_GSSHA_{0}".format(river_id), series_id=34, river_index=river_index, daily=True) Example writing subset of time series as daily maximum to file: .. code:: python from datetime import datetime from RAPIDpy import RAPIDDataset river_id = 3624735 path_to_rapid_qout = '/path/to/Qout.nc' with RAPIDDataset(path_to_rapid_qout) as qout_nc: # NOTE: Getting the river index is not necessary # this is just an example of how to use this river_index = qout_nc.get_river_index(river_id) # if file is CF compliant, you can filter by date and # get daily values qout_nc.write_flows_to_gssha_time_series_xys( '/timeseries/Qout_daily_date_filter.xys', series_name="RAPID_TO_GSSHA_{0}".format(river_id), series_id=34, river_index=river_index, date_search_start=datetime(2002, 8, 31), date_search_end=datetime(2002, 9, 15), daily=True, filter_mode="max") """

        if river_id is not None:
            river_index = self.get_river_index(river_id)
        elif river_id is None and river_index is None:
            raise ValueError(" Need reach id or reach index ...")

        self.raise_time_valid()

        # analyze and write
        qout_df = self.get_qout_index(river_index,
                                      date_search_start=date_search_start,
                                      date_search_end=date_search_end,
                                      daily=daily,
                                      filter_mode=filter_mode,
                                      as_dataframe=True)

        with open_csv(path_to_output_file, 'w') as out_ts:
            out_ts.write("XYS {0} {1} \"{2}\"\r\n".format(series_id,
                                                          len(qout_df.index),
                                                          series_name))
            for index, pd_row in qout_df.iterrows():
                date_str = index.strftime("%m/%d/%Y %I:%M:%S %p")
                out_ts.write("\"{0}\" {1:.5f}\n".format(date_str,
                                                        pd_row[0]))