Exemplo n.º 1
0
 def test_AverageAccessor(self):
     dv=np.arange(self.ta.size())
     v=api.DoubleVector.FromNdArray(dv)
     t=api.UtcTimeVector();
     for i in xrange(self.ta.size()):
         t.push_back(self.ta(i).start)
     t.push_back(self.ta(self.ta.size()-1).end) #important! needs n+1 points to determine n periods in the timeaxis
     tsf=api.TsFactory()
     ts1=tsf.create_point_ts(self.ta.size(), self.t, self.d, v)
     ts2=tsf.create_time_point_ts(self.ta.total_period(),t,v)
     tax=api.Timeaxis(self.ta.start()+api.deltaminutes(30),api.deltahours(1),self.ta.size())
     avg1=api.AverageAccessorTs(ts1,tax)
     self.assertEquals(avg1.size(),tax.size())
Exemplo n.º 2
0
 def test_extract_conversion_factors_from_string(self):
     u = utime('hours since 1970-01-01 00:00:00')
     t_origin = api.Calendar(u.tzoffset).time(
         api.YMDhms(u.origin.year, u.origin.month, u.origin.day,
                    u.origin.hour, u.origin.minute, u.origin.second))
     delta_t_dic = {
         'days': api.deltahours(24),
         'hours': api.deltahours(1),
         'minutes': api.deltaminutes(1)
     }
     delta_t = delta_t_dic[u.units]
     self.assertIsNotNone(u)
     self.assertEqual(delta_t, api.deltahours(1))
     self.assertEqual(t_origin, 0)
Exemplo n.º 3
0
 def test_average_accessor(self):
     dv = np.arange(self.ta.size())
     v = api.DoubleVector.from_numpy(dv)
     t = api.UtcTimeVector()
     for i in range(self.ta.size()):
         t.push_back(self.ta(i).start)
     t.push_back(
         self.ta(self.ta.size() - 1).end)  # important! needs n+1 points to determine n periods in the timeaxis
     tsf = api.TsFactory()
     ts1 = tsf.create_point_ts(self.ta.size(), self.t, self.d, v)
     ts2 = tsf.create_time_point_ts(self.ta.total_period(), t, v)
     tax = api.Timeaxis(self.ta.total_period().start + api.deltaminutes(30), api.deltahours(1), self.ta.size())
     avg1 = api.AverageAccessorTs(ts1, tax)
     self.assertEqual(avg1.size(), tax.size())
     self.assertIsNotNone(ts2)
Exemplo n.º 4
0
 def test_ts_transform(self):
     dv=np.arange(self.ta.size())
     v=api.DoubleVector.FromNdArray(dv)
     t=api.UtcTimeVector();
     for i in xrange(self.ta.size()):
         t.push_back(self.ta(i).start)
     #t.push_back(self.ta(self.ta.size()-1).end) #important! needs n+1 points to determine n periods in the timeaxis
     tax=api.Timeaxis(self.ta.start()+api.deltaminutes(30),api.deltahours(1),self.ta.size())
     tsf=api.TsFactory()
     ts1=tsf.create_point_ts(self.ta.size(), self.t, self.d, v)
     ts2=tsf.create_time_point_ts(self.ta.total_period(),t,v)
     ts3=api.TsFixed(tax,v)
     
     tst=api.TsTransform()
     tt1=tst.to_average(tax.start(),tax.delta(),tax.size(),ts1)
     tt2=tst.to_average(tax.start(),tax.delta(),tax.size(),ts2)
     tt3=tst.to_average(tax.start(),tax.delta(),tax.size(),ts3)
     self.assertEqual(tt1.size(),tax.size())
     self.assertEqual(tt2.size(),tax.size())
     self.assertEqual(tt3.size(),tax.size())
Exemplo n.º 5
0
    def test_ts_transform(self):
        dv=np.arange(self.ta.size())
        v=api.DoubleVector.from_numpy(dv)
        t=api.UtcTimeVector();
        for i in range(self.ta.size()):
            t.push_back(self.ta(i).start)
        # t.push_back(self.ta(self.ta.size()-1).end) #important! needs n+1 points to determine n periods in the timeaxis
        t_start=self.ta.total_period().start
        dt=api.deltahours(1)
        tax=api.TimeAxisFixedDeltaT(t_start + api.deltaminutes(30), dt, self.ta.size())
        tsf=api.TsFactory()
        ts1=tsf.create_point_ts(self.ta.size(), self.t, self.d, v)
        ts2=tsf.create_time_point_ts(self.ta.total_period(), t, v)
        ts3=api.TsFixed(tax, v, api.POINT_INSTANT_VALUE)

        tst=api.TsTransform()
        tt1=tst.to_average(t_start, dt, tax.size(), ts1)
        tt2=tst.to_average(t_start, dt, tax.size(), ts2)
        tt3=tst.to_average(t_start, dt, tax.size(), ts3)
        self.assertEqual(tt1.size(), tax.size())
        self.assertEqual(tt2.size(), tax.size())
        self.assertEqual(tt3.size(), tax.size())
Exemplo n.º 6
0
    def test_rating_curve_ts(self):
        t0=api.utctime_now()
        ta=api.TimeAxis(t0, api.deltaminutes(30), 48*2)
        data=np.linspace(0, 10, ta.size())
        ts=api.TimeSeries(ta, data, api.POINT_INSTANT_VALUE)

        rcf1=api.RatingCurveFunction()
        rcf1.add_segment(0, 1, 0, 1)
        rcf1.add_segment(api.RatingCurveSegment(5, 2, 0, 1))

        rcf2=api.RatingCurveFunction()
        rcf2.add_segment(0, 3, 0, 1)
        rcf2.add_segment(api.RatingCurveSegment(8, 4, 0, 1))

        rcp=api.RatingCurveParameters()
        rcp.add_curve(t0, rcf1)
        rcp.add_curve(t0 + api.deltahours(24), rcf2)

        sts=api.TimeSeries("a")
        rcsts=sts.rating_curve(rcp)

        rcsts_blob=rcsts.serialize()
        rcsts_2=api.TimeSeries.deserialize(rcsts_blob)

        self.assertTrue(rcsts_2.needs_bind())
        fbi=rcsts_2.find_ts_bind_info()
        self.assertEqual(len(fbi), 1)
        fbi[0].ts.bind(ts)
        rcsts_2.bind_done()
        self.assertFalse(rcsts_2.needs_bind())

        self.assertEqual(len(rcsts_2), len(ts))
        for i in range(rcsts_2.size()):
            expected=(1*ts.get(i).v if ts.get(i).v < 5 else 2*ts.get(i).v) if ts.get(i).t < t0 + api.deltahours(24) else (
                3*ts.get(i).v if ts.get(i).v < 8 else 4*ts.get(i).v)
            self.assertEqual(rcsts_2.get(i).t, ts.get(i).t)
            self.assertEqual(rcsts_2.get(i).v, expected)
Exemplo n.º 7
0
def run_radiation(latitude_deg, slope_deg, aspect_deg, elevation, albedo, turbidity, temperature, rhumidity, flag = 'instant', method='dingman'):

    """Module creates shyft radiation model with different timesteps and run it for a defined period of time (1 year with 24-hours averaging) """

    import numpy as np
    import math

    from shyft import api


    # single method test

    # here I will try to reproduce the Fig.1b from Allen2006 (reference)
    utc = api.Calendar()

    n = 365 # nr of time steps: 1 year, daily data
    t_start = utc.time(2002, 1, 1) # starting at the beginning of the year 1970

    # converting station data
    tempP1 = temperature # [degC], real data should be used
    rhP1 = rhumidity #[%], real data should be used
    rsm = 0.0

    radparam = api.RadiationParameter(albedo,turbidity)
    radcal_inst = api.RadiationCalculator(radparam)
    radcal_1h = api.RadiationCalculator(radparam)
    radcal_24h = api.RadiationCalculator(radparam)
    radcal_3h = api.RadiationCalculator(radparam)
    radres_inst = api.RadiationResponse()
    radres_1h = api.RadiationResponse()
    radres_24h = api.RadiationResponse()
    radres_3h = api.RadiationResponse()

    rv_rso = [] # clear-sky radiation, result vector
    rv_ra = [] # extraterrestrial radiation, result vector
    rv_net = [] # net radiation
    rv_net_sw = [] #net short-wave
    rv_net_lw = [] #net long-wave

    dayi = 0
    doy = api.DoubleVector()
    # running 24-h timestep
    step = api.deltahours(24)
    tadays = api.TimeAxis(t_start, step, n + 1)  # days
    k = 1
    while (k <= n):
        doy.append(dayi)
        k += 1
        dayi += 1

    if flag=='24-hour':
        dayi = 0
        doy = api.DoubleVector()

        # running 24-h timestep
        step = api.deltahours(24)
        tadays = api.TimeAxis(t_start, step, n+1)  # days
        k = 1
        while (k<=n):
            time1 = tadays.time(k-1)
            if method=='dingman':
                radcal_24h.net_radiation_step(radres_24h, latitude_deg, time1, step, slope_deg, aspect_deg,
                                                      tempP1, rhP1, elevation, rsm)
            else:
                radcal_24h.net_radiation_step_asce_st(radres_24h, latitude_deg, time1, step, slope_deg, aspect_deg, tempP1, rhP1, elevation, rsm)
            rv_rso.append(radres_24h.sw_t)
            rv_ra.append(radres_24h.ra)
            rv_net.append(radres_24h.net)
            rv_net_sw.append(radres_24h.net_sw)
            rv_net_lw.append(radres_24h.net_lw)
            # print(radres_24h.ra)
            doy.append(dayi)
            k+=1
            dayi += 1
        # doy.append(dayi)
    elif flag=='3-hour':

        # running 3h timestep
        step = api.deltahours(3)
        ta3 = api.TimeAxis(t_start, step, n * 8)  # hours, 1h timestep
        rso_3h = [] #clear-sky radiation
        ra_3h = [] # extraterrestrial radiation
        net_sw_3h = []
        net_lw_3h = []
        net_3h = []
        k = 1
        while (k<n*8):
            time0 = ta3.time(k-1)
            if method=='dingman':
                radcal_3h.net_radiation_step(radres_3h, latitude_deg, time0, step, slope_deg, aspect_deg, tempP1,
                                                    rhP1, elevation, rsm)
            else:
                radcal_3h.net_radiation_step_asce_st(radres_3h, latitude_deg, time0, step, slope_deg, aspect_deg, tempP1, rhP1, elevation, rsm)
            rso_3h.append(radres_3h.sw_t)
            ra_3h.append(radres_3h.ra)
            net_sw_3h.append(radres_3h.net_sw)
            net_lw_3h.append(radres_3h.net_lw)
            net_3h.append(radres_3h.net)
            k+=1
        rv_rso = [sum(rso_3h[i:i + 8]) for i in range(0, len(rso_3h), 8)]
        rv_ra = [sum(ra_3h[i:i + 8]) for i in range(0, len(ra_3h), 8)]
        rv_net_sw = [sum(net_sw_3h[i:i + 8]) for i in range(0, len(net_sw_3h), 8)]
        rv_net_lw = [sum(net_lw_3h[i:i + 8])/8 for i in range(0, len(net_lw_3h), 8)]
        rv_net = [sum(net_3h[i:i + 8]) for i in range(0, len(net_3h), 8)]
    elif flag=='1-hour':
        # runing 1h timestep
        step = api.deltahours(1)
        ta = api.TimeAxis(t_start, step, n * 24)  # hours, 1h timestep
        rso_1h = []
        ra_1h = []
        net_sw_1h = []
        net_lw_1h = []
        net_1h = []
        k = 1
        while (k<n*24):
            time1 = ta.time(k-1)
            if method=='dingman':
                radcal_1h.net_radiation_step(radres_1h, latitude_deg, time1, step, slope_deg, aspect_deg,
                                                     tempP1, rhP1, elevation, rsm)
            else:
                radcal_1h.net_radiation_step_asce_st(radres_1h, latitude_deg, time1, step, slope_deg, aspect_deg, tempP1, rhP1, elevation,rsm)
            rso_1h.append(radres_1h.sw_t)
            ra_1h.append(radres_1h.ra)
            net_sw_1h.append(radres_1h.net_sw)
            net_lw_1h.append(radres_1h.net_lw)
            net_1h.append(radres_1h.net)
            k += 1
        rv_rso = [sum(rso_1h[i:i + 24]) for i in range(0, len(rso_1h), 24)]
        rv_ra = [sum(ra_1h[i:i + 24]) for i in range(0, len(ra_1h), 24)]
        rv_net_sw = [sum(net_sw_1h[i:i + 24]) for i in range(0, len(net_sw_1h), 24)]
        rv_net_lw = [sum(net_lw_1h[i:i + 24])/24 for i in range(0, len(net_lw_1h), 24)]
        rv_net = [sum(net_1h[i:i + 24]) for i in range(0, len(net_1h), 24)]
    elif flag=='instant':
        # running instantaneous with dmin timstep
        minutes = 60
        dmin = 1
        step = api.deltaminutes(dmin)
        tamin = api.TimeAxis(t_start,step , n * 24 * minutes)
        rso_inst = []
        ra_inst = []
        net_sw_inst = []
        net_lw_inst = []
        net_inst = []
        doy1 = []
        k = 0
        while (k < n*24*minutes):
            timemin = tamin.time(k)
            radcal_inst.net_radiation(radres_inst, latitude_deg, timemin, slope_deg, aspect_deg, tempP1, rhP1,
                                      elevation, rsm)
            rso_inst.append(radres_inst.sw_t)
            ra_inst.append(radres_inst.ra)
            net_sw_inst.append(radres_inst.net_sw)
            net_lw_inst.append(radres_inst.net_lw)
            net_inst.append(radres_inst.net)
            doy1.append(k)
            k += 1
        rv_rso = [sum(rso_inst[i:i+24*minutes])/(24*minutes) for i in range(0,len(rso_inst),24*minutes)]
        rv_ra = [sum(ra_inst[i:i + 24 * minutes]) /(24 * minutes) for i in range(0, len(ra_inst), 24 * minutes)]
        rv_net_sw = [sum(net_sw_inst[i:i + 24*minutes])/(24*minutes) for i in range(0, len(net_sw_inst), 24*minutes)]
        rv_net_lw = [sum(net_lw_inst[i:i + 24*minutes])/(24*minutes) for i in range(0, len(net_lw_inst), 24*minutes)]
        rv_net = [sum(net_inst[i:i + 24*minutes])/(24*minutes) for i in range(0, len(net_inst), 24*minutes)]
    else:
        return 'Nothing todo. Please, specify timestep'



    return doy, rv_ra, rv_rso, rv_net_sw, rv_net_lw, rv_net
Exemplo n.º 8
0
from shyft import api
from netcdftime import utime
import numpy as np

""" These are the current supported regular time-step intervals """
delta_t_dic = {'days': api.deltahours(24), 'hours': api.deltahours(1), 'minutes': api.deltaminutes(1),
               'seconds': api.Calendar.SECOND}


def convert_netcdf_time(time_spec, t):
    """
    Converts supplied numpy array to  shyft utctime given netcdf time_spec.
    Throws exception if time-unit is not supported, i.e. not part of delta_t_dic
    as specified in this file.

    Parameters
    ----------
        time_spec: string
           from netcdef  like 'hours since 1970-01-01 00:00:00'
        t: numpy array
    Returns
    -------
        numpy array type int64 with new shyft utctime units (seconds since 1970utc)
    """
    u = utime(time_spec)
    t_origin = api.Calendar(int(u.tzoffset)).time(
        api.YMDhms(u.origin.year, u.origin.month, u.origin.day, u.origin.hour, u.origin.minute, u.origin.second))

    delta_t = delta_t_dic[u.units]
    return (t_origin + delta_t * t[:]).astype(np.int64)
Exemplo n.º 9
0
# This file is part of Shyft. Copyright 2015-2018 SiH, JFB, OS, YAS, Statkraft AS
# See file COPYING for more details **/
from shyft import api
from netcdftime import utime
import numpy as np

""" These are the current supported regular time-step intervals """
delta_t_dic = {'days': api.deltahours(24), 'hours': api.deltahours(1), 'minutes': api.deltaminutes(1),
               'seconds': api.Calendar.SECOND}


def convert_netcdf_time(time_spec, t):
    """
    Converts supplied numpy array to  shyft utctime given netcdf time_spec.
    Throws exception if time-unit is not supported, i.e. not part of delta_t_dic
    as specified in this file.

    Parameters
    ----------
        time_spec: string
           from netcdef  like 'hours since 1970-01-01 00:00:00'
        t: numpy array
    Returns
    -------
        numpy array type int64 with new shyft utctime units (seconds since 1970utc)
    """
    u = utime(time_spec)
    t_origin = api.Calendar(int(u.tzoffset)).time(
        api.YMDhms(u.origin.year, u.origin.month, u.origin.day, u.origin.hour, u.origin.minute, u.origin.second))
    #print (t[0],t_origin,delta_t_dic[u.units])
    delta_t = delta_t_dic[u.units]
Exemplo n.º 10
0
 def test_extract_conversion_factors_from_string(self):
     u = utime('hours since 1970-01-01 00:00:00')
     t_origin = api.Calendar(u.tzoffset).time(
         api.YMDhms(u.origin.year, u.origin.month, u.origin.day, u.origin.hour, u.origin.minute, u.origin.second))
     delta_t_dic = {'days': api.deltahours(24), 'hours': api.deltahours(1), 'minutes': api.deltaminutes(1)}
     delta_t = delta_t_dic[u.units]
     self.assertIsNotNone(u)
     self.assertEqual(delta_t, api.deltahours(1))
     self.assertEqual(t_origin, 0)
Exemplo n.º 11
0
from shyft import api
from netcdftime import utime
import numpy as np
""" These are the current supported regular time-step intervals """
delta_t_dic = {
    'days': api.deltahours(24),
    'hours': api.deltahours(1),
    'minutes': api.deltaminutes(1),
    'seconds': api.Calendar.SECOND
}


def convert_netcdf_time(time_spec, t):
    """
    Converts supplied numpy array to  shyft utctime given netcdf time_spec.
    Throws exception if time-unit is not supported, i.e. not part of delta_t_dic
    as specified in this file.

    Parameters
    ----------
        time_spec: string
           from netcdef  like 'hours since 1970-01-01 00:00:00'
        t: numpy array
    Returns
    -------
        numpy array type int64 with new shyft utctime units (seconds since 1970utc)
    """
    u = utime(time_spec)
    t_origin = api.Calendar(int(u.tzoffset)).time(
        api.YMDhms(u.origin.year, u.origin.month, u.origin.day, u.origin.hour,
                   u.origin.minute, u.origin.second))