def nwm_stage(self, forecast):
        from pynwm import nwm
        """Retrieve nwm forecast using pynwm module, and retrieve rating curves
		for self.comid, and provide probabilistic stage height distribution"""
        series = nwm.get_streamflow(forecast,
                                    self.comid,
                                    timezone='US/Central')
        interp = scipy.interpolate.interp1d(self.handq,
                                            self.handh)  # kind=cubic

        self.qdict = {}
        if len(series) > 1:  # long_range ensemble
            for s in series:
                datevals = zip(s['dates'], s['values'])
                for (d, v) in (datevals):
                    d = d.strftime('%y-%m-%d %H')
                    self.qdict[d] = v
        else:  # all other models (short_range, med_range, analysis_assym)
            s = series[0]
            datevals = zip(s['dates'], s['values'])
            for (d, v) in (datevals):
                d = d.strftime('%y-%m-%d %H')
                self.qdict[d] = v

        fcast_d = scipy.array(self.qdict.keys())
        fcast_v = scipy.array(self.qdict.values())
        return (fcast_d, interp(fcast_v))
예제 #2
0
def make_graph(products, comid, gage, start_date, clip_to_gage_dates, width):
    print 'COMID:', comid
    fig_size = [width, width / 1.61803398875]  # golden ratio
    gage_vals = None
    gage_dates = None
    s_series = None
    m_series = None
    l_series = None

    # Download data and figure out data ranges
    print '    Getting USGS data'
    gage_start = start_date - timedelta(hours=6)
    gage_name, gage_vals, gage_dates = _get_usgs_flow(gage, gage_start)
    if 'short' in products:
        print '    Getting short range forecast from HydroShare'
        s_series = nwm.get_streamflow('short_range', comid, start_date,
                                      'America/Chicago')
    if 'medium' in products:
        medium_start = datetime.combine(start_date.date(), time(6))
        print '    Getting medium range forecast from HydroShare'
        m_series = nwm.get_streamflow('medium_range', comid, medium_start,
                                      'America/Chicago')
    if 'long' in products:
        print '    Getting long range forecast from HydroShare'
        l_series = nwm.get_streamflow('long_range', comid, start_date,
                                      'America/Chicago')
    max_val, min_date, max_date = _get_limits(gage_vals, gage_dates, s_series,
                                              m_series, l_series,
                                              clip_to_gage_dates)

    if max_val > 0 and min_date < max_date:
        try:
            # Make the plot
            fig, ax = plt.subplots(figsize=fig_size)
            plt.title(gage_name, fontsize=12)
            if gage_vals:
                ax.plot(gage_dates,
                        gage_vals,
                        label='Gage',
                        color='green',
                        linewidth='4')

            # Short
            if s_series:
                for s in s_series:
                    nwm_dates = s['dates']
                    nwm_vals = s['values']
                    ax.plot(nwm_dates,
                            nwm_vals,
                            label='Short Range',
                            color='blue',
                            linewidth='7')

            # Medium
            if m_series:
                for s in m_series:
                    nwm_dates = s['dates']
                    nwm_vals = s['values']
                    ax.plot(nwm_dates,
                            nwm_vals,
                            label='Medium Range',
                            color='purple',
                            linewidth='2')

            # Long
            if l_series:
                for i, s in enumerate(l_series):
                    nwm_dates = s['dates']
                    nwm_vals = s['values']
                    if i == 0:
                        ax.plot(nwm_dates,
                                nwm_vals,
                                label='Ensemble',
                                color='brown',
                                linestyle=':')
                    else:
                        ax.plot(nwm_dates,
                                nwm_vals,
                                color='brown',
                                linestyle=':')

            # Format plot layout
            ax.legend(loc='best', fontsize=10)
            plt.ylabel('Flow (cfs)', fontsize=12)
            ax.tick_params(labelsize=10)
            ax.xaxis.set_major_formatter(DateFormatter('%b %d'))
            ax.set_ylim(0.0, max_val * 1.05)
            ax.set_xlim(min_date, max_date)

            ax.margins(0, 0.1)
            fig.autofmt_xdate()  # Show date labels at an angle
            fig.tight_layout()  # Removes some of the margin around the graph

            # Save the result
            png_file = '{0}-{1}-{2}.png'.format(gage_name, gage, comid)
            plt.savefig(png_file, facecolor=fig.get_facecolor())
            print '    Saved', png_file
        except:
            raise
        finally:
            plt.close()  # free memory after each plot
    else:
        print '  Max value is zero or no date intersection found. Skipping plot.'