def test_reindex_time(self):
        """Tests whether the reindex_time function works as expected."""
        [yr, month, day] = [1850, 1, 15]
        expected_times = xr.DataArray(
            np.array([
                cftime.DatetimeProlepticGregorian(yr, month, day, 0, 0),
                cftime.DatetimeProlepticGregorian(yr, month + 1, day, 0, 0)
            ]))
        testdates1 = xr.DataArray(
            np.array([
                np.datetime64(datetime.datetime(yr, month, day, 0, 0)),
                np.datetime64(datetime.datetime(yr, month + 1, day, 0, 0))
            ]))
        testdates2 = xr.DataArray(
            np.array([
                cftime.DatetimeNoLeap(yr, month, day, 0, 0),
                cftime.DatetimeNoLeap(yr, month + 1, day, 0, 0)
            ]))
        testdates3 = xr.DataArray(
            np.array([
                cftime.Datetime360Day(yr, month, day, 0, 0),
                cftime.Datetime360Day(yr, month + 1, day, 0, 0)
            ]))
        dates_to_test = [testdates1, testdates2, testdates3]

        # Run test
        dates_converted = True
        for date_to_test in dates_to_test:
            newtimes = reindex_time(date_to_test)
            if not np.all((newtimes == expected_times).values):
                dates_converted = False
            else:
                continue
        self.assertTrue(dates_converted)
Esempio n. 2
0
 def _get_timesteps_submod(self, mod_nr):
     ts_mod = self.time_sub["ts_sub"][mod_nr]+self.start_year
     sub_end = self.time_sub["ends"][mod_nr]+self.start_year
     
     timesteps_mod = [cftime.DatetimeProlepticGregorian(t, 1, 1) for t in ts_mod]
     endtime=cftime.DatetimeProlepticGregorian(sub_end, 1, 1)        
     return(timesteps_mod, endtime)
def test_datetime_to_numeric_potential_overflow():
    import cftime

    times = pd.date_range("2000", periods=5,
                          freq="7D").values.astype("datetime64[us]")
    cftimes = cftime_range("2000",
                           periods=5,
                           freq="7D",
                           calendar="proleptic_gregorian").values

    offset = np.datetime64("0001-01-01")
    cfoffset = cftime.DatetimeProlepticGregorian(1, 1, 1)

    result = duck_array_ops.datetime_to_numeric(times,
                                                offset=offset,
                                                datetime_unit="D",
                                                dtype=int)
    cfresult = duck_array_ops.datetime_to_numeric(cftimes,
                                                  offset=cfoffset,
                                                  datetime_unit="D",
                                                  dtype=int)

    expected = 730119 + np.arange(0, 35, 7)

    np.testing.assert_array_equal(result, expected)
    np.testing.assert_array_equal(cfresult, expected)
Esempio n. 4
0
def reindex_time(startingtimes):
    newtimes = startingtimes.values
    for i in range(0, len(startingtimes)):
        yr = int(str(startingtimes.values[i])[0:4])
        mon = int(str(startingtimes.values[i])[5:7])
        day = int(str(startingtimes.values[i])[8:10])
        hr = int(str(startingtimes.values[i])[11:13])
        newdate = cftime.DatetimeProlepticGregorian(yr, mon, 15)
        newtimes[i] = newdate
    return newtimes
