def plot_val_table(fsp, func, freq, quantile, stationlist):
    #
    freq = climpy.check_freq(freq)
    if freq == _c.FR_MTH:
        (periodtype, periodnames) = ("monthly", hdf5io.monthenum._names.keys())
        periodnames = ['Dec', 'Jan', 'Feb', 'Mar', 'Apr', 'May',
                       'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov']
    elif (freq // _c.FR_QTR == 1):
        (periodtype, periodnames) = ("quarterly", fqtr2names[freq])
    else:
        raise NotImplementedError, "Unrecognized frequency %s" % freq
    #
    funcdict = dict(med='median',
                    mean='mean',
                    )
    funcname = funcdict[func]
    qtlname = '%03i' % (100 * quantile)
    qtlleg = {'000':'Minimum',
              '025':'Lower Quartile'}['%03i' % (100 * quantile)]
    #
    freq = climpy.check_freq(freq)
    if freq == _c.FR_MTH:
        (periodtype, periodnames) = ("monthly", hdf5io.monthenum._names.keys())
        periodnames = ['Dec', 'Jan', 'Feb', 'Mar', 'Apr', 'May',
                       'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov']
    elif (freq // _c.FR_QTR == 1):
        (periodtype, periodnames) = ("quarterly", fqtr2names[freq])
    else:
        raise NotImplementedError, "Unrecognized frequency %s" % freq
    #
    #
    flow_basename = "USGS_Q%s_%s%s.hdf5" % (qtlname, fqname, func)
    # Define the HDF5 files ....................................
    hdf5flow_filename = os.path.join(hdf5_archive_dir, flow_basename)
    file = hdf5io.openFile(hdf5flow_filename, 'r+')
    hdf5flow = hdf5io.remove_duplicates(file)
    #
    val = hdf5flow.getNode("/".join(['', 'values', 'modified', 'noenso'])).read()
    for (marker, color, station) in zip(['o'] * 3 + ['s'] * 4,
                                        ['k'] * 3 + ['r'] * 4,
                                        stationlist):
        locval = val[val['id'] == station]
        fsp.plot(range(12), [locval[p] for p in periodnames],
                marker=marker, label=station, c=color)
    fsp.set_yscale('log')
    fsp.xaxis.set_major_locator(pylab.MultipleLocator(1))
    fsp.set_xticklabels(periodnames)
    fsp.set_ylabel("Flows (cfs)", fontweight='bold')
    fsp.set_title("%s flows - %s" % (qtlleg, funcname), fontweight='bold')
    pylab.show()
def plot_val(fsp, station, func, quantile, group='modified'):
    #
    freq = _c.FR_MTH
    freq = climpy.check_freq(freq)
    if freq == _c.FR_MTH:
#        (periodtype, periodnames) = ("monthly", hdf5io.monthenum._names.keys())
        periodnames = ['Dec', 'Jan', 'Feb', 'Mar', 'Apr', 'May',
                       'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov']
    elif (freq // _c.FR_QTR == 1):
        (periodtype, periodnames) = ("quarterly", fqtr2names[freq])
    else:
        raise NotImplementedError, "Unrecognized frequency %s" % freq
    #
    funcdict = dict(med='median',
                    mean='mean',
                    )
    funcname = funcdict[func]
    qtlname = '%03i' % (100 * quantile)
    qtlleg = {'000':'Minimum',
              '025':'Lower Quartile'}['%03i' % (100 * quantile)]
    #
    #
    flow_basename = "USGS_Q%s_%s%s.hdf5" % (qtlname, fqname, func)
    # Define the HDF5 files ....................................
    hdf5flow_filename = os.path.join(hdf5_archive_dir, flow_basename)
    file = hdf5io.openFile(hdf5flow_filename, 'r+')
    hdf5flow = hdf5io.remove_duplicates(file)
    #
    if fsp.is_first_col():
        labels = {'G':u"Global",
                  'C':u"La Ni\xf1a",
                  'N':u"Neutral",
                  'W':u"El Ni\xf1o",
                  'NS':"N/S",
                  '90': '_nolabel_',
                  '95': '_nolabel_',
                  '99': '_nolabel_'}
    else:
        labels = {'G':'_nolabel_',
                  'C':'_nolabel_',
                  'N':'_nolabel_',
                  'W':'_nolabel_',
                  'NS':"N/S",
                  '90': "90%",
                  '95': "95%",
                  '99': "99%"}

    #
    val = hdf5flow.getNode("/".join(['', 'values', group, 'noenso'])).read()
    val = val[val['id'] == station]
    fsp.plot(range(12), [val[p] for p in periodnames],
             marker='^', ms=6, label=labels['G'], c=ENSOlines['G'])
    #
    val = hdf5flow.getNode("/".join(['', 'values', group, 'cold'])).read()
    val = val[val['id'] == station]
    val = numpy.array([val[p] for p in periodnames])
    apx = hdf5flow.getNode("/".join(['', 'apxpvalues', group, 'CN'])).read()
    apx = apx[apx['id'] == station]
    apx = numpy.array([apx[p] for p in periodnames])
    col_C = ENSOlines['C']
    fsp.plot(range(12), val, marker='', label='_nolabel_', c=col_C)
    fsp.plot(range(12), ma.array(val, mask=(apx < 0.1)),
             marker='^', label=labels['C'], c=col_C)
    fsp.plot(range(12), ma.array(val, mask=(apx >= 0.1) | (apx < 0.05)) ,
             marker='d', label='_nolabel_', ls='', c=col_C, ms=10)
    fsp.plot(range(12), ma.array(val, mask=(apx >= 0.05) | (apx < 0.01)) ,
             marker='s', label='_nolabel_', ls='', c=col_C, ms=10)
    fsp.plot(range(12), ma.array(val, mask=(apx > 0.01)) ,
             marker='o', label='_nolabel_', ls='', c=col_C, ms=10)
    #
    val = hdf5flow.getNode("/".join(['', 'values', group, 'neutral'])).read()
    val = val[val['id'] == station]
    fsp.plot(range(12), [val[p] for p in periodnames],
             marker='o', ms=6, label=labels['N'], c=ENSOlines['N'])
    #
    val = hdf5flow.getNode("/".join(['', 'values', group, 'warm'])).read()
    val = val[val['id'] == station]
    val = numpy.array([val[p] for p in periodnames])
    apx = hdf5flow.getNode("/".join(['', 'apxpvalues', group, 'WN'])).read()
    apx = apx[apx['id'] == station]
    apx = numpy.array([apx[p] for p in periodnames])
    col_W = ENSOlines['W']
    fsp.plot(range(12), val, marker='', label='_nolabel_', c=col_W)
    fsp.plot(range(12), ma.array(val, mask=(apx < 0.1)),
             marker='^', label=labels['W'], c=col_W)
    fsp.plot(range(12), ma.array(val, mask=(apx >= 0.1) | (apx < 0.05)) ,
             marker='d', label=labels['90'], ls='', c=col_W, ms=10)
    fsp.plot(range(12), ma.array(val, mask=(apx >= 0.05) | (apx < 0.01)) ,
             marker='s', label=labels['95'], ls='', c=col_W, ms=10)
    fsp.plot(range(12), ma.array(val, mask=(apx > 0.01)) ,
             marker='o', label=labels['99'], ls='', c=col_W, ms=10)
    #
    #fsp.set_yscale('log')
    #
    fsp.xaxis.set_major_locator(pylab.MultipleLocator(1))
    if fsp.is_last_row():
        fsp.set_xticklabels(periodnames, fontsize=10)
    else:
        fsp.set_xticklabels([])
    fsp.set_ylabel("Flows (cfs)", fontweight='bold', fontsize=10)
    fsp.yaxis.major.formatter.set_powerlimits((-3, 3))
#    fsp.set_title("%s Q%02i flows - %s" % (funcname.capitalize(),
#                                           100.*quantile,
#                                           station), fontweight='bold')
    fsp.set_title("%s" % (station), fontweight='bold', fontsize=10)
    if fsp.is_last_row():
        fsp.legend(numpoints=1, prop=dict(size='x-small'))

    hdf5flow.close()
    return
def plot_jma_vs_oni_comparison(freq='M'):
    """
    Plots the comparison of ONI vs JMAI ENSO indices at the given frequency.
    """
    # Check that we have a valid frequency
    freq = climpy.check_freq(freq)
    # Get the ENSO indices: annual for 12 months period, monthly on a monthly basis
    JMAi = JMA.indices(full_year=False, reference_season='NDJ', minimum_size=6)
    ONIi = ONI.indices(full_year=False, reference_season=None, minimum_size=5)
    # Convert to the new frequency
    if freq != climpy._c.FR_MTH:
        JMAi = np.round_(JMAi.convert(freq, func=ma.mean))
        ONIi = np.round_(ONIi.convert(freq, func=ma.mean))
    # Convert the indices to a Nx12 series (N the nb of years)
    JMAi = JMAi.convert('A')
    ONIi = ONIi.convert('A')
    #
    JMAi = JMAi[JMAi.year >= 1900]
    ONIi = ONIi[ONIi.year >= 1900]
    #
    j_colors = get_colormatrix(JMAi)
    o_colors = get_colormatrix(ONIi)
    #
    fig = cpl.figure(num=1, figsize=(13, 5))
    fig.clear()
    fsp = fig.add_axes([0.05, 0.1, 0.85, 0.85])
    (nrows, ncols) = j_colors.shape
    edgecolors = 'none'
    #
    j_vert = np.array([[((i, j + 1), (i, j), (i + 1, j)) for i in range(ncols)]
                          for j in range(nrows)]).reshape(-1, 3, 2)
    j_coll = matplotlib.collections.PolyCollection(j_vert,
                                                  facecolors=j_colors.ravel(),
                                                  edgecolors=edgecolors)
    fsp.add_collection(j_coll)
    #............................................
    o_vert = np.array([[((i, j + 1), (i + 1, j + 1), (i + 1, j)) for i in range(ncols)]
                          for j in range(nrows)]).reshape(-1, 3, 2)
    o_coll = matplotlib.collections.PolyCollection(o_vert,
                                                  facecolors=o_colors.ravel(),
                                                  edgecolors=edgecolors)
    fsp.add_collection(o_coll)
    #............................................
    xy_vert = [((i, 0), (i, nrows)) for i in range(0, ncols, 12)]
    xy_vert += [((0, j), (ncols, j)) for j in range(nrows)]
    xy_coll = matplotlib.collections.LineCollection(xy_vert,
                                                    colors='k')
    fsp.add_collection(xy_coll)
    #............................................
    fsp.xaxis.set_major_locator(cpl.MultipleLocator(12))
    fsp.set_xticks(np.arange(0, ncols, 12) + 6)
    fsp.set_xticklabels(["%02i" % i for i in range(12)])
    fsp.set_xlim(0, ncols)
    #............................................
    fsp.set_yticks(np.arange(0, nrows) + 0.5)
    fsp.set_yticklabels(["%i" % (i * 10 + 1900) for i in range(nrows + 1)])
    fsp.set_ylim(0, nrows)
    fsp.set_ylim(fsp.get_ylim()[::-1])
    add_plot_legend(fig, lableft='JMA', labright='ONI')
    cpl.show()
    return fig
                 "%s Q%02i flows" % (funcname.capitalize(), quantile * 100),
                 fontweight='bold', ha='center')
    return fig




#####-------------------------------------------------------------------------
if 1:
    freq_names = dict([(v, k) for (k, v) in _c.freq_constants.iteritems()])
    onlyGA = False
    func = 'mean'
    funclist = ('med', 'mean')
    freq = 'M'
#    freq = _c.FR_QTRSFEB
    fqname = freq_names[climpy.check_freq(freq)][3:]
    orientation = 'horizontal'
    reference = 'G'
    reference = 'N'
    #
    quantilelist = (0, 0.25)
    stationlist = ('02226500', '02228000', '02314500',
                   '02353000', '02353500', '02356000', '02357000')
    phasenames = ('CN', 'WN')

if 0:
    fig = pylab.figure()
    fsp = fig.add_subplot(221)
    plot_val_table(fsp, 'mean', freq, 0, stationlist)
    fsp = fig.add_subplot(222)
    plot_val_table(fsp, 'med', freq, 0, stationlist)
def plot_jma_vs_jma_comparison(freq='M'):
    """
    Plots the comparison of monthly and annual indices for the JMA index.
    The monthly indices are defined for each month using Nov-Jan as reference
    season and a minimum of 6 consecutive months in the same ENSO conditions.
    The annual indices are defined for each period of 12 months, starting in Oct.,
    using Oct-Dec as reference season and a minimum of 6 consecutive months in
    the same ENSO conditions
    
    Parameters
    ----------
    freq : {int, string}
        Valid frequency specifier.
    """
    # Check that we have a valid frequency
    freq = climpy.check_freq(freq)
    # Get the ENSO indices: annual for 12 months period, monthly on a monthly basis
    logger.debug("Get ENSO JMAI...")
    annual = JMA.set_indices(full_year=True, reference_season='OND',
                             minimum_size=6)
    monthly = JMA.set_indices(full_year=False, reference_season='NDJ',
                              minimum_size=6)
    # Convert to the new frequency
    if freq != climpy._c.FR_MTH:
        annual = np.round_(annual.convert(freq, func=ma.mean))
        monthly = np.round_(monthly.convert(freq, func=ma.mean))
    # Convert the indices to a Nx12 series (N the nb of years)
    annual = annual.convert('A')
    monthly = monthly.convert('A')
    # Trim to select only the data after 1900.
    annual = annual[annual.year >= 1900]
    monthly = monthly[monthly.year >= 1900]
    # Get the colors
#    logger.debug("Get color matrices")
#    a_colors = get_colormatrix(annual)
#    m_colors = get_colormatrix(monthly)
#    edegecolors = 'none'
    ensomap = ENSOmap
    #
    logger.debug("Prepare figure...")
    fig = cpl.figure(num=1, figsize=(13, 5))
    fig.clear()
    fsp = fig.add_axes([0.05, 0.1, 0.85, 0.85])
    (nrows, ncols) = annual.shape #a_colors.shape
    logger.debug("Get vertices #1...")
    # Get the vertices of the polygons corresponding to standard indices
    a_vert = np.array([[((i, j + 1), (i, j), (i + 1, j)) for i in range(ncols)]
                       for j in range(nrows)])
    a_vert.shape = (-1, 3, 2)
    # Create a PolyCollection for the polygons.
    a_coll = matplotlib.collections.PolyCollection(a_vert,
                                                   norm=ENSOmap)
    fsp.add_collection(a_coll)
    # Same process, this time with the monthly indices
    logger.debug("Get vertices #2...")
    m_vert = np.array([[((i, j + 1), (i + 1, j + 1), (i + 1, j)) for i in range(ncols)]
                       for j in range(nrows)])
    m_vert.shape = (-1, 3, 2)
    m_coll = matplotlib.collections.PolyCollection(m_vert,
                                                  norm=ENSOmap)
    fsp.add_collection(m_coll)
    # Plot the underlying grid.
    logger.debug("Plot grid...")
    xy_vert = [((i, 0), (i, nrows)) for i in range(0, ncols, 12)]
    xy_vert += [((0, j), (ncols, j)) for j in range(nrows)]
    xy_coll = matplotlib.collections.LineCollection(xy_vert, colors='k')
    fsp.add_collection(xy_coll)
    fsp.grid(True, which='minor', ls=':', c='#999999', lw=0.5)
    # Set the ticks on the
    logger.debug("Set ticks...")
    fsp.xaxis.set_major_locator(cpl.MultipleLocator(12))
    fsp.xaxis.set_minor_locator(cpl.MultipleLocator(1))
    fsp.set_xticks(np.arange(0, ncols, 12) + 6, minor=False)
    fsp.set_xticklabels(["%02i" % i for i in range(10)])
    fsp.set_xlim(0, ncols)
    #............................................
    fsp.set_yticks(np.arange(0, nrows) + 0.5)
    fsp.set_yticklabels(["%i" % (i * 10 + 1900) for i in range(nrows + 1)])
    fsp.set_ylim(0, nrows)
    fsp.set_ylim(fsp.get_ylim()[::-1])
    #............................................
    logger.debug("Add legend")
    add_plot_legend(fig)
    #
    #cpl.show()
    return fig