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