Blame view

src/DATA/TOOLS/ConvertDDTimeToDouble.c 15.2 KB
acefa41c   Benjamin Renard   New version of Co...
1
/* $Id: ConvertDDTimeToDouble.c,v 1.0 2019/10/03 13:39:00 benjamin Exp $*/
616e9e0b   Benjamin Renard   Fix SetTime to su...
2
3
4
/*=====================================================================
 *                  DD SYSTEM base package
 *                   DD_Server library
acefa41c   Benjamin Renard   New version of Co...
5
 *                    ConvertDDTimeToDouble.c
616e9e0b   Benjamin Renard   Fix SetTime to su...
6
 *
acefa41c   Benjamin Renard   New version of Co...
7
 *  usage ConvertDDTimeToDouble
616e9e0b   Benjamin Renard   Fix SetTime to su...
8
9
 *
 *  Versions:
616e9e0b   Benjamin Renard   Fix SetTime to su...
10
11
12
 *=====================================================================*/
 /*=====================================================================
 * Description:
acefa41c   Benjamin Renard   New version of Co...
13
 * Program to convert DDTime of a list of nc_data_files to timestamp (double). Data file can be  gzipped
616e9e0b   Benjamin Renard   Fix SetTime to su...
14
 *======================================================================*/
acefa41c   Benjamin Renard   New version of Co...
15

616e9e0b   Benjamin Renard   Fix SetTime to su...
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <dirent.h>
#include <netcdf.h>
#include <math.h>
#include <DD.h>

/*==================================================================================
acefa41c   Benjamin Renard   New version of Co...
26
 *                           READ NC FILE INFO
616e9e0b   Benjamin Renard   Fix SetTime to su...
27
28
 *==================================================================================*/

acefa41c   Benjamin Renard   New version of Co...
29
30
31
32
33
34
35
typedef struct
{
   int    id;
   char   name[NC_MAX_NAME+1];
   size_t length;
   int    isUnlimited;
} NCDimInfo;
616e9e0b   Benjamin Renard   Fix SetTime to su...
36

acefa41c   Benjamin Renard   New version of Co...
37
typedef struct
616e9e0b   Benjamin Renard   Fix SetTime to su...
38
{
acefa41c   Benjamin Renard   New version of Co...
39
40
41
42
43
44
   int     id;
   char    name[NC_MAX_NAME+1];
   size_t  length;
   nc_type type;
   void*   value;
} NCAttInfo;
616e9e0b   Benjamin Renard   Fix SetTime to su...
45

acefa41c   Benjamin Renard   New version of Co...
46
47
48
49
50
51
52
53
54
55
typedef struct
{
   int id;
   char name[NC_MAX_NAME+1];
   int ndims;
   int dimids[NC_MAX_VAR_DIMS]; 
   int natts;
   NCAttInfo *atts;
   nc_type type;
} NCVarInfo;
616e9e0b   Benjamin Renard   Fix SetTime to su...
56

acefa41c   Benjamin Renard   New version of Co...
57
58
59
60
61
62
63
64
65
66
typedef struct
{
   int id;
   int ndims;
   NCDimInfo *dims;
   int natts;
   NCAttInfo *atts;
   int nvars;
   NCVarInfo *vars;
} NCFileInfo;
616e9e0b   Benjamin Renard   Fix SetTime to su...
67

acefa41c   Benjamin Renard   New version of Co...
68
69
70
/*==================================================================================
 *                           READ NC FILE INFO
 *==================================================================================*/
616e9e0b   Benjamin Renard   Fix SetTime to su...
71

