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