| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 5 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 6 |
# |
| 7 |
# This program is free software: you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation, either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# This program is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
"""cylc cycle-point [OPTIONS] ARGS |
| 21 |
|
| 22 |
Utility for simple date-time cycle point arithmetic. |
| 23 |
|
| 24 |
For more generic date-time manipulations see the "isodatetime" command. |
| 25 |
|
| 26 |
Filename templating replaces elements of a template string with corresponding |
| 27 |
elements of the current or given cycle point. |
| 28 |
|
| 29 |
Use ISO 8601 or posix date-time format elements: |
| 30 |
$ cylc cycle-point 2010080T00 --template foo-CCYY-MM-DD-Thh.nc |
| 31 |
foo-2010-08-08-T00.nc |
| 32 |
$ cylc cycle-point 2010080T00 --template foo-%Y-%m-%d-T%H.nc |
| 33 |
foo-2010-08-08-T00.nc |
| 34 |
|
| 35 |
Other examples: |
| 36 |
# print offset from an explicit cycle point: |
| 37 |
$ cylc cycle-point --offset-hours=6 20100823T1800Z |
| 38 |
20100824T0000Z |
| 39 |
|
| 40 |
# print offset from $CYLC_TASK_CYCLE_POINT (as in workflow tasks): |
| 41 |
$ export CYLC_TASK_CYCLE_POINT=20100823T1800Z |
| 42 |
$ cylc cycle-point --offset-hours=-6 |
| 43 |
20100823T1200Z |
| 44 |
|
| 45 |
# cycle point filename templating, explicit template: |
| 46 |
$ export CYLC_TASK_CYCLE_POINT=2010-08 |
| 47 |
$ cylc cycle-point --offset-years=2 --template=foo-CCYY-MM.nc |
| 48 |
foo-2012-08.nc |
| 49 |
|
| 50 |
# cycle point filename templating, template in a variable: |
| 51 |
$ export CYLC_TASK_CYCLE_POINT=2010-08 |
| 52 |
$ export MYTEMPLATE=foo-CCYY-MM.nc |
| 53 |
$ cylc cycle-point --offset-years=2 --template=MYTEMPLATE |
| 54 |
foo-2012-08.nc |
| 55 |
""" |
| 56 |
|
| 57 |
import os |
| 58 |
import sys |
| 59 |
|
| 60 |
import cylc.flow.cycling.iso8601 |
| 61 |
from cylc.flow.option_parsers import CylcOptionParser as COP |
| 62 |
from cylc.flow.terminal import cli_function |
| 63 |
|
| 64 |
import metomi.isodatetime.data |
| 65 |
import metomi.isodatetime.dumpers |
| 66 |
import metomi.isodatetime.parsers |
| 67 |
from metomi.isodatetime.exceptions import IsodatetimeError |
| 68 |
|
| 69 |
|
| 70 |
def get_option_parser() -> COP: |
| 71 |
parser = COP( |
| 72 |
__doc__, |
| 73 |
color=False, |
| 74 |
argdoc=[ |
| 75 |
COP.optional( |
| 76 |
('POINT', 'ISO8601 date-time, default=$CYLC_TASK_CYCLE_POINT') |
| 77 |
) |
| 78 |
] |
| 79 |
) |
| 80 |
|
| 81 |
parser.add_option( |
| 82 |
"--offset-hours", metavar="HOURS", |
| 83 |
help="Add N hours to CYCLE (may be negative)", |
| 84 |
action="store", dest="offsethours") |
| 85 |
|
| 86 |
parser.add_option( |
| 87 |
"--offset-days", metavar="DAYS", |
| 88 |
help="Add N days to CYCLE (N may be negative)", |
| 89 |
action="store", dest="offsetdays") |
| 90 |
|
| 91 |
parser.add_option( |
| 92 |
"--offset-months", metavar="MONTHS", |
| 93 |
help="Add N months to CYCLE (N may be negative)", |
| 94 |
action="store", dest="offsetmonths") |
| 95 |
|
| 96 |
parser.add_option( |
| 97 |
"--offset-years", metavar="YEARS", |
| 98 |
help="Add N years to CYCLE (N may be negative)", |
| 99 |
action="store", dest="offsetyears") |
| 100 |
|
| 101 |
parser.add_option( |
| 102 |
"--offset", metavar="ISO_OFFSET", |
| 103 |
help="Add an ISO 8601-based interval representation to CYCLE", |
| 104 |
action="store", dest="offset") |
| 105 |
|
| 106 |
parser.add_option( |
| 107 |
"--equal", metavar="POINT2", |
| 108 |
help="Succeed if POINT2 is equal to POINT (format agnostic).", |
| 109 |
action="store", dest="point2") |
| 110 |
|
| 111 |
parser.add_option( |
| 112 |
"--template", metavar="TEMPLATE", |
| 113 |
help="Filename template string or variable", |
| 114 |
action="store", dest="template") |
| 115 |
|
| 116 |
parser.add_option( |
| 117 |
"--time-zone", metavar="TEMPLATE", |
| 118 |
help="Control the formatting of the result's timezone (e.g. " |
| 119 |
"Z, +13:00, -hh)", |
| 120 |
action="store", default=None, dest="time_zone") |
| 121 |
|
| 122 |
parser.add_option( |
| 123 |
"--num-expanded-year-digits", metavar="NUMBER", |
| 124 |
help="Specify a number of expanded year digits to print in the result", |
| 125 |
action="store", default=0, dest="num_expanded_year_digits") |
| 126 |
|
| 127 |
parser.add_option( |
| 128 |
"--print-year", help="Print only CCYY of result", |
| 129 |
action="store_true", default=False, dest="print_year") |
| 130 |
|
| 131 |
parser.add_option( |
| 132 |
"--print-month", help="Print only MM of result", |
| 133 |
action="store_true", default=False, dest="print_month") |
| 134 |
|
| 135 |
parser.add_option( |
| 136 |
"--print-day", help="Print only DD of result", |
| 137 |
action="store_true", default=False, dest="print_day") |
| 138 |
|
| 139 |
parser.add_option( |
| 140 |
"--print-hour", help="Print only hh of result", |
| 141 |
action="store_true", default=False, dest="print_hour") |
| 142 |
|
| 143 |
return parser |
| 144 |
|
| 145 |
|
| 146 |
@cli_function(get_option_parser) |
| 147 |
def main(parser, options, *args): |
| 148 |
if len(args) == 0: |
| 149 |
# input cycle point must be defined in the environment. |
| 150 |
if 'CYLC_TASK_CYCLE_POINT' not in os.environ: |
| 151 |
parser.error("Provide CYCLE arg, or define $CYLC_TASK_CYCLE_POINT") |
| 152 |
cycle_point_string = os.environ['CYLC_TASK_CYCLE_POINT'] |
| 153 |
|
| 154 |
else: |
| 155 |
# must be cycle point |
| 156 |
cycle_point_string = args[0] |
| 157 |
|
| 158 |
# template string |
| 159 |
template = None |
| 160 |
if options.template: |
| 161 |
if (options.print_month or options.print_year or options.print_day or |
| 162 |
options.print_hour): |
| 163 |
parser.error( |
| 164 |
'"print only" options are incompatible with templating') |
| 165 |
tmp = options.template |
| 166 |
template = os.environ.get(tmp, tmp) |
| 167 |
else: |
| 168 |
n_chosen = 0 |
| 169 |
|
| 170 |
if options.print_year: |
| 171 |
n_chosen += 1 |
| 172 |
if options.num_expanded_year_digits: |
| 173 |
template = "±XCCYY" |
| 174 |
else: |
| 175 |
template = "CCYY" |
| 176 |
|
| 177 |
if options.print_month: |
| 178 |
n_chosen += 1 |
| 179 |
template = "MM" |
| 180 |
|
| 181 |
if options.print_day: |
| 182 |
n_chosen += 1 |
| 183 |
template = "DD" |
| 184 |
|
| 185 |
if options.print_hour: |
| 186 |
n_chosen += 1 |
| 187 |
template = "%H" |
| 188 |
|
| 189 |
if n_chosen != 0 and n_chosen != 1: |
| 190 |
parser.error("Choose NONE or ONE of print_(year|month|day|hour)") |
| 191 |
|
| 192 |
cylc.flow.cycling.iso8601.init( |
| 193 |
num_expanded_year_digits=options.num_expanded_year_digits, |
| 194 |
time_zone=options.time_zone |
| 195 |
) |
| 196 |
iso_point_parser = metomi.isodatetime.parsers.TimePointParser( |
| 197 |
num_expanded_year_digits=options.num_expanded_year_digits |
| 198 |
) |
| 199 |
iso_point_dumper = metomi.isodatetime.dumpers.TimePointDumper( |
| 200 |
num_expanded_year_digits=options.num_expanded_year_digits |
| 201 |
) |
| 202 |
try: |
| 203 |
cycle_point = iso_point_parser.parse( |
| 204 |
cycle_point_string, dump_as_parsed=(template is None)) |
| 205 |
except IsodatetimeError as exc: |
| 206 |
parser.error('ERROR: invalid cycle: %s' % exc) |
| 207 |
|
| 208 |
if options.point2: |
| 209 |
try: |
| 210 |
cycle_point2 = iso_point_parser.parse( |
| 211 |
options.point2, dump_as_parsed=(template is None)) |
| 212 |
except IsodatetimeError as exc: |
| 213 |
parser.error('ERROR: invalid cycle: %s' % exc) |
| 214 |
if cycle_point2 == cycle_point: |
| 215 |
sys.exit(0) |
| 216 |
else: |
| 217 |
sys.exit(1) |
| 218 |
|
| 219 |
offset_props = {} |
| 220 |
|
| 221 |
if options.offsethours: |
| 222 |
try: |
| 223 |
offset_props["hours"] = int(options.offsethours) |
| 224 |
except ValueError: |
| 225 |
parser.error('ERROR: offset must be integer') |
| 226 |
|
| 227 |
if options.offsetdays: |
| 228 |
try: |
| 229 |
offset_props["days"] = int(options.offsetdays) |
| 230 |
except ValueError: |
| 231 |
parser.error('ERROR: offset must be integer') |
| 232 |
|
| 233 |
if options.offsetmonths: |
| 234 |
try: |
| 235 |
offset_props["months"] = int(options.offsetmonths) |
| 236 |
except ValueError: |
| 237 |
parser.error('ERROR: offset must be integer') |
| 238 |
|
| 239 |
if options.offsetyears: |
| 240 |
try: |
| 241 |
offset_props["years"] = int(options.offsetyears) |
| 242 |
except ValueError: |
| 243 |
parser.error('ERROR: offset must be integer') |
| 244 |
|
| 245 |
offset = metomi.isodatetime.data.Duration(**offset_props) |
| 246 |
|
| 247 |
if options.offset: |
| 248 |
opt_offset = options.offset |
| 249 |
sign_factor = 1 |
| 250 |
if options.offset.startswith("-"): |
| 251 |
opt_offset = options.offset[1:] |
| 252 |
sign_factor = -1 |
| 253 |
try: |
| 254 |
offset += metomi.isodatetime.parsers.DurationParser().parse( |
| 255 |
opt_offset) * sign_factor |
| 256 |
except IsodatetimeError as exc: |
| 257 |
parser.error('ERROR: offset not valid: %s' % exc) |
| 258 |
cycle_point += offset |
| 259 |
if template is None: |
| 260 |
print(cycle_point) |
| 261 |
else: |
| 262 |
dump_string = iso_point_dumper.dump(cycle_point, template) |
| 263 |
if (dump_string == template and |
| 264 |
not any(_ in template for _ in ["CCYY", "MM", "DD", "%"])): |
| 265 |
# A pure time string, no date - not well handled in isodatetime. |
| 266 |
print(iso_point_dumper.dump(cycle_point, "T" + template)[1:]) |
| 267 |
else: |
| 268 |
print(dump_string) |