How to use the cftime.num2date function in cftime

To help you get started, we’ve selected a few cftime examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Unidata / cftime / test / test_cftime.py View on Github external
def test_num2date_hour_units(calendar, unit, shape, dtype):
    date_type = _EXPECTED_DATE_TYPES[calendar]
    expected = np.array([date_type(2000, 1, 1, 1, 0, 0, 0),
                         date_type(2000, 1, 1, 2, 0, 0, 0),
                         date_type(2000, 1, 1, 3, 0, 0, 0),
                         date_type(2000, 1, 1, 4, 0, 0, 0)]).reshape(shape)
    numeric_times = np.array([1, 2, 3, 4]).reshape(shape).astype(dtype)
    units = "{} since 2000-01-01".format(unit)
    result = num2date(numeric_times, units=units, calendar=calendar)
    np.testing.assert_equal(result, expected)
github Unidata / cftime / test / test_cftime.py View on Github external
d = datetime.strptime('2018-01-23 09:27:10.950000',"%Y-%m-%d %H:%M:%S.%f")
        units = 'seconds since 2018-01-23 09:31:42.94'
        assert(cftime.date2num(d, units) == -271.99)
        # issue 143 - same answer for arrays vs scalars.
        units = 'seconds since 1970-01-01 00:00:00'
        times_in = [1261440000.0, 1261440001.0, 1261440002.0, 1261440003.0,
                    1261440004.0, 1261440005.0]
        times_out1 = cftime.num2date(times_in, units)
        times_out2 = []
        for time_in in times_in:
            times_out2.append(cftime.num2date(time_in, units))
        dates1 = [str(d) for d in times_out1]
        dates2 = [str(d) for d in times_out2]
        assert(dates1 == dates2)
        # issue #143 formatting of microseconds
        d = cftime.num2date(1261440000.015625,units)
        # on windows only 100 ms precision
        assert(str(d)[0:24] == '2009-12-22 00:00:00.0156')
        # issue #152 add isoformat()
        assert(d.isoformat()[0:24] == '2009-12-22T00:00:00.0156')
        assert(d.isoformat(sep=' ')[0:24] == '2009-12-22 00:00:00.0156')
        assert(d.isoformat(sep=' ',timespec='milliseconds') == '2009-12-22 00:00:00.015')
        assert(d.isoformat(sep=' ',timespec='seconds') == '2009-12-22 00:00:00')
        assert(d.isoformat(sep=' ',timespec='minutes') == '2009-12-22 00:00')
        assert(d.isoformat(sep=' ',timespec='hours') == '2009-12-22 00')
        # issue #165: make sure python datetime returned
        d=num2date(0,units="seconds since 2000-01-01 00:00:00",only_use_cftime_datetimes=False)
        assert isinstance(d, datetime)
        # issue #169: cftime.datetime has no calendar attribute, causing dayofwk,dayofyr methods
        # to fail.
        c = cftime.datetime(*cftime._parse_date('7480-01-01 00:00:00'))
        assert(c.strftime() == '7480-01-01 00:00:00')
github Unidata / cftime / test / test_cftime.py View on Github external
err = np.abs(mins1 - mins2)
                maxerr = max(err,maxerr)
                assert(err < eps)
                assert(date1.strftime(dateformat) == date2.strftime(dateformat))
            if verbose:
                print('calendar = %s max abs err (mins) = %s eps = %s' % \
                     (calendar,maxerr,eps))
            eps = 1.e-6
            units = 'hours since 0001-01-30 01:01:01'
            hrs1 = date2num(dateref,units,calendar=calendar)
            maxerr = 0.
            for n in range(ntimes):
                hrs1 += 0.001
                date1 = num2date(hrs1, units, calendar=calendar)
                hrs2 = date2num(date1, units, calendar=calendar)
                date2 = num2date(hrs2, units, calendar=calendar)
                err = np.abs(hrs1 - hrs2)
                maxerr = max(err,maxerr)
                assert(err < eps)
                assert(date1.strftime(dateformat) == date2.strftime(dateformat))
            if verbose:
                print('calendar = %s max abs err (hours) = %s eps = %s' % \
                     (calendar,maxerr,eps))
            eps = 1.e-8
            units = 'days since 0001-01-30 01:01:01'
            days1 = date2num(dateref,units,calendar=calendar)
            maxerr = 0.
            for n in range(ntimes):
                days1 += 0.00001
                date1 = num2date(days1, units, calendar=calendar)
                days2 = date2num(date1, units, calendar=calendar)
                date2 = num2date(days2, units, calendar=calendar)
