def create_time_axis(freq, path, name, has_bounds): global log, start_date_, ref_date_ date_times = cmor_utils.read_time_stamps(path) if len(date_times) == 0: log.error("Empty time step list encountered at time axis creation for files %s" % str(path)) return 0, [], [] if has_bounds: bounds = numpy.empty([len(date_times), 2]) rounded_times = map(lambda time: (cmor_utils.get_rounded_time(freq, time)), date_times) dt_low = rounded_times dt_up = rounded_times[1:] + [cmor_utils.get_rounded_time(freq, date_times[-1], 1)] bounds[:, 0], units = cmor_utils.date2num([t - timeshift for t in dt_low], ref_date_) bounds[:, 1], units = cmor_utils.date2num([t - timeshift for t in dt_up], ref_date_) times = bounds[:, 0] + (bounds[:, 1] - bounds[:, 0]) / 2 return cmor.axis(table_entry=str(name), units=units, coord_vals=times, cell_bounds=bounds), dt_low, dt_up step = cmor_utils.make_cmor_frequency(freq) if date_times[0] >= start_date_ + step: date = date_times[0] extra_dates = [] while date > start_date_: date = date - step extra_dates.append(date) log.warning("The file %s seems to be missing %d time stamps at the beginning, these will be added" % (path, len(extra_dates))) date_times = extra_dates[::-1] + date_times if date_times[0] < start_date_: date_times = [t for t in date_times if t >= start_date_] log.warning("The file %s seems to be containing %d too many time stamps at the beginning, these will be " "removed" % (path, len([t for t in date_times if t >= start_date_]))) times, units = cmor_utils.date2num([t - timeshift for t in date_times], ref_date_) return cmor.axis(table_entry=str(name), units=units, coord_vals=times), date_times, date_times
def create_time_axis(freq, path, name, has_bounds): global log, start_date_, ref_date_ command = cdo.Cdo() times = command.showtimestamp(input=path)[0].split() datetimes = sorted( set( map(lambda s: datetime.datetime.strptime(s, "%Y-%m-%dT%H:%M:%S"), times))) if len(datetimes) == 0: log.error( "Empty time step list encountered at time axis creation for files %s" % str(path)) return refdate = cmor_utils.make_datetime(ref_date_) if has_bounds: n = len(datetimes) bounds = numpy.empty([n, 2]) rounded_times = map( lambda time: (cmor_utils.get_rounded_time(freq, time) - refdate).total_seconds( ) / 3600., datetimes) bounds[:, 0] = rounded_times[:] bounds[0:n - 1, 1] = rounded_times[1:n] bounds[n - 1, 1] = (cmor_utils.get_rounded_time(freq, datetimes[n - 1], 1) - refdate).total_seconds() / 3600. times[:] = bounds[:, 0] + (bounds[:, 1] - bounds[:, 0]) / 2 return cmor.axis(table_entry=str(name), units="hours since " + str(ref_date_), coord_vals=times, cell_bounds=bounds) times = numpy.array([(d - refdate).total_seconds() / 3600 for d in datetimes]) return cmor.axis(table_entry=str(name), units="hours since " + str(ref_date_), coord_vals=times)