Ejemplo n.º 1
0
 def setUp(self):
     "Setting common information"
     self.JMA_std = load_jma('standard')
     self.JMA_coaps = load_jma('coaps')
     self.JMA_slide = load_jma('sliding')
def dump_seasonal_averages(observations, dbname="coaps.sqlite",
                           period="monthly", ensostr="ONI"):
    """
    Dump seasonal averages of observations in a SQLite database.

    The database is named following the template ``avg%(observations)s.sdb``.
    For each season, there are four tables named following the template 
    ``%(period)s_%(phase)s_%(ENSOindicator)``.
    Each table has N+1 columns: 
        * Station COOP ID (format ``COOP%06i``)
        * ENSO indicator (used to define the phase): in ('ONI', 'JMAI', 'MEI')
        * ENSO phase : in ('A', 'C', 'N', 'W')
        * N values for each season

    Parameters
    ----------
    observations: string
        String representing the observations to store.
    period : ('M', 'NDJ', 'DJF', 'JFM'), optional
        String representing the seasons. 
        Use 'M' for monthly, 'DJF' for 'DJF,MAM,JJA,SON'...
    ensostr : ('ONI', 'JMAI', 'MEI'), optional
        String representing the ENSO indicator to define ENSO phases
    """
    seasons = dict(monthly=['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN',
                            'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC', ],
                   JFM=['JFM', 'AMJ', 'JAS', 'OND'],
                   DJF=['DJF', 'MAM', 'JJA', 'SON'],
                   NDJ=['NDJ', 'FMA', 'MJJ', 'ASO'])

    # Get the seasonal frequency
    period = period.upper()
    period_to_freq = dict(M=hydro._c.FR_MTH,
                          MONTHLY=hydro._c.FR_MTH,
                          NDJ=hydro._c.FR_QTREOCT,
                          DJF=hydro._c.FR_QTRENOV,
                          JFM=hydro._c.FR_QTREDEC,)
    try:
        freq = period_to_freq[period]
    except KeyError:
        errmsg = "Invalid period '%s': should be in %s."
        raise ValueError(errmsg % (period, period_to_freq.keys()))

    # Get the names of the fields
    # fieldslist = ["COOPID text primary key", "ENSOI text", "phase text"]
    fieldslist = ["id integer primary key autoincrement",
                  "COOPID text", "ENSOI text", "phase text"]
    if period in ("M", "MONTHLY"):
        period = "monthly"
    fieldslist += ["%s real" % _ for _ in seasons[period]]
    nbfields = len(fieldslist)

    # Get the conversion function:
    if freq == hydro._c.FR_MTH:
        conversion_func = None
    else:
        if observations == 'rain':
            conversion_func = ma.sum
        else:
            conversion_func = ma.mean

    # Load the ENSO information
    ensostr = ensostr.upper()
    if ensostr == 'ONI':
        ENSO = enso.load_oni()
    elif ensostr == 'JMAI':
        ENSO = enso.load_jma()
    elif ensostr == 'MEI':
        raise NotImplementedError
    else:
        errmsg = "Invalid ENSO indicator '%s': should be in ('ONI','JMAI','MEI')"
        raise ValueError(errmsg % ensostr)

    # Get the monthly series from the datatable and set the ENSO indicator
    tbname = "series_monthly_%s" % observations
    monthly = sql.tsfromsqlite(dbname, tbname, freq="M")
    monthly = enso.set_ensoindicator(monthly, ENSO)

    # Define dictionaries storing the averages
    avg_r = {}
    # Loop on the stations
    for station in monthly.dtype.names:
	    # Get the current station
        current = monthly[station]
        # Get the season
        seasonal = current.convert(freq, func=conversion_func)
        if (observations == "rain") and (freq != hydro._c.FR_MTH):
            mask = hydro.time_series(current.mask, dates=current.dates)
            seasonal.mask = mask.convert(freq, func=ma.sum)
        # Get the values per phase
        coopid = station[-6:]
        avg_r[(coopid, ensostr, 'A')] = seasonal.convert("A").mean(0)
        avg_r[(coopid, ensostr, 'C')] = seasonal.cold.convert("A").mean(0)
        avg_r[(coopid, ensostr, 'N')] = seasonal.neutral.convert("A").mean(0)
        avg_r[(coopid, ensostr, 'W')] = seasonal.warm.convert("A").mean(0)

    # Get the database name
    detect_types = sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES
    connection = sqlite3.connect(dbname, detect_types=detect_types)
    # Define the creation/insertion lines
    tbname = "averages_%(period)s_%(observations)s" % locals()
    createstr = "create table %s (%s)" % (tbname, ", ".join(fieldslist))
    insertstr_template = "insert into %s values (%s)"
    insertstr = insertstr_template % (tbname, ", ".join(['?']*nbfields))
    # Create the table
    try:
        connection.execute(createstr)
    except sqlite3.OperationalError:
        pass
    # Define a generator for the values
    generator = ([None, ] + list(k) + list(v.astype(np.object).filled((None,)))
                 for (k, v) in avg_r.items())
    connection.executemany(insertstr, generator)
    connection.commit()
    connection.close()
Ejemplo n.º 3
0
 def setUp(self):
     "Setting common information"
     self.JMA_std = load_jma('standard')
     self.JMA_coaps = load_jma('coaps')
     self.JMA_slide = load_jma('sliding')


import matplotlib
#matplotlib.use('Agg')
import scikits.hydroclimpy.plotlib as cpl
from  scikits.hydroclimpy.plotlib.ensotools import csfigure, \
                                                   ENSOcolors, ENSOpolygons, \
                                                   ENSOmap

import logging
logger = logging.getLogger('compare_enso')

logger.debug("The fun starts here...")

JMA = enso.load_jma()
ONI = enso.load_oni()


print JMA.ensoindices.convert('A')

def plot_jma(num=None, fill=True, full_year=False, fill_neutral=False):
    if num is None:
        fig = csfigure(series=JMA)
    else:
        fig = csfigure(num=num, series=JMA)
    fig.clear()
    fsp = fig.add_csplot(111)
    fill_colors = ENSOcolors['fill']
    polygon_colors = ENSOcolors['polygons']
    marker_colors = ENSOcolors['markers']
import multibarplot
from multibarplot import MultiBarPlot
from timescales import CalendarScaleSystem, MYScales, to_seconds_from_epoch
from zoom_plot_container import ReversedZoomOverlay, ZoomedPlotContainer
from enso_info import ElNinoOverlay, NeutralOverlay, LaNinaOverlay
import misc_tools
from misc_tools import print_decimal_coordinates, select_stations_around_reference

periods_dict = dict(MON=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                         'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
                    JFM=['JFM', 'AMJ', 'JAS', 'OND'],
                    DJF=['DJF', 'MAM', 'JJA', 'SON'],
                    NDJ=['NDJ', 'FMA', 'MJJ', 'ASO'])

oni = enso.load_oni()
jmai = enso.load_jma()



class PeriodSelector(HasTraits):
    period = Enum("MON", "JFM", "DJF", "NDJ")
    entries = dict(MON=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                         'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
                  JFM=['JFM', 'AMJ', 'JAS', 'OND'],
                  DJF=['DJF', 'MAM', 'JJA', 'SON'],
                  NDJ=['NDJ', 'FMA', 'MJJ', 'ASO'])
    seasons = Property(depends_on="selector")

    def _get_seasons(self):
        return self.entries[self.period]