github Unidata / cftime / test / test_cftime.py View on Github external
# also tests error found in issue #349
        calendars=['standard', 'gregorian', 'proleptic_gregorian', 'noleap', 'julian',\
                   'all_leap', '365_day', '366_day', '360_day']
        dateref = datetime(2015,2,28,12)
        ntimes = 1001
        verbose = True # print out max error diagnostics
        for calendar in calendars:
            eps = 100.
            units = 'microseconds since 2000-01-30 01:01:01'
            microsecs1 = date2num(dateref,units,calendar=calendar)
            maxerr = 0
            for n in range(ntimes):
                microsecs1 += 1.
                date1 = num2date(microsecs1, units, calendar=calendar)
                microsecs2 = date2num(date1, units, calendar=calendar)
                date2 = num2date(microsecs2, units, calendar=calendar)
                err = np.abs(microsecs1 - microsecs2)
                maxerr = max(err,maxerr)
                assert(err < eps)
                assert(date1.strftime(dateformat) == date2.strftime(dateformat))
            if verbose:
                print('calendar = %s max abs err (microsecs) = %s eps = %s' % \
                     (calendar,maxerr,eps))
            units = 'milliseconds since 1800-01-30 01:01:01'
            eps = 0.1
            millisecs1 = date2num(dateref,units,calendar=calendar)
            maxerr = 0.
            for n in range(ntimes):
                millisecs1 += 0.001
                date1 = num2date(millisecs1, units, calendar=calendar)
                millisecs2 = date2num(date1, units, calendar=calendar)
                date2 = num2date(millisecs2, units, calendar=calendar)
github Unidata / cftime / test / test_cftime.py View on Github external
assert(cftime.date2num(cftime.datetime(1, 12, 1, 0, 0, 0, 0, -1, 1), units='days since 01-01-01',calendar='noleap')  == 334.0)
        assert(cftime.date2num(cftime.num2date(1.0,units='days since 01-01-01',calendar='noleap'),units='days since 01-01-01',calendar='noleap') == 1.0)
        assert(cftime.date2num(cftime.DatetimeNoLeap(1980, 1, 1, 0, 0, 0, 0, 6, 1),'days since 1970-01-01','noleap') == 3650.0)
        # issue #126
        d = cftime.DatetimeProlepticGregorian(1, 1, 1)
        assert(cftime.date2num(d, 'days since 0001-01-01',\
            'proleptic_gregorian') == 0.0)
        # issue #140 (fractional seconds in reference date)
        d = datetime.strptime('2018-01-23 09:27:10.950000',"%Y-%m-%d %H:%M:%S.%f")
        units = 'seconds since 2018-01-23 09:31:42.94'
        assert(cftime.date2num(d, units) == -271.99)
        # issue 143 - same answer for arrays vs scalars.
        units = 'seconds since 1970-01-01 00:00:00'
        times_in = [1261440000.0, 1261440001.0, 1261440002.0, 1261440003.0,
                    1261440004.0, 1261440005.0]
        times_out1 = cftime.num2date(times_in, units)
        times_out2 = []
        for time_in in times_in:
            times_out2.append(cftime.num2date(time_in, units))
        dates1 = [str(d) for d in times_out1]
        dates2 = [str(d) for d in times_out2]
        assert(dates1 == dates2)
        # issue #143 formatting of microseconds
        d = cftime.num2date(1261440000.015625,units)
        # on windows only 100 ms precision
        assert(str(d)[0:24] == '2009-12-22 00:00:00.0156')
        # issue #152 add isoformat()
        assert(d.isoformat()[0:24] == '2009-12-22T00:00:00.0156')
        assert(d.isoformat(sep=' ')[0:24] == '2009-12-22 00:00:00.0156')
        assert(d.isoformat(sep=' ',timespec='milliseconds') == '2009-12-22 00:00:00.015')
        assert(d.isoformat(sep=' ',timespec='seconds') == '2009-12-22 00:00:00')
        assert(d.isoformat(sep=' ',timespec='minutes') == '2009-12-22 00:00')
github Unidata / cftime / test / test_cftime.py View on Github external
# issue #126
        d = cftime.DatetimeProlepticGregorian(1, 1, 1)
        assert(cftime.date2num(d, 'days since 0001-01-01',\
            'proleptic_gregorian') == 0.0)
        # issue #140 (fractional seconds in reference date)
        d = datetime.strptime('2018-01-23 09:27:10.950000',"%Y-%m-%d %H:%M:%S.%f")
        units = 'seconds since 2018-01-23 09:31:42.94'
        assert(cftime.date2num(d, units) == -271.99)
        # issue 143 - same answer for arrays vs scalars.
        units = 'seconds since 1970-01-01 00:00:00'
        times_in = [1261440000.0, 1261440001.0, 1261440002.0, 1261440003.0,
                    1261440004.0, 1261440005.0]
        times_out1 = cftime.num2date(times_in, units)
        times_out2 = []
        for time_in in times_in:
            times_out2.append(cftime.num2date(time_in, units))
        dates1 = [str(d) for d in times_out1]
        dates2 = [str(d) for d in times_out2]
        assert(dates1 == dates2)
        # issue #143 formatting of microseconds
        d = cftime.num2date(1261440000.015625,units)
        # on windows only 100 ms precision
        assert(str(d)[0:24] == '2009-12-22 00:00:00.0156')
        # issue #152 add isoformat()
        assert(d.isoformat()[0:24] == '2009-12-22T00:00:00.0156')
        assert(d.isoformat(sep=' ')[0:24] == '2009-12-22 00:00:00.0156')
        assert(d.isoformat(sep=' ',timespec='milliseconds') == '2009-12-22 00:00:00.015')
        assert(d.isoformat(sep=' ',timespec='seconds') == '2009-12-22 00:00:00')
        assert(d.isoformat(sep=' ',timespec='minutes') == '2009-12-22 00:00')
        assert(d.isoformat(sep=' ',timespec='hours') == '2009-12-22 00')
        # issue #165: make sure python datetime returned
        d=num2date(0,units="seconds since 2000-01-01 00:00:00",only_use_cftime_datetimes=False)
