source: flex_extract.git/source/python/mods/checks.py @ 3f36e42

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

outsourced some checks from CONTROL class to checks module; translated namelist generation by genshi templating; corrected a bug in grib2 conversion

  • Property mode set to 100644
File size: 4.4 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
36    if 'N' in grid:
37        return grid
38    if '/' in grid:
39        gridx, gridy = grid.split('/')
40        if gridx == gridy:
41            grid = gridx
42        else:
43            raise ValueError('GRID parameter contains two values '
44                             'which are unequal %s' (grid))
45    # determine grid format
46    if float(grid) / 100. >= 0.5:
47        # grid is defined in 1/1000 degrees; old format
48        grid = '{}/{}'.format(float(grid) / 1000.,
49                              float(grid) / 1000.)
50    elif float(grid) / 100. < 0.5:
51        # grid is defined in normal degree; new format
52        grid = '{}/{}'.format(float(grid), float(grid))
53
54    return grid
55
56def check_area(grid, area, upper, lower, left , right):
57    '''
58
59
60    '''
61    if 'N' in grid:  # Gaussian output grid
62        area = 'G'
63        return area
64
65    # if area was provided decompose area into its 4 components
66    if area:
67        components = area.split('/')
68        upper, left, lower, right = components
69
70    # determine area format
71    if (float(upper) / 1000. >= 0.05 and
72        float(lower) / 1000. >= 0.05 and
73        float(left) / 1000. >= 0.05 and
74        float(right) / 1000. >= 0.05):
75        # area is defined in 1/1000 degrees; old format
76        area = '{}/{}/{}/{}'.format(float(upper) / 1000.,
77                                    float(left) / 1000.,
78                                    float(lower) / 1000.,
79                                    float(right) / 1000.)
80    elif (float(upper) / 1000. < 0.05 and
81          float(lower) / 1000. < 0.05 and
82          float(left) / 1000. < 0.05 and
83          float(right) / 1000. < 0.05):
84        # area is already in new format
85        area = '{}/{}/{}/{}'.format(float(upper),
86                                    float(left),
87                                    float(lower),
88                                    float(right))
89    else:
90        raise ValueError('The area components have different '
91                         'formats: %s ' (area))
92
93    return area
94
95def check_levels(levelist, level):
96    '''
97
98    Parameters
99    ----------
100    par : :obj:``
101        ...
102
103    Return
104    ------
105
106    '''
107    # assure consistency of levelist and level
108    if not levelist and not level:
109        raise ValueError('ERROR: neither levelist nor level '
110                         'specified in CONTROL file')
111    elif not levelist and level:
112        levelist = '1/to/' + level
113    elif (levelist and not level) or \
114         (levelist[-1] != level[-1]):
115        level = levelist.split('/')[-1]
116    else:
117        pass
118
119    # check if max level is a valid level
120    if int(level) not in _config.MAX_LEVEL_LIST:
121        raise ValueError('ERROR: \n'
122                         'LEVEL must be the maximum level of a specified '
123                         'level list from ECMWF, e.g. {} \n'
124                         'Check parameter "LEVEL" or the max level of '
125                         '"LEVELIST"!'.format(str(_config.MAX_LEVEL_LIST)))
126
127    return levelist, level
128
129
130def check_ppid(c, ppid):
131    '''Sets the current PPID.
132
133    Parameters
134    ----------
135    c : :obj:`ControlFile`
136            Contains all the parameters of CONTROL file and
137            command line.
138
139    ppid : :obj:`int` or :obj:`None`
140        Contains the ppid number provided by the command line parameter
141        of is None otherwise.
142
143    Return
144    ------
145
146    '''
147
148    if not ppid:
149        c.ppid = str(os.getppid())
150    else:
151        c.ppid = ppid
152
153    return
154
155
156def check_():
157    '''
158
159    Parameters
160    ----------
161    par : :obj:``
162        ...
163
164    Return
165    ------
166
167    '''
168    return
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG