[d69b677] | 1 | #!/usr/bin/env python |
---|
[02c8c50] | 2 | # -*- coding: utf-8 -*- |
---|
| 3 | #************************************************************************ |
---|
| 4 | # TODO AP |
---|
| 5 | # - wieso cleanup in main wenn es in prepareflexpart bereits abgefragt wurde? |
---|
| 6 | # doppelt gemoppelt? |
---|
| 7 | # - wieso start=startm1 wenn basetime = 0 ? wenn die fluxes nicht mehr |
---|
| 8 | # relevant sind? verstehe ich nicht |
---|
| 9 | #************************************************************************ |
---|
[991df6a] | 10 | #******************************************************************************* |
---|
| 11 | # @Author: Anne Fouilloux (University of Oslo) |
---|
| 12 | # |
---|
| 13 | # @Date: October 2014 |
---|
| 14 | # |
---|
| 15 | # @Change History: |
---|
| 16 | # |
---|
| 17 | # November 2015 - Leopold Haimberger (University of Vienna): |
---|
| 18 | # - using the WebAPI also for general MARS retrievals |
---|
| 19 | # - job submission on ecgate and cca |
---|
| 20 | # - job templates suitable for twice daily operational dissemination |
---|
| 21 | # - dividing retrievals of longer periods into digestable chunks |
---|
| 22 | # - retrieve also longer term forecasts, not only analyses and |
---|
| 23 | # short term forecast data |
---|
| 24 | # - conversion into GRIB2 |
---|
| 25 | # - conversion into .fp format for faster execution of FLEXPART |
---|
| 26 | # |
---|
| 27 | # February 2018 - Anne Philipp (University of Vienna): |
---|
| 28 | # - applied PEP8 style guide |
---|
| 29 | # - added documentation |
---|
| 30 | # - minor changes in programming style for consistence |
---|
| 31 | # - BUG: removed call of cleanup-Function after call of |
---|
| 32 | # prepareFlexpart in main since it is already called in |
---|
| 33 | # prepareFlexpart at the end! |
---|
| 34 | # - created function main and moved the two function calls for |
---|
| 35 | # arguments and prepareFLEXPART into it |
---|
| 36 | # |
---|
| 37 | # @License: |
---|
| 38 | # (C) Copyright 2014-2018. |
---|
| 39 | # |
---|
| 40 | # This software is licensed under the terms of the Apache Licence Version 2.0 |
---|
| 41 | # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
---|
| 42 | # |
---|
| 43 | # @Program Functionality: |
---|
| 44 | # This program prepares the final version of the grib files which are |
---|
| 45 | # then used by FLEXPART. It converts the bunch of grib files extracted |
---|
| 46 | # via getMARSdata by doing for example the necessary conversion to get |
---|
| 47 | # consistent grids or the disaggregation of flux data. Finally, the |
---|
| 48 | # program combines the data fields in files per available hour with the |
---|
| 49 | # naming convention xxYYMMDDHH, where xx should be 2 arbitrary letters |
---|
| 50 | # (mostly xx is chosen to be "EN"). |
---|
| 51 | # |
---|
| 52 | # @Program Content: |
---|
| 53 | # - main |
---|
| 54 | # - prepareFLEXPART |
---|
| 55 | # |
---|
| 56 | #******************************************************************************* |
---|
| 57 | |
---|
[efdb01a] | 58 | # ------------------------------------------------------------------------------ |
---|
| 59 | # MODULES |
---|
| 60 | # ------------------------------------------------------------------------------ |
---|
[d69b677] | 61 | import shutil |
---|
| 62 | import datetime |
---|
[991df6a] | 63 | #import time |
---|
[efdb01a] | 64 | import os |
---|
| 65 | import inspect |
---|
| 66 | import sys |
---|
[d69b677] | 67 | import socket |
---|
[02c8c50] | 68 | from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter |
---|
[d69b677] | 69 | |
---|
[efdb01a] | 70 | hostname = socket.gethostname() |
---|
| 71 | ecapi = 'ecmwf' not in hostname |
---|
[d69b677] | 72 | try: |
---|
| 73 | if ecapi: |
---|
| 74 | import ecmwfapi |
---|
| 75 | except ImportError: |
---|
[64cf353] | 76 | ecapi = False |
---|
| 77 | |
---|
[991df6a] | 78 | # add path to pythonpath so that python finds its buddies |
---|
| 79 | localpythonpath = os.path.dirname(os.path.abspath( |
---|
| 80 | inspect.getfile(inspect.currentframe()))) |
---|
[efdb01a] | 81 | if localpythonpath not in sys.path: |
---|
| 82 | sys.path.append(localpythonpath) |
---|
[991df6a] | 83 | |
---|
| 84 | # software specific classes and modules from flex_extract |
---|
| 85 | from UIOFiles import UIOFiles |
---|
| 86 | from Tools import interpret_args_and_control, cleanup |
---|
| 87 | from ECFlexpart import ECFlexpart |
---|
[efdb01a] | 88 | # ------------------------------------------------------------------------------ |
---|
| 89 | # FUNCTION |
---|
| 90 | # ------------------------------------------------------------------------------ |
---|
[991df6a] | 91 | def main(): |
---|
[02c8c50] | 92 | ''' |
---|
| 93 | @Description: |
---|
[991df6a] | 94 | If prepareFLEXPART is called from command line, this function controls |
---|
| 95 | the program flow and calls the argumentparser function and |
---|
| 96 | the prepareFLEXPART function for preparation of GRIB data for FLEXPART. |
---|
| 97 | |
---|
| 98 | @Input: |
---|
| 99 | <nothing> |
---|
[02c8c50] | 100 | |
---|
[991df6a] | 101 | @Return: |
---|
| 102 | <nothing> |
---|
| 103 | ''' |
---|
| 104 | args, c = interpret_args_and_control() |
---|
| 105 | prepareFLEXPART(args, c) |
---|
| 106 | |
---|
| 107 | return |
---|
| 108 | |
---|
| 109 | def prepareFLEXPART(args, c): |
---|
| 110 | ''' |
---|
| 111 | @Description: |
---|
| 112 | Lists all grib files retrieved from MARS with getMARSdata and |
---|
| 113 | uses prepares data for the use in FLEXPART. Specific data fields |
---|
| 114 | are converted to a different grid and the flux data are going to be |
---|
| 115 | disaggregated. The data fields are collected by hour and stored in |
---|
| 116 | a file with a specific FLEXPART relevant naming convention. |
---|
[d69b677] | 117 | |
---|
[02c8c50] | 118 | @Input: |
---|
| 119 | args: instance of ArgumentParser |
---|
| 120 | Contains the commandline arguments from script/program call. |
---|
[d69b677] | 121 | |
---|
[991df6a] | 122 | c: instance of class ControlFile |
---|
| 123 | Contains all the parameters of CONTROL file, which are e.g.: |
---|
[02c8c50] | 124 | DAY1(start_date), DAY2(end_date), DTIME, MAXSTEP, TYPE, TIME, |
---|
| 125 | STEP, CLASS(marsclass), STREAM, NUMBER, EXPVER, GRID, LEFT, |
---|
| 126 | LOWER, UPPER, RIGHT, LEVEL, LEVELIST, RESOL, GAUSS, ACCURACY, |
---|
| 127 | OMEGA, OMEGADIFF, ETA, ETADIFF, DPDETA, SMOOTH, FORMAT, |
---|
| 128 | ADDPAR, WRF, CWC, PREFIX, ECSTORAGE, ECTRANS, ECFSDIR, |
---|
| 129 | MAILOPS, MAILFAIL, GRIB2FLEXPART, FLEXPARTDIR, BASETIME |
---|
| 130 | DATE_CHUNK, DEBUG, INPUTDIR, OUTPUTDIR, FLEXPART_ROOT_SCRIPTS |
---|
[d69b677] | 131 | |
---|
[02c8c50] | 132 | For more information about format and content of the parameter |
---|
| 133 | see documentation. |
---|
| 134 | |
---|
| 135 | @Return: |
---|
| 136 | <nothing> |
---|
| 137 | ''' |
---|
[64cf353] | 138 | |
---|
[d69b677] | 139 | if not args.ppid: |
---|
[02c8c50] | 140 | c.ppid = str(os.getppid()) |
---|
[d69b677] | 141 | else: |
---|
[02c8c50] | 142 | c.ppid = args.ppid |
---|
| 143 | |
---|
| 144 | c.ecapi = ecapi |
---|
[d69b677] | 145 | |
---|
[02c8c50] | 146 | # create the start and end date |
---|
| 147 | start = datetime.date(year=int(c.start_date[:4]), |
---|
| 148 | month=int(c.start_date[4:6]), |
---|
| 149 | day=int(c.start_date[6:])) |
---|
[d69b677] | 150 | |
---|
[02c8c50] | 151 | end = datetime.date(year=int(c.end_date[:4]), |
---|
| 152 | month=int(c.end_date[4:6]), |
---|
| 153 | day=int(c.end_date[6:])) |
---|
[d69b677] | 154 | |
---|
[efdb01a] | 155 | # to deaccumulate the fluxes correctly |
---|
[02c8c50] | 156 | # one day ahead of the start date and |
---|
| 157 | # one day after the end date is needed |
---|
| 158 | startm1 = start - datetime.timedelta(days=1) |
---|
| 159 | endp1 = end + datetime.timedelta(days=1) |
---|
[d69b677] | 160 | |
---|
[02c8c50] | 161 | # get all files with flux data to be deaccumulated |
---|
[991df6a] | 162 | inputfiles = UIOFiles('*OG_acc_SL*.' + c.ppid + '.*') |
---|
| 163 | inputfiles.listFiles(c.inputdir) |
---|
[d69b677] | 164 | |
---|
[02c8c50] | 165 | # create output dir if necessary |
---|
[d69b677] | 166 | if not os.path.exists(c.outputdir): |
---|
[02c8c50] | 167 | os.makedirs(c.outputdir) |
---|
[64cf353] | 168 | |
---|
[02c8c50] | 169 | # deaccumulate the flux data |
---|
[991df6a] | 170 | flexpart = ECFlexpart(c, fluxes=True) |
---|
[02c8c50] | 171 | flexpart.write_namelist(c, 'fort.4') |
---|
[d69b677] | 172 | flexpart.deacc_fluxes(inputfiles, c) |
---|
| 173 | |
---|
[02c8c50] | 174 | print('Prepare ' + start.strftime("%Y%m%d") + |
---|
| 175 | "/to/" + end.strftime("%Y%m%d")) |
---|
[d69b677] | 176 | |
---|
[02c8c50] | 177 | # get a list of all files from the root inputdir |
---|
[991df6a] | 178 | inputfiles = UIOFiles('????__??.*' + c.ppid + '.*') |
---|
| 179 | inputfiles.listFiles(c.inputdir) |
---|
[d69b677] | 180 | |
---|
[02c8c50] | 181 | # produce FLEXPART-ready GRIB files and |
---|
| 182 | # process GRIB files - |
---|
| 183 | # copy/transfer/interpolate them or make them GRIB2 |
---|
| 184 | if c.basetime == '00': |
---|
| 185 | start = startm1 |
---|
| 186 | |
---|
[991df6a] | 187 | flexpart = ECFlexpart(c, fluxes=False) |
---|
[02c8c50] | 188 | flexpart.create(inputfiles, c) |
---|
| 189 | flexpart.process_output(c) |
---|
| 190 | |
---|
| 191 | # check if in debugging mode, then store all files |
---|
[efdb01a] | 192 | # otherwise delete temporary files |
---|
[02c8c50] | 193 | if int(c.debug) != 0: |
---|
| 194 | print('Temporary files left intact') |
---|
[d69b677] | 195 | else: |
---|
[991df6a] | 196 | cleanup(c) |
---|
[02c8c50] | 197 | |
---|
| 198 | return |
---|
[d69b677] | 199 | |
---|
| 200 | if __name__ == "__main__": |
---|
[991df6a] | 201 | main() |
---|