Commit 207daf69fd7a40b40397522dca8c48fdc9ad8a7d
1 parent
fc040e73
Exists in
master
and in
8 other branches
exclude obsolete cdf2nc
Showing
4 changed files
with
4 additions
and
1644 deletions
Show diff stats
CMakeLists.txt.All
... | ... | @@ -28,6 +28,9 @@ endif() |
28 | 28 | |
29 | 29 | set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules/") |
30 | 30 | |
31 | +# Fedora30 | |
32 | +# include_directories( AFTER "/usr/include/tirpc" ) | |
33 | + | |
31 | 34 | find_package( Threads REQUIRED ) |
32 | 35 | find_package( CRYPT REQUIRED ) |
33 | 36 | find_package( NetCDF REQUIRED ) |
... | ... | @@ -62,7 +65,6 @@ configure_file ( |
62 | 65 | MESSAGE( STATUS "Build DD_Server Project" ) |
63 | 66 | add_subdirectory(src/SERVER) |
64 | 67 | add_subdirectory(src/DECODERS/ascii2nc) |
65 | -add_subdirectory(src/DECODERS/cdf2nc) | |
66 | 68 | add_subdirectory(src/DECODERS/cdfnew2nc) |
67 | 69 | add_subdirectory(src/DECODERS/nc2nc) |
68 | 70 | add_subdirectory(src/DECODERS/themis) |
... | ... | @@ -84,7 +86,7 @@ install(DIRECTORY "src/DDADMIN/MANAGER/" DESTINATION bin/USERMANAGER) |
84 | 86 | install(DIRECTORY "src/DDSERVICES/SOAP/" DESTINATION DDService) |
85 | 87 | install(DIRECTORY "src/DDSERVICES/REST/" DESTINATION DDService) |
86 | 88 | |
87 | -install(FILES "src/DECODERS/cdf2nc/cdf_to_netcdf_mapping.dat" DESTINATION bin PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ) | |
89 | +install(FILES "src/DECODERS/cdfnew2nc/cdf_to_netcdf_mapping.dat" DESTINATION bin PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ) | |
88 | 90 | |
89 | 91 | file(GLOB config_files "info/REMOTEDATA/*.xml") |
90 | 92 | install(FILES ${config_files} DESTINATION ${DDBASEINFO} PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ) |
... | ... |
src/DECODERS/cdf2nc/CMakeLists.txt deleted
... | ... | @@ -1,26 +0,0 @@ |
1 | - | |
2 | -PROJECT(cdf2nc) | |
3 | - | |
4 | -include_directories( | |
5 | - ${DDCLIENTINCLUDE_DIR} | |
6 | - ${NETCDFINCLUDE_DIR} | |
7 | - ${libcdf_INCLUDE_DIR} | |
8 | -) | |
9 | - | |
10 | -#Configuration de l'exรฉcutable | |
11 | -file( | |
12 | - GLOB_RECURSE | |
13 | - source_files | |
14 | - ./* | |
15 | -) | |
16 | - | |
17 | -ADD_EXECUTABLE (cdf2nc ${source_files} ) | |
18 | - | |
19 | -target_link_libraries( | |
20 | - cdf2nc | |
21 | - ${DDCLIENTLIBRARY} | |
22 | - ${NETCDFLIBRARY} | |
23 | - ${libcdf_LIBRARIES} | |
24 | -) | |
25 | - | |
26 | -install (TARGETS cdf2nc DESTINATION bin) |
src/DECODERS/cdf2nc/cdf2nc.c deleted
... | ... | @@ -1,1616 +0,0 @@ |
1 | -/** | |
2 | -* @file cdf2nc.c | |
3 | -* @version $Id: cdf2nc.c,v 1.7 2014/06/25 10:32:50 budnik Exp $ | |
4 | -* @brief DD Server Decoder | |
5 | -*/ | |
6 | - | |
7 | -/*===================================================================== | |
8 | - * cdf2nc.c | |
9 | - * V.1.2 | |
10 | - * CDF to NetCDF converter | |
11 | - * usage: cdf2nc <FILE NAME>.cdf | |
12 | - * | |
13 | - * Versions: | |
14 | - * Aug 4, 2007, V.1.0, Knizhnikova-Fedorova | |
15 | - * Aug 6, 2007, V.1.1, Fedorov, "Time" dimension | |
16 | - * Jan 16, 2008 V.1.2 Exclude "false" CDF dimension (1) | |
17 | - *=====================================================================*/ | |
18 | - | |
19 | -#include <stdio.h> | |
20 | -#include <stdlib.h> | |
21 | -#include <netcdf.h> | |
22 | -#include <cdf.h> | |
23 | -#include <string.h> | |
24 | -#include <time.h> | |
25 | -#include <math.h> | |
26 | -#include <DD.h> | |
27 | - | |
28 | -#define MAX_FILE_NAME_LEN 120 /* Max. file name length */ | |
29 | -#define MAX_ATTRS 3000 /* Max # of CDF attributes */ | |
30 | -#define UNKNOWN_DT -99 /* Unsupported CDF datatype indicator */ | |
31 | - | |
32 | -#define DEFAULT_MAPPING_FILE "cdf_to_netcdf_mapping.dat" | |
33 | -#define MAX_REC_LENGTH NC_MAX_NAME+CDF_ATTR_NAME_LEN+2 | |
34 | - /* Max record length of DEFAULT_MAPPING_FILE */ | |
35 | - | |
36 | -#define TRUE 1 | |
37 | -#define FALSE 0 | |
38 | - | |
39 | -#define TIMESHIFT 62167219200.0 /*01/01/1970 - 01/01/0000 Gregorian Calendar*/ | |
40 | -#define UNLIMITDIM "Time" /* Name of unlimided dimension */ | |
41 | -#define DD_TIME_STRING_LEN 17 | |
42 | - | |
43 | -/************************************* | |
44 | - Global variables and structures | |
45 | -**************************************/ | |
46 | -CDFid id; /* CDF file ID */ | |
47 | -int ncid; /* netCDF file ID */ | |
48 | - | |
49 | -short isTimeSet = 0; | |
50 | - | |
51 | -long nZvars, /* Number of zVariables in a CDF file */ | |
52 | - nAttrs; /* Number of attributes (global & variable) in a CDF file */ | |
53 | - | |
54 | -typedef struct ncdim { /* netCDF dimension structure */ | |
55 | - char name[NC_MAX_NAME+1]; /* Dimension name */ | |
56 | - int size; /* Dimension size */ | |
57 | - int id; /* Dimension id */ | |
58 | -} netcdfDimension; | |
59 | - | |
60 | -netcdfDimension ncDims[NC_MAX_DIMS]; /* max dimensions per netCDF file */ | |
61 | -int totNCdims = 0; /* Total # of netCDF dimensions created */ | |
62 | - | |
63 | -int StartTimeID, StopTimeID, TimeLengthID; | |
64 | - | |
65 | -char StartT[TIMELENGTH]; // Start time | |
66 | -char StopT[TIMELENGTH]; // Stop time | |
67 | - | |
68 | -dd_time_t StartTime, StopTime; | |
69 | - | |
70 | -typedef struct cdfvar { /* CDF variable structure */ | |
71 | - char name[CDF_VAR_NAME_LEN+1]; | |
72 | - long datatype; | |
73 | - long numElements; /* string length for CDF_CHAR, 1 otherwise */ | |
74 | - long dimensionality; /* variable dimensionality */ | |
75 | - long dimSizes[CDF_MAX_DIMS]; /* variable dimension sizes */ | |
76 | - long recVariance; /* record variance */ | |
77 | - long numRecs; /* # of records this variable contains */ | |
78 | -} CDFvar; | |
79 | - | |
80 | -char SpecialSymbol[9] = {' ', '#', '%', '@', '+', '.', '>', '<', '/'}; | |
81 | - | |
82 | -/**************************************************************************** | |
83 | - * Arrays to hold the CDF-to-netCDF mapping information that are loaded | |
84 | - * from the default or user-define mapping file. | |
85 | - ****************************************************************************/ | |
86 | -char netcdfAttrNames[MAX_ATTRS][NC_MAX_NAME]; | |
87 | -char cdfAttrNames[MAX_ATTRS][CDF_ATTR_NAME_LEN+1]; | |
88 | -char comments[MAX_ATTRS][CDF_ATTR_NAME_LEN+1]; | |
89 | - | |
90 | -int totAttrsInMappingFile; /* # of attributes in the mapping file */ | |
91 | -char mappingFile[MAX_FILE_NAME_LEN]; /* Mapping file name */ | |
92 | - | |
93 | -int DEBUG = FALSE; | |
94 | - | |
95 | - | |
96 | -/************************** | |
97 | - Function prototypes | |
98 | -**************************/ | |
99 | -int endsWith(char *, char *); | |
100 | -char * strlaststr(char *, char *); | |
101 | -void get_cdf_attribute(long, long, long, int); | |
102 | -void get_cdf_attr_data(char *, long, long, long, int); | |
103 | -char * cdf_str_datatype (long); | |
104 | -char * NC_datatype (nc_type); | |
105 | -nc_type get_netcdf_datatype (long); | |
106 | -void usage(); | |
107 | -CDFid cdf_open(char *); | |
108 | -void read_cdf_file_info(); | |
109 | -void get_zVariables(); | |
110 | -void create_netcdf_dimensions (CDFvar, int *, int *); | |
111 | -int create_netcdf_variable (CDFvar, int, short *); | |
112 | -void get_cdf_variable_data (long, CDFvar); | |
113 | -void read_cdf_variable_data (long, long, long, long, long [], void *); | |
114 | -void removeFilepath(); | |
115 | -void handle_netcdf_error (int); | |
116 | -void memory_error(char *, char *); | |
117 | -void cdf_status_handler (CDFstatus, char *); | |
118 | -void insufficient_memory(); /* used for debug */ | |
119 | -char * getNextArgument (int, char *[], int); | |
120 | -void parseCmdLine (int, char *[]); | |
121 | -void errorMsg (char *); | |
122 | -void map_CDF_attr_to_netCDF (char *, char *); | |
123 | -char * isotime_to_dd_time (char * str_isotime); | |
124 | - | |
125 | -/**************************************************************************** | |
126 | - NOTE: | |
127 | - | |
128 | - CDF has the following features that are not supported by netCDF: | |
129 | - | |
130 | - - CDF_EPOCH datatype (8-byte real number) that is used to store time | |
131 | - values referenced from a particular epoch that is | |
132 | - 01-Jan-0000 00:00:00.000. CDF_EPOCH values are the number of | |
133 | - milliseconds since the epoch described above. | |
134 | - | |
135 | - - CDF_EPOCH16 datatype (16-byte real number) that is used to store time | |
136 | - values referenced from a particular epoch that is | |
137 | - 01-Jan-0000 00:00:00.000.000.000.000. CDF_EPOCH16 values are the | |
138 | - number of milliseconds since the epoch described above. | |
139 | - | |
140 | - - CDF global attribute (not variable attribute) can have multiple | |
141 | - attribute entries and each entry can have a different datatype. | |
142 | -*****************************************************************************/ | |
143 | -int main(int argc, char *argv[]) | |
144 | -{ | |
145 | - FILE *inFile; /* Pointer to the CDF-to-netCDF mapping file */ | |
146 | - char rec[MAX_REC_LENGTH]; /* CDF-to-netCDF mapping record */ | |
147 | - | |
148 | - /************************/ | |
149 | - /* netCDF declaration */ | |
150 | - /************************/ | |
151 | - int status, /* netCDF status code */ | |
152 | - dummy, i; | |
153 | - | |
154 | - char *ptr, fileName[MAX_FILE_NAME_LEN]; | |
155 | - | |
156 | - /**********************/ | |
157 | - /* CDF declaration */ | |
158 | - /**********************/ | |
159 | - CDFstatus cstatus; /* CDF status code */ | |
160 | - long attrId; /* CDF attribute ID */ | |
161 | - | |
162 | - if (argc <= 1) | |
163 | - usage(); /* CDF input file name not specified */ | |
164 | - else { | |
165 | - strcpy(fileName, argv[argc-1]); /* Get the input file name */ | |
166 | - strcpy(mappingFile, "UNSPECIFIED"); | |
167 | - parseCmdLine(argc, argv); | |
168 | - if (strcmp(mappingFile, fileName) == 0) | |
169 | - errorMsg("** Error - either input file or mapping file is missing"); | |
170 | - if (strcmp(mappingFile,"UNSPECIFIED") == 0) | |
171 | - { | |
172 | - strcpy(mappingFile,getenv("DDLIB")); | |
173 | - strcat(mappingFile, DEFAULT_MAPPING_FILE); | |
174 | - } | |
175 | - } | |
176 | - | |
177 | - /***********************************************/ | |
178 | - /* Load the CDF-to-netCDF mapping information */ | |
179 | - /***********************************************/ | |
180 | - if ((inFile = fopen(mappingFile, "r")) == NULL) { | |
181 | - printf ("** Cannot open file: %s **\n", mappingFile); | |
182 | - exit (1); | |
183 | - } | |
184 | - | |
185 | - totAttrsInMappingFile = 0; | |
186 | - while (fgets(rec, MAX_REC_LENGTH, inFile) != NULL) { | |
187 | - rec[strlen(rec)-1] = '\0'; /* Remove the newline character */ | |
188 | - | |
189 | - /* Process the mapping record if it's not a comment */ | |
190 | - if (rec[0] != '#' && rec[0] != ' ' && strlen(rec) > 0) { | |
191 | - sscanf(rec, "%s %s", | |
192 | - netcdfAttrNames[totAttrsInMappingFile], | |
193 | - cdfAttrNames[totAttrsInMappingFile]); | |
194 | - ptr = (char *) strstr(rec, "//"); | |
195 | - if (ptr != NULL) { | |
196 | - ptr += 3; | |
197 | - strcpy (comments[totAttrsInMappingFile], ptr); | |
198 | - | |
199 | - } | |
200 | - if (DEBUG) | |
201 | - printf("%s %s %s\n", | |
202 | - netcdfAttrNames[totAttrsInMappingFile], | |
203 | - cdfAttrNames[totAttrsInMappingFile], | |
204 | - comments[totAttrsInMappingFile]); | |
205 | - totAttrsInMappingFile++; | |
206 | - } | |
207 | - } | |
208 | - | |
209 | - /*********************************************************** | |
210 | - * Process the CDF information and create a netCDF file | |
211 | - ***********************************************************/ | |
212 | - // printf ("\nInput file name: %s\n", fileName); | |
213 | - id = cdf_open (fileName); | |
214 | - | |
215 | - /* Remove the file path if it exists (/home/mydata.cdf => mydata.cdf). */ | |
216 | - removeFilepath(fileName); | |
217 | - | |
218 | - if (endsWith(fileName,".cdf")) { | |
219 | - /* Strip off the .cdf file extension. */ | |
220 | - ptr = (char *) strlaststr(fileName, ".cdf"); | |
221 | - if (ptr != NULL) *ptr = '\0'; | |
222 | - } | |
223 | - strcat(fileName, ".nc"); | |
224 | - status = nc_create(fileName, NC_CLOBBER, &ncid); | |
225 | - if (status != NC_NOERR) handle_netcdf_error(status); | |
226 | - // printf ("Output file name: %s\n", fileName); | |
227 | - | |
228 | - /*********************************************************************** | |
229 | - * Get the number of dimensions, number of variables, number of global | |
230 | - * attributes. | |
231 | - * | |
232 | - * Note that the information retrieved from read_cdf_file_info are stored | |
233 | - * into the global variables defined at the top. | |
234 | - ***********************************************************************/ | |
235 | - read_cdf_file_info (); | |
236 | - if (DEBUG) printf ("nAttrs=%d, nZvars=%d\n", nAttrs, nZvars); | |
237 | - | |
238 | - /* Get the CDF global attributes and create netCDF global attributes. */ | |
239 | - if (DEBUG) printf ("Global attributes:\n"); | |
240 | - dummy = -1; | |
241 | - for (attrId = 0; attrId < nAttrs; attrId++) | |
242 | - get_cdf_attribute(attrId, GLOBAL_SCOPE, (long) dummy, dummy); | |
243 | - | |
244 | - /* Write the placeholder attributes. */ | |
245 | - for (i=0; i < totAttrsInMappingFile; i++) { | |
246 | - if (strcmp(cdfAttrNames[i],"*") == 0) { | |
247 | - status = nc_put_att_text (ncid, | |
248 | - NC_GLOBAL, /* vavriable ID */ | |
249 | - netcdfAttrNames[i], /* Attribute name */ | |
250 | - strlen(comments[i]), /* # of attr values */ | |
251 | - comments[i]); | |
252 | - } | |
253 | - } | |
254 | - | |
255 | - /* Process CDF variables and create netCDF variables */ | |
256 | - if (nZvars > 0) get_zVariables(); | |
257 | - | |
258 | - /* Close the netCDF and CDF files */ | |
259 | - cstatus = CDFlib (CLOSE_, CDF_, NULL_); | |
260 | - if (cstatus != CDF_OK) cdf_status_handler (cstatus, "CLOSE_, CDF_"); | |
261 | - | |
262 | - status = nc_close(ncid); | |
263 | - if (status != NC_NOERR) handle_netcdf_error(status); | |
264 | -} | |
265 | - | |
266 | - | |
267 | -/*---------------------------------------------------------------------------- | |
268 | - * TRUE if s1 ends with s2. Otherwise, FALSE is returned. | |
269 | - *---------------------------------------------------------------------------*/ | |
270 | -int endsWith (char *s1, char *s2) | |
271 | -{ | |
272 | - int i; | |
273 | - char *ps1, *ps2; | |
274 | - | |
275 | - if (strlen(s2) > strlen(s1)) | |
276 | - return FALSE; | |
277 | - | |
278 | - ps1 = s1 + strlen(s1) - strlen(s2); | |
279 | - ps2 = s2; | |
280 | - | |
281 | - for (i=0; i < strlen(s2); i++) | |
282 | - if (*(ps1++) != *(ps2++)) | |
283 | - return FALSE; | |
284 | - | |
285 | - return TRUE; | |
286 | -} | |
287 | - | |
288 | - | |
289 | -/*--------------------------------------------------------------------------------- | |
290 | - * Find the last occurence of s2 in s1. If s21 is not found, s1 is returned. | |
291 | - *--------------------------------------------------------------------------------*/ | |
292 | -char * strlaststr (char *s1, char *s2) | |
293 | -{ | |
294 | - char *sc2, *psc1, *ps1; | |
295 | - | |
296 | - if (*s2 == '\0') | |
297 | - return((char *)s1); | |
298 | - | |
299 | - ps1 = s1 + strlen(s1); | |
300 | - | |
301 | - while(ps1 != s1) { | |
302 | - --ps1; | |
303 | - for (psc1 = ps1, sc2 = s2; ; ) | |
304 | - if (*(psc1++) != *(sc2++)) | |
305 | - break; | |
306 | - else if (*sc2 == '\0') | |
307 | - return ((char *)ps1); | |
308 | - } | |
309 | - return ((char *)NULL); | |
310 | -} | |
311 | - | |
312 | - | |
313 | -/*-------------------------------------------------------------------------- | |
314 | - * This routine opens a CDF file | |
315 | - *-------------------------------------------------------------------------*/ | |
316 | -CDFid cdf_open (char *fileName) | |
317 | -{ | |
318 | - CDFstatus status; | |
319 | - CDFid id; | |
320 | - char msg[80]; | |
321 | - | |
322 | - status = CDFlib (OPEN_, CDF_, fileName, /* in - file name to be opened */ | |
323 | - &id, /* out - CDF file ID */ | |
324 | - NULL_); | |
325 | - if (status != CDF_OK) { | |
326 | - strcpy(msg, "OPEN_, CDF_, "); | |
327 | - strcat(msg, fileName); | |
328 | - cdf_status_handler (status, msg); | |
329 | - } | |
330 | - return id; | |
331 | -} | |
332 | - | |
333 | - | |
334 | -/*--------------------------------------------------------------------------- | |
335 | - * This routine retrievs the following information: | |
336 | - * | |
337 | - * nAttr - number of attributes (including global and variable) | |
338 | - * nZvars - number of zVariables | |
339 | - * | |
340 | - * CDF file can have both rVariables (old style) and zVariables (new style) | |
341 | - * simultaneously. zVariable is a superset of rVariable, and it is a lot | |
342 | - * more efficient and offers all the functionality a rVariable offers and | |
343 | - * more. Treat all CDF variables as zVariables. | |
344 | - *--------------------------------------------------------------------------*/ | |
345 | -void read_cdf_file_info () | |
346 | -{ | |
347 | - CDFstatus status; | |
348 | - | |
349 | - status = CDFlib (SELECT_, CDF_zMODE_, zMODEon2, | |
350 | - GET_, CDF_NUMATTRS_, &nAttrs, | |
351 | - CDF_NUMzVARS_, &nZvars, | |
352 | - NULL_); | |
353 | - if (status != CDF_OK) cdf_status_handler (status, "GET_, CDF_FILEINFO_"); | |
354 | -} | |
355 | - | |
356 | - | |
357 | -/*---------------------------------------------------------------------------- | |
358 | - * This routine retrieves the CDF attribute (global or variable) name | |
359 | - * and its data for the given CDF attribute ID. | |
360 | - *---------------------------------------------------------------------------*/ | |
361 | -void get_cdf_attribute(long attrNum, /* in - CDF attribute number/id */ | |
362 | - long scope, /* in - CDF attribute scope */ | |
363 | - long cdfVarId, /* in - CDF variable number/id */ | |
364 | - int netcdfVarId) /* in - netCDF variable ID */ | |
365 | -{ | |
366 | - int ncstatus, i, len; | |
367 | - long status, numEntries, datatype, attrScope, entryNum, | |
368 | - numElements; | |
369 | - nc_type ncDatatype; /* netCDF datatype */ | |
370 | - | |
371 | - char *cPtr, attrName[CDF_ATTR_NAME_LEN+1], | |
372 | - mappedAttrName[CDF_ATTR_NAME_LEN+1]; | |
373 | - signed char *scPtr; | |
374 | - short *sPtr; | |
375 | - int *iPtr; | |
376 | - float *fPtr; | |
377 | - double *dPtr; | |
378 | - | |
379 | - status = CDFlib (SELECT_, ATTR_, attrNum, | |
380 | - GET_, ATTR_NAME_, attrName, | |
381 | - ATTR_SCOPE_, &attrScope, | |
382 | - NULL_); | |
383 | - if (status != CDF_OK) cdf_status_handler (status, "SELECT_, ATTR_"); | |
384 | - | |
385 | - /****************************************************************/ | |
386 | - /* If the attribute scope is not the requested attribute scope */ | |
387 | - /* (VARIABLE_SCOPE or GLOBAL_SCOPE), do not process the current */ | |
388 | - /* attribute. */ | |
389 | - /****************************************************************/ | |
390 | - if (attrScope != scope) return; | |
391 | - | |
392 | - map_CDF_attr_to_netCDF(attrName, mappedAttrName); | |
393 | - strcpy(attrName, mappedAttrName); | |
394 | - | |
395 | - if (attrScope == GLOBAL_SCOPE) { | |
396 | - status = CDFlib (GET_, ATTR_NUMgENTRIES_, &numEntries, NULL_); | |
397 | - if (status != CDF_OK) | |
398 | - cdf_status_handler (status, "GET_, ATTR_NUMgENTRIES_"); | |
399 | - if (DEBUG) printf ("\t%s", attrName); | |
400 | - | |
401 | - /********************************************************************* | |
402 | - * While the CDF global attribute can have multiple entries of | |
403 | - * of different datatypes, the CDF variable attribute can only have | |
404 | - * one attribute entry. netCDF doesn't allow more than 1 attribute | |
405 | - * entry - handle this case | |
406 | - ********************************************************************/ | |
407 | - for (entryNum=0; entryNum < numEntries; entryNum++) { | |
408 | - if (entryNum > 0) /* Attribute has more than 1 entry */ | |
409 | - sprintf (attrName, "%s_%d", mappedAttrName, entryNum); | |
410 | - status = CDFlib (SELECT_, gENTRY_, entryNum, | |
411 | - GET_, gENTRY_DATATYPE_, &datatype, | |
412 | - gENTRY_NUMELEMS_, &numElements, | |
413 | - NULL_); | |
414 | - if (status == NO_SUCH_ENTRY) return; | |
415 | - if (status != CDF_OK) cdf_status_handler(status,"GET_ATTR_INFO_"); | |
416 | - get_cdf_attr_data (attrName, gENTRY_DATA_, datatype, numElements, | |
417 | - netcdfVarId); | |
418 | - } | |
419 | - } | |
420 | - else { | |
421 | - /********************************************************************* | |
422 | - * IMPORTANT NOTE: | |
423 | - * For the variable attribute, entry number is the variable ID. | |
424 | - *********************************************************************/ | |
425 | - status = CDFlib (SELECT_, zENTRY_, cdfVarId, | |
426 | - GET_, zENTRY_DATATYPE_, &datatype, | |
427 | - zENTRY_NUMELEMS_, &numElements, | |
428 | - NULL_); | |
429 | - /******************************************************************/ | |
430 | - /* If there's no attribute entry for the current attribute number */ | |
431 | - /* selected, process the next attribute. */ | |
432 | - /******************************************************************/ | |
433 | - if (status == NO_SUCH_ENTRY) return; | |
434 | - | |
435 | - if (status != CDF_OK) cdf_status_handler (status,"GET_ATTR_INFO_"); | |
436 | - if (DEBUG) printf ("\t%s", attrName); | |
437 | - get_cdf_attr_data (attrName, zENTRY_DATA_, datatype, numElements, | |
438 | - netcdfVarId); | |
439 | - } | |
440 | -} | |
441 | - | |
442 | - | |
443 | -/*-------------------------------------------------------------------------- | |
444 | - * This routine retrieves the CDF attribute data (a.k.a. attribute entries) | |
445 | - * and write its values to the target netCDF file. | |
446 | - *--------------------------------------------------------------------------*/ | |
447 | -void get_cdf_attr_data (char *attrName, /* in - attribute name */ | |
448 | - long entryData, /* in - type of attr entry data */ | |
449 | - long datatype, /* in - attribute datatype */ | |
450 | - long numElements, /* in - # of attribute values */ | |
451 | - int netcdfVarId) /* in - netCDF variable ID */ | |
452 | -{ | |
453 | - char entryDataType[20], msg[100], | |
454 | - epString[EPOCH_STRING_LEN+1], | |
455 | - ep16String[EPOCH16_STRING_LEN+1]; | |
456 | - double epoch16[2]; | |
457 | - | |
458 | - nc_type ncDatatype; /* netCDF datatype */ | |
459 | - int nc_status, /* netCDF status code */ | |
460 | - varId; /* netCDF variable ID */ | |
461 | - long status; /* CDF status code */ | |
462 | - | |
463 | - char *cPtr; | |
464 | - signed char *scPtr; | |
465 | - short *sPtr; | |
466 | - int *iPtr, i; | |
467 | - float *fPtr; | |
468 | - double *dPtr; | |
469 | - | |
470 | - /************************************************* | |
471 | - * entryData has one of following values: | |
472 | - * | |
473 | - * gENTRY_DATA_ - global attribute entry | |
474 | - * rENTRY_DATA_ - rVariable attribute entry | |
475 | - * zENTRY_DATA_ - zVariable attribute entry | |
476 | - *************************************************/ | |
477 | - if (entryData == gENTRY_DATA_) { | |
478 | - varId = NC_GLOBAL; | |
479 | - strcpy(entryDataType, "gENTRY_DATA_"); | |
480 | - } | |
481 | - else { | |
482 | - varId = netcdfVarId; | |
483 | - strcpy(entryDataType, "zENTRY_DATA_"); | |
484 | - } | |
485 | - strcpy (msg, "get_cdf_attr_data, GET_, "); | |
486 | - strcat (msg, entryDataType); | |
487 | - strcat (msg, cdf_str_datatype(datatype)); | |
488 | - | |
489 | - /* Map the CDF datatype to an appropriate netCDF datatype. */ | |
490 | - ncDatatype = get_netcdf_datatype (datatype); | |
491 | - | |
492 | - /* Remove trailing blank spaces if there are any */ | |
493 | - for (i=strlen(attrName)-1; i >= 0; i--) { | |
494 | - if (attrName[i] != ' ') { | |
495 | - attrName[i+1] = '\0'; | |
496 | - break; | |
497 | - } | |
498 | - } | |
499 | - /* Replace blanks space(s) with underscore(s). */ | |
500 | - for (i=0; i < strlen(attrName); i++) { | |
501 | - if (attrName[i] == ' ') attrName[i] = '_'; | |
502 | - } | |
503 | - | |
504 | - switch (datatype) { | |
505 | - case CDF_CHAR: | |
506 | - case CDF_UCHAR: | |
507 | - cPtr = (char *) malloc(numElements * sizeof(char) + 1); | |
508 | - if (cPtr == NULL) memory_error("get_cdf_attr_data", "NC_CHAR"); | |
509 | - status = CDFlib (GET_, entryData, cPtr, NULL_); | |
510 | - if (status != CDF_OK) cdf_status_handler (status, msg); | |
511 | - *(cPtr+numElements) = '\0'; /* End of string mark */ | |
512 | - if (DEBUG) printf (" = \"%s\"", cPtr); | |
513 | - nc_status = nc_put_att_text(ncid, varId, attrName, numElements, cPtr); | |
514 | - if (nc_status != NC_NOERR) handle_netcdf_error(nc_status); | |
515 | - free(cPtr); | |
516 | - break; | |
517 | - | |
518 | - case CDF_BYTE: | |
519 | - case CDF_INT1: | |
520 | - scPtr = (signed char *) malloc (sizeof(signed char) * numElements); | |
521 | - if (scPtr == NULL) memory_error("get_cdf_attr_data", "NC_BYTE"); | |
522 | - status = CDFlib (GET_, entryData, scPtr, NULL_); | |
523 | - if (status != CDF_OK) cdf_status_handler (status, msg); | |
524 | - if (DEBUG) { | |
525 | - printf (" = "); | |
526 | - for (i=0; i < numElements; i++) { | |
527 | - if (i > 0) printf (", "); | |
528 | - printf ("%d", *(scPtr+i)); | |
529 | - } | |
530 | - } | |
531 | - nc_status = nc_put_att_schar(ncid, varId, attrName, ncDatatype, | |
532 | - numElements, scPtr); | |
533 | - if (nc_status != NC_NOERR) handle_netcdf_error(nc_status); | |
534 | - free (scPtr); | |
535 | - break; | |
536 | - | |
537 | - case CDF_INT2: | |
538 | - case CDF_UINT1: | |
539 | - sPtr = (short *) malloc (sizeof(short) * numElements); | |
540 | - if (sPtr == NULL) memory_error("get_cdf_attr_data", "NC_SHORT"); | |
541 | - status = CDFlib (GET_, entryData, sPtr, NULL_); | |
542 | - if (status != CDF_OK) cdf_status_handler (status, msg); | |
543 | - if (DEBUG) { | |
544 | - printf (" = "); | |
545 | - for (i=0; i < numElements; i++) { | |
546 | - if (i > 0) printf (", "); | |
547 | - printf ("%d", *(sPtr+i)); | |
548 | - } | |
549 | - } | |
550 | - nc_status = nc_put_att_short(ncid, varId, attrName, ncDatatype, | |
551 | - numElements, sPtr); | |
552 | - if (nc_status != NC_NOERR) handle_netcdf_error(nc_status); | |
553 | - free (sPtr); | |
554 | - break; | |
555 | - | |
556 | - case CDF_INT4: | |
557 | - case CDF_UINT2: | |
558 | - iPtr = (int *) malloc (sizeof(int) * numElements); | |
559 | - if (iPtr == NULL) memory_error("get_cdf_attr_data", "NC_INT"); | |
560 | - status = CDFlib (GET_, entryData, iPtr, NULL_); | |
561 | - if (status != CDF_OK) cdf_status_handler (status, msg); | |
562 | - if (DEBUG) { | |
563 | - printf (" = "); | |
564 | - for (i=0; i < numElements; i++) { | |
565 | - if (i > 0) printf (", "); | |
566 | - printf ("%d", *(iPtr+i)); | |
567 | - } | |
568 | - } | |
569 | - nc_status = nc_put_att_int(ncid, varId, attrName, ncDatatype, | |
570 | - numElements, iPtr); | |
571 | - if (nc_status != NC_NOERR) handle_netcdf_error(nc_status); | |
572 | - free (iPtr); | |
573 | - break; | |
574 | - | |
575 | - case CDF_FLOAT: | |
576 | - case CDF_REAL4: | |
577 | - fPtr = (float *) malloc (sizeof(float) * numElements); | |
578 | - if (fPtr == NULL) memory_error("get_cdf_attr_data", "NC_FLOAT"); | |
579 | - status = CDFlib (GET_, entryData, fPtr, NULL_); | |
580 | - if (status != CDF_OK) cdf_status_handler (status, msg); | |
581 | - if (DEBUG) { | |
582 | - printf (" = "); | |
583 | - for (i=0; i < numElements; i++) { | |
584 | - if (i > 0) printf (", "); | |
585 | - printf ("%g", *(fPtr+i)); | |
586 | - } | |
587 | - } | |
588 | - nc_status = nc_put_att_float(ncid, varId, attrName, ncDatatype, | |
589 | - numElements, fPtr); | |
590 | - if (nc_status != NC_NOERR) handle_netcdf_error(nc_status); | |
591 | - free (fPtr); | |
592 | - break; | |
593 | - | |
594 | - case CDF_DOUBLE: | |
595 | - case CDF_REAL8: | |
596 | - case CDF_UINT4: | |
597 | - dPtr = (double *) malloc (sizeof(double) * numElements); | |
598 | - if (dPtr == NULL) memory_error("get_cdf_attr_data", "NC_DOUBLE"); | |
599 | - status = CDFlib (GET_, entryData, dPtr, NULL_); | |
600 | - if (status != CDF_OK) cdf_status_handler (status, msg); | |
601 | - if (DEBUG) { | |
602 | - printf (" = "); | |
603 | - for (i=0; i < numElements; i++) { | |
604 | - if (i > 0) printf (", "); | |
605 | - printf ("%g", *(dPtr+i)); | |
606 | - } | |
607 | - } | |
608 | - nc_status = nc_put_att_double(ncid, varId, attrName, | |
609 | - ncDatatype, numElements, dPtr); | |
610 | - if (nc_status != NC_NOERR) handle_netcdf_error(nc_status); | |
611 | - free (dPtr); | |
612 | - break; | |
613 | - | |
614 | - case CDF_EPOCH: /* 8-byte real number */ | |
615 | - dPtr = (double *) malloc (sizeof(double) * numElements); | |
616 | - if (dPtr == NULL) memory_error("get_cdf_attr_data", "CDF_EPOCH"); | |
617 | - status = CDFlib (GET_, entryData, dPtr, NULL_); | |
618 | - if (status != CDF_OK) cdf_status_handler (status, msg); | |
619 | - if (DEBUG) printf (" = "); | |
620 | - for (i=0; i < numElements; i++) { | |
621 | - encodeEPOCH (*(dPtr+i), epString); | |
622 | - nc_status = nc_put_att_text(ncid, varId, attrName, | |
623 | - EPOCH_STRING_LEN, epString); | |
624 | - if (nc_status != NC_NOERR) handle_netcdf_error(nc_status); | |
625 | - if (DEBUG) { | |
626 | - if (i > 0) printf (", "); | |
627 | - printf (epString); | |
628 | - } | |
629 | - } | |
630 | - free (dPtr); | |
631 | - break; | |
632 | - | |
633 | - case CDF_EPOCH16: /* 16-byte real number */ | |
634 | - dPtr = (double *) malloc (sizeof(double) * numElements * 2); | |
635 | - if (dPtr == NULL) memory_error("get_cdf_attr_data", "CDF_EPOCH16"); | |
636 | - status = CDFlib (GET_, entryData, dPtr, NULL_); | |
637 | - if (status != CDF_OK) cdf_status_handler (status, msg); | |
638 | - if (DEBUG) printf (" = "); | |
639 | - for (i=0; i < numElements; i++) { | |
640 | - epoch16[0] = *(dPtr+i*2); | |
641 | - epoch16[1] = *(dPtr+i*2+1); | |
642 | - encodeEPOCH16 (epoch16, ep16String); | |
643 | - nc_status = nc_put_att_text(ncid, varId, attrName, | |
644 | - strlen(ep16String), ep16String); | |
645 | - if (nc_status != NC_NOERR) handle_netcdf_error(nc_status); | |
646 | - if (DEBUG) { | |
647 | - if (i > 0) printf (", "); | |
648 | - printf (ep16String); | |
649 | - } | |
650 | - } | |
651 | - free (dPtr); | |
652 | - break; | |
653 | - | |
654 | - default: | |
655 | - printf ("** Error in get_cdf_attribute: bad data type"); | |
656 | - } | |
657 | - if (DEBUG) printf (" ;\n"); | |
658 | -} | |
659 | - | |
660 | - | |
661 | -/*-------------------------------------------------------------------------- | |
662 | - * Get the zVariables in the CDF file. | |
663 | - *--------------------------------------------------------------------------*/ | |
664 | -void get_zVariables() | |
665 | -{ | |
666 | - CDFstatus status; | |
667 | - int i, ncstatus, createUnlimitedDim, unlimitedDimId, | |
668 | - attrId, netcdfVarId; | |
669 | - char netcdfDimName[NC_MAX_NAME+1]; /* netCDF dimension name */ | |
670 | - long holdDim, holdDim0; | |
671 | - long varId, | |
672 | - recInterval, /* No. of recors to skip */ | |
673 | - dimIndices[CDF_MAX_DIMS], /* Beginning location of the array */ | |
674 | - /* to be dumped */ | |
675 | - dimIntervals[CDF_MAX_DIMS]; /* No. of elements to skip */ | |
676 | - CDFvar var; | |
677 | - size_t start[NC_MAX_DIMS], /* index for where to read first */ | |
678 | - count[NC_MAX_DIMS]; /* # of values to read */ | |
679 | - short timeDefined = 0; | |
680 | - | |
681 | - if (DEBUG) printf ("\n"); | |
682 | - | |
683 | - createUnlimitedDim = TRUE; | |
684 | - | |
685 | - for (varId=0; varId < nZvars; varId++) { | |
686 | - status = CDFlib (SELECT_,zVAR_, varId, | |
687 | - GET_, zVAR_NAME_, var.name, | |
688 | - zVAR_DATATYPE_, &var.datatype, | |
689 | - zVAR_NUMELEMS_, &var.numElements, | |
690 | - zVAR_NUMDIMS_, &var.dimensionality, | |
691 | - zVAR_DIMSIZES_, var.dimSizes, | |
692 | - zVAR_NUMRECS_, &var.numRecs, | |
693 | - zVAR_RECVARY_, &var.recVariance, | |
694 | - NULL_); | |
695 | - if (status != CDF_OK) cdf_status_handler (status, "GET_, zVARS_"); | |
696 | - | |
697 | - if (DEBUG) { | |
698 | - printf ("\nvar name = %s, %s/%d, %d:[", | |
699 | - var.name, cdf_str_datatype(var.datatype), | |
700 | - var.numElements, var.dimensionality); | |
701 | - for (i=0; i < var.dimensionality; i++) { | |
702 | - if (i > 0) printf (","); | |
703 | - printf ("%d", var.dimSizes[i]); | |
704 | - } | |
705 | - printf("], numRecs = %d\n", var.numRecs); | |
706 | - } | |
707 | - | |
708 | - create_netcdf_dimensions (var, &createUnlimitedDim, &unlimitedDimId); | |
709 | - | |
710 | - | |
711 | - netcdfVarId = create_netcdf_variable (var, unlimitedDimId, &timeDefined); | |
712 | - | |
713 | - /* Leave the define mode before adding data */ | |
714 | - ncstatus = nc_enddef(ncid); | |
715 | - if (ncstatus != NC_NOERR) handle_netcdf_error(ncstatus); | |
716 | - | |
717 | - get_cdf_variable_data (varId, var); | |
718 | - | |
719 | - /* Enter the define mode before writing netCDF variable attributes */ | |
720 | - /* and creating a variable. */ | |
721 | - ncstatus = nc_redef(ncid); | |
722 | - if (ncstatus != NC_NOERR) handle_netcdf_error(ncstatus); | |
723 | - | |
724 | - /* Get the variable attributes */ | |
725 | - for (attrId = 0; attrId < nAttrs; attrId++) | |
726 | - get_cdf_attribute(attrId, VARIABLE_SCOPE, varId, netcdfVarId); | |
727 | - } | |
728 | -// DD Variables | |
729 | - nc_def_dim (ncid, "TimeLength", TIMELENGTH, &TimeLengthID); | |
730 | - nc_def_var (ncid, "StartTime", NC_CHAR, 1, &TimeLengthID, &StartTimeID); | |
731 | - nc_def_var (ncid, "StopTime", NC_CHAR, 1, &TimeLengthID, &StopTimeID); | |
732 | - ncstatus = nc_enddef(ncid); | |
733 | - if (ncstatus != NC_NOERR) handle_netcdf_error(ncstatus); | |
734 | - start[0] = 0L; | |
735 | - start[1] = 0L; | |
736 | - count[0]= 1L; | |
737 | - count[1] = TIMELENGTH; | |
738 | - ncstatus = nc_put_vara_text(ncid, StartTimeID, &start[1], &count[1], StartTime); | |
739 | - if (ncstatus != NC_NOERR) handle_netcdf_error(ncstatus); | |
740 | - ncstatus = nc_put_vara_text(ncid, StopTimeID, &start[1], &count[1], StopTime); | |
741 | - if (ncstatus != NC_NOERR) handle_netcdf_error(ncstatus); | |
742 | -} | |
743 | - | |
744 | - | |
745 | -/*-------------------------------------------------------------------------- | |
746 | - * This routine creates a netCDF dimension. | |
747 | - *--------------------------------------------------------------------------*/ | |
748 | -void create_netcdf_dimensions (CDFvar var, | |
749 | - int *createUnlimitedDim, | |
750 | - int *unlimitedDimId) | |
751 | -{ | |
752 | - int status, /* netCDF status code */ | |
753 | - i, j, dimensionCreated; | |
754 | - long dimensionality; | |
755 | - char netcdfDimName[NC_MAX_NAME+1]; /* netCDF dimension name */ | |
756 | - | |
757 | - if (*createUnlimitedDim) { | |
758 | - if (var.recVariance == VARY) { | |
759 | - status = nc_def_dim(ncid, UNLIMITDIM, NC_UNLIMITED,unlimitedDimId); | |
760 | - if (status != NC_NOERR) handle_netcdf_error(status); | |
761 | - *createUnlimitedDim = FALSE; | |
762 | - } | |
763 | - } | |
764 | - | |
765 | - dimensionality = var.dimensionality; | |
766 | - | |
767 | - if (var.datatype == CDF_CHAR) { | |
768 | - var.dimSizes[dimensionality] = var.numElements; | |
769 | - dimensionality++; | |
770 | - } | |
771 | - else if (var.datatype == CDF_EPOCH) { | |
772 | - var.dimSizes[dimensionality] = TIMELENGTH; //EPOCH_STRING_LEN; | |
773 | - dimensionality++; | |
774 | - } | |
775 | - else if (var.datatype == CDF_EPOCH16) { | |
776 | - var.dimSizes[dimensionality] = TIMELENGTH; //EPOCH16_STRING_LEN; | |
777 | - dimensionality++; | |
778 | - } | |
779 | - | |
780 | - for (i=0; i < dimensionality; i++) { | |
781 | - dimensionCreated = FALSE; | |
782 | - for (j=0; j < totNCdims; j++) { | |
783 | - if (ncDims[j].size == var.dimSizes[i]) { | |
784 | - dimensionCreated = TRUE; | |
785 | - break; | |
786 | - } | |
787 | - } | |
788 | - if (!dimensionCreated && var.dimSizes[i] > 1) { | |
789 | - ncDims[totNCdims].size = var.dimSizes[i]; | |
790 | - sprintf (netcdfDimName, "dim%d", totNCdims); | |
791 | - strcpy(ncDims[totNCdims].name, netcdfDimName); | |
792 | - status = nc_def_dim(ncid, /* in */ | |
793 | - ncDims[totNCdims].name, /* in */ | |
794 | - ncDims[totNCdims].size, /* in */ | |
795 | - &ncDims[totNCdims].id); /* out */ | |
796 | - if (status != NC_NOERR) handle_netcdf_error(status); | |
797 | - totNCdims++; | |
798 | - } | |
799 | - } | |
800 | -} | |
801 | - | |
802 | - | |
803 | -/*-------------------------------------------------------------------------- | |
804 | - * This routine creates a netCDF variable. | |
805 | - *--------------------------------------------------------------------------*/ | |
806 | -int create_netcdf_variable (CDFvar var, /* in - CDF variable */ | |
807 | - int unlimitedDimId, short *timeDefined) /* in - unlimited dim ID */ | |
808 | -{ | |
809 | - int status, /* netCDF status code */ | |
810 | - i, j, k, ii, | |
811 | - varId, /* netCDF variable ID */ | |
812 | - dimensionality, /* netCDF dimensionality */ | |
813 | - dims[NC_MAX_DIMS]; /* netCDF dimensions */ | |
814 | - nc_type datatype; | |
815 | - | |
816 | - int nc_dimensionality, nc_dims[NC_MAX_DIMS]; | |
817 | - char ourTime[]={"Time\0"}; | |
818 | - | |
819 | - dimensionality = var.dimensionality; | |
820 | - datatype = get_netcdf_datatype (var.datatype); | |
821 | - | |
822 | - /********************************************************************* | |
823 | - * While the string value is treated as 0-d in CDF, it is treated as | |
824 | - * 1-D in netCDF. | |
825 | - *********************************************************************/ | |
826 | - if (var.datatype == CDF_CHAR) { | |
827 | - var.dimSizes[dimensionality] = var.numElements; | |
828 | - dimensionality++; | |
829 | - } | |
830 | - | |
831 | - /********************************************************************* | |
832 | - * In CDF, CDF_EPOCH and CDF_EPOCH16 are used to store a date and time | |
833 | - * value into a 8-byte and 16-byte real value, respectively. Since netCDF | |
834 | - * doesn't support EPOCH, the CDF EPOCH variable needs to be translated | |
835 | - * as a string value. | |
836 | - *********************************************************************/ | |
837 | - else if ((var.datatype == CDF_EPOCH) && (! *timeDefined)) { | |
838 | - | |
839 | - *timeDefined = 1; | |
840 | - var.dimSizes[dimensionality] = TIMELENGTH;//EPOCH_STRING_LEN; | |
841 | - strcpy(var.name, ourTime); | |
842 | - dimensionality++; | |
843 | - } | |
844 | - else if (var.datatype == CDF_EPOCH16) { | |
845 | - var.dimSizes[dimensionality] = TIMELENGTH;//EPOCH16_STRING_LEN; | |
846 | - strcpy(var.name, ourTime); | |
847 | - dimensionality++; | |
848 | - } | |
849 | - | |
850 | - /************************************************************************ | |
851 | - * If CDF's record variance is true, then the netCDF's first dimension | |
852 | - * reflects the record number. | |
853 | - ************************************************************************/ | |
854 | - k = 0; | |
855 | - if (var.recVariance == VARY) { | |
856 | - dims[k++] = unlimitedDimId; | |
857 | - dimensionality++; | |
858 | - } | |
859 | - | |
860 | - for (i=0; i < dimensionality; i++) { | |
861 | - for (j=0; j < totNCdims; j++) { | |
862 | - if (ncDims[j].size == var.dimSizes[i]) { | |
863 | - dims[k++] = ncDims[j].id; | |
864 | - break; | |
865 | - } | |
866 | - } | |
867 | - } | |
868 | - | |
869 | - /* Remove trailing blank spaces if there are any */ | |
870 | - for (i=strlen(var.name)-1; i >= 0; i--) { | |
871 | - if (var.name[i] != ' ') { | |
872 | - var.name[i+1] = '\0'; | |
873 | - break; | |
874 | - } | |
875 | - } | |
876 | - | |
877 | - | |
878 | - /* Replace special symbols with underscore(s). */ | |
879 | - for (ii=0; ii < 9; ii++) | |
880 | - for (i=0; i < strlen(var.name); i++) { | |
881 | - if (var.name[i] == SpecialSymbol[ii]) { | |
882 | - var.name[i] = '_'; | |
883 | - } | |
884 | - } | |
885 | - | |
886 | - | |
887 | -/* Exclude "false" dimension */ | |
888 | - nc_dimensionality = dimensionality; | |
889 | - for (i=0; i < dimensionality; i++) { | |
890 | - if ( var.dimSizes[i] == 1 ) { | |
891 | - nc_dimensionality--; | |
892 | - j = i; | |
893 | - break; | |
894 | - } | |
895 | - } | |
896 | - /* Create a netCDF variable. */ | |
897 | - | |
898 | - status = nc_def_var(ncid, var.name, datatype, nc_dimensionality, dims, &varId); | |
899 | - | |
900 | - if (status != NC_NOERR) handle_netcdf_error(status); | |
901 | - | |
902 | - return varId; | |
903 | -} | |
904 | - | |
905 | - | |
906 | -/*-------------------------------------------------------------------------- | |
907 | - * Get the CDF variable data and write the data just read into the | |
908 | - * netCDF variable created in create_netcdf_variable. | |
909 | - *--------------------------------------------------------------------------*/ | |
910 | -void get_cdf_variable_data (long varId, /* in - variable ID/number */ | |
911 | - CDFvar var) /* in - CDF variable structure */ | |
912 | -{ | |
913 | - CDFstatus status; /* CDF status code */ | |
914 | - int ncstatus; /* netCDF status code */ | |
915 | - char epString[EPOCH_STRING_LEN+1]; | |
916 | - char ep16String[EPOCH16_STRING_LEN+1]; | |
917 | - | |
918 | - double epoch16[2]; | |
919 | - | |
920 | - size_t start[NC_MAX_DIMS], /* index for where to read first */ | |
921 | - count[NC_MAX_DIMS]; /* # of values to read */ | |
922 | - | |
923 | - int i, j, k, divide, | |
924 | - dimensionality, | |
925 | - numValues; /* # of values on each record */ | |
926 | - | |
927 | - int nc_dimensionality, | |
928 | - nc_dimSizes[NC_MAX_DIMS]; | |
929 | - | |
930 | - char *cPtr; | |
931 | - signed char *scPtr; | |
932 | - short *sPtr; | |
933 | - int *iPtr; | |
934 | - float *fPtr; | |
935 | - double *dPtr; | |
936 | - long recNo, | |
937 | - totRecs, /* total # of records to read */ | |
938 | - numRecsLeftToRead, | |
939 | - numRecs; /* # of records to read and write at a time */ | |
940 | - | |
941 | - dd_time_t curTime; | |
942 | - | |
943 | - j = 0; | |
944 | - start[0] = 0; | |
945 | - numValues = 1; /* # of values on each CDF record */ | |
946 | - | |
947 | - if (var.recVariance == VARY) { /* Treat it as a netCDF record variable */ | |
948 | - start[j] = 0; /* start record # */ | |
949 | - count[j++] = var.numRecs; /* # of records to write */ | |
950 | - } | |
951 | - | |
952 | - dimensionality = var.dimensionality; | |
953 | - | |
954 | - if (var.datatype == CDF_CHAR) { | |
955 | - var.dimSizes[dimensionality] = var.numElements; | |
956 | - dimensionality++; | |
957 | - } | |
958 | - | |
959 | - /********************************************************************* | |
960 | - * In CDF, CDF_EPOCH and CDF_EPOCH16 are used to store a date and time | |
961 | - * value into a 8-byte and 16-byte real value, respectively. Since netCDF | |
962 | - * doesn't support EPOCH, the CDF EPOCH variable needs to be translated | |
963 | - * as a string value. | |
964 | - *********************************************************************/ | |
965 | - else if (var.datatype == CDF_EPOCH) { | |
966 | - var.dimSizes[dimensionality] = TIMELENGTH; | |
967 | - dimensionality++; | |
968 | - } | |
969 | - else if (var.datatype == CDF_EPOCH16) { | |
970 | - var.dimSizes[dimensionality] = TIMELENGTH; | |
971 | - dimensionality++; | |
972 | - } | |
973 | - | |
974 | - for (i=0; i < dimensionality; i++) { | |
975 | - start[j] = 0; | |
976 | - if (var.dimSizes[i] > 1) count[j++] = var.dimSizes[i]; | |
977 | - numValues *= var.dimSizes[i]; | |
978 | - } | |
979 | - | |
980 | - if (var.datatype == CDF_EPOCH) | |
981 | - numValues /= TIMELENGTH; | |
982 | - else if (var.datatype == CDF_EPOCH16) | |
983 | - numValues /= TIMELENGTH; | |
984 | - | |
985 | - totRecs = var.numRecs; /* total # of records for this var */ | |
986 | - numRecsLeftToRead = var.numRecs; | |
987 | - | |
988 | - while (numRecsLeftToRead > 0) { | |
989 | - recNo = start[0]; /* record # to start reading */ | |
990 | - divide = 1; | |
991 | - numRecs = numRecsLeftToRead; /* # of records to read at a time */ | |
992 | - | |
993 | - switch (var.datatype) { | |
994 | - case CDF_CHAR: | |
995 | - cPtr = NULL; | |
996 | - while (cPtr == NULL) { | |
997 | - cPtr = (char *) malloc (sizeof(char) * numValues * numRecs); | |
998 | - if (cPtr == NULL) { | |
999 | - if (DEBUG) insufficient_memory(); | |
1000 | - divide *= 2; | |
1001 | - numRecs = totRecs / divide; | |
1002 | - } | |
1003 | - } | |
1004 | - read_cdf_variable_data (varId, | |
1005 | - recNo, /* starting record # */ | |
1006 | - numRecs, /* # of records to read */ | |
1007 | - var.dimensionality, | |
1008 | - var.dimSizes, /* dimesion sizes */ | |
1009 | - cPtr); /* data pointer */ | |
1010 | - if (DEBUG) { | |
1011 | - printf (" retrieved CDF data = \n"); | |
1012 | - for (k=0; k < numRecs; k++) { | |
1013 | - printf("\""); | |
1014 | - for (i=0; i < numValues; i++) | |
1015 | - printf ("%c", *(cPtr+(k*numValues+i))); | |
1016 | - printf("\"\n"); | |
1017 | - } | |
1018 | - } | |
1019 | - | |
1020 | - start[0] = 0; | |
1021 | - start[1] = 0; | |
1022 | - if (var.recVariance == VARY) { | |
1023 | - count[0] = numRecs; | |
1024 | - } | |
1025 | - | |
1026 | - if (DEBUG) { | |
1027 | - for (i=0; i < dimensionality; i++) | |
1028 | - printf (" start[%d] = %d, count[%d] = %d\n", | |
1029 | - i, start[i], i, count[i]); | |
1030 | - } | |
1031 | - ncstatus = nc_put_vara_text(ncid, varId, start, count, cPtr); | |
1032 | - if (ncstatus != NC_NOERR) handle_netcdf_error(ncstatus); | |
1033 | - if (DEBUG && ncstatus != NC_NOERR) { | |
1034 | - if (ncstatus != NC_NOERR) | |
1035 | - printf (" Error putting data...\n"); | |
1036 | - else | |
1037 | - printf (" Done putting data...\n"); | |
1038 | - } | |
1039 | - free (cPtr); | |
1040 | - break; | |
1041 | - | |
1042 | - case CDF_BYTE: | |
1043 | - case CDF_INT1: | |
1044 | - scPtr = NULL; | |
1045 | - while (scPtr == NULL) { | |
1046 | - scPtr = | |
1047 | - (signed char *) malloc (sizeof(signed char)*numValues*numRecs); | |
1048 | - if (scPtr == NULL) { | |
1049 | - if (DEBUG) insufficient_memory(); | |
1050 | - divide *= 2; | |
1051 | - numRecs = totRecs / divide; | |
1052 | - } | |
1053 | - } | |
1054 | - read_cdf_variable_data (varId, | |
1055 | - recNo, /* starting record # */ | |
1056 | - numRecs, /* # of records to read */ | |
1057 | - var.dimensionality, | |
1058 | - var.dimSizes, /* dimesion sizes */ | |
1059 | - scPtr); /* data pointer */ | |
1060 | - if (var.recVariance == VARY) | |
1061 | - count[0] = numRecs; | |
1062 | - ncstatus = nc_put_vara_schar(ncid, varId, start, count, scPtr); | |
1063 | - if (ncstatus != NC_NOERR) handle_netcdf_error(ncstatus); | |
1064 | - free (scPtr); | |
1065 | - break; | |
1066 | - | |
1067 | - case CDF_INT2: | |
1068 | - case CDF_UCHAR: | |
1069 | - case CDF_UINT1: | |
1070 | - sPtr = NULL; | |
1071 | - while (sPtr == NULL) { | |
1072 | - sPtr = (short *) malloc (sizeof(short) * numValues * numRecs); | |
1073 | - if (sPtr == NULL) { | |
1074 | - if (DEBUG) insufficient_memory(); | |
1075 | - divide *= 2; | |
1076 | - numRecs = totRecs / divide; | |
1077 | - } | |
1078 | - } | |
1079 | - read_cdf_variable_data (varId, | |
1080 | - recNo, /* starting record # */ | |
1081 | - numRecs, /* # of records to read */ | |
1082 | - var.dimensionality, | |
1083 | - var.dimSizes, /* dimesion sizes */ | |
1084 | - sPtr); /* data pointer */ | |
1085 | - if (var.recVariance == VARY) | |
1086 | - count[0] = numRecs; | |
1087 | - ncstatus = nc_put_vara_short(ncid, varId, start, count, sPtr); | |
1088 | - if (ncstatus != NC_NOERR) handle_netcdf_error(ncstatus); | |
1089 | - free (sPtr); | |
1090 | - break; | |
1091 | - | |
1092 | - case CDF_INT4: | |
1093 | - case CDF_UINT2: | |
1094 | - iPtr = NULL; | |
1095 | - while (iPtr == NULL) { | |
1096 | - iPtr = (int *) malloc (sizeof(int) * numValues * numRecs); | |
1097 | - if (iPtr == NULL) { | |
1098 | - if (DEBUG) insufficient_memory(); | |
1099 | - divide *= 2; | |
1100 | - numRecs = totRecs / divide; | |
1101 | - } | |
1102 | - } | |
1103 | - read_cdf_variable_data (varId, | |
1104 | - recNo, /* starting record # */ | |
1105 | - numRecs, /* # of records to read */ | |
1106 | - var.dimensionality, | |
1107 | - var.dimSizes, /* dimesion sizes */ | |
1108 | - iPtr); /* data pointer */ | |
1109 | - if (var.recVariance == VARY) | |
1110 | - count[0] = numRecs; | |
1111 | - ncstatus = nc_put_vara_int(ncid, varId, start, count, iPtr); | |
1112 | - if (ncstatus != NC_NOERR) handle_netcdf_error(ncstatus); | |
1113 | - free (iPtr); | |
1114 | - break; | |
1115 | - | |
1116 | - case CDF_FLOAT: | |
1117 | - case CDF_REAL4: | |
1118 | - fPtr = NULL; | |
1119 | - while (fPtr == NULL) { | |
1120 | - fPtr = (float *) malloc (sizeof(float) * numValues * numRecs); | |
1121 | - if (fPtr == NULL) { | |
1122 | - if (DEBUG) insufficient_memory(); | |
1123 | - divide *= 2; | |
1124 | - numRecs = totRecs / divide; | |
1125 | - } | |
1126 | - } | |
1127 | - read_cdf_variable_data (varId, | |
1128 | - recNo, /* starting record # */ | |
1129 | - numRecs, /* # of records to read */ | |
1130 | - var.dimensionality, | |
1131 | - var.dimSizes, /* dimesion sizes */ | |
1132 | - fPtr); /* data pointer */ | |
1133 | - if (var.recVariance == VARY) | |
1134 | - count[0] = numRecs; | |
1135 | - ncstatus = nc_put_vara_float(ncid, varId, start, count, fPtr); | |
1136 | - if (ncstatus != NC_NOERR) handle_netcdf_error(ncstatus); | |
1137 | - free (fPtr); | |
1138 | - break; | |
1139 | - | |
1140 | - case CDF_DOUBLE: | |
1141 | - case CDF_REAL8: | |
1142 | - case CDF_UINT4: | |
1143 | - dPtr = NULL; | |
1144 | - while (dPtr == NULL) { | |
1145 | - dPtr = (double *) malloc (sizeof(double) * numValues * numRecs); | |
1146 | - if (dPtr == NULL) { | |
1147 | - if (DEBUG) insufficient_memory(); | |
1148 | - divide *= 2; | |
1149 | - numRecs = totRecs / divide; | |
1150 | - } | |
1151 | - } | |
1152 | - read_cdf_variable_data (varId, | |
1153 | - recNo, /* starting record # */ | |
1154 | - numRecs, /* # of records to read */ | |
1155 | - var.dimensionality, | |
1156 | - var.dimSizes, /* dimesion sizes */ | |
1157 | - dPtr); /* data pointer */ | |
1158 | - if (var.recVariance == VARY) | |
1159 | - count[0] = numRecs; | |
1160 | - | |
1161 | - ncstatus = nc_put_vara_double(ncid, varId, start, count, dPtr); | |
1162 | - if (ncstatus != NC_NOERR) handle_netcdf_error(ncstatus); | |
1163 | - free (dPtr); | |
1164 | - break; | |
1165 | - | |
1166 | - case CDF_EPOCH: | |
1167 | - dPtr = NULL; | |
1168 | - while (dPtr == NULL) { | |
1169 | - dPtr = (double *) malloc (sizeof(double)*numValues*numRecs); | |
1170 | - if (dPtr == NULL) { | |
1171 | - if (DEBUG) insufficient_memory(); | |
1172 | - divide *= 2; | |
1173 | - numRecs = totRecs / divide; | |
1174 | - } | |
1175 | - } | |
1176 | - read_cdf_variable_data (varId, | |
1177 | - recNo, /* starting record # */ | |
1178 | - numRecs, /* # of records to read */ | |
1179 | - var.dimensionality, | |
1180 | - var.dimSizes, /* dimesion sizes */ | |
1181 | - dPtr); /* data pointer */ | |
1182 | - | |
1183 | - count[0] = 1L; | |
1184 | - | |
1185 | - /* Get the EPOCH value into a DD Timestring. */ | |
1186 | - | |
1187 | - for (i = 0; i < numRecs*numValues; i++) { | |
1188 | - *(dPtr+i) /= 1000.0; | |
1189 | - | |
1190 | - *(dPtr+i) -= TIMESHIFT; | |
1191 | - strcpy(curTime,Double2DD_Time((double)*(dPtr+i))); | |
1192 | - ncstatus = nc_put_vara_text(ncid, varId, start, count, curTime); | |
1193 | - if (ncstatus != NC_NOERR) handle_netcdf_error(ncstatus); | |
1194 | - if ((i == 0) && (isTimeSet == 0)) strcpy(StartTime,curTime); | |
1195 | - start[0]++; | |
1196 | - } | |
1197 | - // First Epoch type is considered as Time | |
1198 | - if (isTimeSet == 0) strcpy(StopTime,curTime); | |
1199 | - isTimeSet = 1; | |
1200 | - free (dPtr); | |
1201 | - break; | |
1202 | - | |
1203 | - case CDF_EPOCH16: | |
1204 | - dPtr = NULL; | |
1205 | - while (dPtr == NULL) { | |
1206 | - dPtr = (double *) malloc (sizeof(double) * numValues*numRecs*2); | |
1207 | - if (dPtr == NULL) { | |
1208 | - if (DEBUG) insufficient_memory(); | |
1209 | - divide *= 2; | |
1210 | - numRecs = totRecs / divide; | |
1211 | - } | |
1212 | - } | |
1213 | - read_cdf_variable_data (varId, | |
1214 | - recNo, /* starting record # */ | |
1215 | - numRecs, /* # of records to read */ | |
1216 | - var.dimensionality, | |
1217 | - var.dimSizes, /* dimesion sizes */ | |
1218 | - dPtr); /* data pointer */ | |
1219 | - | |
1220 | - count[0] = 1L; | |
1221 | - | |
1222 | - | |
1223 | - /* Get all the EPOCH16 values into a single string. */ | |
1224 | - for (i=0; i < numRecs; i++) { | |
1225 | - epoch16[0] = *(dPtr+i*2); | |
1226 | - epoch16[1] = *(dPtr+i*2+1); | |
1227 | - encodeEPOCH16_4 (epoch16, ep16String); | |
1228 | - ncstatus = nc_put_vara_text(ncid, varId, start, count, isotime_to_dd_time(ep16String)); | |
1229 | - if (ncstatus != NC_NOERR) handle_netcdf_error(ncstatus); | |
1230 | - if ((i == 0) && (isTimeSet == 0)) strcpy(StartTime,isotime_to_dd_time(ep16String)); | |
1231 | - start[0]++; | |
1232 | - } | |
1233 | - | |
1234 | - free (dPtr); | |
1235 | - // First Epoch type is considered as Time | |
1236 | - if (isTimeSet == 0) strcpy(StopTime,isotime_to_dd_time(ep16String)); | |
1237 | - isTimeSet = 1; | |
1238 | - break; | |
1239 | - | |
1240 | - default: | |
1241 | - printf ("** Error in getVarData: bad type"); | |
1242 | - } | |
1243 | - | |
1244 | - numRecsLeftToRead = totRecs - numRecs; | |
1245 | - if (numRecsLeftToRead > 0) { | |
1246 | - totRecs = numRecsLeftToRead; | |
1247 | - start[0] = numRecs; | |
1248 | - } | |
1249 | - } /* end of the 'while' loop */ | |
1250 | - | |
1251 | -} | |
1252 | - | |
1253 | - | |
1254 | -/*-------------------------------------------------------------------------- | |
1255 | - * This routine returns the string representation of the given CDF | |
1256 | - * datatype. | |
1257 | - *--------------------------------------------------------------------------*/ | |
1258 | -char * cdf_str_datatype (long type) | |
1259 | -{ | |
1260 | - switch (type) { | |
1261 | - case CDF_BYTE: | |
1262 | - return "CDF_BYTE"; | |
1263 | - | |
1264 | - case CDF_INT1: | |
1265 | - return "CDF_INT1"; | |
1266 | - | |
1267 | - case CDF_CHAR: | |
1268 | - return "CDF_CHAR"; | |
1269 | - | |
1270 | - case CDF_INT2: | |
1271 | - return "CDF_INT2"; | |
1272 | - | |
1273 | - case CDF_UCHAR: | |
1274 | - return "CDF_UCHAR"; | |
1275 | - | |
1276 | - case CDF_UINT1: | |
1277 | - return "CDF_UINT1"; | |
1278 | - | |
1279 | - case CDF_INT4: | |
1280 | - return "CDF_INT4"; | |
1281 | - | |
1282 | - case CDF_UINT2: | |
1283 | - return "CDF_UINT2"; | |
1284 | - | |
1285 | - case CDF_FLOAT: | |
1286 | - return "CDF_FLOAT"; | |
1287 | - | |
1288 | - case CDF_REAL4: | |
1289 | - return "CDF_REAL4"; | |
1290 | - | |
1291 | - case CDF_DOUBLE: | |
1292 | - return "CDF_DOUBLE"; | |
1293 | - | |
1294 | - case CDF_REAL8: | |
1295 | - return "CDF_REAL8"; | |
1296 | - | |
1297 | - case CDF_UINT4: | |
1298 | - return "CDF_UINT4"; | |
1299 | - | |
1300 | - case CDF_EPOCH: | |
1301 | - return "CDF_EPOCH"; | |
1302 | - | |
1303 | - case CDF_EPOCH16: | |
1304 | - return "CDF_EPOCH16"; | |
1305 | - } | |
1306 | -} | |
1307 | - | |
1308 | - | |
1309 | -/*-------------------------------------------------------------------------- | |
1310 | - * This routine returns the string representation of the given netCDF | |
1311 | - * datatype. | |
1312 | - *--------------------------------------------------------------------------*/ | |
1313 | -char * NC_datatype (nc_type type) | |
1314 | -{ | |
1315 | - switch (type) { | |
1316 | - case NC_BYTE: | |
1317 | - return "byte"; | |
1318 | - case NC_CHAR: | |
1319 | - return "char"; | |
1320 | - case NC_SHORT: | |
1321 | - return "short"; | |
1322 | - case NC_INT: | |
1323 | - return "int"; | |
1324 | - case NC_FLOAT: | |
1325 | - return "float"; | |
1326 | - case NC_DOUBLE: | |
1327 | - return "double"; | |
1328 | - default: | |
1329 | - return "UNKNOWN"; | |
1330 | - } | |
1331 | -} | |
1332 | - | |
1333 | - | |
1334 | -/*-------------------------------------------------------------------------- | |
1335 | - * This routine returns the netCDF datatype for the given CDF datatype. | |
1336 | - *--------------------------------------------------------------------------*/ | |
1337 | -nc_type get_netcdf_datatype (long type) | |
1338 | -{ | |
1339 | - nc_type netcdf_type; | |
1340 | - | |
1341 | - switch (type) { | |
1342 | - case CDF_BYTE: | |
1343 | - case CDF_INT1: | |
1344 | - netcdf_type = NC_BYTE; | |
1345 | - break; | |
1346 | - | |
1347 | - case CDF_CHAR: | |
1348 | - case CDF_EPOCH: | |
1349 | - case CDF_EPOCH16: | |
1350 | - netcdf_type = NC_CHAR; | |
1351 | - break; | |
1352 | - | |
1353 | - case CDF_INT2: | |
1354 | - case CDF_UCHAR: | |
1355 | - case CDF_UINT1: | |
1356 | - netcdf_type = NC_SHORT; | |
1357 | - break; | |
1358 | - | |
1359 | - case CDF_INT4: | |
1360 | - case CDF_UINT2: | |
1361 | - netcdf_type = NC_INT; | |
1362 | - break; | |
1363 | - | |
1364 | - case CDF_FLOAT: | |
1365 | - case CDF_REAL4: | |
1366 | - netcdf_type = NC_FLOAT; | |
1367 | - break; | |
1368 | - | |
1369 | - case CDF_DOUBLE: | |
1370 | - case CDF_REAL8: | |
1371 | - case CDF_UINT4: | |
1372 | - netcdf_type = NC_DOUBLE; | |
1373 | - break; | |
1374 | - | |
1375 | - default: { | |
1376 | - printf ("** ERROR: Unknown CDF datatype\n"); | |
1377 | - exit (1); | |
1378 | - } | |
1379 | - } | |
1380 | - return netcdf_type; | |
1381 | -} | |
1382 | - | |
1383 | - | |
1384 | -/*--------------------------------------------------------------------------- | |
1385 | - * Read the CDF variable data for the given CDF variable number. | |
1386 | - *---------------------------------------------------------------------------*/ | |
1387 | -void read_cdf_variable_data (long varNum, /* CDF variable number */ | |
1388 | - long recNo, /* record # for start reading */ | |
1389 | - long recCount, /* # of records to read */ | |
1390 | - long dim, /* CDF dimensionality */ | |
1391 | - long dimSizes[], /* CDF dimesion sizes */ | |
1392 | - void *data) /* data pointer */ | |
1393 | -{ | |
1394 | - CDFstatus status; | |
1395 | - int i; | |
1396 | - long recInterval, /* No. of recors to skip */ | |
1397 | - dimIndices[CDF_MAX_DIMS], /* Beginning location of the array */ | |
1398 | - /* to be dumped */ | |
1399 | - dimIntervals[CDF_MAX_DIMS]; /* No. of elements to skip */ | |
1400 | - | |
1401 | - recInterval = 1L; /* # of records to skip between writes */ | |
1402 | - | |
1403 | - for (i=0; i < dim; i++) { | |
1404 | - dimIndices[i] = 0; /* Dump data from the beginning of array */ | |
1405 | - dimIntervals[i] = 1; /* Dump all the elements in the array */ | |
1406 | - } | |
1407 | - | |
1408 | - status = CDFlib (SELECT_,zVAR_, varNum, | |
1409 | - zVAR_RECNUMBER_, recNo, | |
1410 | - zVAR_RECCOUNT_, recCount, | |
1411 | - zVAR_RECINTERVAL_, recInterval, | |
1412 | - zVAR_DIMINDICES_, dimIndices, | |
1413 | - zVAR_DIMCOUNTS_, dimSizes, | |
1414 | - zVAR_DIMINTERVALS_, dimIntervals, | |
1415 | - GET_, zVAR_HYPERDATA_, data, | |
1416 | - NULL_); | |
1417 | - if (status != CDF_OK) cdf_status_handler (status, "GET_, zVAR_HYPERDATA_"); | |
1418 | -} | |
1419 | - | |
1420 | - | |
1421 | -/*-------------------------------------------------------------------------- | |
1422 | - * Remove the filepath from a file name. | |
1423 | - * | |
1424 | - * Example: | |
1425 | - * /home/data/mydata.txt => mydata.txt | |
1426 | - *--------------------------------------------------------------------------*/ | |
1427 | -void removeFilepath (char *fileName) { | |
1428 | - char *ptr; | |
1429 | - | |
1430 | - ptr = strrchr(fileName, '/'); /* Unix file path separator */ | |
1431 | - if (ptr != NULL) { | |
1432 | - ptr++; | |
1433 | - strcpy(fileName, ptr); | |
1434 | - } | |
1435 | - else { | |
1436 | - ptr = strrchr(fileName, '\\'); /* Windows file separator */ | |
1437 | - if (ptr != NULL) { | |
1438 | - ptr++; | |
1439 | - strcpy(fileName, ptr); | |
1440 | - } | |
1441 | - } | |
1442 | -} | |
1443 | - | |
1444 | - | |
1445 | -/*-------------------------------------------------------------------------- | |
1446 | - * Handles a CDF error. | |
1447 | - *--------------------------------------------------------------------------*/ | |
1448 | -void cdf_status_handler (CDFstatus status, char *source) | |
1449 | -{ | |
1450 | - char message[CDF_STATUSTEXT_LEN+1]; | |
1451 | - | |
1452 | - CDFerror (status, message); /* Get the appropriate message */ | |
1453 | - | |
1454 | - if (status < CDF_WARN) { | |
1455 | - printf ("An error has occurred, halting...\n"); | |
1456 | - printf ("%s\n", message); | |
1457 | - printf ("** Error source: %s\n", source); | |
1458 | - exit (status); | |
1459 | - } | |
1460 | - else if (status < CDF_OK) { | |
1461 | - printf ("Warning, function may not have compeleted as expected...\n"); | |
1462 | - printf ("%s\n", message); | |
1463 | - } | |
1464 | - else if (status > CDF_OK) { | |
1465 | - printf ("Function compeleted successfully, but be advised that...\n"); | |
1466 | - printf ("%s\n", message); | |
1467 | - } | |
1468 | -} | |
1469 | - | |
1470 | - | |
1471 | -/*-------------------------------------------------------------------------- | |
1472 | - * Handles a netCDF error. | |
1473 | - *--------------------------------------------------------------------------*/ | |
1474 | -void handle_netcdf_error(int status) { | |
1475 | - | |
1476 | - fprintf(stderr, "%s\n", nc_strerror(status)); | |
1477 | -} | |
1478 | - | |
1479 | - | |
1480 | -/*-------------------------------------------------------------------------- | |
1481 | - * Handles a memory allocation error. | |
1482 | - *--------------------------------------------------------------------------*/ | |
1483 | -void memory_error(char *source, char *datatype) { | |
1484 | - printf ("** Memory allocation error occurred in %s. data type = %s", | |
1485 | - source, datatype); | |
1486 | - exit (1); | |
1487 | -} | |
1488 | - | |
1489 | -void insufficient_memory () { | |
1490 | - printf("Insufficient memory to load all the records at once"); | |
1491 | -} | |
1492 | - | |
1493 | - | |
1494 | -/*--------------------------------------------------------------------------*/ | |
1495 | -void usage() | |
1496 | -{ | |
1497 | - printf ("\nDescription:\n"); | |
1498 | - printf (" This program converts a CDF file into a netCDF file."); | |
1499 | - printf ("\n"); | |
1500 | - printf ("Usage: cdf2nc -[Options] <CDF file name>\n"); | |
1501 | - printf ("\n"); | |
1502 | - printf ("Options:\n"); | |
1503 | - printf (" -map <mapfile> Use the user-supplied mapping file.\n"); | |
1504 | - printf (" -debug Show debug information.\n"); | |
1505 | - printf ("\n"); | |
1506 | - printf ("Examples of usage: \n"); | |
1507 | - printf ("1) cdf-to-netCDF test.cdf\n"); | |
1508 | - printf (" will convert the test.cdf file into the test.nc file.\n\n"); | |
1509 | - printf ("2) cdf-to-netCDF -map mymap.dat test.cdf\n"); | |
1510 | - printf (" will convert the test.cdf file into the test.nc file using\n"); | |
1511 | - printf (" the user-supplied mapping file called 'mymap.dat' instead\n"); | |
1512 | - printf (" of using the default cdf-to-netCDF mapping file.\n"); | |
1513 | - printf ("\n"); | |
1514 | - exit(1); | |
1515 | -} | |
1516 | - | |
1517 | -void parseCmdLine (int argc, char *argv[]) | |
1518 | -{ | |
1519 | - int i; | |
1520 | - char *nextArg; | |
1521 | - | |
1522 | - for (i=0; i < argc; i++) { | |
1523 | - if (strcmp(argv[i],"-map") == 0) { | |
1524 | - nextArg = getNextArgument(argc, argv, i); | |
1525 | - if (strcmp(nextArg, "No next argument") == 0 || | |
1526 | - *(nextArg+0) == '-') | |
1527 | - errorMsg("** Error - mapping file is not defined"); | |
1528 | - else | |
1529 | - strcpy(mappingFile, nextArg); | |
1530 | - } | |
1531 | - else if (strcmp(argv[i],"-debug") == 0) | |
1532 | - DEBUG = TRUE; | |
1533 | - } | |
1534 | -} | |
1535 | - | |
1536 | - | |
1537 | -char * getNextArgument (int argc, char *argv[], int currentIndex) | |
1538 | -{ | |
1539 | - char *nextArg, msg[30]; | |
1540 | - int nextIndex; | |
1541 | - | |
1542 | - nextIndex = currentIndex+1; | |
1543 | - if (nextIndex == argc) { | |
1544 | - strcpy (msg, "No next argument"); | |
1545 | - nextArg = msg; | |
1546 | - } | |
1547 | - else | |
1548 | - nextArg = argv[nextIndex]; | |
1549 | - | |
1550 | - return nextArg; | |
1551 | -} | |
1552 | - | |
1553 | -void errorMsg (char *msg) | |
1554 | -{ | |
1555 | - printf ("%s\n", msg); | |
1556 | - exit (1); | |
1557 | -} | |
1558 | - | |
1559 | - | |
1560 | -void map_CDF_attr_to_netCDF (char *searchAttr, /* in - CDF attr name */ | |
1561 | - char *mappedAttrName) /* out - netCDF attr name */ | |
1562 | -{ | |
1563 | - int i; | |
1564 | - | |
1565 | - strcpy(mappedAttrName, searchAttr); | |
1566 | - | |
1567 | - for (i=0; i < totAttrsInMappingFile; i++) { | |
1568 | - if (strcmp(cdfAttrNames[i], searchAttr) == 0) { | |
1569 | - strcpy(mappedAttrName, netcdfAttrNames[i]); | |
1570 | - break; | |
1571 | - } | |
1572 | - } | |
1573 | -} | |
1574 | - | |
1575 | -/******************************************************************************* | |
1576 | - * | |
1577 | - * Convert an ISO 8601 date time to a DD Time string | |
1578 | - */ | |
1579 | -char * isotime_to_dd_time (char * input) | |
1580 | -{ | |
1581 | - static char output [DD_TIME_STRING_LEN + 1]; | |
1582 | - int i, count, ddd; | |
1583 | - int yy, mm, dd, h, m, s, ms; | |
1584 | - int duration[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; | |
1585 | - | |
1586 | - count = sscanf (input, "%d-%d-%dT%d:%d:%d.%3d", & yy, & mm, & dd, & h, & m, & s, &ms); | |
1587 | - | |
1588 | - if (count != 7) { | |
1589 | - printf ("Unexpected time format: %s\n", input); | |
1590 | - strcpy (output, "ERROR "); | |
1591 | - goto EXIT; | |
1592 | - } | |
1593 | - | |
1594 | - if (leap_year (yy)) duration [2] = 29; | |
1595 | - | |
1596 | - ddd = dd - 1; /* Jan-01 : 0 => ddd in [0, 366[ */ | |
1597 | - | |
1598 | - for (i = 1; i < mm; i++) ddd += duration [i]; | |
1599 | - | |
1600 | - sprintf (output, "%04.4d%03.3d%02.2d%02.2d%02.2d%03.3d", | |
1601 | - yy, ddd, h, m, s, ms); | |
1602 | - | |
1603 | -EXIT: return output; | |
1604 | -} | |
1605 | -/******************************************************************************* | |
1606 | - * | |
1607 | - * Check if the given year is a leap one | |
1608 | - */ | |
1609 | -int leap_year (int year) | |
1610 | -{ | |
1611 | - if ((year % 400) == 0) return TRUE; | |
1612 | - if ((year % 100) == 0) return FALSE; | |
1613 | - if ((year % 4) == 0) return TRUE; | |
1614 | - return FALSE; | |
1615 | -} | |
1616 | - |
src/DECODERS/cdf2nc/cdf_to_netcdf_mapping.dat renamed to src/DECODERS/cdfnew2nc/cdf_to_netcdf_mapping.dat