[8463d78] | 1 | #!/usr/bin/env python3 |
---|
[efdb01a] | 2 | # -*- coding: utf-8 -*- |
---|
[991df6a] | 3 | #******************************************************************************* |
---|
| 4 | # @Author: Anne Philipp (University of Vienna) |
---|
| 5 | # |
---|
| 6 | # @Date: May 2018 |
---|
| 7 | # |
---|
| 8 | # @Change History: |
---|
| 9 | # October 2014 - Anne Fouilloux (University of Oslo) |
---|
[ff99eae] | 10 | # - created functions silent_remove and product (taken from ECMWF) |
---|
[991df6a] | 11 | # |
---|
| 12 | # November 2015 - Leopold Haimberger (University of Vienna) |
---|
[ff99eae] | 13 | # - created functions: interpret_args_and_control, clean_up |
---|
| 14 | # my_error, normal_exit, init128, to_param_id |
---|
[991df6a] | 15 | # |
---|
[6f951ca] | 16 | # April - December 2018 - Anne Philipp (University of Vienna): |
---|
[991df6a] | 17 | # - applied PEP8 style guide |
---|
| 18 | # - added documentation |
---|
[6f951ca] | 19 | # - moved all non class methods from former file Flexparttools in here |
---|
[8209738] | 20 | # - separated args and control interpretation |
---|
[6f951ca] | 21 | # - added functions get_list_as_string, read_ecenv, send_mail, make_dir, |
---|
| 22 | # put_file_to_ecserver, submit_job_to_ecserver, get_informations, |
---|
| 23 | # get_dimensions, execute_subprocess, none_or_int, none_or_str |
---|
[991df6a] | 24 | # |
---|
[75db9b0] | 25 | # August 2020 - Leopold Haimberger (University of Vienna) |
---|
| 26 | # - added function to check if a specific string is in a file |
---|
| 27 | # - added function to overwrite lines in a file which contain specific string |
---|
| 28 | # |
---|
[01bd8aa] | 29 | # July 2022 - Anne Tipka (formerly Philipp) |
---|
| 30 | # - modified function put_file_to_ecserver to put the file automatically |
---|
| 31 | # to $HOME directory, to avoid static path creation |
---|
| 32 | # |
---|
[991df6a] | 33 | # @License: |
---|
[026b359] | 34 | # (C) Copyright 2014-2020. |
---|
[6f951ca] | 35 | # Anne Philipp, Leopold Haimberger |
---|
[991df6a] | 36 | # |
---|
[44174de] | 37 | # SPDX-License-Identifier: CC-BY-4.0 |
---|
| 38 | # |
---|
[6f951ca] | 39 | # This work is licensed under the Creative Commons Attribution 4.0 |
---|
| 40 | # International License. To view a copy of this license, visit |
---|
| 41 | # http://creativecommons.org/licenses/by/4.0/ or send a letter to |
---|
| 42 | # Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. |
---|
[991df6a] | 43 | # |
---|
[6f951ca] | 44 | # @Methods: |
---|
| 45 | # none_or_str |
---|
| 46 | # none_or_int |
---|
| 47 | # get_cmdline_args |
---|
| 48 | # read_ecenv |
---|
| 49 | # clean_up |
---|
| 50 | # my_error |
---|
| 51 | # send_mail |
---|
| 52 | # normal_exit |
---|
| 53 | # product |
---|
| 54 | # silent_remove |
---|
| 55 | # init128 |
---|
| 56 | # to_param_id |
---|
| 57 | # get_list_as_string |
---|
| 58 | # make_dir |
---|
| 59 | # put_file_to_ecserver |
---|
| 60 | # submit_job_to_ecserver |
---|
| 61 | # get_informations |
---|
| 62 | # get_dimensions |
---|
| 63 | # execute_subprocess |
---|
[991df6a] | 64 | #******************************************************************************* |
---|
[6f951ca] | 65 | '''This module contains a collection of diverse tasks within flex_extract. |
---|
| 66 | ''' |
---|
[efdb01a] | 67 | |
---|
| 68 | # ------------------------------------------------------------------------------ |
---|
| 69 | # MODULES |
---|
| 70 | # ------------------------------------------------------------------------------ |
---|
[6470422] | 71 | from __future__ import print_function |
---|
| 72 | |
---|
[efdb01a] | 73 | import os |
---|
| 74 | import errno |
---|
| 75 | import sys |
---|
| 76 | import glob |
---|
[ff99eae] | 77 | import subprocess |
---|
[991df6a] | 78 | import traceback |
---|
[0f89116] | 79 | # pylint: disable=unused-import |
---|
[f20af73] | 80 | try: |
---|
| 81 | import exceptions |
---|
| 82 | except ImportError: |
---|
| 83 | import builtins as exceptions |
---|
[0f89116] | 84 | # pylint: enable=unused-import |
---|
[d727af2] | 85 | from datetime import datetime, timedelta |
---|
[991df6a] | 86 | from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter |
---|
[efdb01a] | 87 | |
---|
| 88 | # ------------------------------------------------------------------------------ |
---|
[6f951ca] | 89 | # METHODS |
---|
[efdb01a] | 90 | # ------------------------------------------------------------------------------ |
---|
| 91 | |
---|
[f20af73] | 92 | def setup_controldata(): |
---|
| 93 | '''Collects, stores and checks controlling arguments from command line, |
---|
| 94 | CONTROL file and ECMWF_ENV file. |
---|
| 95 | |
---|
| 96 | Parameters |
---|
| 97 | ---------- |
---|
| 98 | |
---|
| 99 | Return |
---|
| 100 | ------ |
---|
| 101 | c : ControlFile |
---|
| 102 | Contains all the parameters of CONTROL file and |
---|
| 103 | command line. |
---|
| 104 | |
---|
| 105 | ppid : str |
---|
| 106 | Parent process id. |
---|
| 107 | |
---|
| 108 | queue : str |
---|
| 109 | Name of queue for submission to ECMWF (e.g. ecgate or cca ) |
---|
| 110 | |
---|
| 111 | job_template : str |
---|
| 112 | Name of the job template file for submission to ECMWF server. |
---|
| 113 | ''' |
---|
| 114 | import _config |
---|
[ba99230] | 115 | from Classes.ControlFile import ControlFile |
---|
[f20af73] | 116 | |
---|
| 117 | args = get_cmdline_args() |
---|
| 118 | c = ControlFile(args.controlfile) |
---|
| 119 | c.assign_args_to_control(args) |
---|
| 120 | if os.path.isfile(_config.PATH_ECMWF_ENV): |
---|
| 121 | env_parameter = read_ecenv(_config.PATH_ECMWF_ENV) |
---|
| 122 | c.assign_envs_to_control(env_parameter) |
---|
| 123 | c.check_conditions(args.queue) |
---|
| 124 | |
---|
| 125 | return c, args.ppid, args.queue, args.job_template |
---|
| 126 | |
---|
[ca867de] | 127 | def none_or_str(value): |
---|
[8209738] | 128 | '''Converts the input string into Pythons None type if it |
---|
| 129 | contains the string "None". |
---|
[274f9ef] | 130 | |
---|
| 131 | Parameters |
---|
| 132 | ---------- |
---|
[6f951ca] | 133 | value : str |
---|
[274f9ef] | 134 | String to be checked for the "None" word. |
---|
| 135 | |
---|
| 136 | Return |
---|
| 137 | ------ |
---|
| 138 | None or value: |
---|
| 139 | Return depends on the content of the input value. If it was "None", |
---|
[8209738] | 140 | then the Python type None is returned, otherwise the string itself. |
---|
[ca867de] | 141 | ''' |
---|
| 142 | if value == 'None': |
---|
| 143 | return None |
---|
| 144 | return value |
---|
| 145 | |
---|
| 146 | def none_or_int(value): |
---|
[8209738] | 147 | '''Converts the input string into Pythons None-type if it |
---|
| 148 | contains string "None"; otherwise it is converted to an integer value. |
---|
[274f9ef] | 149 | |
---|
| 150 | Parameters |
---|
| 151 | ---------- |
---|
[6f951ca] | 152 | value : str |
---|
[274f9ef] | 153 | String to be checked for the "None" word. |
---|
| 154 | |
---|
| 155 | Return |
---|
| 156 | ------ |
---|
| 157 | None or int(value): |
---|
| 158 | Return depends on the content of the input value. If it was "None", |
---|
| 159 | then the python type None is returned. Otherwise the string is |
---|
| 160 | converted into an integer value. |
---|
[ca867de] | 161 | ''' |
---|
| 162 | if value == 'None': |
---|
| 163 | return None |
---|
| 164 | return int(value) |
---|
| 165 | |
---|
[9aefaad] | 166 | def get_cmdline_args(): |
---|
[274f9ef] | 167 | '''Decomposes the command line arguments and assigns them to variables. |
---|
[8209738] | 168 | Apply default values for arguments not present. |
---|
[efdb01a] | 169 | |
---|
[274f9ef] | 170 | Parameters |
---|
| 171 | ---------- |
---|
[efdb01a] | 172 | |
---|
[274f9ef] | 173 | Return |
---|
| 174 | ------ |
---|
[6f951ca] | 175 | args : Namespace |
---|
[8209738] | 176 | Contains the command line arguments from the script / program call. |
---|
[efdb01a] | 177 | ''' |
---|
[54a8a01] | 178 | |
---|
[efdb01a] | 179 | parser = ArgumentParser(description='Retrieve FLEXPART input from \ |
---|
[54a8a01] | 180 | ECMWF MARS archive', |
---|
[efdb01a] | 181 | formatter_class=ArgumentDefaultsHelpFormatter) |
---|
| 182 | |
---|
[095dc73] | 183 | # control parameters that override control file values |
---|
[ca867de] | 184 | parser.add_argument("--start_date", dest="start_date", |
---|
| 185 | type=none_or_str, default=None, |
---|
[efdb01a] | 186 | help="start date YYYYMMDD") |
---|
[ca867de] | 187 | parser.add_argument("--end_date", dest="end_date", |
---|
| 188 | type=none_or_str, default=None, |
---|
[efdb01a] | 189 | help="end_date YYYYMMDD") |
---|
[ca867de] | 190 | parser.add_argument("--date_chunk", dest="date_chunk", |
---|
| 191 | type=none_or_int, default=None, |
---|
[efdb01a] | 192 | help="# of days to be retrieved at once") |
---|
[f2616a3] | 193 | parser.add_argument("--job_chunk", dest="job_chunk", |
---|
| 194 | type=none_or_int, default=None, |
---|
| 195 | help="# of days to be retrieved within a single job") |
---|
[ca867de] | 196 | parser.add_argument("--controlfile", dest="controlfile", |
---|
[6470422] | 197 | type=none_or_str, default='CONTROL_EA5', |
---|
| 198 | help="The file with all CONTROL parameters.") |
---|
[ca867de] | 199 | parser.add_argument("--basetime", dest="basetime", |
---|
| 200 | type=none_or_int, default=None, |
---|
[8209738] | 201 | help="base time such as 0 or 12 (for half day retrievals)") |
---|
[ca867de] | 202 | parser.add_argument("--step", dest="step", |
---|
| 203 | type=none_or_str, default=None, |
---|
[6470422] | 204 | help="Forecast steps such as 00/to/48") |
---|
[ca867de] | 205 | parser.add_argument("--levelist", dest="levelist", |
---|
| 206 | type=none_or_str, default=None, |
---|
[efdb01a] | 207 | help="Vertical levels to be retrieved, e.g. 30/to/60") |
---|
[ca867de] | 208 | parser.add_argument("--area", dest="area", |
---|
| 209 | type=none_or_str, default=None, |
---|
[8209738] | 210 | help="area, defined by north/west/south/east") |
---|
[efdb01a] | 211 | |
---|
[095dc73] | 212 | # some switches |
---|
| 213 | parser.add_argument("--debug", dest="debug", |
---|
| 214 | type=none_or_int, default=None, |
---|
[8209738] | 215 | help="debug mode - temporary files will be conserved") |
---|
[d4696e0] | 216 | parser.add_argument("--oper", dest="oper", |
---|
| 217 | type=none_or_int, default=None, |
---|
[8209738] | 218 | help='operational mode - prepares dates from ' |
---|
[6470422] | 219 | 'environment variables') |
---|
[095dc73] | 220 | parser.add_argument("--request", dest="request", |
---|
| 221 | type=none_or_int, default=None, |
---|
[8209738] | 222 | help="list all MARS requests in file mars_requests.dat") |
---|
[095dc73] | 223 | parser.add_argument("--public", dest="public", |
---|
| 224 | type=none_or_int, default=None, |
---|
[8209738] | 225 | help="public mode - retrieves public datasets") |
---|
[095dc73] | 226 | parser.add_argument("--rrint", dest="rrint", |
---|
| 227 | type=none_or_int, default=None, |
---|
[8209738] | 228 | help='Selection of old or new ' |
---|
| 229 | 'interpolation method for precipitation:\n' |
---|
[6470422] | 230 | ' 0 - old method\n' |
---|
| 231 | ' 1 - new method (additional subgrid points)') |
---|
[095dc73] | 232 | |
---|
| 233 | # set directories |
---|
[ca867de] | 234 | parser.add_argument("--inputdir", dest="inputdir", |
---|
| 235 | type=none_or_str, default=None, |
---|
[8209738] | 236 | help='Path to temporary directory for ' |
---|
| 237 | 'retrieved grib files and other processing files.') |
---|
[ca867de] | 238 | parser.add_argument("--outputdir", dest="outputdir", |
---|
| 239 | type=none_or_str, default=None, |
---|
[8209738] | 240 | help='Path to final directory where ' |
---|
| 241 | 'FLEXPART input files will be stored.') |
---|
[efdb01a] | 242 | |
---|
[ff99eae] | 243 | # this is only used by prepare_flexpart.py to rerun a postprocessing step |
---|
[ca867de] | 244 | parser.add_argument("--ppid", dest="ppid", |
---|
[786cfd6] | 245 | type=none_or_str, default=None, |
---|
[8209738] | 246 | help='This is the specify the parent process id of a ' |
---|
[6470422] | 247 | 'single flex_extract run to identify the files. ' |
---|
| 248 | 'It is the second number in the GRIB files.') |
---|
[efdb01a] | 249 | |
---|
| 250 | # arguments for job submission to ECMWF, only needed by submit.py |
---|
| 251 | parser.add_argument("--job_template", dest='job_template', |
---|
[ca867de] | 252 | type=none_or_str, default="job.temp", |
---|
[8209738] | 253 | help='Job template file. Will be used for submission ' |
---|
| 254 | 'to the batch system on the ECMWF server after ' |
---|
| 255 | 'modification.') |
---|
[ca867de] | 256 | parser.add_argument("--queue", dest="queue", |
---|
| 257 | type=none_or_str, default=None, |
---|
[8209738] | 258 | help='The name of the ECMWF server name where the' |
---|
[47be2684] | 259 | 'job script is to be submitted ' |
---|
[6470422] | 260 | '(e.g. ecgate | cca | ccb)') |
---|
[efdb01a] | 261 | |
---|
| 262 | args = parser.parse_args() |
---|
| 263 | |
---|
[54a8a01] | 264 | return args |
---|
[efdb01a] | 265 | |
---|
[96e1533] | 266 | def read_ecenv(filepath): |
---|
[274f9ef] | 267 | '''Reads the file into a dictionary where the key values are the parameter |
---|
| 268 | names. |
---|
| 269 | |
---|
| 270 | Parameters |
---|
| 271 | ---------- |
---|
[6f951ca] | 272 | filepath : str |
---|
[274f9ef] | 273 | Path to file where the ECMWF environment parameters are stored. |
---|
| 274 | |
---|
| 275 | Return |
---|
| 276 | ------ |
---|
[6f951ca] | 277 | envs : dict |
---|
[274f9ef] | 278 | Contains the environment parameter ecuid, ecgid, gateway |
---|
| 279 | and destination for ECMWF server environments. |
---|
[54a8a01] | 280 | ''' |
---|
[0f89116] | 281 | envs = {} |
---|
[96e1533] | 282 | try: |
---|
| 283 | with open(filepath, 'r') as f: |
---|
| 284 | for line in f: |
---|
| 285 | data = line.strip().split() |
---|
| 286 | envs[str(data[0])] = str(data[1]) |
---|
| 287 | except OSError as e: |
---|
| 288 | print('... ERROR CODE: ' + str(e.errno)) |
---|
| 289 | print('... ERROR MESSAGE:\n \t ' + str(e.strerror)) |
---|
[2fb99de] | 290 | |
---|
[96e1533] | 291 | sys.exit('\n... Error occured while trying to read ECMWF_ENV ' |
---|
[0f89116] | 292 | 'file: ' + str(filepath)) |
---|
[efdb01a] | 293 | |
---|
[54a8a01] | 294 | return envs |
---|
[efdb01a] | 295 | |
---|
[ff99eae] | 296 | def clean_up(c): |
---|
[96e1533] | 297 | '''Remove files from the intermediate directory (inputdir). |
---|
| 298 | |
---|
| 299 | It keeps the final FLEXPART input files if program runs without |
---|
[8209738] | 300 | ECMWF API and keywords "ectrans" or "ecstorage" are set to "1". |
---|
[274f9ef] | 301 | |
---|
| 302 | Parameters |
---|
| 303 | ---------- |
---|
[6f951ca] | 304 | c : ControlFile |
---|
[274f9ef] | 305 | Contains all the parameters of CONTROL file and |
---|
| 306 | command line. |
---|
| 307 | |
---|
| 308 | Return |
---|
| 309 | ------ |
---|
| 310 | |
---|
[efdb01a] | 311 | ''' |
---|
| 312 | |
---|
[96e1533] | 313 | print("... clean inputdir!") |
---|
[efdb01a] | 314 | |
---|
[0f89116] | 315 | cleanlist = [filename for filename in |
---|
| 316 | glob.glob(os.path.join(c.inputdir, "*")) |
---|
| 317 | if not os.path.basename(filename).startswith(c.prefix)] |
---|
[efdb01a] | 318 | |
---|
[96e1533] | 319 | if cleanlist: |
---|
| 320 | for element in cleanlist: |
---|
[f20af73] | 321 | silent_remove(element) |
---|
[96e1533] | 322 | print("... done!") |
---|
| 323 | else: |
---|
| 324 | print("... nothing to clean!") |
---|
[efdb01a] | 325 | |
---|
| 326 | return |
---|
| 327 | |
---|
| 328 | |
---|
[f20af73] | 329 | def my_error(message='ERROR'): |
---|
[274f9ef] | 330 | '''Prints a specified error message which can be passed to the function |
---|
| 331 | before exiting the program. |
---|
| 332 | |
---|
| 333 | Parameters |
---|
| 334 | ---------- |
---|
[6f951ca] | 335 | message : str, optional |
---|
[274f9ef] | 336 | Error message. Default value is "ERROR". |
---|
[efdb01a] | 337 | |
---|
[274f9ef] | 338 | Return |
---|
| 339 | ------ |
---|
[efdb01a] | 340 | |
---|
| 341 | ''' |
---|
[ff99eae] | 342 | |
---|
[96e1533] | 343 | trace = '\n'.join(traceback.format_stack()) |
---|
| 344 | full_message = message + '\n\n' + trace |
---|
[ff99eae] | 345 | |
---|
[96e1533] | 346 | print(full_message) |
---|
[54a8a01] | 347 | |
---|
| 348 | sys.exit(1) |
---|
[efdb01a] | 349 | |
---|
| 350 | return |
---|
| 351 | |
---|
| 352 | |
---|
[96e1533] | 353 | def send_mail(users, success_mode, message): |
---|
[274f9ef] | 354 | '''Prints a specific exit message which can be passed to the function. |
---|
[efdb01a] | 355 | |
---|
[274f9ef] | 356 | Parameters |
---|
| 357 | ---------- |
---|
[6f951ca] | 358 | users : list of str |
---|
[274f9ef] | 359 | Contains all email addresses which should be notified. |
---|
| 360 | It might also contain just the ecmwf user name which wil trigger |
---|
| 361 | mailing to the associated email address for this user. |
---|
[efdb01a] | 362 | |
---|
[6f951ca] | 363 | success_mode : str |
---|
[96e1533] | 364 | States the exit mode of the program to put into |
---|
| 365 | the mail subject line. |
---|
| 366 | |
---|
[6f951ca] | 367 | message : str, optional |
---|
[274f9ef] | 368 | Message for exiting program. Default value is "Done!". |
---|
[efdb01a] | 369 | |
---|
[274f9ef] | 370 | Return |
---|
| 371 | ------ |
---|
[efdb01a] | 372 | |
---|
| 373 | ''' |
---|
[ff99eae] | 374 | |
---|
[54a8a01] | 375 | for user in users: |
---|
| 376 | if '${USER}' in user: |
---|
| 377 | user = os.getenv('USER') |
---|
| 378 | try: |
---|
[96e1533] | 379 | p = subprocess.Popen(['mail', '-s flex_extract_v7.1 ' + |
---|
| 380 | success_mode, os.path.expandvars(user)], |
---|
[ff99eae] | 381 | stdin=subprocess.PIPE, |
---|
| 382 | stdout=subprocess.PIPE, |
---|
| 383 | stderr=subprocess.PIPE, |
---|
| 384 | bufsize=1) |
---|
[0f89116] | 385 | pout = p.communicate(input=message + '\n\n')[0] |
---|
[54a8a01] | 386 | except ValueError as e: |
---|
[96e1533] | 387 | print('... ERROR: ' + str(e)) |
---|
| 388 | sys.exit('... Email could not be sent!') |
---|
| 389 | except OSError as e: |
---|
| 390 | print('... ERROR CODE: ' + str(e.errno)) |
---|
| 391 | print('... ERROR MESSAGE:\n \t ' + str(e.strerror)) |
---|
| 392 | sys.exit('... Email could not be sent!') |
---|
[54a8a01] | 393 | else: |
---|
[96e1533] | 394 | print('Email sent to ' + os.path.expandvars(user)) |
---|
| 395 | |
---|
| 396 | return |
---|
| 397 | |
---|
| 398 | |
---|
| 399 | def normal_exit(message='Done!'): |
---|
| 400 | '''Prints a specific exit message which can be passed to the function. |
---|
| 401 | |
---|
| 402 | Parameters |
---|
| 403 | ---------- |
---|
[6f951ca] | 404 | message : str, optional |
---|
[96e1533] | 405 | Message for exiting program. Default value is "Done!". |
---|
| 406 | |
---|
| 407 | Return |
---|
| 408 | ------ |
---|
| 409 | |
---|
| 410 | ''' |
---|
| 411 | |
---|
| 412 | print(str(message)) |
---|
[efdb01a] | 413 | |
---|
| 414 | return |
---|
| 415 | |
---|
| 416 | |
---|
| 417 | def product(*args, **kwds): |
---|
[96e1533] | 418 | '''Creates combinations of all passed arguments. |
---|
| 419 | |
---|
| 420 | This method combines the single characters of the passed arguments |
---|
[8209738] | 421 | with each other in a way that each character of each argument value |
---|
[274f9ef] | 422 | will be combined with each character of the other arguments as a tuple. |
---|
| 423 | |
---|
| 424 | Note |
---|
| 425 | ---- |
---|
| 426 | This method is taken from an example at the ECMWF wiki website. |
---|
| 427 | https://software.ecmwf.int/wiki/display/GRIB/index.py; 2018-03-16 |
---|
| 428 | |
---|
[d1bfa24] | 429 | It was released under the following license: |
---|
| 430 | https://confluence.ecmwf.int/display/ECC/License |
---|
| 431 | |
---|
[274f9ef] | 432 | Example |
---|
| 433 | ------- |
---|
| 434 | product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy |
---|
| 435 | |
---|
| 436 | product(range(2), repeat = 3) --> 000 001 010 011 100 101 110 111 |
---|
| 437 | |
---|
| 438 | Parameters |
---|
| 439 | ---------- |
---|
[6f951ca] | 440 | \*args : list or str |
---|
[274f9ef] | 441 | Positional arguments (arbitrary number). |
---|
| 442 | |
---|
[6f951ca] | 443 | \*\*kwds : dict |
---|
[274f9ef] | 444 | Contains all the keyword arguments from \*args. |
---|
| 445 | |
---|
| 446 | Return |
---|
| 447 | ------ |
---|
| 448 | prod : :obj:`tuple` |
---|
| 449 | Return will be done with "yield". A tuple of combined arguments. |
---|
| 450 | See example in description above. |
---|
[efdb01a] | 451 | ''' |
---|
[96e1533] | 452 | try: |
---|
[79729d5] | 453 | pools = [tuple(arg) for arg in args] * kwds.get('repeat', 1) |
---|
[96e1533] | 454 | result = [[]] |
---|
| 455 | for pool in pools: |
---|
| 456 | result = [x + [y] for x in result for y in pool] |
---|
| 457 | for prod in result: |
---|
| 458 | yield tuple(prod) |
---|
| 459 | except TypeError as e: |
---|
| 460 | sys.exit('... PRODUCT GENERATION FAILED!') |
---|
[efdb01a] | 461 | |
---|
| 462 | return |
---|
| 463 | |
---|
| 464 | |
---|
[ff99eae] | 465 | def silent_remove(filename): |
---|
[274f9ef] | 466 | '''Remove file if it exists. |
---|
| 467 | The function does not fail if the file does not exist. |
---|
| 468 | |
---|
| 469 | Parameters |
---|
| 470 | ---------- |
---|
[6f951ca] | 471 | filename : str |
---|
[274f9ef] | 472 | The name of the file to be removed without notification. |
---|
[efdb01a] | 473 | |
---|
[274f9ef] | 474 | Return |
---|
| 475 | ------ |
---|
[efdb01a] | 476 | |
---|
| 477 | ''' |
---|
| 478 | try: |
---|
| 479 | os.remove(filename) |
---|
| 480 | except OSError as e: |
---|
[96e1533] | 481 | # errno.ENOENT = no such file or directory |
---|
| 482 | if e.errno == errno.ENOENT: |
---|
| 483 | pass |
---|
| 484 | else: |
---|
[efdb01a] | 485 | raise # re-raise exception if a different error occured |
---|
| 486 | |
---|
| 487 | return |
---|
| 488 | |
---|
| 489 | |
---|
[ff99eae] | 490 | def init128(filepath): |
---|
[274f9ef] | 491 | '''Opens and reads the grib file with table 128 information. |
---|
| 492 | |
---|
| 493 | Parameters |
---|
| 494 | ---------- |
---|
[6f951ca] | 495 | filepath : str |
---|
[274f9ef] | 496 | Path to file of ECMWF grib table number 128. |
---|
| 497 | |
---|
| 498 | Return |
---|
| 499 | ------ |
---|
[6f951ca] | 500 | table128 : dict |
---|
[274f9ef] | 501 | Contains the ECMWF grib table 128 information. |
---|
| 502 | The key is the parameter number and the value is the |
---|
| 503 | short name of the parameter. |
---|
[efdb01a] | 504 | ''' |
---|
| 505 | table128 = dict() |
---|
[96e1533] | 506 | try: |
---|
| 507 | with open(filepath) as f: |
---|
| 508 | fdata = f.read().split('\n') |
---|
| 509 | except OSError as e: |
---|
| 510 | print('... ERROR CODE: ' + str(e.errno)) |
---|
| 511 | print('... ERROR MESSAGE:\n \t ' + str(e.strerror)) |
---|
| 512 | |
---|
| 513 | sys.exit('\n... Error occured while trying to read parameter ' |
---|
| 514 | 'table file: ' + str(filepath)) |
---|
| 515 | else: |
---|
| 516 | for data in fdata: |
---|
[f20af73] | 517 | if data != '' and data[0] != '!': |
---|
| 518 | table128[data[0:3]] = data[59:65].strip() |
---|
[efdb01a] | 519 | |
---|
| 520 | return table128 |
---|
| 521 | |
---|
| 522 | |
---|
[ff99eae] | 523 | def to_param_id(pars, table): |
---|
[274f9ef] | 524 | '''Transform parameter names to parameter ids with ECMWF grib table 128. |
---|
| 525 | |
---|
| 526 | Parameters |
---|
| 527 | ---------- |
---|
[6f951ca] | 528 | pars : str |
---|
[274f9ef] | 529 | Addpar argument from CONTROL file in the format of |
---|
[8209738] | 530 | parameter names instead of IDs. The parameter short |
---|
| 531 | names are separated by "/" and passed as one single string. |
---|
[274f9ef] | 532 | |
---|
[6f951ca] | 533 | table : dict |
---|
[274f9ef] | 534 | Contains the ECMWF grib table 128 information. |
---|
| 535 | The key is the parameter number and the value is the |
---|
| 536 | short name of the parameter. |
---|
| 537 | |
---|
| 538 | Return |
---|
| 539 | ------ |
---|
[6f951ca] | 540 | ipar : list of int |
---|
[274f9ef] | 541 | List of addpar parameters from CONTROL file transformed to |
---|
| 542 | parameter ids in the format of integer. |
---|
[efdb01a] | 543 | ''' |
---|
[96e1533] | 544 | if not pars: |
---|
| 545 | return [] |
---|
| 546 | if not isinstance(pars, str): |
---|
[0f89116] | 547 | pars = str(pars) |
---|
[96e1533] | 548 | |
---|
[efdb01a] | 549 | cpar = pars.upper().split('/') |
---|
| 550 | ipar = [] |
---|
| 551 | for par in cpar: |
---|
[6470422] | 552 | par = par.strip() |
---|
[79729d5] | 553 | for k, v in table.items(): |
---|
[6470422] | 554 | if par.isdigit(): |
---|
| 555 | par = str(int(par)).zfill(3) |
---|
[efdb01a] | 556 | if par == k or par == v: |
---|
| 557 | ipar.append(int(k)) |
---|
| 558 | break |
---|
[ff99eae] | 559 | else: |
---|
[2fb99de] | 560 | print('Warning: par ' + par + ' not found in table 128') |
---|
[efdb01a] | 561 | |
---|
| 562 | return ipar |
---|
| 563 | |
---|
[f20af73] | 564 | def to_param_id_with_tablenumber(pars, table): |
---|
[8209738] | 565 | '''Transform parameter names to parameter IDs and add table ID. |
---|
[f20af73] | 566 | |
---|
| 567 | Conversion with ECMWF grib table 128. |
---|
| 568 | |
---|
| 569 | Parameters |
---|
| 570 | ---------- |
---|
| 571 | pars : str |
---|
| 572 | Addpar argument from CONTROL file in the format of |
---|
[8209738] | 573 | parameter names instead of ID. The parameter short |
---|
| 574 | names are separated by "/" and passed as one single string. |
---|
[f20af73] | 575 | |
---|
| 576 | table : dict |
---|
| 577 | Contains the ECMWF grib table 128 information. |
---|
| 578 | The key is the parameter number and the value is the |
---|
| 579 | short name of the parameter. |
---|
| 580 | |
---|
| 581 | Return |
---|
| 582 | ------ |
---|
| 583 | spar : str |
---|
| 584 | List of addpar parameters from CONTROL file transformed to |
---|
[8209738] | 585 | parameter IDs in the format of integer. |
---|
[f20af73] | 586 | ''' |
---|
| 587 | if not pars: |
---|
| 588 | return [] |
---|
| 589 | if not isinstance(pars, str): |
---|
[0f89116] | 590 | pars = str(pars) |
---|
[f20af73] | 591 | |
---|
| 592 | cpar = pars.upper().split('/') |
---|
| 593 | spar = [] |
---|
| 594 | for par in cpar: |
---|
[79729d5] | 595 | for k, v in table.items(): |
---|
[6470422] | 596 | if par.isdigit(): |
---|
| 597 | par = str(int(par)).zfill(3) |
---|
[f20af73] | 598 | if par == k or par == v: |
---|
| 599 | spar.append(k + '.128') |
---|
| 600 | break |
---|
| 601 | else: |
---|
| 602 | print('\n\n\t\tWarning: par ' + par + ' not found in table 128\n\n') |
---|
| 603 | |
---|
| 604 | return '/'.join(spar) |
---|
| 605 | |
---|
[ff99eae] | 606 | def get_list_as_string(list_obj, concatenate_sign=', '): |
---|
[8209738] | 607 | '''Converts a list of arbitrary content into a single string using a given |
---|
| 608 | concatenation character. |
---|
[991df6a] | 609 | |
---|
[274f9ef] | 610 | Parameters |
---|
| 611 | ---------- |
---|
[6f951ca] | 612 | list_obj : list of * |
---|
[274f9ef] | 613 | A list with arbitrary content. |
---|
[991df6a] | 614 | |
---|
[6f951ca] | 615 | concatenate_sign : str, optional |
---|
[274f9ef] | 616 | A string which is used to concatenate the single |
---|
| 617 | list elements. Default value is ", ". |
---|
[ff99eae] | 618 | |
---|
[274f9ef] | 619 | Return |
---|
| 620 | ------ |
---|
[6f951ca] | 621 | str_of_list : str |
---|
[274f9ef] | 622 | The content of the list as a single string. |
---|
[efdb01a] | 623 | ''' |
---|
[991df6a] | 624 | |
---|
[96e1533] | 625 | if not isinstance(list_obj, list): |
---|
| 626 | list_obj = list(list_obj) |
---|
[ff99eae] | 627 | str_of_list = concatenate_sign.join(str(l) for l in list_obj) |
---|
[991df6a] | 628 | |
---|
[ff99eae] | 629 | return str_of_list |
---|
[54a8a01] | 630 | |
---|
| 631 | def make_dir(directory): |
---|
[96e1533] | 632 | '''Creates a directory. |
---|
| 633 | |
---|
[47be2684] | 634 | If the directory already exists, an information is printed and the creation |
---|
[8209738] | 635 | skipped. The program stops only if there is another problem. |
---|
[54a8a01] | 636 | |
---|
[274f9ef] | 637 | Parameters |
---|
| 638 | ---------- |
---|
[6f951ca] | 639 | directory : str |
---|
[96e1533] | 640 | The path to directory which should be created. |
---|
[274f9ef] | 641 | |
---|
| 642 | Return |
---|
| 643 | ------ |
---|
[54a8a01] | 644 | |
---|
| 645 | ''' |
---|
| 646 | try: |
---|
| 647 | os.makedirs(directory) |
---|
| 648 | except OSError as e: |
---|
[96e1533] | 649 | # errno.EEXIST = directory already exists |
---|
| 650 | if e.errno == errno.EEXIST: |
---|
[8209738] | 651 | print('INFORMATION: Directory {0} already exists!'.format(directory)) |
---|
[96e1533] | 652 | else: |
---|
| 653 | raise # re-raise exception if a different error occured |
---|
[54a8a01] | 654 | |
---|
| 655 | return |
---|
| 656 | |
---|
[b6ea29e] | 657 | def put_file_to_ecserver(ecd, filename): |
---|
[274f9ef] | 658 | '''Uses the ecaccess-file-put command to send a file to the ECMWF servers. |
---|
[97e09f4] | 659 | |
---|
[274f9ef] | 660 | Note |
---|
| 661 | ---- |
---|
| 662 | The return value is just for testing reasons. It does not have |
---|
| 663 | to be used from the calling function since the whole error handling |
---|
| 664 | is done in here. |
---|
[54a8a01] | 665 | |
---|
[274f9ef] | 666 | Parameters |
---|
| 667 | ---------- |
---|
[6f951ca] | 668 | ecd : str |
---|
[274f9ef] | 669 | The path were the file is stored. |
---|
[54a8a01] | 670 | |
---|
[6f951ca] | 671 | filename : str |
---|
[274f9ef] | 672 | The name of the file to send to the ECMWF server. |
---|
[54a8a01] | 673 | |
---|
[274f9ef] | 674 | Return |
---|
| 675 | ------ |
---|
[96e1533] | 676 | |
---|
[54a8a01] | 677 | ''' |
---|
| 678 | |
---|
| 679 | try: |
---|
[96e1533] | 680 | subprocess.check_output(['ecaccess-file-put', |
---|
[b6ea29e] | 681 | ecd + '/' + filename], |
---|
[96e1533] | 682 | stderr=subprocess.STDOUT) |
---|
[54a8a01] | 683 | except subprocess.CalledProcessError as e: |
---|
[96e1533] | 684 | print('... ERROR CODE: ' + str(e.returncode)) |
---|
| 685 | print('... ERROR MESSAGE:\n \t ' + str(e)) |
---|
[54a8a01] | 686 | |
---|
[2fb99de] | 687 | print('\n... Do you have a valid ecaccess certification key?') |
---|
[97e09f4] | 688 | sys.exit('... ECACCESS-FILE-PUT FAILED!') |
---|
[96e1533] | 689 | except OSError as e: |
---|
| 690 | print('... ERROR CODE: ' + str(e.errno)) |
---|
| 691 | print('... ERROR MESSAGE:\n \t ' + str(e.strerror)) |
---|
| 692 | |
---|
| 693 | print('\n... Most likely the ECACCESS library is not available!') |
---|
| 694 | sys.exit('... ECACCESS-FILE-PUT FAILED!') |
---|
[97e09f4] | 695 | |
---|
[96e1533] | 696 | return |
---|
[54a8a01] | 697 | |
---|
[b1d07c9] | 698 | def submit_job_to_ecserver(target, jobname): |
---|
[274f9ef] | 699 | '''Uses ecaccess-job-submit command to submit a job to the ECMWF server. |
---|
| 700 | |
---|
| 701 | Note |
---|
| 702 | ---- |
---|
| 703 | The return value is just for testing reasons. It does not have |
---|
| 704 | to be used from the calling function since the whole error handling |
---|
| 705 | is done in here. |
---|
| 706 | |
---|
| 707 | Parameters |
---|
| 708 | ---------- |
---|
[6f951ca] | 709 | target : str |
---|
[274f9ef] | 710 | The target where the file should be sent to, e.g. the queue. |
---|
| 711 | |
---|
[6f951ca] | 712 | jobname : str |
---|
[274f9ef] | 713 | The name of the jobfile to be submitted to the ECMWF server. |
---|
| 714 | |
---|
| 715 | Return |
---|
| 716 | ------ |
---|
[6f951ca] | 717 | job_id : int |
---|
[8209738] | 718 | The id number of the job as a reference at the ECMWF server. |
---|
[54a8a01] | 719 | ''' |
---|
| 720 | |
---|
| 721 | try: |
---|
[96e1533] | 722 | job_id = subprocess.check_output(['ecaccess-job-submit', '-queueName', |
---|
| 723 | target, jobname]) |
---|
[2fb99de] | 724 | |
---|
[96e1533] | 725 | except subprocess.CalledProcessError as e: |
---|
| 726 | print('... ERROR CODE: ' + str(e.returncode)) |
---|
| 727 | print('... ERROR MESSAGE:\n \t ' + str(e)) |
---|
[97e09f4] | 728 | |
---|
[2fb99de] | 729 | print('\n... Do you have a valid ecaccess certification key?') |
---|
[8209738] | 730 | sys.exit('... ecaccess-job-submit FAILED!') |
---|
[96e1533] | 731 | except OSError as e: |
---|
| 732 | print('... ERROR CODE: ' + str(e.errno)) |
---|
| 733 | print('... ERROR MESSAGE:\n \t ' + str(e.strerror)) |
---|
| 734 | |
---|
| 735 | print('\n... Most likely the ECACCESS library is not available!') |
---|
[8209738] | 736 | sys.exit('... ecaccess-job-submit FAILED!') |
---|
[54a8a01] | 737 | |
---|
[79729d5] | 738 | return job_id.decode() |
---|
[095dc73] | 739 | |
---|
| 740 | |
---|
| 741 | def get_informations(filename): |
---|
[8209738] | 742 | '''Extracts basic information from a sample grib file. |
---|
[095dc73] | 743 | |
---|
[8209738] | 744 | This information is needed for later use and the |
---|
| 745 | initialization of numpy arrays where data are stored. |
---|
[095dc73] | 746 | |
---|
| 747 | Parameters |
---|
| 748 | ---------- |
---|
[6f951ca] | 749 | filename : str |
---|
[095dc73] | 750 | Name of the file which will be opened to extract basic information. |
---|
| 751 | |
---|
| 752 | Return |
---|
| 753 | ------ |
---|
[6f951ca] | 754 | data : dict |
---|
[095dc73] | 755 | Contains basic informations of the ECMWF grib files, e.g. |
---|
| 756 | 'Ni', 'Nj', 'latitudeOfFirstGridPointInDegrees', |
---|
| 757 | 'longitudeOfFirstGridPointInDegrees', 'latitudeOfLastGridPointInDegrees', |
---|
| 758 | 'longitudeOfLastGridPointInDegrees', 'jDirectionIncrementInDegrees', |
---|
| 759 | 'iDirectionIncrementInDegrees', 'missingValue' |
---|
| 760 | ''' |
---|
[2fef1f2] | 761 | from eccodes import codes_grib_new_from_file, codes_get, codes_release |
---|
[095dc73] | 762 | |
---|
| 763 | data = {} |
---|
| 764 | |
---|
| 765 | # --- open file --- |
---|
[8209738] | 766 | print("Opening grib file for extraction of information --- %s" % filename) |
---|
[c77630a] | 767 | with open(filename, 'rb') as f: |
---|
[095dc73] | 768 | # load first message from file |
---|
| 769 | gid = codes_grib_new_from_file(f) |
---|
| 770 | |
---|
| 771 | # information needed from grib message |
---|
[0f89116] | 772 | keys = ['Ni', |
---|
[095dc73] | 773 | 'Nj', |
---|
| 774 | 'latitudeOfFirstGridPointInDegrees', |
---|
| 775 | 'longitudeOfFirstGridPointInDegrees', |
---|
| 776 | 'latitudeOfLastGridPointInDegrees', |
---|
| 777 | 'longitudeOfLastGridPointInDegrees', |
---|
| 778 | 'jDirectionIncrementInDegrees', |
---|
| 779 | 'iDirectionIncrementInDegrees', |
---|
| 780 | 'missingValue', |
---|
| 781 | ] |
---|
| 782 | |
---|
[8209738] | 783 | print('\nInformation extracted: ') |
---|
[095dc73] | 784 | for key in keys: |
---|
| 785 | # Get the value of the key in a grib message. |
---|
[0f89116] | 786 | data[key] = codes_get(gid, key) |
---|
| 787 | print("%s = %s" % (key, data[key])) |
---|
[095dc73] | 788 | |
---|
| 789 | # Free the memory for the message referred as gribid. |
---|
| 790 | codes_release(gid) |
---|
| 791 | |
---|
| 792 | return data |
---|
| 793 | |
---|
| 794 | |
---|
[2fef1f2] | 795 | def get_dimensions(info, purefc, dtime, index_vals, start_date, end_date): |
---|
[8209738] | 796 | '''This function specifies the correct dimensions for x, y, and t. |
---|
[095dc73] | 797 | |
---|
| 798 | Parameters |
---|
| 799 | ---------- |
---|
[6f951ca] | 800 | info : dict |
---|
[095dc73] | 801 | Contains basic informations of the ECMWF grib files, e.g. |
---|
| 802 | 'Ni', 'Nj', 'latitudeOfFirstGridPointInDegrees', |
---|
| 803 | 'longitudeOfFirstGridPointInDegrees', 'latitudeOfLastGridPointInDegrees', |
---|
| 804 | 'longitudeOfLastGridPointInDegrees', 'jDirectionIncrementInDegrees', |
---|
| 805 | 'iDirectionIncrementInDegrees', 'missingValue' |
---|
| 806 | |
---|
[6f951ca] | 807 | purefc : int |
---|
[2fef1f2] | 808 | Switch for definition of pure forecast mode or not. |
---|
| 809 | |
---|
[6f951ca] | 810 | dtime : str |
---|
[2fef1f2] | 811 | Time step in hours. |
---|
| 812 | |
---|
[6f951ca] | 813 | index_vals : list of list of str |
---|
[2fef1f2] | 814 | Contains the values from the keys used for a distinct selection |
---|
[8209738] | 815 | of GRIB messages in processing the grib files. |
---|
[2fef1f2] | 816 | Content looks like e.g.: |
---|
| 817 | index_vals[0]: ('20171106', '20171107', '20171108') ; date |
---|
| 818 | index_vals[1]: ('0', '1200', '1800', '600') ; time |
---|
| 819 | index_vals[2]: ('0', '12', '3', '6', '9') ; stepRange |
---|
| 820 | |
---|
[6f951ca] | 821 | start_date : str |
---|
[2fef1f2] | 822 | The start date of the retrieval job. |
---|
| 823 | |
---|
[6f951ca] | 824 | end_date : str |
---|
[2fef1f2] | 825 | The end date of the retrieval job. |
---|
| 826 | |
---|
[095dc73] | 827 | Return |
---|
| 828 | ------ |
---|
[6f951ca] | 829 | (ix, jy, it) : tuple of int |
---|
[095dc73] | 830 | Dimension in x-direction, y-direction and in time. |
---|
| 831 | ''' |
---|
| 832 | |
---|
| 833 | ix = info['Ni'] |
---|
| 834 | |
---|
| 835 | jy = info['Nj'] |
---|
| 836 | |
---|
[2fef1f2] | 837 | if not purefc: |
---|
[79729d5] | 838 | it = ((end_date - start_date).days + 1) * 24 // int(dtime) |
---|
[2fef1f2] | 839 | else: |
---|
| 840 | # #no of step * #no of times * #no of days |
---|
| 841 | it = len(index_vals[2]) * len(index_vals[1]) * len(index_vals[0]) |
---|
[095dc73] | 842 | |
---|
| 843 | return (ix, jy, it) |
---|
[bf48c8a] | 844 | |
---|
| 845 | |
---|
| 846 | def execute_subprocess(cmd_list, error_msg='SUBPROCESS FAILED!'): |
---|
[8209738] | 847 | '''Executes a command via a subprocess. |
---|
[bf48c8a] | 848 | |
---|
| 849 | Error handling is done if an error occures. |
---|
| 850 | |
---|
| 851 | Parameters |
---|
| 852 | ---------- |
---|
[6f951ca] | 853 | cmd_list : list of str |
---|
[47be2684] | 854 | A list of the components for the command line execution. |
---|
| 855 | They will be concatenated with blank space for the command |
---|
[8209738] | 856 | to be submitted, like ['mv', file1, file2] for mv file1 file2. |
---|
[bf48c8a] | 857 | |
---|
| 858 | Return |
---|
| 859 | ------ |
---|
[6f951ca] | 860 | error_msg : str, optional |
---|
[8209738] | 861 | Error message if the subprocess fails. |
---|
| 862 | By default it will just say "SUBPROCESS FAILED!". |
---|
[bf48c8a] | 863 | ''' |
---|
| 864 | |
---|
| 865 | try: |
---|
| 866 | subprocess.check_call(cmd_list) |
---|
| 867 | except subprocess.CalledProcessError as e: |
---|
| 868 | print('... ERROR CODE: ' + str(e.returncode)) |
---|
| 869 | print('... ERROR MESSAGE:\n \t ' + str(e)) |
---|
| 870 | |
---|
| 871 | sys.exit('... ' + error_msg) |
---|
| 872 | except OSError as e: |
---|
| 873 | print('... ERROR CODE: ' + str(e.errno)) |
---|
| 874 | print('... ERROR MESSAGE:\n \t ' + str(e.strerror)) |
---|
| 875 | |
---|
| 876 | sys.exit('... ' + error_msg) |
---|
| 877 | |
---|
| 878 | return |
---|
[d727af2] | 879 | |
---|
| 880 | |
---|
| 881 | def generate_retrieval_period_boundary(c): |
---|
| 882 | '''Generates retrieval period boundary datetimes from CONTROL information. |
---|
| 883 | |
---|
| 884 | Parameters |
---|
| 885 | ---------- |
---|
| 886 | c : ControlFile |
---|
| 887 | Contains all the parameters of CONTROL file and |
---|
| 888 | command line. |
---|
| 889 | |
---|
| 890 | Return |
---|
| 891 | ------ |
---|
| 892 | start_period : datetime |
---|
| 893 | The first timestamp of the actual retrieval period disregarding |
---|
| 894 | the temporary times which were used for processing reasons. |
---|
| 895 | |
---|
| 896 | end_period : datetime |
---|
| 897 | The last timestamp of the actual retrieval period disregarding |
---|
| 898 | the temporary times which were used for processing reasons. |
---|
| 899 | ''' |
---|
| 900 | # generate start and end timestamp of the retrieval period |
---|
| 901 | start_period = datetime.strptime(c.start_date + c.time[0], '%Y%m%d%H') |
---|
| 902 | start_period = start_period + timedelta(hours=int(c.step[0])) |
---|
| 903 | end_period = datetime.strptime(c.end_date + c.time[-1], '%Y%m%d%H') |
---|
| 904 | end_period = end_period + timedelta(hours=int(c.step[-1])) |
---|
| 905 | |
---|
| 906 | |
---|
| 907 | return start_period, end_period |
---|
[47be2684] | 908 | |
---|
| 909 | |
---|
| 910 | def check_for_string_in_file(filepath, search_string): |
---|
| 911 | """ |
---|
| 912 | Search for a specific string in a file and return True if |
---|
| 913 | the string was found. |
---|
| 914 | |
---|
| 915 | Parameters |
---|
| 916 | ---------- |
---|
| 917 | filepath : str |
---|
| 918 | The full file path which is to be examined. |
---|
| 919 | |
---|
| 920 | search_string : str |
---|
| 921 | The string which is looked up for in the file. |
---|
| 922 | |
---|
| 923 | Return |
---|
| 924 | ------ |
---|
| 925 | Boolean : |
---|
| 926 | True : String was found |
---|
| 927 | False : String was not found |
---|
| 928 | """ |
---|
| 929 | with open(filepath, 'r') as fio: |
---|
| 930 | for line in fio: |
---|
| 931 | if search_string in line: |
---|
| 932 | return True |
---|
| 933 | return False |
---|
| 934 | |
---|
| 935 | |
---|
| 936 | def overwrite_lines_in_file(filepath, search_string, sub_string): |
---|
| 937 | """ |
---|
| 938 | Overwrites lines which contain the given search string with the |
---|
| 939 | substitution string. |
---|
| 940 | |
---|
| 941 | Parameters |
---|
| 942 | ---------- |
---|
| 943 | search_string : str |
---|
| 944 | The string which is looked up for in the file. |
---|
| 945 | |
---|
| 946 | sub_string : str |
---|
| 947 | The string which overwrites the search string. |
---|
| 948 | |
---|
| 949 | Return |
---|
| 950 | ------ |
---|
| 951 | """ |
---|
| 952 | with open(filepath, 'r') as fio: |
---|
| 953 | data = fio.readlines() |
---|
| 954 | |
---|
| 955 | with open(filepath, 'w') as fio: |
---|
| 956 | for line in data: |
---|
| 957 | if search_string in line: |
---|
| 958 | fio.write(sub_string) |
---|
| 959 | else: |
---|
| 960 | fio.write(line) |
---|
| 961 | |
---|
| 962 | return |
---|
| 963 | |
---|