cdf.c 4.46 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
5
6
7
8
9
10
11
12
#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"
13
#include "cdi_int.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
14
15
16
17
18
19
20
21
22
23
24
25
#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
58
59
60

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


void cdfDebug(int debug)
{
  CDF_Debug = debug;

  if ( CDF_Debug )
61
    Message("debug level %d", debug);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
62
63
}

64
#if  defined  (HAVE_LIBNETCDF)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
65
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
66
67
68
69
70
71
72
73
void cdfComment(int ncid)
{
  static char comment[256] = "Climate Data Interface version ";
  static int init = 0;

  if ( ! init )
    {
      init = 1;
Thomas Jahns's avatar
Thomas Jahns committed
74
75
76
      const char *libvers = cdiLibraryVersion();
      const char *blank = strchr(libvers, ' ');
      size_t size = blank ? (size_t)(blank - libvers) : 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
77

Thomas Jahns's avatar
Thomas Jahns committed
78
      if ( size == 0 || ! isdigit((int) *libvers) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
79
80
	strcat(comment, "??");
      else
Thomas Jahns's avatar
Thomas Jahns committed
81
	strncat(comment, libvers, size);
82
      strcat(comment, " (https://code.zmaw.de/projects/cdi)");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
83
84
85
    }

  cdf_put_att_text(ncid, NC_GLOBAL, "CDI", strlen(comment), comment);
86
  cdf_put_att_text(ncid, NC_GLOBAL, "Conventions", 6, "CF-1.4");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
87
}
88
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
89

90
int cdfOpenFile(const char *filename, const char *mode, int *filetype)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
91
92
93
{
  int ncid = -1;
#if  defined  (HAVE_LIBNETCDF)
94
  int fmode = tolower(*mode);
95
  int writemode = NC_CLOBBER;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
96
  int readmode = NC_NOWRITE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
97
  int status;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
98
99
100
101
102

  if ( filename == NULL )
    ncid = CDI_EINVAL;
  else
    {
103
      switch (fmode)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
104
105
	{
	case 'r':
Uwe Schulzweida's avatar
Uwe Schulzweida committed
106
	  status = cdf_open(filename, readmode, &ncid);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
107
	  if ( status > 0 && ncid < 0 ) ncid = CDI_ESYSTEM;
108
#if  defined  (HAVE_NETCDF4)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
109
110
111
112
	  else
	    {
	      int format;
	      (void) nc_inq_format(ncid, &format);
113
	      if ( format == NC_FORMAT_NETCDF4_CLASSIC )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
114
		{
115
		  *filetype = FILETYPE_NC4C;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
116
117
118
		}
	    }
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
119
120
121
	  break;
	case 'w':
#if  defined  (NC_64BIT_OFFSET)
122
	  if      ( *filetype == FILETYPE_NC2  ) writemode |= NC_64BIT_OFFSET;
123
#endif
124
#if  defined  (HAVE_NETCDF4)
125
126
	  if      ( *filetype == FILETYPE_NC4  ) writemode |= NC_NETCDF4;
	  else if ( *filetype == FILETYPE_NC4C ) writemode |= NC_NETCDF4 | NC_CLASSIC_MODEL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#endif
	  cdf_create(filename, writemode, &ncid);
	  cdfComment(ncid);
	  break;
	case 'a':
	  cdf_open(filename, NC_WRITE, &ncid);
	  break;
	default:
	  ncid = CDI_EINVAL;
	}
    }
#endif

  return (ncid);
}


int cdfOpen(const char *filename, const char *mode)
{
  int fileID = 0;
147
  int filetype = FILETYPE_NC;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
148
149

  if ( CDF_Debug )
150
    Message("Open %s with mode %c", filename, *mode);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
151

152
  fileID = cdfOpenFile(filename, mode, &filetype);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
153
154

  if ( CDF_Debug )
155
    Message("File %s opened with id %d", filename, fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
156
157
158
159
160
161
162
163

  return (fileID);
}


int cdfOpen64(const char *filename, const char *mode)
{
  int fileID = -1;
164
165
  int open_file = TRUE;
  int filetype = FILETYPE_NC2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
166
167

  if ( CDF_Debug )
168
    Message("Open %s with mode %c", filename, *mode);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
169
170
171

#if  defined  (HAVE_LIBNETCDF)
#if  ! defined  (NC_64BIT_OFFSET)
172
  open_file = FALSE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
173
174
175
#endif
#endif

176
177
178
  if ( open_file )
    {
      fileID = cdfOpenFile(filename, mode, &filetype);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
179

180
181
182
183
184
185
186
      if ( CDF_Debug )
	Message("File %s opened with id %d", filename, fileID);
    }
  else
    {
      fileID = CDI_ELIBNAVAIL;
    }
187
188
189
190
191

  return (fileID);
}


192
int cdf4Open(const char *filename, const char *mode, int *filetype)
193
194
{
  int fileID = -1;
195
  int open_file = FALSE;
196
197

  if ( CDF_Debug )
198
    Message("Open %s with mode %c", filename, *mode);
199

200
201
#if  defined  (HAVE_NETCDF4)
  open_file = TRUE;
202
203
#endif

204
205
206
  if ( open_file )
    {
      fileID = cdfOpenFile(filename, mode, filetype);
207

208
209
210
211
212
213
214
      if ( CDF_Debug )
	Message("File %s opened with id %d", filename, fileID);
    }
  else
    {
      fileID = CDI_ELIBNAVAIL;
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230

  return (fileID);
}


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

void cdfClose(int fileID)
{
  cdfCloseFile(fileID);
}
231
232
233
234
235
236
237
238
239
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */