Changeset 991df6a in flex_extract.git for python/Tools.py
- Timestamp:
- May 14, 2018, 10:11:29 PM (6 years ago)
- Branches:
- master, ctbto, dev
- Children:
- 812283d
- Parents:
- efdb01a
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/Tools.py
refdb01a r991df6a 3 3 #************************************************************************ 4 4 # TODO AP 5 #AP 6 # - 5 # - check myerror 6 # - check normalexit 7 # - check getListAsString 8 # - seperate args and control interpretation 7 9 #************************************************************************ 8 """ 9 10 """ 10 #******************************************************************************* 11 # @Author: Anne Philipp (University of Vienna) 12 # 13 # @Date: May 2018 14 # 15 # @Change History: 16 # October 2014 - Anne Fouilloux (University of Oslo) 17 # - created functions silentremove and product (taken from ECMWF) 18 # 19 # November 2015 - Leopold Haimberger (University of Vienna) 20 # - created functions: interpret_args_and_control, cleanup 21 # myerror, normalexit, init128, toparamId 22 # 23 # April 2018 - Anne Philipp (University of Vienna): 24 # - applied PEP8 style guide 25 # - added documentation 26 # - moved all functions from file FlexpartTools to this file Tools 27 # - added function getListAsString 28 # 29 # @License: 30 # (C) Copyright 2014-2018. 31 # 32 # This software is licensed under the terms of the Apache Licence Version 2.0 33 # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. 34 # 35 # @Modul Description: 36 # This module contains a couple of helpful functions which are 37 # used in different places in flex_extract. 38 # 39 # @Module Content: 40 # - interpret_args_and_control 41 # - cleanup 42 # - myerror 43 # - normalexit 44 # - product 45 # - silentremove 46 # - init128 47 # - toparamId 48 # - getListAsString 49 # 50 #******************************************************************************* 51 11 52 # ------------------------------------------------------------------------------ 12 53 # MODULES 13 54 # ------------------------------------------------------------------------------ 14 from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter15 55 import os 16 56 import errno 17 57 import sys 18 58 import glob 59 import traceback 19 60 from numpy import * 20 61 from gribapi import * 21 import Control 62 from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter 63 64 # software specific class from flex_extract 65 from ControlFile import ControlFile 22 66 23 67 # ------------------------------------------------------------------------------ … … 28 72 ''' 29 73 @Description: 30 Assigns the command line arguments and reads controlfile74 Assigns the command line arguments and reads CONTROL file 31 75 content. Apply default values for non mentioned arguments. 32 76 … … 38 82 Contains the commandline arguments from script/program call. 39 83 40 c: instance of class Control 41 Contains all necessary information of a controlfile. The parameters84 c: instance of class ControlFile 85 Contains all necessary information of a CONTROL file. The parameters 42 86 are: DAY1, DAY2, DTIME, MAXSTEP, TYPE, TIME, STEP, CLASS, STREAM, 43 87 NUMBER, EXPVER, GRID, LEFT, LOWER, UPPER, RIGHT, LEVEL, LEVELIST, … … 62 106 help="# of days to be retrieved at once") 63 107 64 # some arguments that override the default in the controlfile108 # some arguments that override the default in the CONTROL file 65 109 parser.add_argument("--basetime", dest="basetime", 66 110 help="base such as 00/12 (for half day retrievals)") … … 96 140 parser.add_argument("--controlfile", dest="controlfile", 97 141 default='CONTROL.temp', 98 help="file with controlparameters")142 help="file with CONTROL parameters") 99 143 parser.add_argument("--debug", dest="debug", default=0, 100 144 help="Debug mode - leave temporary files intact") … … 102 146 args = parser.parse_args() 103 147 104 # create instance of Control for specified controlfile148 # create instance of ControlFile for specified controlfile 105 149 # and assign the parameters (and default values if necessary) 106 150 try: 107 c = Control .Control(args.controlfile)151 c = ControlFile(args.controlfile) 108 152 except IOError: 109 153 try: 110 c = Control .Control(localpythonpath + args.controlfile)154 c = ControlFile(localpythonpath + args.controlfile) 111 155 except: 112 print('Could not read controlfile "' + args.controlfile + '"')156 print('Could not read CONTROL file "' + args.controlfile + '"') 113 157 print('Either it does not exist or its syntax is wrong.') 114 158 print('Try "' + sys.argv[0].split('/')[-1] + … … 119 163 if args.start_date is None and getattr(c, 'start_date') is None: 120 164 print('start_date specified neither in command line nor \ 121 in controlfile ' + args.controlfile)165 in CONTROL file ' + args.controlfile) 122 166 print('Try "' + sys.argv[0].split('/')[-1] + 123 167 ' -h" to print usage information') 124 168 exit(1) 125 169 126 # save all existing command line parameter to the Control instance170 # save all existing command line parameter to the ControlFile instance 127 171 # if parameter is not specified through the command line or CONTROL file 128 172 # set default values … … 199 243 @Description: 200 244 Remove all files from intermediate directory 201 (inputdir from controlfile).202 203 @Input: 204 c: instance of class Control 205 Contains all the parameters of control files, which are e.g.:245 (inputdir from CONTROL file). 246 247 @Input: 248 c: instance of class ControlFile 249 Contains all the parameters of CONTROL file, which are e.g.: 206 250 DAY1(start_date), DAY2(end_date), DTIME, MAXSTEP, TYPE, TIME, 207 251 STEP, CLASS(marsclass), STREAM, NUMBER, EXPVER, GRID, LEFT, … … 240 284 241 285 @Input: 242 c: instance of class Control 243 Contains all the parameters of control files, which are e.g.:286 c: instance of class ControlFile 287 Contains all the parameters of CONTROL file, which are e.g.: 244 288 DAY1(start_date), DAY2(end_date), DTIME, MAXSTEP, TYPE, TIME, 245 289 STEP, CLASS(marsclass), STREAM, NUMBER, EXPVER, GRID, LEFT, … … 290 334 291 335 @Input: 292 c: instance of class Control 293 Contains all the parameters of control files, which are e.g.:336 c: instance of class ControlFile 337 Contains all the parameters of CONTROL file, which are e.g.: 294 338 DAY1(start_date), DAY2(end_date), DTIME, MAXSTEP, TYPE, TIME, 295 339 STEP, CLASS(marsclass), STREAM, NUMBER, EXPVER, GRID, LEFT, … … 357 401 See example in description above. 358 402 ''' 359 360 403 pools = map(tuple, args) * kwds.get('repeat', 1) 361 404 result = [[]] … … 371 414 ''' 372 415 @Description: 373 Removes the file which name is passed to the function if 374 it exists. The function does not fail if the file does not 375 exist. 416 If "filename" exists , it is removed. 417 The function does not fail if the file does not exist. 376 418 377 419 @Input: … … 426 468 @Input: 427 469 pars: string 428 Addpar argument from controlfile in the format of470 Addpar argument from CONTROL file in the format of 429 471 parameter names instead of ids. The parameter short 430 472 names are sepearted with "/" and they are passed as … … 438 480 @Return: 439 481 ipar: list of integer 440 List of addpar parameters from controlfile transformed to482 List of addpar parameters from CONTROL file transformed to 441 483 parameter ids in the format of integer. 442 484 ''' … … 455 497 return ipar 456 498 457 def getListAsString(listobj): 458 ''' 459 @Description: 460 ''' 461 return ", ".join( str(l) for l in listobj) 499 def getListAsString(listObj): 500 ''' 501 @Description: 502 Converts a list of arbitrary content into a single string. 503 504 @Input: 505 listObj: list 506 A list with arbitrary content. 507 508 @Return: 509 strOfList: string 510 The content of the list as a single string. 511 ''' 512 513 strOfList = ", ".join( str(l) for l in listObj) 514 515 return strOfList
Note: See TracChangeset
for help on using the changeset viewer.