mirror of https://github.com/ecmwf/eccodes.git
Metar example: metar_get_keys.py
This commit is contained in:
parent
c0d050ac07
commit
a11654746a
|
@ -0,0 +1,70 @@
|
|||
# Copyright 2005-2015 ECMWF.
|
||||
#
|
||||
# This software is licensed under the terms of the Apache Licence Version 2.0
|
||||
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
|
||||
#
|
||||
# In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
|
||||
# virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
|
||||
|
||||
#
|
||||
# Python implementation: metar_get_keys
|
||||
#
|
||||
# Description: how to read values of different type of keys from METAR messages.
|
||||
#
|
||||
#
|
||||
|
||||
import traceback
|
||||
import sys
|
||||
|
||||
from eccodes import *
|
||||
|
||||
INPUT='../../data/metar/metar.txt'
|
||||
VERBOSE=1 # verbose error reporting
|
||||
|
||||
def example():
|
||||
|
||||
# open metar file
|
||||
f = open(INPUT)
|
||||
|
||||
cnt=0
|
||||
|
||||
# loop for the messages in the file
|
||||
while 1:
|
||||
# get handle for message
|
||||
gid = codes_metar_new_from_file(f)
|
||||
if gid is None: break
|
||||
|
||||
print "message: %s" % cnt
|
||||
|
||||
#----------------------------------------------
|
||||
# get values for keys holding a single value
|
||||
#----------------------------------------------
|
||||
keys=['CCCC', 'latitude', 'longitude', 'dateTime', 'elevation', 'temperature', 'dewPointTemperature', 'qnh']
|
||||
|
||||
for key in keys:
|
||||
try:
|
||||
print ' %s: %s' % (key,codes_get(gid,key))
|
||||
except CodesInternalError,err:
|
||||
print 'Error with key="%s" : %s' % (key,err.msg)
|
||||
|
||||
cnt+=1
|
||||
|
||||
# delete handle
|
||||
codes_release(gid)
|
||||
|
||||
# close the file
|
||||
f.close()
|
||||
|
||||
def main():
|
||||
try:
|
||||
example()
|
||||
except CodesInternalError,err:
|
||||
if VERBOSE:
|
||||
traceback.print_exc(file=sys.stderr)
|
||||
else:
|
||||
print >>sys.stderr,err.msg
|
||||
|
||||
return 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
|
@ -0,0 +1,29 @@
|
|||
#!/bin/sh
|
||||
# Copyright 2005-2015 ECMWF.
|
||||
#
|
||||
# This software is licensed under the terms of the Apache Licence Version 2.0
|
||||
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
|
||||
#
|
||||
# In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
|
||||
# virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
|
||||
#
|
||||
|
||||
. ./include.sh
|
||||
|
||||
|
||||
#Define a common label for all the tmp files
|
||||
label="metar_get_keys_test_p"
|
||||
|
||||
#Define tmp file
|
||||
fTmp=${label}.tmp.txt
|
||||
rm -f $fTmp | true
|
||||
|
||||
REDIRECT=/dev/null
|
||||
|
||||
#Write the key values into a file
|
||||
$PYTHON metar_get_keys.py 2> $REDIRECT > $fTmp
|
||||
|
||||
#TODO: check the results
|
||||
|
||||
#Clean up
|
||||
rm -f $fTmp | true
|
|
@ -43,6 +43,7 @@ int grib_c_new_from_file(FILE* f, int* INOUT, int headers_only);
|
|||
int grib_c_new_any_from_file(FILE* f, int headers_only, int* INOUT);
|
||||
int grib_c_new_bufr_from_file(FILE* f, int headers_only, int* INOUT);
|
||||
int grib_c_new_gts_from_file(FILE* f, int headers_only, int* INOUT);
|
||||
int grib_c_new_metar_from_file(FILE* f, int headers_only, int* INOUT);
|
||||
int grib_c_iterator_new(int* INPUT, int* OUTPUT, int* INPUT);
|
||||
int grib_c_keys_iterator_new(int* INPUT, int* OUTPUT, char* name_space);
|
||||
int grib_c_new_from_samples(int* INOUT, char* name);
|
||||
|
|
|
@ -4218,6 +4218,58 @@ fail:
|
|||
}
|
||||
|
||||
|
||||
SWIGINTERN PyObject *_wrap_grib_c_new_metar_from_file(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
|
||||
PyObject *resultobj = 0;
|
||||
FILE *arg1 = (FILE *) 0 ;
|
||||
int arg2 ;
|
||||
int *arg3 = (int *) 0 ;
|
||||
int val2 ;
|
||||
int ecode2 = 0 ;
|
||||
int temp3 ;
|
||||
int res3 = 0 ;
|
||||
PyObject * obj0 = 0 ;
|
||||
PyObject * obj1 = 0 ;
|
||||
PyObject * obj2 = 0 ;
|
||||
int result;
|
||||
|
||||
if (!PyArg_ParseTuple(args,(char *)"OOO:grib_c_new_metar_from_file",&obj0,&obj1,&obj2)) SWIG_fail;
|
||||
{
|
||||
if ( PyFile_Check(obj0) ){
|
||||
arg1 = PyFile_AsFile(obj0);
|
||||
} else {
|
||||
PyErr_SetString(PyExc_TypeError, "f must be a file type.");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
ecode2 = SWIG_AsVal_int(obj1, &val2);
|
||||
if (!SWIG_IsOK(ecode2)) {
|
||||
SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "grib_c_new_metar_from_file" "', argument " "2"" of type '" "int""'");
|
||||
}
|
||||
arg2 = (int)(val2);
|
||||
if (!(SWIG_IsOK((res3 = SWIG_ConvertPtr(obj2,SWIG_as_voidptrptr(&arg3),SWIGTYPE_p_int,0))))) {
|
||||
int val;
|
||||
int ecode = SWIG_AsVal_int(obj2, &val);
|
||||
if (!SWIG_IsOK(ecode)) {
|
||||
SWIG_exception_fail(SWIG_ArgError(ecode), "in method '" "grib_c_new_metar_from_file" "', argument " "3"" of type '" "int""'");
|
||||
}
|
||||
temp3 = (int)(val);
|
||||
arg3 = &temp3;
|
||||
res3 = SWIG_AddTmpMask(ecode);
|
||||
}
|
||||
result = (int)grib_c_new_metar_from_file(arg1,arg2,arg3);
|
||||
resultobj = SWIG_From_int((int)(result));
|
||||
if (SWIG_IsTmpObj(res3)) {
|
||||
resultobj = SWIG_Python_AppendOutput(resultobj, SWIG_From_int((*arg3)));
|
||||
} else {
|
||||
int new_flags = SWIG_IsNewObj(res3) ? (SWIG_POINTER_OWN | 0 ) : 0 ;
|
||||
resultobj = SWIG_Python_AppendOutput(resultobj, SWIG_NewPointerObj((void*)(arg3), SWIGTYPE_p_int, new_flags));
|
||||
}
|
||||
return resultobj;
|
||||
fail:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
SWIGINTERN PyObject *_wrap_grib_c_iterator_new(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
|
||||
PyObject *resultobj = 0;
|
||||
int *arg1 = (int *) 0 ;
|
||||
|
@ -7445,6 +7497,7 @@ static PyMethodDef SwigMethods[] = {
|
|||
{ (char *)"grib_c_new_any_from_file", _wrap_grib_c_new_any_from_file, METH_VARARGS, NULL},
|
||||
{ (char *)"grib_c_new_bufr_from_file", _wrap_grib_c_new_bufr_from_file, METH_VARARGS, NULL},
|
||||
{ (char *)"grib_c_new_gts_from_file", _wrap_grib_c_new_gts_from_file, METH_VARARGS, NULL},
|
||||
{ (char *)"grib_c_new_metar_from_file", _wrap_grib_c_new_metar_from_file, METH_VARARGS, NULL},
|
||||
{ (char *)"grib_c_iterator_new", _wrap_grib_c_iterator_new, METH_VARARGS, NULL},
|
||||
{ (char *)"grib_c_keys_iterator_new", _wrap_grib_c_keys_iterator_new, METH_VARARGS, NULL},
|
||||
{ (char *)"grib_c_new_from_samples", _wrap_grib_c_new_from_samples, METH_VARARGS, NULL},
|
||||
|
|
|
@ -275,6 +275,10 @@ def grib_c_new_gts_from_file(*args):
|
|||
return _gribapi_swig.grib_c_new_gts_from_file(*args)
|
||||
grib_c_new_gts_from_file = _gribapi_swig.grib_c_new_gts_from_file
|
||||
|
||||
def grib_c_new_metar_from_file(*args):
|
||||
return _gribapi_swig.grib_c_new_metar_from_file(*args)
|
||||
grib_c_new_metar_from_file = _gribapi_swig.grib_c_new_metar_from_file
|
||||
|
||||
def grib_c_iterator_new(*args):
|
||||
return _gribapi_swig.grib_c_iterator_new(*args)
|
||||
grib_c_iterator_new = _gribapi_swig.grib_c_iterator_new
|
||||
|
|
|
@ -4672,6 +4672,58 @@ fail:
|
|||
}
|
||||
|
||||
|
||||
SWIGINTERN PyObject *_wrap_grib_c_new_metar_from_file(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
|
||||
PyObject *resultobj = 0;
|
||||
FILE *arg1 = (FILE *) 0 ;
|
||||
int arg2 ;
|
||||
int *arg3 = (int *) 0 ;
|
||||
int val2 ;
|
||||
int ecode2 = 0 ;
|
||||
int temp3 ;
|
||||
int res3 = 0 ;
|
||||
PyObject * obj0 = 0 ;
|
||||
PyObject * obj1 = 0 ;
|
||||
PyObject * obj2 = 0 ;
|
||||
int result;
|
||||
|
||||
if (!PyArg_ParseTuple(args,(char *)"OOO:grib_c_new_metar_from_file",&obj0,&obj1,&obj2)) SWIG_fail;
|
||||
{
|
||||
if ( PyFile_Check(obj0) ){
|
||||
arg1 = PyFile_AsFile(obj0);
|
||||
} else {
|
||||
PyErr_SetString(PyExc_TypeError, "f must be a file type.");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
ecode2 = SWIG_AsVal_int(obj1, &val2);
|
||||
if (!SWIG_IsOK(ecode2)) {
|
||||
SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "grib_c_new_metar_from_file" "', argument " "2"" of type '" "int""'");
|
||||
}
|
||||
arg2 = (int)(val2);
|
||||
if (!(SWIG_IsOK((res3 = SWIG_ConvertPtr(obj2,SWIG_as_voidptrptr(&arg3),SWIGTYPE_p_int,0))))) {
|
||||
int val;
|
||||
int ecode = SWIG_AsVal_int(obj2, &val);
|
||||
if (!SWIG_IsOK(ecode)) {
|
||||
SWIG_exception_fail(SWIG_ArgError(ecode), "in method '" "grib_c_new_metar_from_file" "', argument " "3"" of type '" "int""'");
|
||||
}
|
||||
temp3 = (int)(val);
|
||||
arg3 = &temp3;
|
||||
res3 = SWIG_AddTmpMask(ecode);
|
||||
}
|
||||
result = (int)grib_c_new_metar_from_file(arg1,arg2,arg3);
|
||||
resultobj = SWIG_From_int((int)(result));
|
||||
if (SWIG_IsTmpObj(res3)) {
|
||||
resultobj = SWIG_Python_AppendOutput(resultobj, SWIG_From_int((*arg3)));
|
||||
} else {
|
||||
int new_flags = SWIG_IsNewObj(res3) ? (SWIG_POINTER_OWN | 0 ) : 0 ;
|
||||
resultobj = SWIG_Python_AppendOutput(resultobj, SWIG_NewPointerObj((void*)(arg3), SWIGTYPE_p_int, new_flags));
|
||||
}
|
||||
return resultobj;
|
||||
fail:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
SWIGINTERN PyObject *_wrap_grib_c_iterator_new(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
|
||||
PyObject *resultobj = 0;
|
||||
int *arg1 = (int *) 0 ;
|
||||
|
@ -8269,6 +8321,7 @@ static PyMethodDef SwigMethods[] = {
|
|||
{ (char *)"grib_c_new_any_from_file", _wrap_grib_c_new_any_from_file, METH_VARARGS, NULL},
|
||||
{ (char *)"grib_c_new_bufr_from_file", _wrap_grib_c_new_bufr_from_file, METH_VARARGS, NULL},
|
||||
{ (char *)"grib_c_new_gts_from_file", _wrap_grib_c_new_gts_from_file, METH_VARARGS, NULL},
|
||||
{ (char *)"grib_c_new_metar_from_file", _wrap_grib_c_new_metar_from_file, METH_VARARGS, NULL},
|
||||
{ (char *)"grib_c_iterator_new", _wrap_grib_c_iterator_new, METH_VARARGS, NULL},
|
||||
{ (char *)"grib_c_keys_iterator_new", _wrap_grib_c_keys_iterator_new, METH_VARARGS, NULL},
|
||||
{ (char *)"grib_c_new_from_samples", _wrap_grib_c_new_from_samples, METH_VARARGS, NULL},
|
||||
|
|
|
@ -275,6 +275,10 @@ def grib_c_new_gts_from_file(*args):
|
|||
return _gribapi_swig.grib_c_new_gts_from_file(*args)
|
||||
grib_c_new_gts_from_file = _gribapi_swig.grib_c_new_gts_from_file
|
||||
|
||||
def grib_c_new_metar_from_file(*args):
|
||||
return _gribapi_swig.grib_c_new_metar_from_file(*args)
|
||||
grib_c_new_metar_from_file = _gribapi_swig.grib_c_new_metar_from_file
|
||||
|
||||
def grib_c_iterator_new(*args):
|
||||
return _gribapi_swig.grib_c_iterator_new(*args)
|
||||
grib_c_iterator_new = _gribapi_swig.grib_c_iterator_new
|
||||
|
|
|
@ -34,7 +34,7 @@ export METAR_YEAR=2015
|
|||
export METAR_MONTH=4
|
||||
|
||||
echo $f >> $fLog
|
||||
${tools_dir}/metar_ls $f >> $fLog
|
||||
${tools_dir}metar_ls $f >> $fLog
|
||||
|
||||
#-------------------------------------------
|
||||
# Test "-p" switch
|
||||
|
|
Loading…
Reference in New Issue