From 48313c1ebd46f8fec46d9640f4d3fc6286a27ac6 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Thu, 10 Jan 2019 16:02:47 +0000 Subject: [PATCH] ECC-866: Python3: codes_gts_new_from_file does not return all GTS bulletins --- python3/grib_interface.c | 11 ++++++-- python3/grib_interface.h | 2 +- python3/gribapi/gribapi.py | 4 ++- python3/gribapi_swig.i | 2 +- python3/swig_wrap_numpy.c | 51 +++++++++++++++++++++++++++----------- python3/swig_wrap_numpy.py | 4 +-- 6 files changed, 52 insertions(+), 22 deletions(-) diff --git a/python3/grib_interface.c b/python3/grib_interface.c index 660a2dffe..62b09d066 100644 --- a/python3/grib_interface.c +++ b/python3/grib_interface.c @@ -1327,19 +1327,26 @@ int grib_c_count_in_file(FILE* f,int* n) return err; } -int grib_c_new_gts_from_file(FILE* f,int headers_only, int* gid) +int grib_c_new_gts_from_file(FILE* f, int fd, char* fname, int headers_only, int* gid) { grib_handle *h = NULL; int err = 0; if(f){ - h = gts_new_from_file(0,f,&err); + FILE* p = retrieve_file_info(fd); + if (p) { + h = gts_new_from_file(0,p,&err); //use cached value + } else { + h = gts_new_from_file(0,f,&err); //use FILE pointer passed in + store_file_info(fd, f); //store it for next time + } if(h){ push_handle(h,gid); return GRIB_SUCCESS; } else { *gid=-1; + clear_file_info(fd); return GRIB_END_OF_FILE; } } diff --git a/python3/grib_interface.h b/python3/grib_interface.h index 0f4e6c8a6..cac057571 100644 --- a/python3/grib_interface.h +++ b/python3/grib_interface.h @@ -42,8 +42,8 @@ int codes_c_close_file(int fd, char* fname); int grib_c_new_from_file(FILE *f, int fd, char* fname, int *gid, int headers_only); int grib_c_new_any_from_file(FILE *f, int fd, char* fname, int headers_only,int *gid); int grib_c_new_bufr_from_file(FILE *f, int fd, char* fname, int headers_only,int *gid); +int grib_c_new_gts_from_file(FILE *f, int fd, char* fname, int headers_only, int *gid); -int grib_c_new_gts_from_file(FILE *f,int headers_only, int *gid); int grib_c_new_metar_from_file(FILE* f,int headers_only, int* gid); int grib_c_new_from_index(int *iid, int *gid); int grib_c_index_new_from_file(char *file, char *keys, int *gid); diff --git a/python3/gribapi/gribapi.py b/python3/gribapi/gribapi.py index b4e5b0e01..c077681d3 100644 --- a/python3/gribapi/gribapi.py +++ b/python3/gribapi/gribapi.py @@ -157,7 +157,9 @@ def gts_new_from_file(fileobj, headers_only=False): @return id of the GTS loaded in memory @exception GribInternalError """ - err, gtsid = _internal.grib_c_new_gts_from_file(fileobj, headers_only, 0) + fd = fileobj.fileno() + fn = fileobj.name + err, gtsid = _internal.grib_c_new_gts_from_file(fileobj, fd, fn, headers_only, 0) if err: if err == _internal.GRIB_END_OF_FILE: return None diff --git a/python3/gribapi_swig.i b/python3/gribapi_swig.i index cb0f47b7a..268e609bf 100644 --- a/python3/gribapi_swig.i +++ b/python3/gribapi_swig.i @@ -44,7 +44,7 @@ import_array(); int grib_c_new_from_file(FILE* f, int fd, char* fname, int* INOUT, int headers_only); int grib_c_new_any_from_file(FILE* f, int fd, char* fname, int headers_only, int* INOUT); int grib_c_new_bufr_from_file(FILE* f, int fd, char* fname, int headers_only, int* INOUT); -int grib_c_new_gts_from_file(FILE* f, int headers_only, int* INOUT); +int grib_c_new_gts_from_file(FILE* f, int fd, char* fname, 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); diff --git a/python3/swig_wrap_numpy.c b/python3/swig_wrap_numpy.c index 1c6af8a4e..a5d31f6e5 100644 --- a/python3/swig_wrap_numpy.c +++ b/python3/swig_wrap_numpy.c @@ -5492,17 +5492,26 @@ SWIGINTERN PyObject *_wrap_grib_c_new_gts_from_file(PyObject *SWIGUNUSEDPARM(sel PyObject *resultobj = 0; FILE *arg1 = (FILE *) 0 ; int arg2 ; - int *arg3 = (int *) 0 ; + char *arg3 = (char *) 0 ; + int arg4 ; + int *arg5 = (int *) 0 ; int val2 ; int ecode2 = 0 ; - int temp3 ; - int res3 = 0 ; + int res3 ; + char *buf3 = 0 ; + int alloc3 = 0 ; + int val4 ; + int ecode4 = 0 ; + int temp5 ; + int res5 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; PyObject * obj2 = 0 ; + PyObject * obj3 = 0 ; + PyObject * obj4 = 0 ; int result; - if (!PyArg_ParseTuple(args,(char *)"OOO:grib_c_new_gts_from_file",&obj0,&obj1,&obj2)) SWIG_fail; + if (!PyArg_ParseTuple(args,(char *)"OOOOO:grib_c_new_gts_from_file",&obj0,&obj1,&obj2,&obj3,&obj4)) SWIG_fail; { int fileDescriptor = PyObject_AsFileDescriptor(obj0); /*printf("swig.i fileDescriptor=%d\n", fileDescriptor);*/ @@ -5520,26 +5529,38 @@ SWIGINTERN PyObject *_wrap_grib_c_new_gts_from_file(PyObject *SWIGUNUSEDPARM(sel SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "grib_c_new_gts_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))))) { + res3 = SWIG_AsCharPtrAndSize(obj2, &buf3, NULL, &alloc3); + if (!SWIG_IsOK(res3)) { + SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "grib_c_new_gts_from_file" "', argument " "3"" of type '" "char *""'"); + } + arg3 = (char *)(buf3); + ecode4 = SWIG_AsVal_int(obj3, &val4); + if (!SWIG_IsOK(ecode4)) { + SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "grib_c_new_gts_from_file" "', argument " "4"" of type '" "int""'"); + } + arg4 = (int)(val4); + if (!(SWIG_IsOK((res5 = SWIG_ConvertPtr(obj4,SWIG_as_voidptrptr(&arg5),SWIGTYPE_p_int,0))))) { int val; - int ecode = SWIG_AsVal_int(obj2, &val); + int ecode = SWIG_AsVal_int(obj4, &val); if (!SWIG_IsOK(ecode)) { - SWIG_exception_fail(SWIG_ArgError(ecode), "in method '" "grib_c_new_gts_from_file" "', argument " "3"" of type '" "int""'"); + SWIG_exception_fail(SWIG_ArgError(ecode), "in method '" "grib_c_new_gts_from_file" "', argument " "5"" of type '" "int""'"); } - temp3 = (int)(val); - arg3 = &temp3; - res3 = SWIG_AddTmpMask(ecode); + temp5 = (int)(val); + arg5 = &temp5; + res5 = SWIG_AddTmpMask(ecode); } - result = (int)grib_c_new_gts_from_file(arg1,arg2,arg3); + result = (int)grib_c_new_gts_from_file(arg1,arg2,arg3,arg4,arg5); resultobj = SWIG_From_int((int)(result)); - if (SWIG_IsTmpObj(res3)) { - resultobj = SWIG_Python_AppendOutput(resultobj, SWIG_From_int((*arg3))); + if (SWIG_IsTmpObj(res5)) { + resultobj = SWIG_Python_AppendOutput(resultobj, SWIG_From_int((*arg5))); } 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)); + int new_flags = SWIG_IsNewObj(res5) ? (SWIG_POINTER_OWN | 0 ) : 0 ; + resultobj = SWIG_Python_AppendOutput(resultobj, SWIG_NewPointerObj((void*)(arg5), SWIGTYPE_p_int, new_flags)); } + if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); return resultobj; fail: + if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); return NULL; } diff --git a/python3/swig_wrap_numpy.py b/python3/swig_wrap_numpy.py index 0c75d8eff..8e185e46d 100644 --- a/python3/swig_wrap_numpy.py +++ b/python3/swig_wrap_numpy.py @@ -387,8 +387,8 @@ def grib_c_new_bufr_from_file(f: 'FILE *', fd: 'int', fname: 'char *', headers_o return _gribapi_swig.grib_c_new_bufr_from_file(f, fd, fname, headers_only, INOUT) grib_c_new_bufr_from_file = _gribapi_swig.grib_c_new_bufr_from_file -def grib_c_new_gts_from_file(f: 'FILE *', headers_only: 'int', INOUT: 'int *') -> "int *": - return _gribapi_swig.grib_c_new_gts_from_file(f, headers_only, INOUT) +def grib_c_new_gts_from_file(f: 'FILE *', fd: 'int', fname: 'char *', headers_only: 'int', INOUT: 'int *') -> "int *": + return _gribapi_swig.grib_c_new_gts_from_file(f, fd, fname, headers_only, INOUT) grib_c_new_gts_from_file = _gribapi_swig.grib_c_new_gts_from_file def grib_c_new_metar_from_file(f: 'FILE *', headers_only: 'int', INOUT: 'int *') -> "int *":