예제 #1
0
    def stageData(self, m):

        y0 = float(self.keywords.get(
            "y0", 1994.))  # [yr] beginning year to include in analysis
        yf = float(self.keywords.get(
            "yf", 2007.))  # [yr] end year to include in analysis

        obs = Variable(filename=self.source,
                       variable_name=self.variable,
                       alternate_vars=self.alternate_vars)
        #if obs.time is None: raise il.NotTemporalVariable()
        self.pruneRegions(obs)

        mod = m.extractTimeSeries(self.variable,
                                  alt_vars=self.alternate_vars,
                                  expression=self.derived,
                                  initial_time=(y0 - 1850) * 365,
                                  final_time=(yf - 1850) * 365,
                                  lats=None if obs.spatial else obs.lat,
                                  lons=None if obs.spatial else obs.lon)

        # remove the time dimension
        obs = obs.integrateInTime(mean=True)
        mod = mod.integrateInTime(mean=True)

        return obs, mod
예제 #2
0
    def stageData(self, m):

        energy_threshold = float(self.keywords.get("energy_threshold", 10))

        # Handle obs data
        dn_obs = Variable(filename=self.source.replace("albedo", "rsds"),
                          variable_name="rsds")
        up_obs = Variable(filename=self.source.replace("albedo", "rsus"),
                          variable_name="rsus")
        dn_obs, up_obs, obs = _albedo(dn_obs, up_obs, self.variable,
                                      energy_threshold)

        # Prune out uncovered regions
        if obs.time is None: raise il.NotTemporalVariable()
        self.pruneRegions(obs)

        # Handle model data
        dn_mod = m.extractTimeSeries("rsds",
                                     initial_time=obs.time_bnds[0, 0],
                                     final_time=obs.time_bnds[-1, 1],
                                     lats=None if obs.spatial else obs.lat,
                                     lons=None if obs.spatial else obs.lon)
        up_mod = m.extractTimeSeries("rsus",
                                     initial_time=obs.time_bnds[0, 0],
                                     final_time=obs.time_bnds[-1, 1],
                                     lats=None if obs.spatial else obs.lat,
                                     lons=None if obs.spatial else obs.lon)
        dn_mod, up_mod, mod = _albedo(dn_mod, up_mod, self.variable,
                                      energy_threshold)

        # Make variables comparable
        obs, mod = il.MakeComparable(obs,
                                     mod,
                                     mask_ref=True,
                                     clip_ref=True,
                                     logstring="[%s][%s]" %
                                     (self.longname, m.name))
        dn_obs, dn_mod = il.MakeComparable(dn_obs,
                                           dn_mod,
                                           mask_ref=True,
                                           clip_ref=True,
                                           logstring="[%s][%s]" %
                                           (self.longname, m.name))
        up_obs, up_mod = il.MakeComparable(up_obs,
                                           up_mod,
                                           mask_ref=True,
                                           clip_ref=True,
                                           logstring="[%s][%s]" %
                                           (self.longname, m.name))

        # Compute the mean albedo
        dn_obs = dn_obs.integrateInTime(mean=True)
        up_obs = up_obs.integrateInTime(mean=True)
        np.seterr(over='ignore', under='ignore')
        obs_timeint = np.ma.masked_array(up_obs.data / dn_obs.data,
                                         mask=(dn_obs.data.mask +
                                               up_obs.data.mask))
        np.seterr(over='warn', under='warn')
        obs_timeint = Variable(name=self.variable,
                               unit="1",
                               data=obs_timeint,
                               lat=dn_obs.lat,
                               lat_bnds=dn_obs.lat_bnds,
                               lon=dn_obs.lon,
                               lon_bnds=dn_obs.lon_bnds)
        dn_mod = dn_mod.integrateInTime(mean=True)
        up_mod = up_mod.integrateInTime(mean=True)
        np.seterr(over='ignore', under='ignore')
        mod_timeint = np.ma.masked_array(up_mod.data / dn_mod.data,
                                         mask=(dn_mod.data.mask +
                                               up_mod.data.mask))
        np.seterr(over='warn', under='warn')
        mod_timeint = Variable(name=self.variable,
                               unit="1",
                               data=mod_timeint,
                               lat=dn_mod.lat,
                               lat_bnds=dn_mod.lat_bnds,
                               lon=dn_mod.lon,
                               lon_bnds=dn_mod.lon_bnds)

        return obs, mod, obs_timeint, mod_timeint
예제 #3
0
def getDiurnalDataForGivenYear(var, year):
    """
    """

    # Get this year's data, make sure there is enough
    spd = int(round(1 / np.diff(var.time_bnds, axis=1).mean()))
    datum = cftime.date2num(cftime.datetime(year, 1, 1), "days since 1850-1-1")
    ind = np.where(year == var.year)[0]
    t = var.time[ind] - datum
    tb = var.time_bnds[ind] - datum
    data = var.data[ind, 0]

    # Reshape the data
    begin = np.argmin(tb[:(spd - 1), 0] % 1)
    end = begin + int(t[begin:].size / float(spd) - 1) * spd
    shift = int(round((var.tmax - 12) / (var.dt * 24)))
    begin += shift
    end += shift
    shp = (-1, spd) + data.shape[1:]
    data = data[begin:end].reshape(shp)
    t = t[begin:end].reshape(shp).mean(axis=1)

    # Diurnal magnitude
    mag = Variable(name="mag%d" % year,
                   unit=var.unit,
                   time=t,
                   data=data.max(axis=1) - data.min(axis=1))

    # Some of the tower data is 'intelligently' masked which leads to
    # too much of the data being removed to use my change-detection
    # algorithm to determine season begin/end.
    mag.skip = False
    if mag.data.mask.all(): raise NotEnoughDataInYear  # if year is all masked
    dmag = (mag.data.max() - mag.data.min())
    if dmag < 1e-14:
        raise NotEnoughDataInYear  # if diurnal mag has no amplitude

    # Some mask out off seasons, season is taken to be all the data
    begin_day, end_day = mag.time[mag.data.mask == False][[
        0, -1
    ]]  # begin/end of the mask data
    if ((begin_day < 2 and end_day < 363)
            or (begin_day > 2 and end_day > 363)):
        # this is likely a dataset which is a partial year
        raise NotEnoughDataInYear
    elif (begin_day > 2 and end_day < 363):
        # this is likely a dataset that masks out off-seasons
        season = np.asarray([begin_day, end_day])
    else:
        season = findSeasonalTiming(mag.time, mag.data)
    centroid = findSeasonalCentroid(mag.time, mag.data)
    mag.season = season
    mag.centroid = centroid

    # Mask out off season
    mask = (t < season[0]) + (t > season[1])
    data = np.ma.masked_array(data,
                              mask=mask[:, np.newaxis] *
                              np.ones(data.shape[1], dtype=bool))

    # Mean seasonal diurnal cycle
    uncert = np.zeros((data.shape[1], 2))
    for i in range(data.shape[1]):
        d = data[:, i].compressed()
        if d.size == 0: continue
        uncert[i, :] = np.percentile(d, [10, 90])
    day = np.linspace(0, 1, spd + 1)
    day = 0.5 * (day[:-1] + day[1:])
    with np.errstate(under='ignore', over='ignore'):
        cycle = Variable(name="cycle%d" % year,
                         unit=var.unit,
                         time=day,
                         data=data.mean(axis=0),
                         data_bnds=uncert)

    # Mean seasonal uptake
    uptake = Variable(unit=var.unit,
                      time=var.time[ind] - datum,
                      time_bnds=var.time_bnds[ind] - datum,
                      data=var.data[ind, 0])
    uptake.data = np.ma.masked_array(uptake.data,
                                     mask=((uptake.time < season[0]) +
                                           (uptake.time > season[1])))
    uptake = uptake.integrateInTime(mean=True)
    cycle.uptake = uptake.data

    # Timing of peak seasonal cycle, could be a maximum or minimum,
    # check second derivative of a best-fit parabola to the daytime
    # data.
    begin = int(spd / 4)
    end = int(spd * 3 / 4)
    p = np.polyfit(cycle.time[begin:end], cycle.data[begin:end], 2)
    if p[0] < 0:
        cycle.peak = day[cycle.data.argmax()] * 24
    else:
        cycle.peak = day[cycle.data.argmin()] * 24

    return mag, cycle
