source: flex_extract.git/source/python/mods/checks.py @ a55ac71

ctbtodev
Last change on this file since a55ac71 was a55ac71, checked in by Anne Philipp <anne.philipp@…>, 5 years ago

added docstrings for all check functions

  • Property mode set to 100644
File size: 6.7 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3##*******************************************************************************
4# @Author: Anne Philipp (University of Vienna)
5#
6# @Date: November 2018
7#
8# @Change History:
9#
10# @License:
11#    (C) Copyright 2014-2018.
12#
13#    This software is licensed under the terms of the Apache Licence Version 2.0
14#    which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
15#
16# @Modul Description:
17#
18#
19# @Module Content:
20
21#
22#*******************************************************************************
23
24# ------------------------------------------------------------------------------
25# MODULES
26# ------------------------------------------------------------------------------
27
28import _config
29# ------------------------------------------------------------------------------
30# FUNCTIONS
31# ------------------------------------------------------------------------------
32
33
34def check_grid(grid):
35    '''Convert grid into correct Lat/Lon format. E.g. '0.5/0.5'
36
37    Checks on format of original grid. Wether it is in the order of 1000 or 1.
38    Convert to correct grid format and substitute into "Lat/Lon" format string.
39
40    Parameters
41    ----------
42    grid : :obj:`string`
43        Contains grid information
44
45    Return
46    ------
47    grid : :obj:``string`
48        Contains grid in format Lat/lon. E.g. 0.1/0.1
49    '''
50
51    if 'N' in grid:
52        return grid
53    if '/' in grid:
54        gridx, gridy = grid.split('/')
55        if gridx == gridy:
56            grid = gridx
57        else:
58            raise ValueError('GRID parameter contains two values '
59                             'which are unequal %s' (grid))
60    # determine grid format
61    if float(grid) / 100. >= 0.5:
62        # grid is defined in 1/1000 degrees; old format
63        grid = '{}/{}'.format(float(grid) / 1000.,
64                              float(grid) / 1000.)
65    elif float(grid) / 100. < 0.5:
66        # grid is defined in normal degree; new format
67        grid = '{}/{}'.format(float(grid), float(grid))
68
69    return grid
70
71def check_area(grid, area, upper, lower, left , right):
72    '''Defines the correct area string.
73
74    Checks on the format of the four area components. Wether it is of
75    the order of 1000 or 1. Also checks wether area was already set by command
76    line, then the four components are overwritten.
77    Convert to correct format of the order of magnitude "1" and sets the
78    area parameter (North/West/South/East).
79    E.g.: -5./20./10./10.
80
81    Parameters
82    ----------
83    grid : :obj:`string`
84        Contains grid information
85
86    Return
87    ------
88    grid : :obj:``string`
89        Contains grid in format Lat/lon. E.g. 0.1/0.1
90    '''
91    if 'N' in grid:  # Gaussian output grid
92        area = 'G'
93        return area
94
95    # if area was provided decompose area into its 4 components
96    if area:
97        components = area.split('/')
98        upper, left, lower, right = components
99
100    # determine area format
101    if (abs(float(upper) / 1000.) >= 0.05 and
102        abs(float(lower) / 1000.) >= 0.05 and
103        abs(float(left) / 1000.) >= 0.05 and
104        abs(float(right) / 1000.) >= 0.05):
105        # area is defined in 1/1000 degrees; old format
106        area = '{}/{}/{}/{}'.format(float(upper) / 1000.,
107                                    float(left) / 1000.,
108                                    float(lower) / 1000.,
109                                    float(right) / 1000.)
110    elif (abs(float(upper) / 1000.) < 0.05 and
111          abs(float(lower) / 1000.) < 0.05 and
112          abs(float(left) / 1000.) < 0.05 and
113          abs(float(right) / 1000.) < 0.05):
114        # area is already in new format
115        area = '{}/{}/{}/{}'.format(float(upper),
116                                    float(left),
117                                    float(lower),
118                                    float(right))
119    else:
120        raise ValueError('The area components have different '
121                         'formats (upper, lower, left, right): '
122                         '{}/{}/{}/{}'.format(str(upper), str(lower),
123                                              str(left) , str(right)))
124
125    return area
126
127def check_levels(levelist, level):
128    '''Defines correct level list and guarantees that the maximum level is
129    one of the available maximum levels.
130
131    Parameters
132    ----------
133    levelist : :obj:`string`
134        Specifies the level list.
135        Examples: model level: 1/to/137, pressure levels: 500/to/1000
136
137    level : :obj:`string`
138        Specifies the maximum level.
139
140    Return
141    ------
142    levelist : :obj:`string`
143        Specifies the required levels. It has to have a valid
144        correspondence to the selected levtype.
145        Examples: model level: 1/to/137, pressure levels: 500/to/1000
146
147    level : :obj:`string`
148        Specifies the maximum level. It has to be one of the
149        available maximum level number as contained in the variable
150        MAX_LEVEL_LIST in "_config". E.g. [16, 19, 31, 40, 50, 60, 62, 91, 137]
151
152    '''
153    # assure consistency of levelist and level
154    if not levelist and not level:
155        raise ValueError('ERROR: neither levelist nor level '
156                         'specified in CONTROL file')
157    elif not levelist and level:
158        levelist = '1/to/' + level
159    elif (levelist and not level) or \
160         (levelist[-1] != level[-1]):
161        level = levelist.split('/')[-1]
162    else:
163        pass
164
165    # check if max level is a valid level
166    if int(level) not in _config.MAX_LEVEL_LIST:
167        raise ValueError('ERROR: \n'
168                         'LEVEL must be the maximum level of a specified '
169                         'level list from ECMWF, e.g. {} \n'
170                         'Check parameter "LEVEL" or the max level of '
171                         '"LEVELIST"!'.format(str(_config.MAX_LEVEL_LIST)))
172
173    return levelist, level
174
175
176def check_ppid(c, ppid):
177    '''Sets the current PPID.
178
179    Parameters
180    ----------
181    c : :obj:`ControlFile`
182            Contains all the parameters of CONTROL file and
183            command line.
184
185    ppid : :obj:`int` or :obj:`None`
186        Contains the ppid number provided by the command line parameter
187        of is None otherwise.
188
189    Return
190    ------
191
192    '''
193
194    if not ppid:
195        c.ppid = str(os.getppid())
196    else:
197        c.ppid = ppid
198
199    return
200
201
202def check_purefc(type):
203    '''Check for a pure forecast mode.
204
205    Parameters
206    ----------
207    type : :obj:`list` of :obj:`string`
208        List of field types.
209
210    Return
211    ------
212    True or False:
213        True if pure forecasts are to be retrieved. False if there are
214        analysis fields in between.
215    '''
216
217    if 'AN' not in type and '4V' not in type:
218        # pure forecast
219        return True
220
221    return False
222
223
224def check_():
225    '''
226
227    Parameters
228    ----------
229    par : :obj:``
230        ...
231
232    Return
233    ------
234
235    '''
236    return
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG