makeNcFile.c
10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/**********************************************************************************************
Name : makeNcFile.c
Author : Arnaud BIEGUN
Version : 1.0 (30/06/2015)
Brief : Creates the NetCDF file from ASCII footprints file
*************************************************************************************************/
/**************** HEADERS ******************/
// C lib
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
// NETCDF lib
#include <netcdf.h>
// Other functions
#include "ctools.h"
// CONSTANTS
#define SAT_TIME_RES 60
#define PLASMA_TIME_RES 300
#define INDEX_TIME_RES 60
#define RE 6371.0
#define TIME_DIM "Time"
#define TIMELEN_DIM "TimeLength"
#define VECTOR_DIM "Vector"
#define TIME_VAR "Time"
#define FPN_VAR "North"
#define FPS_VAR "South"
#define DISTMGNP_VAR "Dist_mgnp"
#define SWFLAG_VAR "SW_flag"
#define START_VAR "StartTime"
#define STOP_VAR "StopTime"
#define SOURCE "T96 geomagnetic field model"
#define FOOTPRINT_UNIT "degrees, degrees"
#define DISTMGNP_UNIT "Re"
#define TIME_STR_LEN 17
/**********END OF HEADERS ******************/
int main(int argc, char const *argv[])
{
/**************** LOCAL VARIABLES ***********************/
// DATA
int iyr, iday, ihour, min__, isec;
float fpn_r_i, fpn_lat_i, fpn_lon_i;
float fps_r_i, fps_lat_i, fps_lon_i;
float satX_i, satY_i, satZ_i;
float BT96X_i, BT96Y_i, BT96Z_i;
float dist_mgnp_i;
int SWFLAG_i;
Footprint *fpArray = NULL;
// ID
int ncid, time_dimid, timelength_dimid, dataId;
int time_varid, fpn_varid, fps_varid, SWFLAG_varid, startTime_varid, stopTime_varid;
// int distmgnp_varid;
int time_tab_dimid[2], data_tab_dimid[2];
// BUFFERS
size_t start[2], timeCount[2], dataCount[2];
// TIME
char ncYear[4], ncDay[3], ncHour[2], ncMin[2], ncSec[2], ncMls[2];
char ncDate[17];
Date *dateArray = NULL;
time_t p;
char *s;
// OTHERS
FILE *dataFile = NULL;
char line[1000], errMsg[256];
int retval, off, count, i;
/*************** END OF LOCAL VARIABLES ********************/
/*************** GET DATA FROM OUTPUT FILE *****************/
// Date dateArray[500];
count = 0;
const char* footprintsFilename = argv[1];
dataFile = fopen(footprintsFilename, "r");
if ( dataFile != NULL )
{
while ( fgets(line, 1000, dataFile) != NULL )
{
// GET VALUES FROM INPUT FILE
sscanf(line,
"%4d %3d %2d %2d %f %f %f %f %f %f %d %f %f %f %f %f %f %f",
&iyr, &iday, &ihour, &min__,
&fpn_r_i, &fpn_lat_i, &fpn_lon_i,
&fps_r_i, &fps_lat_i, &fps_lon_i,
&SWFLAG_i,
&dist_mgnp_i,
&satX_i, &satY_i, &satZ_i,
&BT96X_i, &BT96Y_i, &BT96Z_i
);
fpArray = (Footprint*) realloc( fpArray, (count+1)*sizeof(Footprint) );
fpArray[count].geoCoordN[0] = fpn_lat_i;
fpArray[count].geoCoordN[1] = fpn_lon_i;
fpArray[count].geoCoordS[0] = fps_lat_i;
fpArray[count].geoCoordS[1] = fps_lon_i;
fpArray[count].distmgnp = dist_mgnp_i;
fpArray[count].SWFLAG = SWFLAG_i;
// DATE : INT --> STR
setDateStr(iyr, iday-1, ihour, min__, 0, 0,
ncYear, ncDay, ncHour, ncMin, ncSec, ncMls, ncDate);
dateArray = (Date*) realloc( dateArray, (count+1)*sizeof(Date) );
strcpy(dateArray[count].strDate, ncDate);
// printf("%s %f %f %f %f %f %f %f %f %f %f %f %f %f %d\n",
// dateArray[count].strDate,
// fpn_r_i, fpArray[count].geoCoordN[0], fpArray[count].geoCoordN[1],
// fps_r_i, fpArray[count].geoCoordS[0], fpArray[count].geoCoordS[1],
// fpArray[count].distmgnp,
// satX_i, satY_i, satZ_i,
// BT96X_i, BT96Y_i, BT96Z_i,
// SWFLAG_i);
count++;
}
} else {
strcpy(errMsg, "[ERROR] : NC file --> Unable to read ");
strcat(errMsg, footprintsFilename);
getError(errMsg);
exit(EXIT_FAILURE);
}
/*************** END OF GET DATA FROM OUTPUT FILE *****************/
/************************ OPEN NC FILE *****************************/
const char* ncFilename = argv[2];
retval = nc_create(ncFilename, NC_CLOBBER, &ncid);
if (retval != NC_NOERR)
{
strcpy(errMsg, "[ERROR] : NC file --> Failed to create NC file ");
strcat(errMsg, ncFilename);
getError(errMsg);
exit(EXIT_FAILURE);
}
/************************** END OF OPEN NC FILE *********************/
/*************************** DEFINE MODE ****************************/
// DIMENSIONS
retval = nc_def_dim(ncid, TIME_DIM, NC_UNLIMITED, &time_dimid);
if (retval != NC_NOERR)
{
getError("[ERROR] : NC file --> Failed to define time dimension");
exit(EXIT_FAILURE);
}
retval = nc_def_dim(ncid, TIMELEN_DIM, TIME_STR_LEN, &timelength_dimid);
if (retval != NC_NOERR)
{
getError("[ERROR] : NC file --> Failed to define time length dimension");
exit(EXIT_FAILURE);
}
retval = nc_def_dim(ncid, VECTOR_DIM, 2, &dataId);
if (retval != NC_NOERR)
{
getError("[ERROR] : NC file --> Failed to define vector dimension");
exit(EXIT_FAILURE);
}
// NETCDF VARIABLES
time_tab_dimid[0] = time_dimid;
time_tab_dimid[1] = timelength_dimid;
data_tab_dimid[0] = time_dimid;
data_tab_dimid[1] = dataId;
retval = nc_def_var(ncid, TIME_VAR, NC_CHAR, 2, time_tab_dimid, &time_varid);
if (retval != NC_NOERR)
{
getError("[ERROR] : NC file --> Failed to define time variable");
exit(EXIT_FAILURE);
}
retval = nc_def_var(ncid, FPN_VAR, NC_FLOAT, 2, data_tab_dimid, &fpn_varid);
if (retval != NC_NOERR)
{
getError("[ERROR] : NC file --> Failed to define north footprints variable");
exit(EXIT_FAILURE);
}
retval = nc_def_var(ncid, FPS_VAR, NC_FLOAT, 2, data_tab_dimid, &fps_varid);
if (retval != NC_NOERR)
{
getError("[ERROR] : NC file --> Failed to define south footprints variable");
exit(EXIT_FAILURE);
}
// retval = nc_def_var(ncid, DISTMGNP_VAR, NC_FLOAT, 1, &time_dimid, &distmgnp_varid);
// if (retval != NC_NOERR)
// {
// getError("[ERROR] : NC file --> Failed to define dist mgnp variable");
// exit(EXIT_FAILURE);
// }
retval = nc_def_var(ncid, SWFLAG_VAR, NC_INT, 1, &time_dimid, &SWFLAG_varid);
if (retval != NC_NOERR)
{
getError("[ERROR] : NC file --> Failed to define solar wind quality flag variable");
exit(EXIT_FAILURE);
}
retval = nc_def_var(ncid, START_VAR, NC_CHAR, 1, &timelength_dimid, &startTime_varid);
if (retval != NC_NOERR)
{
getError("[ERROR] : NC file --> Failed to define start time variable");
exit(EXIT_FAILURE);
}
retval = nc_def_var(ncid, STOP_VAR, NC_CHAR, 1, &timelength_dimid, &stopTime_varid);
if (retval != NC_NOERR)
{
getError("[ERROR] : NC file --> Failed to define stop time variable");
exit(EXIT_FAILURE);
}
// UNITS
retval = nc_put_att_text(ncid, fpn_varid, "units", strlen(FOOTPRINT_UNIT), FOOTPRINT_UNIT);
if (retval != NC_NOERR)
{
getError("[ERROR] : NC file --> Failed to define footprints unit attribute");
exit(EXIT_FAILURE);
}
retval = nc_put_att_text(ncid, fps_varid, "units", strlen(FOOTPRINT_UNIT), FOOTPRINT_UNIT);
if (retval != NC_NOERR)
{
getError("[ERROR] : NC file --> Failed to define footprints unit attribute");
exit(EXIT_FAILURE);
}
// retval = nc_put_att_text(ncid, distmgnp_varid, "units", strlen(DISTMGNP_UNIT), DISTMGNP_UNIT);
// if (retval != NC_NOERR)
// {
// getError("[ERROR] : NC file --> Failed to define dist mgnp unit attribute");
// exit(EXIT_FAILURE);
// }
// DATA SOURCE
retval = nc_put_att_text(ncid, NC_GLOBAL, "Source", strlen(SOURCE), SOURCE);
if (retval != NC_NOERR)
{
getError("[ERROR] : NC file --> Failed to define data source attribute");
exit(EXIT_FAILURE);
}
// NC FILE CREATION DATE
time(&p);
s = ctime(&p);
retval = nc_put_att_text(ncid, NC_GLOBAL, "Created", 24, s);
if (retval != NC_NOERR)
{
getError("[ERROR] : NC file --> Failed to define NC file creation date attribute");
exit(EXIT_FAILURE);
}
// CLOSE DEFINE MODE
retval = nc_enddef(ncid);
if (retval != NC_NOERR)
{
getError("[ERROR] : NC file --> Failed to close define mode");
exit(EXIT_FAILURE);
}
/*********************** END OF DEFINE MODE ****************************/
/************************ WRITING MODE ********************************/
// BUFFERS CONFIGURATION
timeCount[0] = 1;
timeCount[1] = TIME_STR_LEN;
dataCount[0] = 1;
dataCount[1] = 2;
start[1] = 0;
// WRITE DATA
off = 0;
for (i = 0; i < count; i++)
{
start[0] = off;
retval = nc_put_vara_text(ncid, time_varid,
start, timeCount, dateArray[i].strDate);
if (retval != NC_NOERR)
{
getError("[ERROR] : NC file --> Failed to write time variable");
exit(EXIT_FAILURE);
}
retval = nc_put_vara_float(ncid, fpn_varid,
start, dataCount, fpArray[i].geoCoordN);
if (retval != NC_NOERR)
{
getError("[ERROR] : NC file --> Failed to write north footprints variable");
exit(EXIT_FAILURE);
}
retval = nc_put_vara_float(ncid, fps_varid,
start, dataCount, fpArray[i].geoCoordS);
if (retval != NC_NOERR)
{
getError("[ERROR] : NC file --> Failed to write south footprints variable");
exit(EXIT_FAILURE);
}
// retval = nc_put_vara_float(ncid, distmgnp_varid, &start[0],
// &dataCount[0], &fpArray[i].distmgnp);
// if (retval != NC_NOERR)
// {
// getError("[ERROR] : NC file --> Failed to write dist mgnp variable");
// exit(EXIT_FAILURE);
// }
retval = nc_put_vara_int(ncid, SWFLAG_varid, &start[0],
&dataCount[0], &fpArray[i].SWFLAG);
if (retval != NC_NOERR)
{
getError("[ERROR] : NC file --> Failed to write solar wind variable");
exit(EXIT_FAILURE);
}
off++;
}
retval = nc_put_var_text(ncid, startTime_varid, dateArray[0].strDate);
if (retval != NC_NOERR)
{
getError("[ERROR] : NC file --> Failed to write start time variable");
exit(EXIT_FAILURE);
}
retval = nc_put_var_text(ncid, stopTime_varid, dateArray[count-1].strDate);
if (retval != NC_NOERR)
{
getError("[ERROR] : NC file --> Failed to write stop time variable");
exit(EXIT_FAILURE);
}
/************************ END OF WRITING MODE ********************************/
/**************** CLOSE NC FILE ***********************/
//Close the file
retval = nc_close(ncid);
if (retval != NC_NOERR)
{
getError("[ERROR] : NC file --> Failed to close NC file");
exit(EXIT_FAILURE);
}
/**************** END OF CLOSE NC FILE ***********************/
/*********************** FREE MEMORY ********************************/
free(dateArray);
free(fpArray);
/******************** END OF FREE MEMORY ****************************/
return 0;
}