Example #1
0
    def __init__(self, **kwargs):
        """
        kwargs ={'baisnName':'Mackenzie',
                 'start_date':'2000-06-01',
                 'end_date':'2010-06-31',
                 'info_fl':r'C:\00_Work\02_Sim\00_Mackenzie\01_Data\01_Selected_RiverDischarge\RiverGages_description.csv',
                 'pthIn':r'C:\00_Work\02_Sim\00_Mackenzie\01_Data\01_Selected_RiverDischarge'}
        """

        for key in kwargs:

            setattr(self, key, kwargs[key])

        self._load_rivergages()

        self._idate = ts.Date('D', self.start_date)

        self._idateHStr = self._idate.strfmt('%Y/%m/%d') + ' 00:00'

        self._fdate = ts.Date('D', self.end_date)

        self._dates = ts.date_array(start_date=self._idate,
                                    end_date=self._fdate,
                                    freq='d')

        self.nr_days = self._fdate - self._idate + 1

        self._get_matrix()
Example #2
0
    def Write_MESH_tsi(self, start_date, end_date, pthOut, nodata=0.0):

        self._idate = ts.Date(self.data.freq, start_date)
        self._fdate = ts.Date(self.data.freq, end_date)

        data = self.data['Flow']
        indx = data.date_to_index(self._idate)
        fndx = data.date_to_index(self._fdate)
        data = data[indx:fndx + 1]

        salida = open(pthOut + '\\MESH_input_streamflow.txt', 'w')

        salida.write('%s\n' % ('Only one river gage'))

        salida.write('%5d %5d %5d %5d %5d %5d %5s \n' %
                     (1, data.size, data.size, self._nhours, self._idate.year,
                      self._idate.day_of_year, ' 00 '))

        salida.write(
            '%5d %5d %5s \n' %
            (int(round(self._latMIN)), int(round(self._lonMIN)), self.idGage))
        for i in xrange(data.size):

            if data[i]:

                val = data[i]

            else:

                val = nodata

            salida.write('%10.3f\n' % (val))

        salida.close()
Example #3
0
 def test_load_usgs_flows(self):
     "Test loadind USGS water data."
     #
     F02217900 = load_usgs_flows('02217900')
     data_192810 = [180, 180, 180, 180, 180, 180, 180, 180, 180, 180,
                    180, 180, 180, 180, 180, 180, 230, 300, 350, 300,
                    230, 200, 250, 270, 240, 200, 180, 180, 180, 180, 180]
     data_194912 = [276, 267, 267, 267, 262, 264, 269, 302, 291, 279,
                    310, 307, 304, 358, 425, 425, 371, 336, 336, 371,
                    336, 333, 336, 333, 317, 336, 410, 425, 384, 336, 330]
     assert(isinstance(F02217900, ts.TimeSeries))
     assert_equal(F02217900.start_date, ts.Date('D', '1928-10-01'))
     assert_equal(F02217900.end_date, ts.Date('D', '1949-12-31'))
     dates = F02217900.dates
     assert_equal(F02217900[(dates.year == 1928) & (dates.month == 10)],
                  data_192810)
     assert_equal(F02217900[(dates.year == 1949) & (dates.month == 12)],
                  data_194912)
     #
     F02217500 = load_usgs_flows('02217500')
     data_190110 = [ 570, 1000, 1200, 900, 650, 580, 540, 520, 510, 530,
                     499,  535,  535, 572, 535, 535, 499, 499, 499, 499,
                     499,  499,  499, 499, 499, 499, 464, 499, 499, 499, 499]
     assert(isinstance(F02217500, ts.TimeSeries))
     assert_equal(F02217500.start_date, ts.Date('D', '1901-10-01'))
     dates = F02217500.dates
     assert_equal(F02217500[(dates.year == 1901) & (dates.month == 10)],
                  data_190110)
 def setUp(self):
     series = ReferencedSeries(np.random.rand(360),
                               start_date=ts.Date('M', '1970-01-01'))
     series[1:-1:10] = masked
     refperiod = ts.DateArray(
         [ts.Date('M', '1980-01-01'),
          ts.Date('M', '1990-01-01')], freq='M')
     self.series = series
     self.refperiod = refperiod
 def test_get_refperiod(self):
     "Check that the reference period has always the proper frequency"
     a = ReferencedSeries(np.arange(2000, 2010),
                          start_date=ts.Date('Y', '2000'),
                          refperiod=(ts.Date('Y',
                                             '2003'), ts.Date('A', '2006')))
     assert (isinstance(a.refperiod, ts.DateArray))
     assert_equal(a.refperiod.freq, a.dates.freq)
     a = a.convert('M')
     assert_equal(a.refperiod.freq, a.dates.freq)
Example #6
0
def _idx_from_dates(d1, d2, freq):
    """
    Returns an index offset from datetimes d1 and d2. d1 is expected to be the
    last date in a date series and d2 is the out of sample date.

    Note that it rounds down the index if the date is before the next date at
    freq.
    """
    d1 = ts.Date(freq, datetime=d1)
    d2 = ts.Date(freq, datetime=d2)
    return d2 - d1
Example #7
0
 def testPysDate(self):
     print "testPysDate"
     cusum = list(csv.reader(open("cusumpys.csv", "r")))
     outof = len(self.pyssuite)
     for n, case in enumerate(self.pyssuite):
         print n+1, '/', outof
         case.train()
         progdate = case.cusum.dates.tolist()
         print progdate[0]
         print ts.Date('M', '1999-1')
         if progdate[0] != ts.Date('M', '1999-1'):
             raise AssertionError
Example #8
0
    def setUp(self):
        self.pdssuite = []
        self.pyssuite = []

        self.pdscross = {0:ts.Date('M', '2002-11'), 1:ts.Date('M', '2002-2'), 2:ts.Date('M', '2001-12')}
        self.pyscross = {0:ts.Date('M', '1999-10')}
        
        data = np.matrix(list(csv.reader(open("test.csv", "r"))))
        data = cs.dataFormat(data)
        self.pdssuite.append(cs.Cusum(data[0], 4, fcn = "pds", para = (1,"twoside", 36)))
        self.pdssuite.append(cs.Cusum(data[1], 4, fcn = "pds", para = (1,"upper", 36)))
        self.pdssuite.append(cs.Cusum(data[2], 4, fcn = "pds", para = (1,"lower", 36)))
        self.pyssuite.append(cs.Cusum(data[3], 4, fcn = "pys", para = (7.5, 0.5, 0., 0.9, 1.0)))
Example #9
0
def _collect_and_plot(files):
    TS = []
    location = []
    for f in files:
        temperatures = [ section[1] for section in parse.parse_file(f)[1:-1] if section[1]['Plant'] == ['tmp'] ]
        for t in temperatures:
            if t['Step'][0] != '0000-00-00.01:00:00':
                print 'Not hourly readings of temperature. Abort.'
                break
            dates = ts.date_array(start_date=ts.Date('H', t['Start'][0]), length=len(t['Value']))
            data = [ float(value.rsplit('/')[0]) for value in t['Value'] ]
            TS.append(ts.TimeSeries(data=data, dates=dates))
            if location and t['Installation'][0] != location:
                print 'Location changed during reading of gs2 files. Probably some bad grouping of gs2 files.'
            location = t['Installation'][0]
    if TS:
        path = '/Users/tidemann/Documents/NTNU/devel/data/eklima/Telemark/'
        for file in os.listdir(path):
            try:
                series = xml.parse(path + file)
                sg.utils.plot_time_series([ts.concatenate((TS)), series], ['b-','r-'], [location, file])
            except:
                print file, 'had no data.'
    else:
        print 'No temperature data.'
Example #10
0
 def setUp(self):
     "Setting common information"
     try:
         from BeautifulSoup import BeautifulSoup, SoupStrainer
     except ImportError:
         self.indices = None
         return
     # Load the file as a tree, but only take the SST table (border=1)
     from urllib import urlopen
     url = "http://www.cpc.noaa.gov/products/analysis_monitoring/"\
           "ensostuff/ensoyears.shtml"
     url = urlopen(url)
     table = BeautifulSoup(url.read(),
                           parseOnlyThese=SoupStrainer("table", border=1))
     # Separate it by rows, but skip the first one (the header)
     years = []
     indices = []
     color = dict(red=+1, white=0, blue=-1)
     deft = [(None, 'color:white')]
     for row in table.findAll("tr")[1:]:
         cols = row.findAll('td')
         years.append(int(cols.pop(0).strong.string))
         indices.append([
             color[getattr(_.span, 'attrs', deft)[0][-1].split(':')[-1]]
             for _ in cols
         ])
     start_date = ts.Date('M', year=years[0], month=1)
     self.indices = time_series(np.array(indices).ravel(),
                                start_date=start_date)
