[8463d78] | 1 | #!/usr/bin/env python3 |
---|
[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 |
---|
[6f951ca] | 24 | # - BUGFIX: 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: |
---|
[6f951ca] | 31 | # (C) Copyright 2014-2019. |
---|
| 32 | # Anne Philipp, Leopold Haimberger |
---|
[991df6a] | 33 | # |
---|
[44174de] | 34 | # SPDX-License-Identifier: CC-BY-4.0 |
---|
| 35 | # |
---|
[6f951ca] | 36 | # This work is licensed under the Creative Commons Attribution 4.0 |
---|
| 37 | # International License. To view a copy of this license, visit |
---|
| 38 | # http://creativecommons.org/licenses/by/4.0/ or send a letter to |
---|
| 39 | # Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. |
---|
[991df6a] | 40 | #******************************************************************************* |
---|
[6f951ca] | 41 | '''This script prepares the final version of the grib files which are |
---|
| 42 | then used by FLEXPART. |
---|
| 43 | |
---|
| 44 | It converts the bunch of grib files extracted via get_mars_data before, |
---|
| 45 | by doing the necessary conversion to get consistent grids or the |
---|
| 46 | disaggregation of flux data. Finally, the data fields are combined |
---|
| 47 | in files per available hour with the naming convention xxYYMMDDHH, |
---|
| 48 | where xx should be 2 arbitrary letters (mostly xx is chosen to be "EN"). |
---|
| 49 | |
---|
| 50 | This file can also be imported as a module which then contains the following |
---|
| 51 | functions: |
---|
| 52 | |
---|
| 53 | * main |
---|
| 54 | * prepare_flexpart |
---|
| 55 | |
---|
| 56 | Type: prepare_flexpart.py --help |
---|
| 57 | to get information about command line parameters. |
---|
| 58 | Read the documentation for usage instructions. |
---|
| 59 | ''' |
---|
[991df6a] | 60 | |
---|
[efdb01a] | 61 | # ------------------------------------------------------------------------------ |
---|
| 62 | # MODULES |
---|
| 63 | # ------------------------------------------------------------------------------ |
---|
[0e08483] | 64 | from __future__ import print_function |
---|
| 65 | |
---|
[d69b677] | 66 | import datetime |
---|
[efdb01a] | 67 | import os |
---|
| 68 | import inspect |
---|
| 69 | import sys |
---|
[d69b677] | 70 | import socket |
---|
| 71 | |
---|
[ff99eae] | 72 | # software specific classes and modules from flex_extract |
---|
[6f951ca] | 73 | # add path to local main python path for flex_extract to get full access |
---|
[70fee58] | 74 | sys.path.append(os.path.dirname(os.path.abspath( |
---|
| 75 | inspect.getfile(inspect.currentframe()))) + '/../') |
---|
[2fb99de] | 76 | import _config |
---|
[ba99230] | 77 | from Mods.checks import check_ppid |
---|
| 78 | from Classes.UioFiles import UioFiles |
---|
| 79 | from Classes.ControlFile import ControlFile |
---|
| 80 | from Mods.tools import (setup_controldata, clean_up, get_cmdline_args, |
---|
[90a1ca0] | 81 | read_ecenv, make_dir, normal_exit) |
---|
[ba99230] | 82 | from Classes.EcFlexpart import EcFlexpart |
---|
[ff99eae] | 83 | |
---|
[efdb01a] | 84 | # ------------------------------------------------------------------------------ |
---|
| 85 | # FUNCTION |
---|
| 86 | # ------------------------------------------------------------------------------ |
---|
[991df6a] | 87 | def main(): |
---|
[274f9ef] | 88 | '''Controls the program to prepare flexpart input files from mars data. |
---|
| 89 | |
---|
| 90 | This is done if it is called directly from command line. |
---|
| 91 | Then it also takes program call arguments and control file input. |
---|
| 92 | |
---|
| 93 | Parameters |
---|
| 94 | ---------- |
---|
[991df6a] | 95 | |
---|
[274f9ef] | 96 | Return |
---|
| 97 | ------ |
---|
[02c8c50] | 98 | |
---|
[991df6a] | 99 | ''' |
---|
[54a8a01] | 100 | |
---|
[f20af73] | 101 | c, ppid, _, _ = setup_controldata() |
---|
| 102 | prepare_flexpart(ppid, c) |
---|
| 103 | normal_exit('Preparing FLEXPART output files: Done!') |
---|
[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 | ---------- |
---|
[6f951ca] | 117 | ppid : int |
---|
[274f9ef] | 118 | Contains the ppid number of the current ECMWF job. It will be None if |
---|
| 119 | the method was called within this module. |
---|
| 120 | |
---|
[6f951ca] | 121 | c : ControlFile |
---|
[274f9ef] | 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 | # create the start and end date |
---|
| 132 | start = datetime.date(year=int(c.start_date[:4]), |
---|
| 133 | month=int(c.start_date[4:6]), |
---|
| 134 | day=int(c.start_date[6:])) |
---|
[d69b677] | 135 | |
---|
[02c8c50] | 136 | end = datetime.date(year=int(c.end_date[:4]), |
---|
| 137 | month=int(c.end_date[4:6]), |
---|
| 138 | day=int(c.end_date[6:])) |
---|
[d69b677] | 139 | |
---|
[38e83ba] | 140 | # if basetime is 00 |
---|
[54a8a01] | 141 | # assign starting date minus 1 day |
---|
[38e83ba] | 142 | # since we need the 12 hours upfront |
---|
[54a8a01] | 143 | # (the day before from 12 UTC to current day 00 UTC) |
---|
[d4696e0] | 144 | if c.basetime == 0: |
---|
[54a8a01] | 145 | start = start - datetime.timedelta(days=1) |
---|
[d69b677] | 146 | |
---|
[2fb99de] | 147 | print('Prepare ' + start.strftime("%Y%m%d") + |
---|
| 148 | "/to/" + end.strftime("%Y%m%d")) |
---|
[d69b677] | 149 | |
---|
[02c8c50] | 150 | # create output dir if necessary |
---|
[d69b677] | 151 | if not os.path.exists(c.outputdir): |
---|
[5bad6ec] | 152 | make_dir(c.outputdir) |
---|
[64cf353] | 153 | |
---|
[54a8a01] | 154 | # get all files with flux data to be deaccumulated |
---|
[70fee58] | 155 | inputfiles = UioFiles(c.inputdir, '*OG_acc_SL*.' + str(c.ppid) + '.*') |
---|
[54a8a01] | 156 | |
---|
[02c8c50] | 157 | # deaccumulate the flux data |
---|
[ff99eae] | 158 | flexpart = EcFlexpart(c, fluxes=True) |
---|
[c5074d2] | 159 | flexpart.write_namelist(c) |
---|
[d69b677] | 160 | flexpart.deacc_fluxes(inputfiles, c) |
---|
| 161 | |
---|
[38e83ba] | 162 | # get a list of all other files |
---|
[70fee58] | 163 | inputfiles = UioFiles(c.inputdir, '????__??.*' + str(c.ppid) + '.*') |
---|
[d69b677] | 164 | |
---|
[54a8a01] | 165 | # produce FLEXPART-ready GRIB files and process them - |
---|
[02c8c50] | 166 | # copy/transfer/interpolate them or make them GRIB2 |
---|
[ff99eae] | 167 | flexpart = EcFlexpart(c, fluxes=False) |
---|
[02c8c50] | 168 | flexpart.create(inputfiles, c) |
---|
[90a1ca0] | 169 | if c.stream.lower() == 'elda' and c.doubleelda: |
---|
[e811e1a] | 170 | flexpart.calc_extra_elda(c.inputdir, c.prefix) |
---|
[02c8c50] | 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() |
---|