Skip to content

Commit

Permalink
Ute timezone aware objects
Browse files Browse the repository at this point in the history
utcnow to be depricated
  • Loading branch information
Nina.Hakansson committed Apr 15, 2024
1 parent 1bde12c commit f520c0e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 17 deletions.
6 changes: 3 additions & 3 deletions bin/pps_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import os
import sys
import threading
from datetime import datetime
from datetime import datetime, timezone
from subprocess import PIPE, Popen

from queue import Empty, Queue
Expand Down Expand Up @@ -97,7 +97,7 @@ def pps_worker(scene, publish_q, input_msg, options):

try:
LOG.info("Starting pps runner for scene %s", str(scene))
job_start_time = datetime.utcnow()
job_start_time = datetime.now(tz=timezone.utc)

LOG.debug("Level-1c file: %s", scene['file4pps'])
LOG.debug("Platform name: %s", scene['platform_name'])
Expand Down Expand Up @@ -178,7 +178,7 @@ def pps_worker(scene, publish_q, input_msg, options):
servername=options['servername'],
station=options['station'])

dt_ = datetime.utcnow() - job_start_time
dt_ = datetime.now(tz=timezone.utc) - job_start_time
LOG.info("PPS on scene " + str(scene) + " finished. It took: " + str(dt_))

t__.cancel()
Expand Down
8 changes: 4 additions & 4 deletions bin/run_nwp_preparation.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import logging
import time

from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from nwcsafpps_runner.logger import setup_logging
from nwcsafpps_runner.prepare_nwp import update_nwp
from nwcsafpps_runner.utils import NwpPrepareError
Expand All @@ -37,12 +37,12 @@

LOG = logging.getLogger('nwp-preparation')

# datetime.datetime.utcnow => datetime.datetime.now(datetime.UTC) ~python 3.12
# Eventually timezone.utz => UTC. UTC in python 3.12 not in python 3.9


def prepare_and_publish(pub, options, flens):
config_file_name = options.config_file
starttime = datetime.utcnow() - timedelta(days=1)
starttime = datetime.now(tz=timezone.utc) - timedelta(days=1)
ok_files, publish_topic = update_nwp(starttime, flens, config_file_name)
if publish_topic is not None:
for filename in ok_files:
Expand All @@ -60,7 +60,7 @@ def _run_subscribe_publisher(pub, options, flens):
if every_hour_minute > 60:
return
while True:
minute = datetime.utcnow().minute
minute = datetime.now(tz=timezone.utc).minute
if minute < every_hour_minute or minute > every_hour_minute + 7:
LOG.info("Not time for nwp preparation for pps yet, waiting 5 minutes")
time.sleep(60 * 5)
Expand Down
8 changes: 4 additions & 4 deletions nwcsafpps_runner/prepare_nwp.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

from glob import glob
import os
from datetime import datetime
from datetime import datetime, timezone
import time
import tempfile
from trollsift import Parser
Expand Down Expand Up @@ -71,8 +71,8 @@ def set_time_info(self, filename, cfg):
res = parser.parse("{}".format(os.path.basename(self.nhsf_file)))
if 'analysis_time' in res:
if res['analysis_time'].year == 1900:
res['analysis_time'] = res['analysis_time'].replace(year=datetime.utcnow().year)
self.analysis_time = res['analysis_time']
res['analysis_time'] = res['analysis_time'].replace(year=datetime.now(timezone.utc).year)
self.analysis_time = res['analysis_time'].replace(tzinfo=timezone.utc)
self.timestamp = self.analysis_time.strftime("%Y%m%d%H%M")
else:
raise NwpPrepareError("Can not parse analysis_time in file name. Check config and filename timestamp")
Expand Down Expand Up @@ -338,5 +338,5 @@ def check_and_reduce_nwp_content(gribfile, result_file, nwp_req_filename):
LOG = logging.getLogger('test_update_nwp')

from datetime import timedelta
now = datetime.utcnow()
now = datetime.now(tz=timezone.utc)
update_nwp(now - timedelta(days=2), [9])
10 changes: 4 additions & 6 deletions nwcsafpps_runner/tests/test_nwp_prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"""Test the nwp_prepare runner code."""
import pytest
import unittest
from datetime import datetime
from datetime import datetime, timezone
import os
import logging
from datetime import timedelta
Expand Down Expand Up @@ -110,7 +110,7 @@ def test_update_nwp(self, fake_file_dir):
my_temp_dir = fake_file_dir
outfile = os.path.join(str(my_temp_dir), "PPS_ECMWF_202205100000+009H00M")
cfg_file = my_temp_dir + '/pps_config.yaml'
date = datetime(year=2022, month=5, day=10, hour=0)
date = datetime(year=2022, month=5, day=10, hour=0, tzinfo=timezone.utc)
nwc_prep.update_nwp(date - timedelta(days=2), [9], cfg_file)
# Run again when file is already created
nwc_prep.update_nwp(date - timedelta(days=2), [9], cfg_file)
Expand All @@ -123,7 +123,7 @@ def test_update_nwp_no_requirement_file(self, fake_file_dir):
requirement_name = str(my_temp_dir) + '/pps_nwp_req.txt'
outfile = os.path.join(str(my_temp_dir), "PPS_ECMWF_202205100000+009H00M")
os.remove(requirement_name)
date = datetime(year=2022, month=5, day=10, hour=0)
date = datetime(year=2022, month=5, day=10, hour=0, tzinfo=timezone.utc)
nwc_prep.update_nwp(date - timedelta(days=2), [9], cfg_file)
assert os.path.exists(outfile)

Expand All @@ -132,9 +132,7 @@ def test_update_nwp_missing_fields(self, fake_file_dir):
my_temp_dir = fake_file_dir
outfile = os.path.join(str(my_temp_dir), "PPS_ECMWF_MANDATORY_202205100000+009H00M")
cfg_file = my_temp_dir + '/pps_config_missing_fields.yaml'
date = datetime(year=2022, month=5, day=10, hour=0)
nwc_prep.update_nwp(date - timedelta(days=2), [9], cfg_file)
date = datetime(year=2022, month=5, day=10, hour=0)
date = datetime(year=2022, month=5, day=10, hour=0, tzinfo=timezone.utc)
nwc_prep.update_nwp(date - timedelta(days=2), [9], cfg_file)
assert not (os.path.exists(outfile))

Expand Down

0 comments on commit f520c0e

Please sign in to comment.