def runTest(self):
        # Get the real dates
        # skip this until cftime pull request #55 is in a released
        # version (1.0.1?). Otherwise, fix for issue #808 breaks this
        if parse_version(cftime.__version__) >= parse_version('1.0.1'):
            dates = []
            for file in self.files:
                f = Dataset(file)
                t = f.variables['time']
                dates.extend(num2date(t[:], t.units, t.calendar))
                f.close()

        # Compare with the MF dates
        f = MFDataset(self.files,check=True)
        t = f.variables['time']
        mfdates = num2date(t[:], t.units, t.calendar)

        T = MFTime(t)
        assert_equal(len(T), len(t))
        assert_equal(T.shape, t.shape)
        assert_equal(T.dimensions, t.dimensions)
        assert_equal(T.typecode(), t.typecode())
        # skip this until cftime pull request #55 is in a released
        # version (1.0.1?). Otherwise, fix for issue #808 breaks this
        if parse_version(cftime.__version__) >= parse_version('1.0.1'):
            assert_array_equal(num2date(T[:], T.units, T.calendar), dates)
        assert_equal(date2index(datetime.datetime(1980, 1, 2), T), 366)
        f.close()
    def runTest(self):
        # Get the real dates
        # skip this until cftime pull request #55 is in a released
        # version (1.0.1?). Otherwise, fix for issue #808 breaks this
        if parse_version(cftime.__version__) >= parse_version('1.0.1'):
            dates = []
            for file in self.files:
                f = Dataset(file)
                t = f.variables['time']
                dates.extend(cftime.num2date(t[:], t.units, t.calendar))
                f.close()

        # Compare with the MF dates
        f = MFDataset(self.files, check=True)
        t = f.variables['time']
        mfdates = cftime.num2date(t[:], t.units, t.calendar)

        T = MFTime(t)
        assert_equal(len(T), len(t))
        assert_equal(T.shape, t.shape)
        assert_equal(T.dimensions, t.dimensions)
        assert_equal(T.typecode(), t.typecode())
        # skip this until cftime pull request #55 is in a released
        # version (1.0.1?). Otherwise, fix for issue #808 breaks this
        if parse_version(cftime.__version__) >= parse_version('1.0.1'):
            assert_array_equal(cftime.num2date(T[:], T.units, T.calendar),
                               dates)
        assert_equal(cftime.date2index(datetime.datetime(1980, 1, 2), T), 366)
        f.close()
