[d69b677] | 1 | #!/usr/bin/env python |
---|
[02c8c50] | 2 | # -*- coding: utf-8 -*- |
---|
[991df6a] | 3 | #******************************************************************************* |
---|
| 4 | # @Author: Anne Fouilloux (University of Oslo) |
---|
| 5 | # |
---|
| 6 | # @Date: October 2014 |
---|
| 7 | # |
---|
| 8 | # @Change History: |
---|
| 9 | # |
---|
| 10 | # November 2015 - Leopold Haimberger (University of Vienna): |
---|
| 11 | # - using the WebAPI also for general MARS retrievals |
---|
| 12 | # - job submission on ecgate and cca |
---|
| 13 | # - job templates suitable for twice daily operational dissemination |
---|
| 14 | # - dividing retrievals of longer periods into digestable chunks |
---|
| 15 | # - retrieve also longer term forecasts, not only analyses and |
---|
| 16 | # short term forecast data |
---|
| 17 | # - conversion into GRIB2 |
---|
| 18 | # - conversion into .fp format for faster execution of FLEXPART |
---|
| 19 | # |
---|
| 20 | # February 2018 - Anne Philipp (University of Vienna): |
---|
| 21 | # - applied PEP8 style guide |
---|
| 22 | # - added documentation |
---|
| 23 | # - minor changes in programming style for consistence |
---|
[ff99eae] | 24 | # - BUG: removed call of clean_up-Function after call of |
---|
[991df6a] | 25 | # prepareFlexpart in main since it is already called in |
---|
| 26 | # prepareFlexpart at the end! |
---|
| 27 | # - created function main and moved the two function calls for |
---|
[ff99eae] | 28 | # arguments and prepare_flexpart into it |
---|
[991df6a] | 29 | # |
---|
| 30 | # @License: |
---|
| 31 | # (C) Copyright 2014-2018. |
---|
| 32 | # |
---|
| 33 | # This software is licensed under the terms of the Apache Licence Version 2.0 |
---|
| 34 | # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
---|
| 35 | # |
---|
| 36 | # @Program Functionality: |
---|
| 37 | # This program prepares the final version of the grib files which are |
---|
| 38 | # then used by FLEXPART. It converts the bunch of grib files extracted |
---|
[ff99eae] | 39 | # via get_mars_data by doing for example the necessary conversion to get |
---|
[991df6a] | 40 | # consistent grids or the disaggregation of flux data. Finally, the |
---|
| 41 | # program combines the data fields in files per available hour with the |
---|
| 42 | # naming convention xxYYMMDDHH, where xx should be 2 arbitrary letters |
---|
| 43 | # (mostly xx is chosen to be "EN"). |
---|
| 44 | # |
---|
| 45 | # @Program Content: |
---|
| 46 | # - main |
---|
[ff99eae] | 47 | # - prepare_flexpart |
---|
[991df6a] | 48 | # |
---|
| 49 | #******************************************************************************* |
---|
| 50 | |
---|
[efdb01a] | 51 | # ------------------------------------------------------------------------------ |
---|
| 52 | # MODULES |
---|
| 53 | # ------------------------------------------------------------------------------ |
---|
[d69b677] | 54 | import datetime |
---|
[efdb01a] | 55 | import os |
---|
| 56 | import inspect |
---|
| 57 | import sys |
---|
[d69b677] | 58 | import socket |
---|
| 59 | |
---|
[ff99eae] | 60 | # software specific classes and modules from flex_extract |
---|
[70fee58] | 61 | |
---|
| 62 | sys.path.append(os.path.dirname(os.path.abspath( |
---|
| 63 | inspect.getfile(inspect.currentframe()))) + '/../') |
---|
[2fb99de] | 64 | import _config |
---|
[3f36e42] | 65 | from checks import check_ppid |
---|
[25b14be] | 66 | from classes.UioFiles import UioFiles |
---|
[ca867de] | 67 | from classes.ControlFile import ControlFile |
---|
[9aefaad] | 68 | from tools import clean_up, get_cmdline_args, read_ecenv, make_dir |
---|
[25b14be] | 69 | from classes.EcFlexpart import EcFlexpart |
---|
[ff99eae] | 70 | |
---|
| 71 | ecapi = 'ecmwf' not in socket.gethostname() |
---|
[d69b677] | 72 | try: |
---|
| 73 | if ecapi: |
---|
| 74 | import ecmwfapi |
---|
| 75 | except ImportError: |
---|
[64cf353] | 76 | ecapi = False |
---|
| 77 | |
---|
[efdb01a] | 78 | # ------------------------------------------------------------------------------ |
---|
| 79 | # FUNCTION |
---|
| 80 | # ------------------------------------------------------------------------------ |
---|
[991df6a] | 81 | def main(): |
---|
[274f9ef] | 82 | '''Controls the program to prepare flexpart input files from mars data. |
---|
| 83 | |
---|
| 84 | This is done if it is called directly from command line. |
---|
| 85 | Then it also takes program call arguments and control file input. |
---|
| 86 | |
---|
| 87 | Parameters |
---|
| 88 | ---------- |
---|
[991df6a] | 89 | |
---|
[274f9ef] | 90 | Return |
---|
| 91 | ------ |
---|
[02c8c50] | 92 | |
---|
[991df6a] | 93 | ''' |
---|
[54a8a01] | 94 | |
---|
[9aefaad] | 95 | args = get_cmdline_args() |
---|
[4971f63] | 96 | c = ControlFile(args.controlfile) |
---|
[2fb99de] | 97 | |
---|
| 98 | env_parameter = read_ecenv(_config.PATH_ECMWF_ENV) |
---|
[295ff45] | 99 | c.assign_args_to_control(args) |
---|
[54a8a01] | 100 | c.assign_envs_to_control(env_parameter) |
---|
[2fb99de] | 101 | c.check_conditions(args.queue) |
---|
[295ff45] | 102 | |
---|
[54a8a01] | 103 | prepare_flexpart(args.ppid, c) |
---|
[991df6a] | 104 | |
---|
| 105 | return |
---|
| 106 | |
---|
[54a8a01] | 107 | def prepare_flexpart(ppid, c): |
---|
[274f9ef] | 108 | '''Converts the mars data into flexpart ready input files. |
---|
| 109 | |
---|
| 110 | Specific data fields are converted to a different grid and the flux |
---|
| 111 | data are going to be disaggregated. The data fields are collected by |
---|
| 112 | hour and stored in a file with a specific FLEXPART relevant naming |
---|
| 113 | convention. |
---|
| 114 | |
---|
| 115 | Parameters |
---|
| 116 | ---------- |
---|
| 117 | ppid : :obj:`int` |
---|
| 118 | Contains the ppid number of the current ECMWF job. It will be None if |
---|
| 119 | the method was called within this module. |
---|
| 120 | |
---|
| 121 | c : :obj:`ControlFile` |
---|
| 122 | Contains all the parameters of CONTROL file and |
---|
| 123 | command line. |
---|
| 124 | |
---|
| 125 | Return |
---|
| 126 | ------ |
---|
| 127 | |
---|
[02c8c50] | 128 | ''' |
---|
[3f36e42] | 129 | check_ppid(c, ppid) |
---|
[02c8c50] | 130 | |
---|
| 131 | c.ecapi = ecapi |
---|
[d69b677] | 132 | |
---|
[02c8c50] | 133 | # create the start and end date |
---|
| 134 | start = datetime.date(year=int(c.start_date[:4]), |
---|
| 135 | month=int(c.start_date[4:6]), |
---|
| 136 | day=int(c.start_date[6:])) |
---|
[d69b677] | 137 | |
---|
[02c8c50] | 138 | end = datetime.date(year=int(c.end_date[:4]), |
---|
| 139 | month=int(c.end_date[4:6]), |
---|
| 140 | day=int(c.end_date[6:])) |
---|
[d69b677] | 141 | |
---|
[38e83ba] | 142 | # if basetime is 00 |
---|
[54a8a01] | 143 | # assign starting date minus 1 day |
---|
[38e83ba] | 144 | # since we need the 12 hours upfront |
---|
[54a8a01] | 145 | # (the day before from 12 UTC to current day 00 UTC) |
---|
| 146 | if c.basetime == '00': |
---|
| 147 | start = start - datetime.timedelta(days=1) |
---|
[d69b677] | 148 | |
---|
[2fb99de] | 149 | print('Prepare ' + start.strftime("%Y%m%d") + |
---|
| 150 | "/to/" + end.strftime("%Y%m%d")) |
---|
[d69b677] | 151 | |
---|
[02c8c50] | 152 | # create output dir if necessary |
---|
[d69b677] | 153 | if not os.path.exists(c.outputdir): |
---|
[5bad6ec] | 154 | make_dir(c.outputdir) |
---|
[64cf353] | 155 | |
---|
[54a8a01] | 156 | # get all files with flux data to be deaccumulated |
---|
[70fee58] | 157 | inputfiles = UioFiles(c.inputdir, '*OG_acc_SL*.' + str(c.ppid) + '.*') |
---|
[54a8a01] | 158 | |
---|
[02c8c50] | 159 | # deaccumulate the flux data |
---|
[ff99eae] | 160 | flexpart = EcFlexpart(c, fluxes=True) |
---|
[c5074d2] | 161 | flexpart.write_namelist(c) |
---|
[d69b677] | 162 | flexpart.deacc_fluxes(inputfiles, c) |
---|
| 163 | |
---|
[38e83ba] | 164 | # get a list of all other files |
---|
[70fee58] | 165 | inputfiles = UioFiles(c.inputdir, '????__??.*' + str(c.ppid) + '.*') |
---|
[d69b677] | 166 | |
---|
[54a8a01] | 167 | # produce FLEXPART-ready GRIB files and process them - |
---|
[02c8c50] | 168 | # copy/transfer/interpolate them or make them GRIB2 |
---|
[ff99eae] | 169 | flexpart = EcFlexpart(c, fluxes=False) |
---|
[02c8c50] | 170 | flexpart.create(inputfiles, c) |
---|
| 171 | flexpart.process_output(c) |
---|
[38e83ba] | 172 | |
---|
| 173 | # make use of a possible conversion to a |
---|
| 174 | # specific flexpart binary format |
---|
[27fe969] | 175 | if c.grib2flexpart: |
---|
| 176 | flexpart.prepare_fp_files(c) |
---|
[02c8c50] | 177 | |
---|
| 178 | # check if in debugging mode, then store all files |
---|
[efdb01a] | 179 | # otherwise delete temporary files |
---|
[27fe969] | 180 | if c.debug: |
---|
[2fb99de] | 181 | print('\nTemporary files left intact') |
---|
[d69b677] | 182 | else: |
---|
[ff99eae] | 183 | clean_up(c) |
---|
[02c8c50] | 184 | |
---|
| 185 | return |
---|
[d69b677] | 186 | |
---|
| 187 | if __name__ == "__main__": |
---|
[991df6a] | 188 | main() |
---|