Example #11
0
 def test_sorted(self):
     dates = [ts.Date('D', string='2007-01-%02i' % i) for i in (3, 2, 1)]
     (a, b) = zip(*[
         (3., 30),
         (2., 20),
         (1., 10),
     ])
     ndtype = [('a', np.float), ('b', np.int)]
     controldates = date_array(dates, freq='D')
     controldates.sort_chronologically()
     series = time_series(zip(*(a, b)), dates, freq='D', dtype=ndtype)
     assert_equal(series._data.tolist(), [(1., 10), (2., 20), (3., 30)])
     assert_equal(series._dates, controldates)
     #
     trec = time_records(zip(*(a, b)), dates, freq='D', dtype=ndtype)
     assert_equal(trec._data.tolist(), [(1., 10), (2., 20), (3., 30)])
     assert_equal(trec._dates, controldates)
     assert_equal(trec['a'], [1., 2., 3.])
     assert_equal(trec.a, [1., 2., 3.])
     #
     trec = fromrecords(zip(a, b), dates, names=('a', 'b'))
     assert_equal(trec._data.tolist(), [(1., 10), (2., 20), (3., 30)])
     assert_equal(trec._dates, controldates)
     assert_equal(trec['a'], [1., 2., 3.])
     assert_equal(trec.a, [1., 2., 3.])
     #
     trec = fromarrays([a, b], dates, names=('a', 'b'))
     assert_equal(trec._data.tolist(), [(1., 10), (2., 20), (3., 30)])
     assert_equal(trec._dates, controldates)
     assert_equal(trec['a'], [1., 2., 3.])
     assert_equal(trec.a, [1., 2., 3.])
 def setUp(self):
     "Initializes"
     data = [
         -0.49, -0.38, -0.26, 0.54, 0.95, 0.60, 0.93, 0.80, 0.52, 0.15,
         -0.50, -0.58, -0.58, -0.70, -0.54, -0.80, -0.75, 0.33, -0.18,
         -0.55, -0.77, -0.73, -0.58, -0.68, -0.62, 0.75, 0.72, 0.50, 0.84,
         0.64, 0.77, -0.03, 0.71, 0.17, 0.64, -0.12, -0.17, 0.34, 0.57,
         -0.57, -0.77, -0.56, -0.51, -0.80, 0.19, -0.83, 0.53, 0.72, 0.77,
         0.64, 0.57, 0.51, 0.51, 0.67, 0.70, 0.40, 0.29, 0.23, 0.13, 0.00
     ]
     refperiod = ts.DateArray(
         [ts.Date('M', '1980-01-01'),
          ts.Date('M', '1990-01-01')], freq='M')
     ensoi = ENSOIndicator(data, start_date=ts.Date('M', '1980-01-01'))
     ensoi.thresholds = (-0.5, 0.5)
     self.ensoi = ensoi
 def test_force_reference(self):
     mseries = ts.time_series(np.arange(24),
                              start_date=ts.Date('M', '2001-01'))
     aseries = ts.time_series([1, 2, 3], start_date=ts.Date('A', '2001-01'))
     #
     mtest = force_reference(aseries, mseries)
     assert_equal(mtest.freq, ts.check_freq('M'))
     assert_equal(mtest.dates[[0, -1]], mseries.dates[[0, -1]])
     assert_equal(mtest, [1] * 12 + [2] * 12)
     mtest = force_reference(aseries, mseries, ma.sum)
     assert_equal(mtest, [1] * 12 + [2] * 12)
     #
     atest = force_reference(mseries, aseries)
     assert_equal(atest.freq, ts.check_freq('A'))
     assert_equal(atest.dates[[0, -1]], aseries.dates[[0, -1]])
     assert_equal(atest, ma.array([5.5, 17.5, 0], mask=[0, 0, 1]))
     atest = force_reference(mseries, aseries, ma.sum)
     assert_equal(atest, ma.array([66, 210, 0], mask=[0, 0, 1]))
Example #14
0
def _date_from_idx(d1, idx, freq):
    """
    Returns the date from an index beyond the end of a date series.
    d1 is the datetime of the last date in the series. idx is the
    index distance of how far the next date should be from d1. Ie., 1 gives
    the next date from d1 at freq.
    """
    tsd1 = ts.Date(freq, datetime=d1)
    tsd2 = datetime.datetime.fromordinal((tsd1 + idx).toordinal())
    return tsd2
 def test_mov_average_expw_mask(self):
     "Make sure that mov_average_expw doesn't modify the initial mask"
     N = 256
     series = ts.time_series(np.random.rand(N),
                             start_date=ts.Date('D', '2008-01-01'))
     series[96:128] = ma.masked
     controlmask = np.zeros(N, dtype=bool)
     controlmask[96:128] = True
     #
     test = mf.mov_average_expw(series, 16)
     assert_not_equal(test.mask, series.mask)
     assert_equal(series.mask, controlmask)
 def test_check_refperiod(self):
     "Test that the reference period is always a DateArray"
     series = ReferencedSeries(np.arange(10),
                               start_date=ts.Date('M', '2001-01'))
     refperiod = series._optinfo['reference_period']
     self.failUnless(refperiod is None)
     series.refperiod = None
     refperiod = series._optinfo['reference_period']
     self.failUnless(isinstance(refperiod, ts.DateArray))
     assert_equal(refperiod.tovalues(), series.dates[[0, -1]].tovalues())
     series.refperiod = None
     assert_equal(refperiod, series.dates[[0, -1]])
     self.failUnless(isinstance(refperiod, ts.DateArray))
Example #17
0
    def formatScikitsTS(self, content):
        """Format a given date in a user friendly way.

        The textual representation of the date index is converted to a Date
        instance that can be easily formatted.

        :Parameter content: the content of the table cell being formatted
        """

        date = ts.Date(self.ts_freq, value=int(content))
        try:
            return date.datetime.strftime(self.ts_format)
        except ValueError:
            return content
 def test_ensoindices_from_daily_indicator(self):
     "Try setting ensoindices from a daily indicator"
     ensoi = self.ensoi
     ensoi.set_monthly_indices(minimum_size=5, reference_season='NDJ')
     #
     bseries = climate_series(range(24),
                              start_date=ts.Date('M', '1982-01'),
                              ensoindicator=ensoi)
     control_indices = [
         -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         1, 1
     ]
     control_indices = ts.time_series(control_indices,
                                      start_date=bseries.dates[0])
     # Basic check
     assert_equal(bseries.ensoindices, control_indices)
     # Define daily series
     dseries = climate_series(range(730),
                              start_date=ts.Date('D', '1982-01-01'),
                              ensoindicator=ensoi)
     mseries = climate_series(range(24),
                              start_date=ts.Date('M', '1982-01'),
                              ensoindicator=dseries.ensoindicator)
     assert_equal(mseries.ensoindices, bseries.ensoindices)
Example #19
0
 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_ensoindices_on_irregular_series(self):
     "Try setting ensoindices on a non-regular series"
     ensoi = self.ensoi
     ensoi.set_monthly_indices(minimum_size=5, reference_season='NDJ')
     #
     bseries = climate_series(range(24),
                              start_date=ts.Date('M', '1982-01'),
                              ensoindicator=ensoi)
     control_indices = [
         -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         1, 1
     ]
     control_indices = ts.time_series(control_indices,
                                      start_date=bseries.dates[0])
     tseries = bseries[::2]
     assert_equal(tseries.ensoindices, control_indices[::2])
     mseries = climate_series(tseries, ensoindicator=bseries.ensoindicator)
     assert_equal(mseries.ensoindices, bseries.ensoindices[::2])
     mseries = climate_series(tseries, ensoindicator=bseries.ensoindicator)
Example #21
0
#    try:
#        import scikits.talkbox as tb
#    except:
#        raise ImportError("You need scikits.talkbox installed for timings")
#    t = timer()
#    mod_tb = tb.lpc(y, 2)
#    t_end = timer()
#    print str(t_end - t) + " seconds for talkbox.lpc"
#    print """For higher lag lengths ours quickly fills up memory and starts
#thrashing the swap.  Should we include talkbox C code or Cythonize the
#Levinson recursion algorithm?"""

    ## Try with a pandas series
    import pandas
    import scikits.timeseries as ts
    d1 = ts.Date(year=1700, freq='A')
    #NOTE: have to have yearBegin offset for annual data until parser rewrite
    #should this be up to the user, or should it be done in TSM init?
    #NOTE: not anymore, it's end of year now
    ts_dr = ts.date_array(start_date=d1, length=len(sunspots.endog))
    pandas_dr = pandas.DateRange(start=d1.datetime,
                                 periods=len(sunspots.endog), timeRule='A@DEC')
    #pandas_dr = pandas_dr.shift(-1, pandas.datetools.yearBegin)

    dates = np.arange(1700, 1700 + len(sunspots.endog))
    dates = ts.date_array(dates, freq='A')
    #sunspots = pandas.Series(sunspots.endog, index=dates)

    #NOTE: pandas only does business days for dates it looks like
    import datetime
    dt_dates = np.asarray(lmap(datetime.datetime.fromordinal,
Example #22
0
#       Author:  Vicent Mas - [email protected]
"""Storing time series created with scikits.timeseries module in PyTables.
Example 1.
"""

import os

import numpy as np

import scikits.timeseries as ts
import scikits.timeseries.lib.tstables as tstab
import scikits.timeseries.lib.reportlib as rl

## a test series
data = np.arange(1, 366)
startdate = ts.Date(freq='D', year=2009, month=1, day=1)
test_series = ts.time_series(data, start_date=startdate, freq='D')

output_dir = '../timeseries'
try:
    os.mkdir(output_dir)
except OSError:
    pass

## write to csv file
csv_file = os.path.abspath(os.path.join(output_dir, 'test_series.csv'))
mycsv = open(csv_file, 'w')
csvReport = rl.Report(test_series, delim=';', fixed_width=True)
csvReport(output=mycsv)
mycsv.close()
Example #23
0
import scikits.timeseries as ts
import numpy as np
import numpy.ma as ma
import csv
import scikits.timeseries.lib.reportlib as rl
import scipy.stats.mstats as stats

data = np.matrix(list(csv.reader(open("spmstar.csv", "r"))))
date = data[0, 1:]
desc = data[1:, 0]
data = np.array(data[1:, 1:])

first_date = ts.Date('M', '1999-01')
desc = list(map(lambda x: x[0, 0], desc))
format = [float] * len(desc)
format = zip(desc, format)
#print format
serieses = [
    ts.time_series(i, start_date=first_date, dtype=float, fill_value=-99)
    for i in data
]
serieses = [
    i[i > -999]
    for i in [ma.masked_values(series, -999) for series in serieses]
    if len(i) > 48
]
print len(serieses)

s = serieses[1]
# for series in serieses:
#     try:
"""
Look at some macro plots, then do some VARs and IRFs.
"""

import numpy as np
import statsmodels.api as sm
import scikits.timeseries as ts
import scikits.timeseries.lib.plotlib as tplt

data = sm.datasets.macrodata.load(as_pandas=False)
data = data.data

### Create Timeseries Representations of a few vars

dates = ts.date_array(start_date=ts.Date('Q', year=1959, quarter=1),
                      end_date=ts.Date('Q', year=2009, quarter=3))

ts_data = data[['realgdp', 'realcons', 'cpi']].view(float).reshape(-1, 3)
ts_data = np.column_stack((ts_data, (1 - data['unemp'] / 100) * data['pop']))
ts_series = ts.time_series(ts_data, dates)

fig = tplt.tsfigure()
fsp = fig.add_tsplot(221)
fsp.tsplot(ts_series[:, 0], '-')
fsp.set_title("Real GDP")
fsp = fig.add_tsplot(222)
fsp.tsplot(ts_series[:, 1], 'r-')
fsp.set_title("Real Consumption")
fsp = fig.add_tsplot(223)
fsp.tsplot(ts_series[:, 2], 'g-')
fsp.set_title("CPI")
Example #25
0
def load_usgs_flows(site_no, cfgdict=usgs_config):
    """
    Downloads hydrological data from the USGS site.

    Parameters
    ----------
    site_no : str
        2- to 15-digit identification number of the site whose information must
        be downloaded.
    netfile : {string}, optional
        URL of the USGS flow gage site.
        By default, the value is read from the configuration file.
    begin_date : {string}, optional
        First date to be retrieved.
        By default, the value is read from the configuration file.
    end_date : {None, string}, optional
        Last date to be retrieved.
        If None, uses the current date.
    datadir : {None, string}, optional
        Directory where to store the output.
        If None, uses the current directory.
    commentchar : {string}, optional
        Character corresponding to a comment.
    headerlines : {int}, optional
        Number of uncommented lines to skip from the beginning of the file.
    archive : {None, string}, optional
        Name of the ZIP archive where to read pre-stored data or when to store 
        data newly retrieved.
        The default is read from the configuration file.

    Notes
    -----
    The list of data codes valid for the ``data_type`` parameters are available
    on the `USGS site <http://waterdata.usgs.gov/nwis/pmcodes/pmcodes?pm_group=All+--+include+all+parameter+groups&pm_search=&format=html_table&show=parameter_group_nm&show=parameter_nm>`_

    """
    # Force the site_no to a 0-padded, 8-character string
    site_no = "%08i" % int(site_no)
    #data_type = cfgdict.get('data_type', '00060')
    data_type = '00060'
    data_letter = data_dict[data_type]
    usgsarchv = _check_archive(cfgdict)
    archfname = "%s%sD" % (data_letter, site_no)
    #
    try:
        zipf = zipfile.ZipFile(usgsarchv, 'r')
        series = cPickle.loads(zipf.read(archfname))
    except IOError:
        zipf = zipfile.ZipFile(usgsarchv, 'w')
    except KeyError:
        zipf = zipfile.ZipFile(usgsarchv, 'a')
    else:
        if series.size > 0:
            return series
        else:
            zipf = zipfile.ZipFile(usgsarchv, 'a')
    #
    options = dict(usgs_site=cfgdict['netfile'],
                   begin_date=cfgdict['begin_date'],
                   data_type=data_type,
                   site_no=site_no)
    end_date = cfgdict.get('end_date', None)
    if end_date is None:
        end_date = ts.now('D').strftime("%Y-%m-%d")
    options['end_date'] = end_date
    commd = "%(usgs_site)s/dv?format=rdb&cb_%(data_type)s=on&" + \
            "begin_date=%(begin_date)s&end_date=%(end_date)s&" + \
            "site_no=%(site_no)s"
    datadir = cfgdict.get('datadir', os.getcwd())
    # We can't use np.lib._datasource.DataSource, the downloaded file has always
    # the same name and would be cached.
    sourcedir = np.lib._datasource.DataSource(datadir)
    dfile = urllib.urlopen(commd % options)
    #
    headers = int(cfgdict['headerlines'])
    #
    (datelist, datalist) = ([], [])
    for line in dfile.readlines():
        line = line.strip().split("\t")
        if (not line) or (line[0][0] == '#'):
            continue
        try:
            datalist.append(line[3])
        except IndexError:
            continue
        else:
            datelist.append(line[2])
    series = ts.time_series(
        [float(_) for _ in datalist[headers:]],
        dates=[ts.Date('D', _) for _ in datelist[headers:]],
        freq='D')
    #
    zipf.writestr(archfname, cPickle.dumps(series))
    zipf.close()
    return series
Example #26
0
#       Author:  Vicent Mas - [email protected]

"""Storing time series created with scikits.timeseries module in PyTables.
Example 3.
"""

import os

import numpy as np

import scikits.timeseries as ts
import scikits.timeseries.lib.tstables as tstab

# Generate random data
data = np.cumprod(1 + np.random.normal(0, 1, 300)/100)
series = ts.time_series(data, start_date=ts.Date(freq='M', year=1982, month=1))

# Write to a PyTables file
output_dir = '../timeseries'
try:
    os.mkdir(output_dir)
except OSError:
    pass

hdf5_name = 'scikits_test3.hdf5'
filepath_hdf5 = os.path.join(output_dir, hdf5_name)
h5file = tstab.open_file(filepath_hdf5, mode="w",
title='Example table with csikits time series')
group_doc = h5file.create_group("/", 'examples', 'Test Data')
table = h5file.createTimeSeriesTable(group_doc, 'Example_3', series)
h5file.close()
Example #27
0
import numpy as np
import numpy.ma as ma
import matplotlib.pyplot as plt
import scikits.timeseries as ts
import scikits.timeseries.lib.plotlib as tpl

# generate some random data
data1 = np.cumprod(1 + np.random.normal(0, 1, 300)/100)
data2 = np.cumprod(1 + np.random.normal(0, 1, 300)/100)*100
start_date = ts.Date(freq='M', year=1982, month=1)
series1 = ts.time_series(data1, start_date=start_date-50)
series2 = ts.time_series(data2, start_date=start_date)
fig = tpl.tsfigure()
fsp = fig.add_tsplot(111)
# plot series on left axis
fsp.tsplot(series1, 'b-', label='<- left series')
fsp.set_ylim(ma.min(series1.series), ma.max(series1.series))
# create right axis
fsp_right = fsp.add_yaxis(position='right', yscale='log')
# plot series on right axis
fsp_right.tsplot(series2, 'r-', label='-> right series')
fsp_right.set_ylim(ma.min(series2.series), ma.max(series2.series))
# setup legend
fsp.legend(
    (fsp.lines[-1], fsp_right.lines[-1]),
    (fsp.lines[-1].get_label(), fsp_right.lines[-1].get_label()),
)
plt.show()
Example #28
0
File: lib.py Project: pwgold/aws
def get_date(datestr, fmt='%Y%m%d', ts_freq=None):
    datestr = datestr.replace('-', '')
    if ts_freq != None:
        return ts.Date(ts_freq, datestr)  #e.g., 'B'
    else:
        return datetime.strptime(datestr, '%Y%m%d')
Example #29
0
def gw_model_file(in_fname, out_fname, figure_dir=None):
    """"
    input:
        in_fname:   name of input xls file
        out_fname:  name of the output xls file
        figure_dir: name of the directory where to save the out figures
    """

    # read the data from xls file
    in_book = xlrd.open_workbook(in_fname)
    sheet_names = in_book.sheet_names()
    sheet_names.remove('legend')

    out_book = xlwt.Workbook()
    for sheet_name in sheet_names:

        sheet = in_book.sheet_by_name(sheet_name)
        # read the input for one well
        t = sheet.nrows - 1
        year = np.empty(t, 'int')
        month = np.empty(t, 'int')
        rainfall = np.empty(t)
        pumping = np.empty(t)
        meas_gwl = np.empty(t)
        r = np.empty(t)
        for i in range(t):
            year[i] = sheet.cell_value(i + 1, 0)
            month[i] = sheet.cell_value(i + 1, 1)
            rainfall[i] = sheet.cell_value(i + 1, 2)
            pumping[i] = sheet.cell_value(i + 1, 3)
            meas_gwl[i] = sheet.cell_value(i + 1, 4)
            r[i] = sheet.cell_value(i + 1, 5)
        F = sheet.cell_value(1, 6)
        G = sheet.cell_value(1, 7)
        hmin = sheet.cell_value(1, 8)

        # run the model
        gw_model = GW_1D(rainfall, pumping)
        gw_model.set_parameters(F, G, r, hmin)

        hini = meas_gwl[0]
        gw_model.run_model(hini, t)
        sim_gwl = gw_model.h
        lam = gw_model.lam
        sy = gw_model.sy
        discharge = gw_model.discharge
        print('Sy = %.5f' % sy)

        #write the output
        sheet = out_book.add_sheet(sheet_name)
        sheet.write(0, 0, 'year')
        sheet.write(0, 1, 'month')
        sheet.write(0, 2, 'rainfall')
        sheet.write(0, 3, 'pumping')
        sheet.write(0, 4, 'measured gwl')
        sheet.write(0, 5, 'simulated gwl')
        sheet.write(0, 6, 'recharge')
        sheet.write(0, 7, 'discharge')
        sheet.write(0, 8, 'lambda')
        sheet.write(0, 9, 'sy')
        for i in range(t):
            sheet.write(i + 1, 0, year[i])
            sheet.write(i + 1, 1, month[i])
            sheet.write(i + 1, 2, rainfall[i])
            sheet.write(i + 1, 3, pumping[i])
            sheet.write(i + 1, 4, meas_gwl[i])
            sheet.write(i + 1, 5, sim_gwl[i])
            sheet.write(i + 1, 6, rainfall[i] * r[i])
            sheet.write(i + 1, 7, discharge[i])
        sheet.write(1, 8, lam)
        sheet.write(1, 9, sy)

        first_date = ts.Date(freq='M', year=year[0], month=month[1])
        gw_meas_series = ts.time_series(meas_gwl, start_date=first_date)
        gw_sim_series = ts.time_series(sim_gwl, start_date=first_date)

        if figure_dir is not None:
            # save the figure
            fig = plt.figure(figsize=(6, 4.5))
            plt.plot(gw_meas_series, 'r', lw=3, label='measured')
            plt.plot(gw_sim_series, 'g', lw=3, label='simulated')
            plt.legend(loc='best')
            plt.ylabel('Groundwater Level')
            plt.savefig(figure_dir + '%s.png' % sheet_name)
            plt.close()

        print('%s completed succesfully' % sheet_name)

    # save the xls file
    out_book.save(out_fname)
Example #30
0
def load_coaps_stationdata(station, cfgdict=coaps_config):
    """
    Loads the temperatures and rainfall data for the current COAPS station.

    Parameters
    ----------
    station : int
        COAPS station identification code.
    cfgdict : dictionary
        Dictionary of configuration options. 
        By default, the configuration options are read from the :file:`.climpyrc`
        file
    
    Returns
    -------
    stationdata : ClimateRecords
        A :class:`~scikits.climpy.core.ClimateRecords` object, with fields:
        tobs : int
            Number of temperature observations for the day.
        tmin : float
            Minimum recorded temperature (oC).
        tmax : float
            Maximum recorded temperature (oC).
        rain : float
            Recorded amount of precipitation (mm).

    """
    #  
    datadir = cfgdict['datadir']
    coapsarchive = _check_archive(cfgdict)
    #
    archfilename = "COAPS%s" % station
    try:
        zipf = zipfile.ZipFile(coapsarchive, 'r')
        stationdata = cPickle.loads(zipf.read(archfilename))
    except IOError:
        zipf = zipfile.ZipFile(coapsarchive, 'w')
    except (KeyError, ValueError):
        zipf = zipfile.ZipFile(coapsarchive, 'a')
    else:
        return stationdata
    #
    netfile = os.path.join(cfgdict['netfile.dir'], "%sascii.txt" % station)
    sourcedir = np.lib._datasource.DataSource(datadir)
    dfile = sourcedir.open(netfile)

    skip = int(cfgdict['headerlines'])
    missing_values = cfgdict['nodata']
    dateconverter = lambda y, m, d : ts.Date('D',
                                             year=int(y), month=int(m),
                                             day=int(d))
    converters = {4 : lambda s: np.round((float(s) - 32) / 1.8, 1),
                  5 : lambda s: np.round((float(s) - 32) / 1.8, 1),
                  6 : lambda s: 0.254 * float(s)}
    ndtype = [('nobs', int), ('tmin', float), ('tmax', float), ('rain', float)]
    series = ts.tsfromtxt(dfile, delimiter="\t", skiprows=skip,
                          missing=missing_values, converters=converters,
                          datecols=(0, 1, 2), dateconverter=dateconverter)
    series = series.view(ndtype)
    # Store metainfo
    metainfo = []
    dfile.seek(0)
    for i in range(skip - 2):
        row = dfile.readline().strip()
        if not len(row):
            continue
        info = tuple([_.strip() for _ in row.split(":")])
        if len(info) == 1:
            info = (info, '')
        metainfo.append(info)
    series._optinfo.update(metainfo=dict(metainfo))

    zipf.writestr(archfilename, cPickle.dumps(series))
    zipf.close()
    return series