gribapi.c 3.57 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
5
#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#if  defined  (HAVE_LIBGRIB_API)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
6
#  include <grib_api.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
7
8
9
10
#endif

#include <stdio.h>

11
#include "cdi.h"
12
#include "cdi_int.h"
13
#include "gribapi.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
14
#include "dmemory.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
15
16
17
18

#define XSTRING(x)	#x
#define STRING(x)	XSTRING(x)

Uwe Schulzweida's avatar
Uwe Schulzweida committed
19
static char gribapi_libvers[64] = "";
20
21
22
#if  defined  (HAVE_LIBGRIB_API)
static int gribapi_libvers_init;
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
23

24
25
26
27
28

void gribapiLibraryVersion(int* major_version, int* minor_version, int* revision_version)
{
#if  defined  (HAVE_LIBGRIB_API)
  long version = grib_get_api_version();
Thomas Jahns's avatar
Thomas Jahns committed
29
30
31
  (*major_version)    = (int)(version/10000);
  (*minor_version)    = (int)((version-(*major_version)*10000)/100);
  (*revision_version) = (int)(version-(*major_version)*10000-(*minor_version)*100);
32
33
34
35
36
37
38
#else
  (*major_version)    = 0;
  (*minor_version)    = 0;
  (*revision_version) = 0;
#endif
}

39
const char *gribapiLibraryVersionString(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
40
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
41
#if  defined  (HAVE_LIBGRIB_API)
42
43
44
  if (!gribapi_libvers_init)
    {
      int major_version, minor_version, revision_version;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
45

46
      gribapiLibraryVersion(&major_version, &minor_version, &revision_version);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
47

48
      sprintf(gribapi_libvers, "%d.%d.%d", major_version, minor_version, revision_version);
49
50
      gribapi_libvers_init = 1;
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
51
52
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
53
54
  return (gribapi_libvers);
}
55
56


57
void gribContainersNew(stream_t * streamptr)
58
{
59
  int editionNumber = 2;
60

61
  if ( streamptr->filetype == FILETYPE_GRB ) editionNumber = 1;
62
  (void)editionNumber;
63
64
65
66
67
68
69
#if  defined  (HAVE_LIBCGRIBEX)
  if ( streamptr->filetype == FILETYPE_GRB )
    {
    }
  else
#endif
    {
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
      int nvars = streamptr->nvars;

#if defined (GRIBCONTAINER2D)
      gribContainer_t **gribContainers;
      gribContainers = (gribContainer_t **) malloc(nvars*sizeof(gribContainer_t *));

      for ( int varID = 0; varID < nvars; ++varID )
        {
          int nlevs = streamptr->vars[varID].nlevs;
          gribContainers[varID] = (gribContainer_t *) malloc(nlevs*sizeof(gribContainer_t));

          for ( int levelID = 0; levelID < nlevs; ++levelID )
            {
              gribContainers[varID][levelID].gribHandle = gribHandleNew(editionNumber);
              gribContainers[varID][levelID].init = FALSE;
            }
	}

      streamptr->gribContainers = (void **) gribContainers;
#else
90
      gribContainer_t *gribContainers
91
        = (gribContainer_t *)malloc((size_t)nvars*sizeof(gribContainer_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
92

93
94
95
96
97
      for ( int varID = 0; varID < nvars; ++varID )
        {
          gribContainers[varID].gribHandle = gribHandleNew(editionNumber);
          gribContainers[varID].init = FALSE;
	}
98

Uwe Schulzweida's avatar
Uwe Schulzweida committed
99
      streamptr->gribContainers = (void *) gribContainers;
100
#endif
101
    }
102
103
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
104

105
void gribContainersDelete(stream_t * streamptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
106
107
108
{
  if ( streamptr->gribContainers )
    {
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
      int nvars = streamptr->nvars;

#if defined (GRIBCONTAINER2D)
      gribContainer_t **gribContainers = (gribContainer_t **) streamptr->gribContainers;

      for ( int varID = 0; varID < nvars; ++varID )
	{
          int nlevs = streamptr->vars[varID].nlevs;
          for ( int levelID = 0; levelID < nlevs; ++levelID )
            {
              gribHandleDelete(gribContainers[varID][levelID].gribHandle);
            }
          free(gribContainers[varID]);
	}
#else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
124
125
      gribContainer_t *gribContainers = (gribContainer_t *) streamptr->gribContainers;

126
      for ( int varID = 0; varID < nvars; ++varID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
127
	{
128
          gribHandleDelete(gribContainers[varID].gribHandle);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
129
	}
130
131
132
133
#endif

      free(gribContainers);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
134
135
136
      streamptr->gribContainers = NULL;
    }
}
137
138
139
140
141
142
143
144
145
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */