def test_ONI_standard(self): "Test the 'standard' option of load_oni" if self.indices is None: return ONI = load_oni('standard') ONI.set_indices(full_year=False, minimum_size=5, reference_season=None) assert_equal(*ts.align_series(ONI.indices, self.indices))
def test_set_ensoindices(self): "Test setting indices" ONI = load_oni('standard') mseries = climate_series(np.random.rand(120), start_date=ts.Date('M', '1991-01'), ensoindicator=ONI) control = ONI['1991-01':'2001-01'] assert_equal(mseries.ensoindices, control.indices) test = mseries.set_ensoindices(full_year=True).copy() self.failUnless(mseries.ensoindicator.optinfo['full_year']) assert_equal(test, control.set_indices(full_year=True)) self.failUnless(control.optinfo['full_year']) assert_equal(test, mseries.ensoindices) assert_equal(mseries.set_ensoindices(lag=6), test.tshift(-6)) # mseries.set_ensoindices(full_year=True, lag=0) series = ts.lib.backward_fill(mseries.convert('D')) series_m = series.convert('M', ma.mean) assert_equal(test, series_m.ensoindices)
def test_set_ensoindices(self): "Test setting indices" ONI = load_oni('standard') mseries = climate_series(np.random.rand(120), start_date=ts.Date('M','1991-01'), ensoindicator=ONI) control = ONI['1991-01':'2001-01'] assert_equal(mseries.ensoindices, control.indices) test = mseries.set_ensoindices(full_year=True).copy() self.failUnless(mseries.ensoindicator.optinfo['full_year']) assert_equal(test, control.set_indices(full_year=True)) self.failUnless(control.optinfo['full_year']) assert_equal(test, mseries.ensoindices) assert_equal(mseries.set_ensoindices(lag=6), test.tshift(-6)) # mseries.set_ensoindices(full_year=True, lag=0) series = ts.lib.backward_fill(mseries.convert('D')) series_m = series.convert('M', ma.mean) assert_equal(test, series_m.ensoindices)
def test_oni_refperiod(self): "Check that we have the proper frequency for the reference period" ONI = load_oni('standard') refperiod = ONI.refperiod assert(isinstance(refperiod, ts.DateArray)) assert_equal(refperiod.freqstr, 'M')
condition, depending on the ``lag`` optional parameter. By default, the ``lag`` parameter is calculated as the time difference between the first month of the ENSO indicator reference (or October, if the ENSO indicator does not have any reference season) and the ``starting_month`` parameter. For example, if ``starting_month`` is 4 (April) and the reference season runs from October to December included, the lag will be 6 months, and each water year will be associated with the ENSO conditions prevailing the previous October. In both cases, the colors corresponding to each ENSO conditions are available through the :data:`ENSOcolors` dictionary. Let us replot the previous FDCs with ENSO information. First, we need to associate an ENSO indicator to the series. """ ONI = enso.load_oni() series = enso.set_ensoindicator(series, ONI) """ We can now create a new figure and a new plot. """ fig = cpl.figure() cpl.plot_fdc(series, enso=True, marker='o') cpl.plot_fdc(series, enso=True, starting_month=4) cpl.gca().legend() fig.savefig('athens_fdc_enso.png') """ .. image:: athens_fdc_enso.* """
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()
import numpy as np import scikits.hydroclimpy as hydro import scikits.hydroclimpy.enso as enso import scikits.hydroclimpy.io.usgs as usgs import scikits.hydroclimpy.plotlib as cpl ONI = enso.load_oni() series = usgs.load_usgs_flows('02217770').fill_missing_dates() series = enso.set_ensoindicator(series, ONI) fig = cpl.figure() fsp = fig.add_subplot(111) cpl.plot_fdc(series, plot_enso=True, marker='o', markersize=6, ax=fsp) cpl.plot_fdc(series, plot_enso=True, starting_month=4, ax=fsp) fsp.legend() fsp.set_ylabel("Flows (cfs)", fontweight='bold') fig.suptitle("Flow duration curve for the North Oconee River at Athens, GA", fontweight="bold", fontsize=12)
def test_oni_refperiod(self): "Check that we have the proper frequency for the reference period" ONI = load_oni('standard') refperiod = ONI.refperiod assert (isinstance(refperiod, ts.DateArray)) assert_equal(refperiod.freqstr, 'M')
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]