cdf.c 4.26 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>

#include "cdf.h"
#include "cdi.h"
#include "stream_int.h"
#include "cdf_int.h"


const char *cdfLibraryVersion(void)
{
#if  defined  (HAVE_LIBNETCDF)
  return (nc_inq_libvers());
#else
  return ("library undefined");
#endif
}

26
#if  defined(HAVE_LIBHDF5)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
27
28
29
30
31
32
33
34
#if defined(__cplusplus)
extern "C" {
#endif
  int H5get_libversion(unsigned *, unsigned *, unsigned *);
#if defined(__cplusplus)
}
#endif
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
35
36
37

const char *hdfLibraryVersion(void)
{
38
#if  defined(HAVE_LIBHDF5)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
39
40
41
  static char hdf_libvers[256];
  unsigned majnum, minnum, relnum;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
42
  H5get_libversion(&majnum, &minnum, &relnum);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
43
44
45
46
47

  sprintf(hdf_libvers, "%u.%u.%u", majnum, minnum, relnum);

  return (hdf_libvers);
#else
48
  return ("library undefined");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
49
50
51
#endif
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
52
53
54
55
56
57

int CDF_Debug   = 0;    /* If set to 1, debugging           */


void cdfDebug(int debug)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
58
  static const char *func = "cdfDebug";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
59
60
61
62
63
64
65

  CDF_Debug = debug;

  if ( CDF_Debug )
    Message(func, "debug level %d", debug);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
66
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
void cdfComment(int ncid)
{
#if  defined  (HAVE_LIBNETCDF)
  static char comment[256] = "Climate Data Interface version ";
  static int init = 0;
  char *blank;
  int size = 0;

  if ( ! init )
    {
      init = 1;
      blank = strchr(cdiLibraryVersion(), ' ');
      if ( blank ) size = blank - cdiLibraryVersion();

      if ( size == 0 || ! isdigit((int) *cdiLibraryVersion()) )
	strcat(comment, "??");
      else
	strncat(comment, cdiLibraryVersion(), size);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
85
      strcat(comment, " (http://code.zmaw.de/projects/cdi)");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
86
87
88
89
90
91
92
93
94
95
96
97
98
    }

  cdf_put_att_text(ncid, NC_GLOBAL, "CDI", strlen(comment), comment);
  cdf_put_att_text(ncid, NC_GLOBAL, "Conventions", 6, "CF-1.0");
#endif
}


int cdfOpenFile(const char *filename, const char *mode, int version)
{
  int ncid = -1;
#if  defined  (HAVE_LIBNETCDF)
  int fmode;
99
  int writemode = NC_CLOBBER;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
100
  int readmode = NC_NOWRITE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
101
  int status;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
102
103
104
105
106
107
108
109
110
111

  if ( filename == NULL )
    ncid = CDI_EINVAL;
  else
    {
      switch (*mode)
	{
	case 'r':
	case 'R':
	  fmode = 'r';
Uwe Schulzweida's avatar
Uwe Schulzweida committed
112
	  status = cdf_open(filename, readmode, &ncid);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
113
	  if ( status > 0 && ncid < 0 ) ncid = CDI_ESYSTEM;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
114
115
116
117
118
119
120
121
122
123
124
125
#if  defined  (NC_NETCDF4)
	  else
	    {
	      int format;
	      (void) nc_inq_format(ncid, &format);
	      if ( format == NC_FORMAT_NETCDF4 )
		{
		  cdf_close(ncid);
		  ncid = CDI_EUNC4;
		}
	    }
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
126
127
128
129
130
	  break;
	case 'w':
	case 'W':
	  fmode = 'w';
#if  defined  (NC_64BIT_OFFSET)
131
132
133
134
	  if ( version == 2 ) writemode = NC_CLOBBER | NC_64BIT_OFFSET;
#endif
#if  defined  (NC_NETCDF4)
	  if ( version == 4 ) writemode = NC_CLOBBER | NC_NETCDF4 | NC_CLASSIC_MODEL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#endif
	  cdf_create(filename, writemode, &ncid);
	  cdfComment(ncid);
	  break;
	case 'a':
	case 'A':
	  fmode = 'a';
	  cdf_open(filename, NC_WRITE, &ncid);
	  break;
	default:
	  ncid = CDI_EINVAL;
	}
    }
#endif

  return (ncid);
}


int cdfOpen(const char *filename, const char *mode)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
156
  static const char *func = "cdfOpen";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
  int fileID = 0;

  if ( CDF_Debug )
    Message(func, "open %s with mode %c", filename, *mode);

  fileID = cdfOpenFile(filename, mode, 1);

  if ( CDF_Debug )
    Message(func, "file %s opened with id %d", filename, fileID);

  return (fileID);
}


int cdfOpen64(const char *filename, const char *mode)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
173
  static const char *func = "cdfOpen64";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
174
175
176
177
178
179
180
181
  int fileID = -1;

  if ( CDF_Debug )
    Message(func, "open %s with mode %c", filename, *mode);

#if  defined  (HAVE_LIBNETCDF)
#if  ! defined  (NC_64BIT_OFFSET)
  fileID = CDI_ELIBNAVAIL;
182
  return (fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
183
184
185
#endif
#endif

186
  fileID = cdfOpenFile(filename, mode, 2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
187

188
189
190
191
192
193
194
195
196
  if ( CDF_Debug )
    Message(func, "file %s opened with id %d", filename, fileID);

  return (fileID);
}


int cdf4Open(const char *filename, const char *mode)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
197
  static const char *func = "cdf4Open";
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
  int fileID = -1;

  if ( CDF_Debug )
    Message(func, "open %s with mode %c", filename, *mode);

#if  defined  (HAVE_LIBNETCDF)
#if  ! defined  (NC_NETCDF4)
  fileID = CDI_ELIBNAVAIL;
  return (fileID);
#endif
#endif

  fileID = cdfOpenFile(filename, mode, 4);

  if ( CDF_Debug )
    Message(func, "file %s opened with id %d", filename, fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229

  return (fileID);
}


void cdfCloseFile(int fileID)
{
#if  defined  (HAVE_LIBNETCDF)
  cdf_close(fileID);
#endif
}

void cdfClose(int fileID)
{
  cdfCloseFile(fileID);
}