acefa41c   Benjamin Renard   New version of Co...
72
73
74
int readNCFileInfo(char* filePath, NCFileInfo* info)
{
   memset(info, 0, sizeof(NCFileInfo));
616e9e0b   Benjamin Renard   Fix SetTime to su...
75

acefa41c   Benjamin Renard   New version of Co...
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
   int status = nc_open(filePath, 0, &info->id);

   if (status != NC_NOERR) {
      fprintf(stderr,"Cannot open NC file %s\n",filePath);
      return 0;
   }

   // General info
   int unlimdimid;
   status = nc_inq(info->id, &info->ndims, &info->nvars, &info->natts, &unlimdimid);

   if (status != NC_NOERR) {
      fprintf(stderr,"Cannot retrieve general info of NC file %s\n",filePath);
      return 0;
   }

   //Load dims
616e9e0b   Benjamin Renard   Fix SetTime to su...
93
   int i, j;
acefa41c   Benjamin Renard   New version of Co...
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
   if (info->ndims > 0) {
      info->dims = malloc(info->ndims * sizeof(NCDimInfo));
   }
   for (i = 0; i < info->ndims; ++i) {
      status = nc_inq_dim(info->id, i, info->dims[i].name, &info->dims[i].length);
      if (status != NC_NOERR) {
         fprintf(stderr,"Cannot retrieve dim info %d of NC file %s\n",i,filePath);
         return 0;
      }
      status = nc_inq_dimid(info->id, info->dims[i].name, &info->dims[i].id);
      if (status != NC_NOERR) {
         fprintf(stderr,"Cannot retrieve dim id %d of NC file %s\n",i,filePath);
         return 0;
      }
      info->dims[i].isUnlimited = (unlimdimid == info->dims[i].id);
   }
616e9e0b   Benjamin Renard   Fix SetTime to su...
110

acefa41c   Benjamin Renard   New version of Co...
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
   //Load global attributes
   if (info->natts > 0) {
      info->atts = malloc(info->natts * sizeof(NCAttInfo));
   }
   for (i = 0; i < info->natts; ++i) {
      status = nc_inq_attname(info->id, NC_GLOBAL, i, info->atts[i].name);
      if (status != NC_NOERR) {
         fprintf(stderr,"Cannot retrieve global att name %d of NC file %s\n",i,filePath);
         return 0;
      }
      status = nc_inq_att(info->id, NC_GLOBAL, info->atts[i].name, &info->atts[i].type, &info->atts[i].length);
      if (status != NC_NOERR) {
         fprintf(stderr,"Cannot retrieve global att info %d of NC file %s\n",i,filePath);
         return 0;
      }
      info->atts[i].value = malloc(info->atts[i].length * nctypelen(info->atts[i].type));
      status = nc_get_att(info->id, NC_GLOBAL, info->atts[i].name, info->atts[i].value);
      if (status != NC_NOERR) {
         fprintf(stderr,"Cannot retrieve global att value %d of NC file %s\n",i,filePath);
      }
   }
616e9e0b   Benjamin Renard   Fix SetTime to su...
132

acefa41c   Benjamin Renard   New version of Co...
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
   //Load variables
   if (info->nvars > 0) {
      info->vars = malloc(info->nvars * sizeof(NCVarInfo));
   }
   for (i = 0; i < info->nvars; ++i) {
      status = nc_inq_var(info->id, i, info->vars[i].name, &info->vars[i].type, &info->vars[i].ndims, info->vars[i].dimids, &info->vars[i].natts);
      if (status != NC_NOERR) {
         fprintf(stderr,"Cannot retrieve variable info %d of NC file %s\n",i,filePath);
         return 0;
      }
      //Load variable attributes
      if (info->vars[i].natts > 0) {
         info->vars[i].atts = malloc(info->vars[i].natts * sizeof(NCAttInfo));
      }
      for (j = 0; j < info->vars[i].natts; ++j) {
         status = nc_inq_attname(info->id, i, j, info->vars[i].atts[j].name);
         if (status != NC_NOERR) {
            fprintf(stderr,"Cannot retrieve variable %d att name %d of NC file %s\n",i,j,filePath);
            return 0;
         }
         status = nc_inq_att(info->id, i, info->vars[i].atts[j].name, &info->vars[i].atts[j].type, &info->vars[i].atts[j].length);
         if (status != NC_NOERR) {
            fprintf(stderr,"Cannot retrieve variable %d att info %d of NC file %s\n",i,j,filePath);
            return 0;
         }
         info->vars[i].atts[j].value = malloc(info->vars[i].atts[j].length * nctypelen(info->vars[i].atts[j].type));
         status = nc_get_att(info->id, i, info->vars[i].atts[j].name, info->vars[i].atts[j].value);
         if (status != NC_NOERR) {
            fprintf(stderr,"Cannot retrieve variable %d att value %d of NC file %s\n",i,j,filePath);
         }
      }
   }
616e9e0b   Benjamin Renard   Fix SetTime to su...
165

acefa41c   Benjamin Renard   New version of Co...
166
167
   return 1;
}
616e9e0b   Benjamin Renard   Fix SetTime to su...
168

acefa41c   Benjamin Renard   New version of Co...
169
170
171
/*==================================================================================
 *                           CLOSE NC FILE and free memory
 *==================================================================================*/
616e9e0b   Benjamin Renard   Fix SetTime to su...
172

acefa41c   Benjamin Renard   New version of Co...
173
174
void closeNCFile(NCFileInfo* info) {
   int i,j;
616e9e0b   Benjamin Renard   Fix SetTime to su...
175

acefa41c   Benjamin Renard   New version of Co...
176
177
178
179
180
181
182
183
184
   if (info->ndims > 0) {
      free(info->dims);
   }

   if (info->natts > 0) {
      for (i = 0; i < info->natts; ++i) {
         if (info->atts[i].value != NULL) {
            free(info->atts[i].value);
         }
616e9e0b   Benjamin Renard   Fix SetTime to su...
185
      }
acefa41c   Benjamin Renard   New version of Co...
186
187
      free(info->atts);
   }
616e9e0b   Benjamin Renard   Fix SetTime to su...
188

acefa41c   Benjamin Renard   New version of Co...
189
190
191
192
193
194
195
196
197
198
199
200
201
   if (info->nvars > 0) {
      for (i = 0; i < info->nvars; ++i) {
         if (info->vars[i].natts > 0) {
            for (j = 0; j < info->vars[i].natts; ++j) {
               if (info->vars[i].atts[j].value != NULL) {
                  free(info->vars[i].atts[j].value);
               }
            }
            free(info->vars[i].atts);
         }
      }
      free(info->vars);
   }
616e9e0b   Benjamin Renard   Fix SetTime to su...
202

acefa41c   Benjamin Renard   New version of Co...
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
   int status = nc_close(info->id);

   if (status != NC_NOERR) {
      fprintf(stderr,"Cannot close NC file\n");
   }

   memset(info, 0, sizeof(NCFileInfo));
}

/*==================================================================================
 *                         convertDDTimeToDouble
 *==================================================================================*/

void convertDDTimeToDouble(void* values, void* values_converted, int nbRecs) {
   int i;
   double TimeStampValue;
   char DDTimeValue[TIMELENGTH+1];
   memset(DDTimeValue,0,(TIMELENGTH+1)*sizeof(char));
   for (i = 0; i < nbRecs; ++i) {
      memcpy(DDTimeValue, values + i * (TIMELENGTH*sizeof(char)), TIMELENGTH*sizeof(char));
      TimeStampValue = DD_Time2Double(DDTimeValue);
      memcpy(values_converted + i * sizeof(double), &TimeStampValue, sizeof(double));
   }
}

/*==================================================================================
 *                         CONVERT NC FILE
 *==================================================================================*/

#define CONV_OK      1
#define CONV_NOTNEED 2
#define CONV_ERROR   0

int convertNCFile(char* oldFilePath, char* newFilePath) {
   char TimeName[] = "Time";

   //Read info of old NC file
   NCFileInfo info;
   int status = readNCFileInfo(oldFilePath,&info);
   if (status == 0) {
      closeNCFile(&info);
      return CONV_ERROR;
   }

   //Retrieve time in old file
   int oldFileTimeVarIndex = -1;
   int i;
   for (i = 0; i < info.nvars; ++i) {
      if (strcmp(info.vars[i].name, TimeName) == 0) {
         oldFileTimeVarIndex = i;
         break;
616e9e0b   Benjamin Renard   Fix SetTime to su...
254
      }
acefa41c   Benjamin Renard   New version of Co...
255
256
257
258
259
260
   }
   if (oldFileTimeVarIndex < 0) {
      fprintf(stderr,"Cannot retrieve %s variable in old NC file\n", TimeName);
      closeNCFile(&info);
      return CONV_ERROR;
   }
616e9e0b   Benjamin Renard   Fix SetTime to su...
261

acefa41c   Benjamin Renard   New version of Co...
262
263
264
265
266
267
268
269
270
271
272
273
274
275
   if (info.vars[oldFileTimeVarIndex].type == NC_DOUBLE) {
      //Time is already in double format => Nothing to do
      closeNCFile(&info);
      return CONV_NOTNEED;
   }

   //Retrieve time dim in old file
   int oldFileTimeDimIndex = -1;
   int isTimeUnlimitedDim = 0;
   for (i = 0; i < info.ndims; ++i) {
      if (info.dims[i].id == info.vars[oldFileTimeVarIndex].dimids[0]) {
         oldFileTimeDimIndex = i;
         isTimeUnlimitedDim = info.dims[i].isUnlimited;
         break;
616e9e0b   Benjamin Renard   Fix SetTime to su...
276
      }
acefa41c   Benjamin Renard   New version of Co...
277
   }
616e9e0b   Benjamin Renard   Fix SetTime to su...
278

acefa41c   Benjamin Renard   New version of Co...
279
280
281
282
283
284
285
286
287
288
289
290
291
292
   //Create new file
   int newFileId;
   status = nc_create(newFilePath, NC_CLOBBER, &newFileId);

   if (status != NC_NOERR) {
      fprintf(stderr,"Cannot create NC file %s %s\n",newFilePath,nc_strerror(status));
      closeNCFile(&info);
      return CONV_ERROR;
   }

   //Create dimensions in new file
   int tmpId;
   for (i = 0; i < info.ndims; ++i) {
      status = nc_def_dim(newFileId, info.dims[i].name, (info.dims[i].isUnlimited == 1) ? NC_UNLIMITED : info.dims[i].length, &tmpId);
616e9e0b   Benjamin Renard   Fix SetTime to su...
293
      if (status != NC_NOERR) {
acefa41c   Benjamin Renard   New version of Co...
294
295
296
         fprintf(stderr,"Cannot create dim %d in new file %s\n",i,newFilePath);
         closeNCFile(&info);
         return CONV_ERROR;
616e9e0b   Benjamin Renard   Fix SetTime to su...
297
      }
acefa41c   Benjamin Renard   New version of Co...
298
   }
616e9e0b   Benjamin Renard   Fix SetTime to su...
299

acefa41c   Benjamin Renard   New version of Co...
300
301
302
   //Create global attributes in new file
   for (i = 0; i < info.natts; ++i) {
      status = nc_put_att(newFileId, NC_GLOBAL, info.atts[i].name, info.atts[i].type, info.atts[i].length, info.atts[i].value);
616e9e0b   Benjamin Renard   Fix SetTime to su...
303
      if (status != NC_NOERR) {
acefa41c   Benjamin Renard   New version of Co...
304
305
306
         fprintf(stderr,"Cannot create global att %d in new file %s\n",i,newFilePath);
         closeNCFile(&info);
         return CONV_ERROR;
616e9e0b   Benjamin Renard   Fix SetTime to su...
307
      }
acefa41c   Benjamin Renard   New version of Co...
308
   }
616e9e0b   Benjamin Renard   Fix SetTime to su...
309

acefa41c   Benjamin Renard   New version of Co...
310
311
312
313
314
315
316
317
318
   //Create variables in new file
   int j;
   for (i = 0; i < info.nvars; ++i) {
      if (oldFileTimeVarIndex == i) {
         status = nc_def_var(newFileId, info.vars[i].name, NC_DOUBLE, 1, info.vars[i].dimids, &tmpId); 
      }
      else {
         status = nc_def_var(newFileId, info.vars[i].name, info.vars[i].type, info.vars[i].ndims, info.vars[i].dimids, &tmpId);
      }
616e9e0b   Benjamin Renard   Fix SetTime to su...
319
      if (status != NC_NOERR) {
acefa41c   Benjamin Renard   New version of Co...
320
321
322
         fprintf(stderr,"Cannot create var %d in new file %s\n",i,newFilePath);
         closeNCFile(&info);
         return CONV_ERROR;
616e9e0b   Benjamin Renard   Fix SetTime to su...
323
      }
acefa41c   Benjamin Renard   New version of Co...
324
325
326
327
328
329
330
331
332
333
      //Create variable attributes
      for (j = 0; j < info.vars[i].natts; ++j) {
         status = nc_put_att(newFileId, i, info.vars[i].atts[j].name, info.vars[i].atts[j].type, info.vars[i].atts[j].length, info.vars[i].atts[j].value);
         if (status != NC_NOERR) {
            fprintf(stderr,"Cannot create local att %d for var %d in new file %s\n",j,i,newFilePath);
            closeNCFile(&info);
            return CONV_ERROR;
         }
      }
   }
616e9e0b   Benjamin Renard   Fix SetTime to su...
334

acefa41c   Benjamin Renard   New version of Co...
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
   status = nc_enddef(newFileId);

   void* values;
   void* values_converted;
   int k;
   int size;
   size_t* start;
   size_t* count;
   for (i = 0; i < info.nvars; ++i) {
      start = malloc(info.vars[i].ndims * sizeof(size_t));
      count = malloc(info.vars[i].ndims * sizeof(size_t));
      //Load old values
      size = 1;
      for (j = 0; j < info.vars[i].ndims; ++j) {
         start[j] = 0;
         for (k = 0; k < info.ndims; ++k) {
            if (info.vars[i].dimids[j] == info.dims[k].id) {
               count[j] = info.dims[k].length;
               size *= info.dims[k].length;
               break;
            }
         }
616e9e0b   Benjamin Renard   Fix SetTime to su...
357
      }
acefa41c   Benjamin Renard   New version of Co...
358
359
360
361
362
363
364
365
366
      values = malloc(size * nctypelen(info.vars[i].type));
      status = nc_get_vara(info.id, i, start, count, values);
      if (status != NC_NOERR) {
         fprintf(stderr,"Cannot load var values %d from old file %s\n",i,oldFilePath);
         free(values);
         free(start);
         free(count);
         closeNCFile(&info);
         return CONV_ERROR;
616e9e0b   Benjamin Renard   Fix SetTime to su...
367
      }
acefa41c   Benjamin Renard   New version of Co...
368
369
370
371
372
373
374
375
376
377
378
379
380
      //Copy or convert values
      if (oldFileTimeVarIndex == i) {
         values_converted = malloc(size * nctypelen(NC_DOUBLE));
         convertDDTimeToDouble(values, values_converted, count[0]);
         status = nc_put_vara(newFileId, i, start, count, values_converted);
         free(values_converted);
      }
      else {
         status = nc_put_vara(newFileId, i, start, count, values);
      }
      free(values);
      free(start);
      free(count);
616e9e0b   Benjamin Renard   Fix SetTime to su...
381
      if (status != NC_NOERR) {
acefa41c   Benjamin Renard   New version of Co...
382
383
384
         fprintf(stderr,"Cannot copy var values %d in new file %s\n",i,newFilePath);
         closeNCFile(&info);
         return CONV_ERROR;
616e9e0b   Benjamin Renard   Fix SetTime to su...
385
      }
acefa41c   Benjamin Renard   New version of Co...
386
   }
616e9e0b   Benjamin Renard   Fix SetTime to su...
387

acefa41c   Benjamin Renard   New version of Co...
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
   nc_close(newFileId);

   closeNCFile(&info);
   return CONV_OK;
}

/*==================================================================================
 *                              MAIN
 *==================================================================================*/

#define COMMAND_MAX_LEN  1000
#define MAX_FILENAME_LEN 100

main(int argc, char **argv)
{
   static char usage[] = "usage: ConvertDDTimeToDouble nc_data_file[*.gz]";

   if (argc < 2) {fprintf(stderr,"%s\n",usage); exit(1); }

   char inputFileName[MAX_FILENAME_LEN+1];
   char unzippedFileName[MAX_FILENAME_LEN+1];
   char workingFileName[] = "ConvertDDTimeToDouble.nc";
   char command[COMMAND_MAX_LEN+1];
   int i;
   int zippedFile = 0;
   int status;

   for (i = 1; i < argc; ++i) {
      if (strlen(argv[i]) > MAX_FILENAME_LEN)
      {
         fprintf(stderr,"File name size exceed the max size : %s\n",argv[i]);
616e9e0b   Benjamin Renard   Fix SetTime to su...
419
420
421
         continue;
      }

acefa41c   Benjamin Renard   New version of Co...
422
423
424
425
426
427
428
429
430
431
432
433
434
435
      memset(inputFileName, 0, (MAX_FILENAME_LEN+1) * sizeof(char));
      strcpy(inputFileName,argv[i]);

      memset(command, 0, (COMMAND_MAX_LEN+1) * sizeof(char));
      if((strlen(inputFileName) > 3) && (strcmp(inputFileName+strlen(inputFileName)-3,".gz") == 0)) // Zipped file
      {
         strcpy(unzippedFileName, inputFileName);
         unzippedFileName[strlen(inputFileName)-3] = '\0';
         sprintf(command,"gunzip -c %s > %s",inputFileName,unzippedFileName);
         zippedFile = 1;
      } 
      else {
         zippedFile = 0;
         strcpy(unzippedFileName, inputFileName);
616e9e0b   Benjamin Renard   Fix SetTime to su...
436
437
      }

acefa41c   Benjamin Renard   New version of Co...
438
439
440
      status = system(command);
      if (status != 0) {
         fprintf(stderr,"Cannot create working input file for  %s\n",argv[i]);
616e9e0b   Benjamin Renard   Fix SetTime to su...
441
442
443
         continue;
      }

acefa41c   Benjamin Renard   New version of Co...
444
445
446
      status = convertNCFile(unzippedFileName, workingFileName);
      if (status == CONV_NOTNEED) {
         fprintf(stderr,"[WARNING] File %s already converted\n", inputFileName);
616e9e0b   Benjamin Renard   Fix SetTime to su...
447
448
         continue;
      }
acefa41c   Benjamin Renard   New version of Co...
449
450
451
      else if (status == CONV_ERROR) {
         fprintf(stderr,"[ERROR] Error detected during %s file conversion\n", inputFileName);
         continue;
616e9e0b   Benjamin Renard   Fix SetTime to su...
452
453
      }

acefa41c   Benjamin Renard   New version of Co...
454
455
456
457
458
459
460
      memset(command, 0, (COMMAND_MAX_LEN+1) * sizeof(char));
      sprintf(command,"cp -p %s %s",workingFileName,unzippedFileName);
      status = system(command);
      if (status != 0) {
         fprintf(stderr,"[ERROR] Cannot copy result file in %s\n",inputFileName);
         continue;
      }
616e9e0b   Benjamin Renard   Fix SetTime to su...
461

acefa41c   Benjamin Renard   New version of Co...
462
463
464
      if (zippedFile == 1) {
         memset(command, 0, (COMMAND_MAX_LEN+1) * sizeof(char));
         sprintf(command,"gzip -f %s",unzippedFileName);
616e9e0b   Benjamin Renard   Fix SetTime to su...
465
466
         status = system(command);
         if (status != 0) {
acefa41c   Benjamin Renard   New version of Co...
467
            fprintf(stderr,"[ERROR] Cannot compress result file\n");
616e9e0b   Benjamin Renard   Fix SetTime to su...
468
         }
616e9e0b   Benjamin Renard   Fix SetTime to su...
469
      }
acefa41c   Benjamin Renard   New version of Co...
470
471

      fprintf(stderr,"[INFO] File %s converted\n", inputFileName);
616e9e0b   Benjamin Renard   Fix SetTime to su...
472
473
   }

acefa41c   Benjamin Renard   New version of Co...
474
475
476
477
   memset(command, 0, (COMMAND_MAX_LEN+1) * sizeof(char));
   sprintf(command,"rm -f %s",workingFileName);
   status = system(command);

616e9e0b   Benjamin Renard   Fix SetTime to su...
478
479
480
   return(0);
} 
/*=========================================================================================================*/