Source code for epygram.formats
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
formats:
Module contains:
- all geometries classes.
- utilities to play with resource formats:
- guess the format of an existing resource in a given container;
- create a Resource instance with a generic function,
eventually (if already existing) without knowing its format *a priori*;
- convert a field identifier from a format to another
- ...
"""
import importlib
from footprints import proxy as fpx
from epygram import config, epygramError, util
__all__ = []
for f in config.implemented_formats:
if f not in [m['name'] for m in config.usermodules]:
importlib.import_module('.' + f, __name__)
#################
### UTILITIES ###
#################
[docs]def guess(filename):
"""
Returns the name of the format of the resource located at a given
**filename**, if succeeded.
"""
# and not the list of dataformat from footprints collectors, because the order is important.
for f in config.implemented_formats:
try:
r = fpx.dataformat(filename=filename, openmode='r', format=f)
r.close()
fmt = f
break
except Exception:
fmt = 'unknown'
return fmt
[docs]def resource(filename, openmode, fmt=None, **kwargs):
"""
Returns an instance of Resource of the requested *fmt* format,
located at the given *filename* (:class:`epygram.base.File`, ...),
and with the given *openmode*.
If *fmt* is not given, tries to guess it (only for *openmode* 'r' or 'a').
Other *kwargs* are passed to the resource constructor.
"""
if fmt == None and openmode in ('r', 'a'):
fmt = guess(filename)
if fmt == 'unknown':
raise epygramError("unable to guess format of resource at: " + \
filename)
elif fmt == None and openmode == 'w':
raise epygramError("must specify 'fmt' argument with\
'openmode' == 'w'.")
return fpx.dataformat(filename=filename, openmode=openmode, format=fmt,
**kwargs)
fid_conv_dicts = {}
[docs]def fid_converter(fid, target_fmt):
"""
Returns a 'fid' (field identifier) converted from a format to another.
*fid* from input has to be {'FMT1':fid1, ...}
*target_fmt* is the name of the target format
The conversion is parameterized using config.CSV_fid_converters.
"""
if target_fmt == 'GRIB':
target_fmt += str(config.GRIB_default_edition)
def GRIBtoFA():
# juste inverser le dictionnaire...
if not 'FAtoGRIB' + str(fid['editionNumber']) in fid_conv_dicts.keys():
conv_dict = util.read_CSV_as_dict(config.CSV_fid_converters['FAtoGRIB' + str(fid['editionNumber'])])[0]
conv_dict = {f.pop('FAname'):f for f in conv_dict}
conv_dict['editionNumber'] = fid['editionNumber']
fid_conv_dicts['FAtoGRIB' + str(fid['editionNumber'])] = conv_dict
else:
conv_dict = fid_conv_dicts['FAtoGRIB' + str(fid['editionNumber'])]
new_fid = None
for f, g in conv_dict.items():
if fid == g:
new_fid = f
break
if new_fid == None:
raise epygramError("conversion not found in CSV_fid_converters.")
return new_fid
def FAtoGRIB(grib_edition=config.GRIB_default_edition):
if not 'FAtoGRIB' + str(grib_edition) in fid_conv_dicts.keys():
conv_dict = util.read_CSV_as_dict(config.CSV_fid_converters['FAtoGRIB' + str(grib_edition)])[0]
conv_dict = {f.pop('FAname'):f for f in conv_dict}
conv_dict['editionNumber'] = fid['editionNumber']
fid_conv_dicts['FAtoGRIB' + str(grib_edition)] = conv_dict
else:
conv_dict = fid_conv_dicts['FAtoGRIB' + str(grib_edition)]
try:
new_fid = conv_dict[fid['FA']]
except KeyError:
raise epygramError("conversion not found in CSV_fid_converters.")
new_fid.update({'editionNumber':grib_edition})
return new_fid
def FAtoLFI():
#TODO: incomplete: pressure levels, surface fields etc...
return (fid['FA'][4:], int(fid['FA'][1:4]))
def LFItoFA():
raise NotImplementedError("Not Yet.")
def LFItoGRIB(grib_edition=config.GRIB_default_edition):
conv_dict = util.read_CSV_as_dict(config.CSV_fid_converters['LFItoGRIB' + str(grib_edition)])[0]
conv_dict = {(f.pop('LFIname'), f.pop('LFIlevel')):f for f in conv_dict}
new_fid = conv_dict[fid['LFI']]
new_fid.update({'editionNumber':grib_edition})
return new_fid
if target_fmt == 'GRIB1' and 'FA' in fid.keys():
target_fid = FAtoGRIB(int(target_fmt[-1]))
elif target_fmt == 'GRIB2' and 'FA' in fid.keys():
target_fid = FAtoGRIB(int(target_fmt[-1]))
elif target_fmt == 'LFI' and 'FA' in fid.keys():
target_fid = FAtoLFI()
elif target_fmt == 'FA' and 'LFI' in fid.keys():
target_fid = LFItoFA()
elif target_fmt == 'FA' and 'GRIB' in fid.keys():
target_fid = GRIBtoFA()
elif target_fmt == 'GRIB2' and 'LFI' in fid.keys():
target_fid = LFItoGRIB(int(target_fmt[-1]))
elif target_fmt == 'GRIB1' and 'LFI' in fid.keys():
target_fid = LFItoGRIB(int(target_fmt[-1]))
return target_fid