Skip to main content

Easy time handling in the context of weather forecasting

Project description

svalbard

Easy time handling in the context of weather forecasting

Concepts

ForecastTime integrates the concept of two central notions of time in a weather forecasting scenario.

anatime is the time when a numerical weather prediction model is initialized.

validtime is the time when the weather forecast is to be validated towards observations.

Between these times is a time interval which ForecastTime keeps track of in seconds, but since hours is a more common time unit the interface of this time interval is given in hours

fchours denotes that time interval. The time stamps are time zone naive.

Usage

Initializing

The user can choose to initialize the class with no parameters and set time attributes with setters like this:

from svalbard import ForecastTime
ft=ForecastTime()
ft.set_anatime('2018122800')
ft.set_fchours(24)

The same can be achieved with

from svalbard import ForecastTime
ft=ForecastTime(anatime='2018122800',fchours=24)

Or if you want to provide validtime instead of fchours:

from svalbard import ForecastTime
ft=ForecastTime(anatime='2018122800',validtime='2018122900')

Updating with consistency checks

ForecastTime has getters and setters for anatime, validtime and fchours. The setters make sure that there is consistency among those three. If one of them is updated, one of the others also need to be updated to maintain consistency.

If fchours is updated while anatime and validtime is set, the default setting keeps the anatime and updates validtime.

from svalbard import ForecastTime
ft=ForecastTime(anatime='2018122800',fchours=24)
ft.get_validtime()
datetime.datetime(2018, 12, 29, 0, 0)
ft.set_fchours(48)
ft.get_validtime()
datetime.datetime(2018, 12, 30, 0, 0)

However if you want to update anatime you can do

from svalbard import ForecastTime
ft=ForecastTime(anatime='2018122800',fchours=24)
ft.set_fchours(48,keep_anatime=False)
ft.get_anatime()
datetime.datetime(2018, 12, 27, 0, 0)

Optional datetime format guessing

You may choose to initialize datetimes with strings or with python datetime objects.

When you set datetimes with strings ForecastTime needs to figure out the string formatting. This is achieved by trying format strings from a list. The first formatting that produces a valid datetime object without trowing an error is chosen. This may seem a bit sloppy, but the list of string formattings to try may be be provided by the user and it may contain only one format string which would eliminate the guesswork. The list is there for convenience.

This is the list that will be used if the user chooses to go with the defaults.

somedatetimeformats=[
    '%Y%m%d%H',
    '%Y%m%d%H',
    '%Y%m%d %H',
    '%Y%m%d%H%M',
    '%Y%m%d%H%M%S',
    '%Y-%m-%d %H',
    '%Y-%m-%d',
    '%Y-%m-%d %H:%M:%S',
    '%Y-%m-%d %H:%M:%SZ',
    '%Y-%m-%dT%H:%M:%SZ',
    '%Y-%m-%d %H%M%SZ',
    '%m-%d-%Y %H:%M:%S'
]

What actually happens is that, if datetimes are provided as strings, the function guess_forecast_datetime_from_string is called. This function can be used separatly.

from svalbard import guess_forecast_datetime_from_string,somedatetimeformats

guess_forecast_datetime_from_string('2010-01-01 12',datetimeformats=somedatetimeformats,verbose=False)

# Returns
[(5, '%Y-%m-%d %H', datetime.datetime(2010, 1, 1, 12, 0))]

guess_forecast_datetime_from_string returns a list of triplets, (index,format string,datetime object).

String interpolation in file names

If you want to download some real world weather forecasts, they often come in files where anatime, validtime or fchours is encoded in the filename.

ForecastTime to the rescue:

from svalbard import ForecastTime
from datetime import datetime
gfs_url_template='http://www.ftp.ncep.noaa.gov/data/nccf/com/gfs/prod/gfs.{anatime:%Y%m%d%H}/gfs.t{anatime:%H}z.pgrb2.0p25.f{fchours:03d}'
anatime=datetime.now().strftime('%Y%m%d00')
ft = ForecastTime(anatime=anatime)
for h in range(0,12,3):
  ft.set_fchours(h)
  print(ft)
  print( gfs_url_template.format(**ft.get_dict()) )

ForecastTime,anatime:2018-12-28 00:00:00,validtime:2018-12-28 00:00:00,fchours:0

http://www.ftp.ncep.noaa.gov/data/nccf/com/gfs/prod/gfs.2018122800/gfs.t12z.pgrb2.0p25.f000

ForecastTime,anatime:2018-12-28 00:00:00,validtime:2018-12-28 03:00:00,fchours:3

http://www.ftp.ncep.noaa.gov/data/nccf/com/gfs/prod/gfs.2018122800/gfs.t12z.pgrb2.0p25.f003

ForecastTime,anatime:2018-12-28 00:00:00,validtime:2018-12-28 06:00:00,fchours:6

http://www.ftp.ncep.noaa.gov/data/nccf/com/gfs/prod/gfs.2018122800/gfs.t12z.pgrb2.0p25.f006

ForecastTime,anatime:2018-12-28 00:00:00,validtime:2018-12-28 09:00:00,fchours:9

http://www.ftp.ncep.noaa.gov/data/nccf/com/gfs/prod/gfs.2018122800/gfs.t12z.pgrb2.0p25.f009

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

svalbard-0.2.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

svalbard-0.2-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file svalbard-0.2.tar.gz.

File metadata

  • Download URL: svalbard-0.2.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.8

File hashes

Hashes for svalbard-0.2.tar.gz
Algorithm Hash digest
SHA256 b4e2ced55a0868030c88ee4c3289c2be58ecbec53edc3c7efaf13c8423a76899
MD5 eed1a7764ccde2b5b6b7a59cf237741d
BLAKE2b-256 5b4ea3c3e7fad482e6b0c29fab8aef26e4c29641da596e59b6e45c21982f2b80

See more details on using hashes here.

File details

Details for the file svalbard-0.2-py3-none-any.whl.

File metadata

  • Download URL: svalbard-0.2-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.8

File hashes

Hashes for svalbard-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d849a1b0ce2a514d5ff9e13968ab429c9d5278d56f8ae1646b60d80b775dfddd
MD5 a75e1fba379f49b16ab622dfefbec09a
BLAKE2b-256 f960ad44dd5a794090a0a905ab61595799ce159ec0eb17ffa6a54a3c8440e36b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page