Esempio n. 5
0
def make_year_constraint_all_calendars(start, end):
    """Utility function to create a dict of time constraints on year-basis

    This create a dict of the same time constraint, but for different calendars.
    Since comparisons between different calendar types are not (always) possible,
    a calendar for a time coordinate should be compared to the specific constraint
    with the same calendar.

    The calendar type (as a string) can be obtained through the coordinate's `units`
    attribute: `cube.coord('time').units.calendar`; the resulting string is the key
    for the dict, which then as a value yields the correct constraint

    Arguments
    ---------

        start, end: integer
            Start and end year. Month and day are 1, 1 for the starting year, and
            31, 12 or 30, 12 (for a 360-day calendar) for the end year

    """

    dates = {
        'default': (cftime.datetime(start, 1, 1), cftime.datetime(end, 12,
                                                                  31)),
        '360_day':
        (cftime.Datetime360Day(start, 1,
                               1), cftime.Datetime360Day(end, 12, 30)),
        '365_day': (cftime.DatetimeNoLeap(start, 1, 1),
                    cftime.DatetimeNoLeap(end, 12, 31)),
        'proleptic_gregorian': (cftime.DatetimeProlepticGregorian(start, 1, 1),
                                cftime.DatetimeProlepticGregorian(end, 12,
                                                                  31)),
        'gregorian': (cftime.DatetimeGregorian(start, 1, 1),
                      cftime.DatetimeGregorian(end, 12, 31)),
        'julian': (cftime.DatetimeJulian(start, 1, 1),
                   cftime.DatetimeJulian(end, 12, 31)),
    }
    constraints = {
        key: make_date_constraint(*value)
        for key, value in dates.items()
    }
    return constraints
Esempio n. 6
0
def test_combine_by_coords_raises_for_differing_calendars():
    # previously failed with uninformative StopIteration instead of TypeError
    # https://github.com/pydata/xarray/issues/4495

    import cftime

    time_1 = [cftime.DatetimeGregorian(2000, 1, 1)]
    time_2 = [cftime.DatetimeProlepticGregorian(2001, 1, 1)]

    da_1 = DataArray([0], dims=["time"], coords=[time_1],
                     name="a").to_dataset()
    da_2 = DataArray([1], dims=["time"], coords=[time_2],
                     name="a").to_dataset()

    with raises_regex(TypeError, r"cannot compare .* \(different calendars\)"):
        combine_by_coords([da_1, da_2])
def reindex_time(startingtimes):
    """Reindexes time series to proleptic Gregorian calendar type.

    Args:
        startingtimes: Array of the time series.
    Returns:
        newtimes: Numpy array of the new, proleptic Gregorian calendar type times.
    """
    newtimes = np.empty(np.shape(startingtimes.values),
                        dtype=cftime.DatetimeProlepticGregorian)
    for i in range(0, len(startingtimes)):
        yr = int(str(startingtimes.values[i])[0:4])
        mon = int(str(startingtimes.values[i])[5:7])
        newdate = cftime.DatetimeProlepticGregorian(yr, mon, 15)
        newtimes[i] = newdate

    return newtimes
Esempio n. 8
0
 def __get_ic_submod(self, mod_nr, geo_mod):
     """Select correct initial conditions"""
     if mod_nr == 0:
         active=(geo_mod["IBOUND"]==1)
         starting_head = xr.where(active,   self.shd, -9999.0)
         starting_conc = xr.where(active, self.sconc, -9999.0)
         
     else:
         year = self.time_sub["ends"][mod_nr-1]+self.start_year
         year_str = cftime.DatetimeProlepticGregorian(
                 year, 1, 1).strftime("%Y%m%d%H%M%S")
         starting_head = "bas/head_{}_l?.idf".format(year_str)
         starting_conc = ["btn/conc_{}_c{}_l?.idf".format(year_str, specie) for specie in self.species]
         starting_conc = xr.DataArray(data=starting_conc, 
                                      coords=dict(species=self.species), 
                                      dims=["species"])
     
     return(starting_head, starting_conc)
Esempio n. 9
0
def to_cftime_datetime(date_str_or_date, calendar=None):
    if cftime is None:
        raise ModuleNotFoundError("No module named 'cftime'")

    if isinstance(date_str_or_date, str):
        if calendar is None:
            raise ValueError(
                "If converting a string to a cftime.datetime object, "
                "a calendar type must be provided")
        date, _ = _parse_iso8601_with_reso(get_date_type(calendar),
                                           date_str_or_date)
        return date
    elif isinstance(date_str_or_date, cftime.datetime):
        return date_str_or_date
    elif isinstance(date_str_or_date, (datetime, pd.Timestamp)):
        return cftime.DatetimeProlepticGregorian(*date_str_or_date.timetuple())
    else:
        raise TypeError("date_str_or_date must be a string or a "
                        "subclass of cftime.datetime. Instead got "
                        "{!r}.".format(date_str_or_date))
Esempio n. 10
0
def test_combine_by_coords_raises_for_differing_calendars():
    # previously failed with uninformative StopIteration instead of TypeError
    # https://github.com/pydata/xarray/issues/4495

    import cftime

    time_1 = [cftime.DatetimeGregorian(2000, 1, 1)]
    time_2 = [cftime.DatetimeProlepticGregorian(2001, 1, 1)]

    da_1 = DataArray([0], dims=["time"], coords=[time_1],
                     name="a").to_dataset()
    da_2 = DataArray([1], dims=["time"], coords=[time_2],
                     name="a").to_dataset()

    if LooseVersion(cftime.__version__) >= LooseVersion("1.5"):
        error_msg = "Cannot combine along dimension 'time' with mixed types."
    else:
        error_msg = r"cannot compare .* \(different calendars\)"

    with pytest.raises(TypeError, match=error_msg):
        combine_by_coords([da_1, da_2])
Esempio n. 11
0
df_mt.loc[:, [15, 16]] -= 1

#%%Parse listfile for other coordinates...
df_top = pattern2df(r" TOP ELEVATION OF LAYER 1 =", path_l)
df_btm = pattern2df(r"   MODEL LAYER BOTTOM EL. =", path_l)

topbots = pd.concat([df_top[6], df_btm[5]]).reset_index(drop=True)

z = (topbots[1:].values + topbots[:-1].values) / 2
dz = topbots[:-1].values - topbots[1:].values
layer = np.arange(len(z)) + 1
#%%Calculate times
start_year = 1999
times = flopy.utils.binaryfile.UcnFile(paths_ucn[0]).times
years = start_year + np.round(np.array(times) / 365.25, 0)
years = [cftime.DatetimeProlepticGregorian(year, 1, 1) for year in years]

#%%Calculate proxy coordinates
#These are only used with concatenation
x = [idrange2coord(i, j + 1, dcell) for i, j in df_mt[[13, 14]].values]
y = [idrange2coord(i, j + 1, dcell) for i, j in df_mt[[15, 16]].values]

#%%Create DataArrays and combine these into a big one.
dims = ("time", "layer", "y", "x")

das_s = [(s, [
    xr.DataArray(ucn,
                 dims=dims,
                 coords={
                     "x": x[i],
                     "y": y[i],
Esempio n. 12
0
mname = os.path.basename(model_folder)
sub_model_path = os.path.join(model_folder,
                              mname + "_nr{:02d}".format(cont_nr))

tempfile = os.path.join(model_folder, "cont_nr.txt")

#%%Get appropriate time

#Created in create_model.py. Update this if changed in create_model.py
sub_ends = np.array([8000., 8000., 5000., 5000., 8000., 8000.,
                     4000.])[-(nr_mod + 1):]
n_ts = (sub_ends / 1000.).astype(np.int64)
start_year = 1999

time = cftime.DatetimeProlepticGregorian(sub_ends[cont_nr - 1] + start_year, 1,
                                         1)

#%%Process data
#Load
ds_last = xr.open_dataset(files[cont_nr - 1])

#Check if this file has all the timesteps, if not go one back and rename wrong file.
times = ds_last.time.values
last_time = (times[-1].year - times[0].year) + 1000
if last_time != sub_ends[cont_nr - 1]:
    ds_last.close()
    os.rename(files[cont_nr - 1], files[cont_nr - 1] + "_fail")
    cont_nr -= 1
    ds_last = xr.open_dataset(files[cont_nr - 1])

ds_ini = ds_last.isel(time=-1)[["conc1", "conc2", "head"]].load()