def fix_single_trajectory(): artifacts = os.path.join(os.path.dirname(__file__), 'artifacts') to_fix_path = os.path.join(artifacts, 'raw', '03-08-2015_09-58-01') fixed_path = fix_trajectory_timezone(to_fix_path, os.path.join(artifacts, 'fixed')) original_trajectory = read_compressed_trajectory(to_fix_path, with_timezone=False) fixed_trajectory = read_compressed_trajectory(fixed_path, with_timezone=True) print(num_to_date_str_converter(original_trajectory[0, 0], with_timezone=False)) print(num_to_date_str_converter(fixed_trajectory[0, 0], with_timezone=True))
def fix_trajectory_timezone(filename, folder_to_put, change_filename=True): ''' Add timezone to the trajectory ''' # http://www.saltycrane.com/blog/2009/05/converting-time-zones-datetime-objects-python/ traj = read_compressed_trajectory(filename, with_timezone=False) tz = timezone('US/Pacific') for i in range(traj.shape[0]): d = num2date(traj[i, 0]) d = d.replace(tzinfo = None) d = tz.localize(d) traj[i, 0] = date2num(d) result_filename = os.path.split(filename)[-1] if change_filename: file_date = num2date(date_str_to_num_converter_no_timezone(result_filename)) file_date = file_date.replace(tzinfo = None) file_date = tz.localize(file_date) result_filename = num_to_date_str_converter(date2num(file_date), with_timezone=True) resulting_path = os.path.join(folder_to_put, result_filename) write_compressed_trajectory(traj, os.path.join(folder_to_put, result_filename), with_timezone=True) return resulting_path
for t in trajectories_to_fix: fix_trajectory_timezone(os.path.join(artifacts, 'raw', t), os.path.join(artifacts, 'fixed')) def fix_single_trajectory(): artifacts = os.path.join(os.path.dirname(__file__), 'artifacts') to_fix_path = os.path.join(artifacts, 'raw', '03-08-2015_09-58-01') fixed_path = fix_trajectory_timezone(to_fix_path, os.path.join(artifacts, 'fixed')) original_trajectory = read_compressed_trajectory(to_fix_path, with_timezone=False) fixed_trajectory = read_compressed_trajectory(fixed_path, with_timezone=True) print(num_to_date_str_converter(original_trajectory[0, 0], with_timezone=False)) print(num_to_date_str_converter(fixed_trajectory[0, 0], with_timezone=True)) if __name__ == '__main__': artifacts = os.path.join(os.path.dirname(__file__), 'artifacts') to_fix_path = os.path.join(artifacts, '_testing', 'testing_raw_0') fixed_path = fix_trajectory_timezone(to_fix_path, os.path.join(artifacts, 'fixed'), change_filename=False) original_trajectory = read_compressed_trajectory(to_fix_path, with_timezone=False) fixed_trajectory = read_compressed_trajectory(fixed_path, with_timezone=True) print(num_to_date_str_converter(original_trajectory[0, 0], with_timezone=False)) print(num_to_date_str_converter(fixed_trajectory[0, 0], with_timezone=True))