github Unidata / cftime / test / test_cftime.py View on Github external
def test_num2date_out_of_range():
    numeric_times = 12 * np.array([200000, 400000, 600000])
    units = "months since 2000-01-01"
    with pytest.raises(OverflowError, match="time values outside range of 64 bit signed integers"):
        num2date(numeric_times, units=units, calendar="360_day")
github Unidata / cftime / test / test_cftime.py View on Github external
maxerr = 0.
            for n in range(ntimes):
                days1 += 0.00001
                date1 = num2date(days1, units, calendar=calendar)
                days2 = date2num(date1, units, calendar=calendar)
                date2 = num2date(days2, units, calendar=calendar)
                err = np.abs(days1 - days2)
                maxerr = max(err,maxerr)
                assert(err < eps)
                assert(date1.strftime(dateformat) == date2.strftime(dateformat))
            if verbose:
                print('calendar = %s max abs err (days) = %s eps = %s' % \
                     (calendar,maxerr,eps))

        # issue 353
        assert (num2date(0, 'hours since 2000-01-01 0') ==
                datetime(2000,1,1,0))

        # issue 354
        num1 = np.array([[0, 1], [2, 3]])
        num2 = np.array([[0, 1], [2, 3]])
        dates1 = num2date(num1, 'days since 0001-01-01')
        dates2 = num2date(num2, 'days since 2001-01-01')
        assert( dates1.shape == (2,2) )
        assert( dates2.shape == (2,2) )
        num1b = date2num(dates1, 'days since 0001-01-01')
        num2b = date2num(dates2, 'days since 2001-01-01')
        assert( num1b.shape == (2,2) )
        assert( num2b.shape == (2,2) )
        assert_almost_equal(num1,num1b)
        assert_almost_equal(num2,num2b)
github pyoceans / pocean-core / pocean / utils.py View on Github external
def get_masked_datetime_array(t, tvar, mask_nan=True):
    # If we are passed in a scalar... return a scalar
    if isinstance(t, np.ma.core.MaskedConstant):
        return t
    elif np.isscalar(t):
        return num2date(t, tvar.units, getattr(tvar, 'calendar', 'standard'))

    if mask_nan is True:
        t = np.ma.masked_invalid(t)

    t_cal = getattr(tvar, 'calendar', 'standard')

    # Get the min value we can have and mask anything else
    # This is limited by **python** datetime objects and not
    # nc4 objects. The min nc4 datetime object is
    # min_date = nc4.netcdftime.datetime(-4713, 1, 1, 12, 0, 0, 40)
    # There is no max date for nc4.
    min_nums = date2num([datetime.min, datetime.max], tvar.units, t_cal)
    t = np.ma.masked_outside(t, *min_nums)
    # Avoid deprecation warnings between numpy 1.11 and 1.14
    # After 1.14 this is the default behavior
    t._sharedmask = False
github pism / pypismtools / scripts / extract_interface.py View on Github external
epsg = options.epsg
extract_type = options.extract_type
level = options.level
shp_filename = options.out_file
ts_fieldname = "timestamp"
dst_fieldname = options.dst_fieldname
step = options.step

nc = NC(filename, "r")
xdim, ydim, zdim, tdim = ppt.get_dims(nc)

if tdim:
    time = nc.variables[tdim]
    time_units = time.units
    time_calendar = time.calendar
    timestamps = cftime.num2date(time[:], time_units, time_calendar)
    has_time = True
else:
    tdim = None
nc.close()

src_ds = gdal.Open("NETCDF:{}:{}".format(filename, dst_fieldname))

# Get Memory Driver
mem_driver = ogr.GetDriverByName("Memory")
mem_ds = mem_driver.CreateDataSource("memory_layer")

# Get SHP Driver
shp_driver = ogr.GetDriverByName("ESRI Shapefile")
shp_filename = validateShapePath(shp_filename)
if os.path.exists(shp_filename):
    os.remove(shp_filename)