Exemple #3
0
    def runTest(self):
        # The test files have no calendar attribute on the time variable.
        calendar = 'standard'

        # Get the real dates
        # skip this until cftime pull request #55 is in a released
        # version (1.0.1?). Otherwise, fix for issue #808 breaks this
        if parse_version(cftime.__version__) >= parse_version('1.0.1'):
            dates = []
            for file in self.files:
                f = Dataset(file)
                t = f.variables['time']
                dates.extend(num2date(t[:], t.units, calendar))
                f.close()

        # Compare with the MF dates
        f = MFDataset(self.files, check=True)
        t = f.variables['time']

        T = MFTime(t, calendar=calendar)
        assert_equal(T.calendar, calendar)
        assert_equal(len(T), len(t))
        assert_equal(T.shape, t.shape)
        assert_equal(T.dimensions, t.dimensions)
        assert_equal(T.typecode(), t.typecode())
        # skip this until cftime pull request #55 is in a released
        # version (1.0.1?). Otherwise, fix for issue #808 breaks this
        if parse_version(cftime.__version__) >= parse_version('1.0.1'):
            assert_array_equal(num2date(T[:], T.units, T.calendar), dates)
        assert_equal(date2index(datetime.datetime(1980, 1, 2), T), 366)
        f.close()

        # Test exception is raised when no calendar attribute is available on the
        # time variable.
        with MFDataset(self.files, check=True) as ds:
            with self.assertRaises(ValueError):
                MFTime(ds.variables['time'])

        # Test exception is raised when the calendar attribute is different on the
        # variables. First, add calendar attributes to file. Note this will modify
        # the files inplace.
        calendars = ['standard', 'gregorian']
        for idx, f in enumerate(self.files):
            with Dataset(f, 'a') as ds:
                ds.variables['time'].calendar = calendars[idx]
        with MFDataset(self.files, check=True) as ds:
            with self.assertRaises(ValueError):
                MFTime(ds.variables['time'])
    def runTest(self):
        # The test files have no calendar attribute on the time variable.
        calendar = 'standard'

        # Get the real dates
        # skip this until cftime pull request #55 is in a released
        # version (1.0.1?). Otherwise, fix for issue #808 breaks this
        if parse_version(cftime.__version__) >= parse_version('1.0.1'):
            dates = []
            for file in self.files:
                f = Dataset(file)
                t = f.variables['time']
                dates.extend(num2date(t[:], t.units, calendar))
                f.close()

        # Compare with the MF dates
        f = MFDataset(self.files,check=True)
        t = f.variables['time']

        T = MFTime(t, calendar=calendar)
        assert_equal(T.calendar, calendar)
        assert_equal(len(T), len(t))
        assert_equal(T.shape, t.shape)
        assert_equal(T.dimensions, t.dimensions)
        assert_equal(T.typecode(), t.typecode())
        # skip this until cftime pull request #55 is in a released
        # version (1.0.1?). Otherwise, fix for issue #808 breaks this
        if parse_version(cftime.__version__) >= parse_version('1.0.1'):
            assert_array_equal(num2date(T[:], T.units, T.calendar), dates)
        assert_equal(date2index(datetime.datetime(1980, 1, 2), T), 366)
        f.close()

        # Test exception is raised when no calendar attribute is available on the
        # time variable.
        with MFDataset(self.files, check=True) as ds:
            with self.assertRaises(ValueError):
                MFTime(ds.variables['time'])

        # Test exception is raised when the calendar attribute is different on the
        # variables. First, add calendar attributes to file. Note this will modify
        # the files inplace.
        calendars = ['standard', 'gregorian']
        for idx, f in enumerate(self.files):
            with Dataset(f, 'a') as ds:
                ds.variables['time'].calendar = calendars[idx]
        with MFDataset(self.files, check=True) as ds:
            with self.assertRaises(ValueError):
                MFTime(ds.variables['time'])
 def runTest(self):
     # Get the real dates
     dates = []
     for file in self.files:
         f = Dataset(file)
         t = f.variables['time']
         dates.extend(num2date(t[:], t.units, t.calendar))
         f.close()
     
     # Compare with the MF dates
     f = MFDataset(self.files,check=True)
     t = f.variables['time']
     mfdates = num2date(t[:], t.units, t.calendar)
     
     T = MFTime(t)
     assert_equal(len(T), len(t))
     assert_equal(T.shape, t.shape)
     assert_equal(T.dimensions, t.dimensions)
     assert_equal(T.typecode(), t.typecode())
     assert_array_equal(num2date(T[:], T.units, T.calendar), dates) 
     assert_equal(date2index(datetime.datetime(1980, 1, 2), T), 366)
Exemple #6
0
    def runTest(self):
        # Get the real dates
        dates = []
        for file in self.files:
            f = Dataset(file)
            t = f.variables['time']
            dates.extend(num2date(t[:], t.units, t.calendar))
            f.close()

        # Compare with the MF dates
        f = MFDataset(self.files, check=True)
        t = f.variables['time']
        mfdates = num2date(t[:], t.units, t.calendar)

        T = MFTime(t)
        assert_equal(len(T), len(t))
        assert_equal(T.shape, t.shape)
        assert_equal(T.dimensions, t.dimensions)
        assert_equal(T.typecode(), t.typecode())
        assert_array_equal(num2date(T[:], T.units, T.calendar), dates)
        assert_equal(date2index(datetime.datetime(1980, 1, 2), T), 366)
Exemple #7
0
 def _read_tcoord(self):
     """ Read time coordinate information from netcdf file(s) """
     nc = self._opennc(self.f)
     t = nc.variables[self.tcoord]
     
     if len(glob.glob(self.f)) > 1:
         try:
             self.dates = num2date(MFTime(t)[:], calendar=t.calendar, units=t.units)
         except:
             print 'netcdf4.MFTime incompatible with NETCDF4. Try concatenating data into a single file.'
             raise NetCDF4ERROR(err)
     else:
         self.dates = num2date(t[:], calendar=t.calendar, units=t.units)            
Exemple #8
0
def get_time_data(options):
    """This function fetches the time and calendar data from the input netcdf files.

    The argument is the options object, which contains the tmaxfile filename.

    Returns a TimeData object containing a collection of variables.
    """
    timedata = TimeData()
    if options.tmaxfile:
        filename = options.tmaxfile
    elif options.tminfile:
        filename = options.tminfile
    if any([(wildcard in filename) for wildcard in ['*', '?', '[']]):
        tempnc = MFDataset(filename, 'r')
        nctime = tempnc.variables[options.timevname]
        nctime = MFTime(nctime)
    else:
        tempnc = Dataset(filename, 'r')
        nctime = tempnc.variables[options.timevname]
    try:
        timedata.calendar = nctime.calendar
    except:
        timedata.calendar = 'proleptic_gregorian'

    if not timedata.calendar:
        print('Unrecognized calendar. Using gregorian.')
        timedata.calendar = 'gregorian'

    if timedata.calendar == '360_day':
        timedata.daysinyear = 360
        # 360 day season start and end indices
        timedata.SHS = (300, 450)
        timedata.SHW = (120, 270)
        timedata.dayone = nc.num2date(nctime[0],
                                      nctime.units,
                                      calendar=timedata.calendar)
        timedata.daylast = nc.num2date(nctime[-1],
                                       nctime.units,
                                       calendar=timedata.calendar)
        timedata.dates = Calendar360(timedata.dayone, timedata.daylast)
    else:
        timedata.daysinyear = 365
        # 365 day season start and end indices
        timedata.SHS = (304, 455)
        timedata.SHW = (120, 273)
        if tempnc.variables[options.timevname].units == 'day as %Y%m%d.%f':
            st = str(int(nctime[0]))
            nd = str(int(nctime[-1]))
            timedata.dayone = dt.datetime(int(st[:4]), int(st[4:6]),
                                          int(st[6:]))
            timedata.daylast = dt.datetime(int(nd[:4]), int(nd[4:6]),
                                           int(nd[6:]))
        else:
            timedata.dayone = nc.num2date(nctime[0],
                                          nctime.units,
                                          calendar=timedata.calendar)
            timedata.daylast = nc.num2date(nctime[-1],
                                           nctime.units,
                                           calendar=timedata.calendar)
        timedata.dates = pd.period_range(str(timedata.dayone),
                                         str(timedata.daylast))
        # Remove leap days. Maybe this should be a separate function?
        timedata.noleapdates = timedata.dates[(timedata.dates.month != 2) |
                                              (timedata.dates.day != 29)]

    return timedata
Exemple #9
0
def load_bp_data(options, timedata, variable='tmax', mask=None):
    """load_bp_data loads the tmax or tmin data for the baseperiod provided in options."""
    # Determine if we need tmax or tmin and whether the data are in multiple files.
    if variable == 'tmax':
        if options.bpfx: files = options.bpfx
        else: files = options.tmaxfile
        varname = options.tmaxvname
    elif variable == 'tmin':
        if options.bpfn: files = options.bpfn
        else: files = options.tminfile
        varname = options.tminvname

    # Load ncfile depending on multi or single-file
    if any([(wildcard in files) for wildcard in ['*', '?', '[']]):
        tempnc = MFDataset(files, 'r')
        bptime = tempnc.variables[options.timevname]
        bptime = MFTime(bptime)
    else:
        tempnc = Dataset(files, 'r')
        bptime = tempnc.variables[options.timevname]

    # Define the base period
    if tempnc.variables[options.timevname].units == 'day as %Y%m%d.%f':
        st = str(int(bptime[0]))
        nd = str(int(bptime[-1]))
        bpdayone = dt.datetime(int(st[:4]), int(st[4:6]), int(st[6:]))
        bpdaylast = dt.datetime(int(nd[:4]), int(nd[4:6]), int(nd[6:]))
    else:
        bpdayone = nc.num2date(bptime[0],
                               bptime.units,
                               calendar=timedata.calendar)
        bpdaylast = nc.num2date(bptime[-1],
                                bptime.units,
                                calendar=timedata.calendar)

    if timedata.calendar == '360_day':
        bpdates = Calendar360(bpdayone, bpdaylast)
    else:
        bpdates = pd.period_range(str(bpdayone), str(bpdaylast))
        if timedata.calendar == '365_day':
            bpdates = bpdates[(bpdates.month != 2) | (bpdates.day != 29)]
        dates_base = bpdates[(options.bpstart <= bpdates.year)
                             & (bpdates.year <= options.bpend)]

    temp = tempnc.variables[varname][(options.bpstart <= bpdates.year)
                                     & (bpdates.year <= options.bpend)]
    if len(temp.shape) == 4: temp = temp.squeeze()

    if options.maskfile:
        temp = temp[:, mask]

    if tempnc.variables[varname].units == 'K': temp -= 273.15

    # Remove leap days in gregorian calendars
    if (timedata.calendar == 'gregorian') | (
            timedata.calendar == 'proleptic_gregorian') | (timedata.calendar
                                                           == 'standard'):
        temp = temp[(dates_base.month != 2) | (dates_base.day != 29), ...]

    tempnc.close()
    return temp
Exemple #10
0
 def _read_tcoord(self):
     """ Read time coordinate information from netcdf file(s) """
     nc = MFDataset(self.f)
     t = nc.variables[self.tcoord]
     self.dates = num2date(MFTime(t)[:], calendar=t.calendar, units=t.units)