grib2io.tables
Functions for retreiving data from NCEP GRIB2 Tables.
1""" 2Functions for retreiving data from NCEP GRIB2 Tables. 3""" 4 5from .section0 import * 6from .section1 import * 7from .section3 import * 8from .section4 import * 9from .section5 import * 10from .originating_centers import * 11 12 13def get_table(table, expand=False): 14 """ 15 Return GRIB2 code table as a dictionary. 16 17 Parameters 18 ---------- 19 20 **`table`**: Code table number (e.g. '1.0'). NOTE: Code table '4.1' requires a 3rd value 21 representing the product discipline (e.g. '4.1.0'). 22 23 **`expand`**: If `True`, expand output dictionary where keys are a range. 24 25 Returns 26 ------- 27 28 **`dict`** 29 """ 30 if len(table) == 3 and table == '4.1': 31 raise Exception('GRIB2 Code Table 4.1 requires a 3rd value representing the discipline.') 32 if len(table) == 3 and table.startswith('4.2'): 33 raise Exception('Use function get_varinfo_from_table() for GRIB2 Code Table 4.2') 34 try: 35 tbl = globals()['table_'+table.replace('.','_')] 36 if expand: 37 _tbl = {} 38 for k,v in tbl.items(): 39 if '-' in k: 40 irng = [int(i) for i in k.split('-')] 41 for i in range(irng[0],irng[1]+1): 42 _tbl[str(i)] = v 43 else: 44 _tbl[k] = v 45 tbl = _tbl 46 return tbl 47 except(KeyError): 48 return {} 49 50 51def get_value_from_table(value, table): 52 """ 53 Return the definition given a GRIB2 code table. 54 55 Parameters 56 ---------- 57 58 **`value`**: `int` or `str` code table value. 59 60 **`table`**: `str` code table number. 61 62 Returns 63 ------- 64 65 Table value or `None` if not found. 66 """ 67 try: 68 tbl = get_table(table,expand=True) 69 if isinstance(value,int): value = str(value) 70 return tbl[value] 71 except(KeyError): 72 return None 73 74 75def get_varinfo_from_table(discipline,parmcat,parmnum,isNDFD=False): 76 """ 77 Return the GRIB2 variable information given values of `discipline`, 78 `parmcat`, and `parmnum`. NOTE: This functions allows for all arguments 79 to be converted to a string type if arguments are integer. 80 81 Parameters 82 ---------- 83 84 **`discipline`**: `int` or `str` of Discipline code value of a GRIB2 message. 85 86 **`parmcat`**: `int` or `str` of Parameter Category value of a GRIB2 message. 87 88 **`parmnum`**: `int` or `str` of Parameter Number value of a GRIB2 message. 89 90 **`isNDFD`**: If `True`, signals function to try to get variable information 91 from the supplemental NDFD tables. 92 93 Returns 94 ------- 95 96 **`list`**: containing variable information. "Unknown" is given for item of 97 information if variable is not found. 98 - list[0] = full name 99 - list[1] = units 100 - list[2] = short name (abbreviated name) 101 """ 102 if isinstance(discipline,int): discipline = str(discipline) 103 if isinstance(parmcat,int): parmcat = str(parmcat) 104 if isinstance(parmnum,int): parmnum = str(parmnum) 105 if isNDFD: 106 try: 107 tblname = 'table_4_2_'+discipline+'_'+parmcat+'_ndfd' 108 modname = '.section4_discipline'+discipline 109 exec('from '+modname+' import *') 110 return locals()[tblname][parmnum] 111 except(ImportError,KeyError): 112 pass 113 #return ['Unknown','Unknown','Unknown'] 114 try: 115 tblname = 'table_4_2_'+discipline+'_'+parmcat 116 modname = '.section4_discipline'+discipline 117 exec('from '+modname+' import *') 118 return locals()[tblname][parmnum] 119 except(ImportError,KeyError): 120 return ['Unknown','Unknown','Unknown'] 121 122 123def get_wgrib2_level_string(type1,sfac1,sval1,type2,sfac2,sval2): 124 """ 125 Return a string that describes the level or layer of the GRIB2 message. The 126 format and language of the string is an exact replica of how wgrib2 produces 127 the level/layer string in its inventory output. 128 129 Contents of wgrib2 source, [Level.c](https://github.com/NOAA-EMC/NCEPLIBS-wgrib2/blob/develop/wgrib2/Level.c), 130 were converted into a Python dictionary and stored in grib2io as table 131 'wgrib2_level_string'. 132 133 Parameters 134 ---------- 135 136 **`type1`**: `int` type of first fixed surface. 137 138 **`sfac1`**: `int` scale factor of first fixed surface. 139 140 **`sval1`**: `int` scaled value of first fixed surface. 141 142 **`type2`**: `int` type of second fixed surface. 143 144 **`sfac2`**: `int` scale factor of second fixed surface. 145 146 **`sval2`**: `int` scaled value of second fixed surface. 147 148 Returns 149 ------- 150 151 **`str`**: wgrib2-formatted level/layer string. 152 """ 153 lvlstr = '' 154 val1 = sval1/10**sfac1 155 if type1 in [100,108]: val1 *= 0.01 156 if type2 != 255: 157 # Layer 158 #assert type2 == type1, "Surface types are not equal: %g - %g" % (type1,type2) 159 val2 = sval2/10**sfac2 160 if type2 in [100,108]: val2 *= 0.01 161 lvlstr = get_value_from_table(type1,table='wgrib2_level_string')[1] 162 vals = (val1,val2) 163 else: 164 # Level 165 lvlstr = get_value_from_table(type1,table='wgrib2_level_string')[0] 166 vals = (val1) 167 if '%g' in lvlstr: lvlstr %= vals 168 return lvlstr
14def get_table(table, expand=False): 15 """ 16 Return GRIB2 code table as a dictionary. 17 18 Parameters 19 ---------- 20 21 **`table`**: Code table number (e.g. '1.0'). NOTE: Code table '4.1' requires a 3rd value 22 representing the product discipline (e.g. '4.1.0'). 23 24 **`expand`**: If `True`, expand output dictionary where keys are a range. 25 26 Returns 27 ------- 28 29 **`dict`** 30 """ 31 if len(table) == 3 and table == '4.1': 32 raise Exception('GRIB2 Code Table 4.1 requires a 3rd value representing the discipline.') 33 if len(table) == 3 and table.startswith('4.2'): 34 raise Exception('Use function get_varinfo_from_table() for GRIB2 Code Table 4.2') 35 try: 36 tbl = globals()['table_'+table.replace('.','_')] 37 if expand: 38 _tbl = {} 39 for k,v in tbl.items(): 40 if '-' in k: 41 irng = [int(i) for i in k.split('-')] 42 for i in range(irng[0],irng[1]+1): 43 _tbl[str(i)] = v 44 else: 45 _tbl[k] = v 46 tbl = _tbl 47 return tbl 48 except(KeyError): 49 return {}
Return GRIB2 code table as a dictionary.
Parameters
table
: Code table number (e.g. '1.0'). NOTE: Code table '4.1' requires a 3rd value
representing the product discipline (e.g. '4.1.0').
expand
: If True
, expand output dictionary where keys are a range.
Returns
dict
52def get_value_from_table(value, table): 53 """ 54 Return the definition given a GRIB2 code table. 55 56 Parameters 57 ---------- 58 59 **`value`**: `int` or `str` code table value. 60 61 **`table`**: `str` code table number. 62 63 Returns 64 ------- 65 66 Table value or `None` if not found. 67 """ 68 try: 69 tbl = get_table(table,expand=True) 70 if isinstance(value,int): value = str(value) 71 return tbl[value] 72 except(KeyError): 73 return None
Return the definition given a GRIB2 code table.
Parameters
value
: int
or str
code table value.
table
: str
code table number.
Returns
Table value or None
if not found.
76def get_varinfo_from_table(discipline,parmcat,parmnum,isNDFD=False): 77 """ 78 Return the GRIB2 variable information given values of `discipline`, 79 `parmcat`, and `parmnum`. NOTE: This functions allows for all arguments 80 to be converted to a string type if arguments are integer. 81 82 Parameters 83 ---------- 84 85 **`discipline`**: `int` or `str` of Discipline code value of a GRIB2 message. 86 87 **`parmcat`**: `int` or `str` of Parameter Category value of a GRIB2 message. 88 89 **`parmnum`**: `int` or `str` of Parameter Number value of a GRIB2 message. 90 91 **`isNDFD`**: If `True`, signals function to try to get variable information 92 from the supplemental NDFD tables. 93 94 Returns 95 ------- 96 97 **`list`**: containing variable information. "Unknown" is given for item of 98 information if variable is not found. 99 - list[0] = full name 100 - list[1] = units 101 - list[2] = short name (abbreviated name) 102 """ 103 if isinstance(discipline,int): discipline = str(discipline) 104 if isinstance(parmcat,int): parmcat = str(parmcat) 105 if isinstance(parmnum,int): parmnum = str(parmnum) 106 if isNDFD: 107 try: 108 tblname = 'table_4_2_'+discipline+'_'+parmcat+'_ndfd' 109 modname = '.section4_discipline'+discipline 110 exec('from '+modname+' import *') 111 return locals()[tblname][parmnum] 112 except(ImportError,KeyError): 113 pass 114 #return ['Unknown','Unknown','Unknown'] 115 try: 116 tblname = 'table_4_2_'+discipline+'_'+parmcat 117 modname = '.section4_discipline'+discipline 118 exec('from '+modname+' import *') 119 return locals()[tblname][parmnum] 120 except(ImportError,KeyError): 121 return ['Unknown','Unknown','Unknown']
Return the GRIB2 variable information given values of discipline
,
parmcat
, and parmnum
. NOTE: This functions allows for all arguments
to be converted to a string type if arguments are integer.
Parameters
discipline
: int
or str
of Discipline code value of a GRIB2 message.
parmcat
: int
or str
of Parameter Category value of a GRIB2 message.
parmnum
: int
or str
of Parameter Number value of a GRIB2 message.
isNDFD
: If True
, signals function to try to get variable information
from the supplemental NDFD tables.
Returns
list
: containing variable information. "Unknown" is given for item of
information if variable is not found.
- list[0] = full name
- list[1] = units
- list[2] = short name (abbreviated name)
124def get_wgrib2_level_string(type1,sfac1,sval1,type2,sfac2,sval2): 125 """ 126 Return a string that describes the level or layer of the GRIB2 message. The 127 format and language of the string is an exact replica of how wgrib2 produces 128 the level/layer string in its inventory output. 129 130 Contents of wgrib2 source, [Level.c](https://github.com/NOAA-EMC/NCEPLIBS-wgrib2/blob/develop/wgrib2/Level.c), 131 were converted into a Python dictionary and stored in grib2io as table 132 'wgrib2_level_string'. 133 134 Parameters 135 ---------- 136 137 **`type1`**: `int` type of first fixed surface. 138 139 **`sfac1`**: `int` scale factor of first fixed surface. 140 141 **`sval1`**: `int` scaled value of first fixed surface. 142 143 **`type2`**: `int` type of second fixed surface. 144 145 **`sfac2`**: `int` scale factor of second fixed surface. 146 147 **`sval2`**: `int` scaled value of second fixed surface. 148 149 Returns 150 ------- 151 152 **`str`**: wgrib2-formatted level/layer string. 153 """ 154 lvlstr = '' 155 val1 = sval1/10**sfac1 156 if type1 in [100,108]: val1 *= 0.01 157 if type2 != 255: 158 # Layer 159 #assert type2 == type1, "Surface types are not equal: %g - %g" % (type1,type2) 160 val2 = sval2/10**sfac2 161 if type2 in [100,108]: val2 *= 0.01 162 lvlstr = get_value_from_table(type1,table='wgrib2_level_string')[1] 163 vals = (val1,val2) 164 else: 165 # Level 166 lvlstr = get_value_from_table(type1,table='wgrib2_level_string')[0] 167 vals = (val1) 168 if '%g' in lvlstr: lvlstr %= vals 169 return lvlstr
Return a string that describes the level or layer of the GRIB2 message. The format and language of the string is an exact replica of how wgrib2 produces the level/layer string in its inventory output.
Contents of wgrib2 source, Level.c, were converted into a Python dictionary and stored in grib2io as table 'wgrib2_level_string'.
Parameters
type1
: int
type of first fixed surface.
sfac1
: int
scale factor of first fixed surface.
sval1
: int
scaled value of first fixed surface.
type2
: int
type of second fixed surface.
sfac2
: int
scale factor of second fixed surface.
sval2
: int
scaled value of second fixed surface.
Returns
str
: wgrib2-formatted level/layer string.