예제 #4
0
    def stageData(self, m):

        energy_threshold = float(self.keywords.get("energy_threshold", 20.))

        # Handle obs data
        sh_obs = Variable(filename=os.path.join(os.environ["ILAMB_ROOT"],
                                                "DATA/sh/GBAF/sh_0.5x0.5.nc"),
                          variable_name="sh")
        le_obs = Variable(filename=os.path.join(os.environ["ILAMB_ROOT"],
                                                "DATA/le/GBAF/le_0.5x0.5.nc"),
                          variable_name="le")
        sh_obs, le_obs, obs = _evapfrac(sh_obs, le_obs, self.variable,
                                        energy_threshold)

        # Prune out uncovered regions
        if obs.time is None: raise il.NotTemporalVariable()
        self.pruneRegions(obs)

        # Handle model data
        sh_mod = m.extractTimeSeries("hfss",
                                     initial_time=obs.time_bnds[0, 0],
                                     final_time=obs.time_bnds[-1, 1],
                                     lats=None if obs.spatial else obs.lat,
                                     lons=None if obs.spatial else obs.lon)
        le_mod = m.extractTimeSeries("hfls",
                                     initial_time=obs.time_bnds[0, 0],
                                     final_time=obs.time_bnds[-1, 1],
                                     lats=None if obs.spatial else obs.lat,
                                     lons=None if obs.spatial else obs.lon)
        sh_mod, le_mod, mod = _evapfrac(sh_mod, le_mod, self.variable,
                                        energy_threshold)

        # Make variables comparable
        obs, mod = il.MakeComparable(obs,
                                     mod,
                                     mask_ref=True,
                                     clip_ref=True,
                                     logstring="[%s][%s]" %
                                     (self.longname, m.name))
        sh_obs, sh_mod = il.MakeComparable(sh_obs,
                                           sh_mod,
                                           mask_ref=True,
                                           clip_ref=True,
                                           logstring="[%s][%s]" %
                                           (self.longname, m.name))
        le_obs, le_mod = il.MakeComparable(le_obs,
                                           le_mod,
                                           mask_ref=True,
                                           clip_ref=True,
                                           logstring="[%s][%s]" %
                                           (self.longname, m.name))

        # Compute the mean ef
        sh_obs = sh_obs.integrateInTime(mean=True)
        le_obs = le_obs.integrateInTime(mean=True)
        np.seterr(over='ignore', under='ignore')
        obs_timeint = np.ma.masked_array(
            le_obs.data / (le_obs.data + sh_obs.data),
            mask=(sh_obs.data.mask + le_obs.data.mask))
        np.seterr(over='warn', under='warn')
        obs_timeint = Variable(name=self.variable,
                               unit="1",
                               data=obs_timeint,
                               lat=sh_obs.lat,
                               lat_bnds=sh_obs.lat_bnds,
                               lon=sh_obs.lon,
                               lon_bnds=sh_obs.lon_bnds)
        sh_mod = sh_mod.integrateInTime(mean=True)
        le_mod = le_mod.integrateInTime(mean=True)
        np.seterr(over='ignore', under='ignore')
        mod_timeint = np.ma.masked_array(
            le_mod.data / (le_mod.data + sh_mod.data),
            mask=(sh_mod.data.mask + le_mod.data.mask))
        np.seterr(over='warn', under='warn')
        mod_timeint = Variable(name=self.variable,
                               unit="1",
                               data=mod_timeint,
                               lat=sh_mod.lat,
                               lat_bnds=sh_mod.lat_bnds,
                               lon=sh_mod.lon,
                               lon_bnds=sh_mod.lon_bnds)

        return obs, mod, obs_timeint, mod_timeint