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.* """
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 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()
mrainfall = rainfall.convert('M',func=ma.sum) arainfall = mrainfall.convert('A') assert(arainfall.shape == (64, 12)) monthly_means = arainfall.mean(axis=0).round(1) print monthly_means """ [ 115.9 112.4 130.8 94.1 99.4 103.5 122.3 90.2 94.5 77.8 92.7 98.6]""" ONI = enso.load_oni() print ONI.dates[[0,-1]] """ [Jan-1950 Dec-2008]""" mrainfall = enso.set_ensoindicator(mrainfall, ONI) print type(mrainfall) assert(isinstance(mrainfall, enso.ClimateSeries)) print mrainfall.ensoindicator.dates[[0,-1]] """ [Jan-1944 Dec-2007]""" mrainfall_2K = mrainfall[(mrainfall.year == 2000)] print mrainfall_2K.cold """[113.792 50.546 86.614 43.18 54.864 50.292 -- -- -- 5.842 106.68 87.884]""" print mrainfall_2K.neutral """[-- -- -- -- -- -- 85.344 93.218 122.174 -- -- --]""" print mrainfall_2K.warm """[-- -- -- -- -- -- -- -- -- -- -- --]""" mdtype = [('cold', float), ('neutral', float), ('warm', float),