Skip to content

Commit

Permalink
Fix time offset direction
Browse files Browse the repository at this point in the history
  • Loading branch information
siiptuo committed Oct 3, 2024
1 parent fcd3be8 commit 6c246d5
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/processing/instrument_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,10 @@ def process_cl51(self):
) = self.download_instrument()
full_paths.sort()
_concatenate_text_files(full_paths, str(self.daily_path))
if calibration and calibration.get("data", {}).get("time_offset"):
logging.info("Shifting timestamps to UTC")
offset_in_hours = round(calibration["data"]["time_offset"] / 60)
_fix_cl51_timestamps(str(self.daily_path), offset_in_hours)
if calibration and (minutes := calibration.get("data", {}).get("time_offset")):
logging.info("Shifting timestamps to UTC by %d minutes", minutes)
offset = datetime.timedelta(minutes=minutes)
_fix_cl51_timestamps(str(self.daily_path), offset)
self._call_ceilo2nc("cl51")

def process_cl61d(self):
Expand Down Expand Up @@ -636,13 +636,13 @@ def _unzip_gz_files(full_paths: list[Path]) -> list[Path]:
return paths_out


def _fix_cl51_timestamps(filename: str, hours: int) -> None:
def _fix_cl51_timestamps(filename: str, offset: datetime.timedelta) -> None:
with open(filename, "r") as file:
lines = file.readlines()
for ind, line in enumerate(lines):
if is_timestamp(line):
date_time = line.strip("-").strip("\n")
date_time_utc = _shift_datetime(date_time, hours)
date_time_utc = _shift_datetime(date_time, offset)
lines[ind] = f"-{date_time_utc}\n"
with open(filename, "w") as file:
file.writelines(lines)
Expand Down Expand Up @@ -872,10 +872,10 @@ def _doppy_wls70_wind_to_nc(
)


def _shift_datetime(date_time: str, offset: int) -> str:
def _shift_datetime(date_time: str, offset: datetime.timedelta) -> str:
"""Shifts datetime N hours."""
dt = datetime.datetime.strptime(date_time, "%Y-%m-%d %H:%M:%S")
dt = dt + datetime.timedelta(hours=offset)
dt = dt - offset
return dt.strftime("%Y-%m-%d %H:%M:%S")


Expand Down

0 comments on commit 6c246d5

Please sign in to comment.