themis_esamom2nc.c
19.4 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
444
445
446
447
448
449
450
451
452
453
454
455
456
/**************************************************************/
/* THEMIS ESA ION AND ELECTRON MOMENTS CDF -> DD netCDF */
/* 04.01.2008 */
/* V 1.1 */
/* */
/* New CDF with ALL modes and new params */
/* */
/**************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <netcdf.h>
#include <cdf.h>
#include <DD.h>
#include <string.h>
#include <time.h>
#include <math.h>
#define TimeGap 1800000.0
#define Source "themis@cdpp2"
#define MAX_FILE_NAME_LEN 250 // Max. file name length
#define MAX_VARS 250 // Max # of VARS in CDF
/*************************************
Global variables and structures
**************************************/
long CDFDims, // Number of dimensions in a CDF file
CDFVars, // Number of variables in a CDF file
CDFDimSizes[CDF_MAX_DIMS], // Dimension Sizes in a CDF file
CDFencoding, // Data encoding
CDFmajority, // Variable majority
CDFmaxRec, // max Record number
CDFAttrs; // number of CDF Attributes
struct cdfvar { // CDF variable structure
char name[CDF_VAR_NAME_LEN+1];
long num; //variable number
long datatype;
long numElem; // variable dimensionality
long recVariance; // variable rec Variance
long dimVariances[CDF_MAX_DIMS]; // # of data values in dimSizes
} cdfVar[MAX_VARS];
char mode[4];
int ncID;
char ncFile[] = "peim000000000.nc";
int TimeDimID, TimeLengthID, DataID, Data2ID;
int TimeDimVector[2], DataDimVector[2], Data2DimVector[2]; // netCDF Dim vectors
size_t Start[2] = {0L,0L};
size_t TimeCount[2] = {1L,TIMELENGTH};
size_t Data1Count[2] = {1L,1L};
size_t DataCount[2] = {1L,3L};
size_t Data2Count[2] = {1L,6L};
char Version[]="v01";
char ThemisID[]="tha\0";
char ThemisTime[]="tha_peim_time";
char PAR0[]="tha_peim_density";
char PAR1[]="tha_peim_ptot";
char PAR2[]="tha_peim_flux";
char PAR3[]="tha_peim_mftens"; // Momentum Flux Tensor DSL
char PAR4[]="tha_peim_eflux";
char PAR5[]="tha_peim_velocity_gse";
char PAR6[]="tha_peim_ptens"; // Pressure Tensor DSL
char PAR7[]="tha_peim_mag";
char PAR8[]="tha_peim_velocity_mag";
char PAR9[]="tha_peim_t3_mag"; // Diagonilized Temperature
char PAR10[]="tha_peim_ptens_mag"; //
int StartTimeID, StopTimeID;
int DeltaTID, DensID, PtotID; // 1D
int FluxID, EfluxID, VelID, MagID, VelMagID, TempMagID; // 2D -> 3
int MfTensID, PtensID, PtensMagID ; // 2D -> 6
int TimeID; // Time netCDF variable
char StartT[TIMELENGTH]; // Start time from data
char StopT[TIMELENGTH]; // Stop time from data
/**************************
Function prototypes
**************************/
void usage();
void cdf_handle_error (CDFstatus);
void nc_handle_error (int);
void ncdefine();
/*--------------------------------------------------------------------------*/
void usage()
{
printf ("\nDescription:\n");
printf (" This program converts a themis CDF file into a netCDF file.\n");
printf ("\n");
printf ("Usage: esa2nc <CDF file name> <ThemisID>\n");
printf ("\n");
printf ("Example: esa2nc testfile tha\n");
printf ("\n");
exit(1);
}
/*--------------------------------------------------------------------------
* Handles a CDF error.
*--------------------------------------------------------------------------*/
void cdf_handle_error(CDFstatus status)
{
char message[CDF_STATUSTEXT_LEN+1];
CDFerror (status, message); /* Get the appropriate message */
fprintf (stderr, "CDF: %s\n", message);
// exit(1);
}
/*--------------------------------------------------------------------------
* Handles a netCDF error.
*--------------------------------------------------------------------------*/
void nc_handle_error(int status)
{
fprintf(stderr, "%s\n", nc_strerror(status));
exit(1);
}
/*--------------------------------------------------------------------------
* NetCDF File Definition *
*--------------------------------------------------------------------------*/
void ncdefine(double Time)
{
char *s;
time_t p;
int status;
char *STime;
/*********************** Create netCDF file *****************************/
memcpy(&(ncFile[0]),&(mode[0]),4);
STime = Double2DD_Time(Time);
memcpy(&(ncFile[4]),&(STime[2]), 9);
if ((status = nc_create(ncFile, NC_CLOBBER, &ncID)) != NC_NOERR)
nc_handle_error(status);
/******************netCDF Dimensions *******************************/
nc_def_dim (ncID, "Time", NC_UNLIMITED, &TimeDimID);
nc_def_dim (ncID, "TimeLength", TIMELENGTH, &TimeLengthID);
nc_def_dim (ncID, "Data",3L, &DataID);
nc_def_dim (ncID, "Data2",6L, &Data2ID);
/********************netCDF Variables ******************/
TimeDimVector[0] = TimeDimID;
DataDimVector[0] = TimeDimID;
Data2DimVector[0] = TimeDimID;
TimeDimVector[1] = TimeLengthID;
DataDimVector[1] = DataID;
Data2DimVector[1] = Data2ID;
//ModeID, DensID, VelID, TempID;
nc_def_var (ncID, "Time", NC_CHAR, 2, TimeDimVector, &TimeID);
nc_def_var (ncID, "Density", NC_DOUBLE, 1, &TimeDimID, &DensID);
nc_def_var (ncID, "Ptot", NC_DOUBLE, 1, &TimeDimID, &PtotID);
nc_def_var (ncID, "Flux", NC_DOUBLE, 2, DataDimVector, &FluxID);
nc_def_var (ncID, "MfTens", NC_DOUBLE, 2, Data2DimVector, &MfTensID);
nc_def_var (ncID, "Eflux", NC_DOUBLE, 2, DataDimVector, &EfluxID);
nc_def_var (ncID, "Velocity", NC_DOUBLE, 2, DataDimVector, &VelID);
nc_def_var (ncID, "Ptens", NC_DOUBLE, 2, Data2DimVector, &PtensID);
nc_def_var (ncID, "Mag", NC_DOUBLE, 2, DataDimVector, &MagID);
nc_def_var (ncID, "VelocityMag", NC_DOUBLE, 2, DataDimVector, &VelMagID);
nc_def_var (ncID, "TempMag", NC_DOUBLE, 2, DataDimVector, &TempMagID);
nc_def_var (ncID, "PtensMag", NC_DOUBLE, 2, Data2DimVector, &PtensMagID);
nc_def_var (ncID, "StartTime",NC_CHAR, 1, &TimeLengthID, &StartTimeID);
nc_def_var (ncID, "StopTime",NC_CHAR, 1, &TimeLengthID , &StopTimeID);
nc_put_att_text(ncID, NC_GLOBAL, "Themis", 3, ThemisID);
nc_put_att_text(ncID, NC_GLOBAL, "Source", 12, Source);
nc_put_att_text(ncID, DensID, "Units", 5, "cm^-3");
time(&p);
s = ctime(&p);
nc_put_att_text(ncID, NC_GLOBAL, "Created", 24, s);
nc_enddef(ncID);
nc_put_vara_text(ncID, StartTimeID, &(Start[1]), &(TimeCount[1]), Double2DD_Time(Time));
;
}
/*--------------------------------------------------------------------------*/
int main(int argc, char *argv[])
{
long RecStart = 0, RecCount, RecCountF = 1, RecInt = 1, MaxRec;
long indices[1] = {0}, counts[1] = {3}, counts2[1] = {6}, intervals[1] = {1};
long countsE[1] = {1}, indicesF[2]={0,3}, indicesF2[2]={0,6};
double *value;
float *value_f;
size_t numElem;
long dimN, varN, par0, par1, par2, par3, par4, par5, par6, par7, par8, par9, par10;
CDFstatus cstatus; // CDF status code
CDFid id;
int i, j, status;
char fileName[MAX_FILE_NAME_LEN];
dd_tmstr_t *dtm;
int First = 1;
char *UT;
char data_set[8]="thx_peim";
/*-------------------------------- Arguments Decoding ----------------------------------------*/
if (argc <= 2) usage(); // CDF input file name and THEMIS Number not specified
else
{
strcpy(fileName, argv[1]);
strncpy(ThemisID,argv[2],3);
strncpy(mode,argv[3],4);
}
/*------------------------------------------ CDF Variables Names Updated according to THEMIS Number -------------*/
memcpy(&(data_set[2]),&(ThemisID[2]),1);
memcpy(&(data_set[6]),&(mode[2]),2);
memcpy(&(ThemisTime[0]),&(data_set[0]),8);
memcpy(&(PAR0[0]),&(data_set[0]),8);
memcpy(&(PAR1[0]),&(data_set[0]),8);
memcpy(&(PAR2[0]),&(data_set[0]),8);
memcpy(&(PAR3[0]),&(data_set[0]),8);
memcpy(&(PAR4[0]),&(data_set[0]),8);
memcpy(&(PAR5[0]),&(data_set[0]),8);
memcpy(&(PAR6[0]),&(data_set[0]),8);
memcpy(&(PAR7[0]),&(data_set[0]),8);
memcpy(&(PAR8[0]),&(data_set[0]),8);
memcpy(&(PAR9[0]),&(data_set[0]),8);
memcpy(&(PAR10[0]),&(data_set[0]),8);
/*********************** Open CDF file *****************************/
if ((cstatus = CDFopen(fileName, &id)) != CDF_OK)
cdf_handle_error(cstatus);
printf(" THEMIS %s \n", ThemisID);
/*********** treat all vars as zVars with eliminated false dimensionality **********/
if ((cstatus = CDFlib(SELECT_, CDF_zMODE_, zMODEon2, NULL_)) != CDF_OK)
cdf_handle_error (cstatus);
/************************ Get CDF Data ************************************/
cstatus = CDFlib(GET_, zVAR_NUMBER_, ThemisTime, &varN, NULL_);
cstatus = CDFlib( SELECT_, zVAR_, varN, GET_, zVAR_MAXREC_, &MaxRec, NULL_);
cstatus = CDFlib(GET_, zVAR_NUMBER_, PAR0, &par0, NULL_);
cstatus = CDFlib(GET_, zVAR_NUMBER_, PAR1, &par1, NULL_);
cstatus = CDFlib(GET_, zVAR_NUMBER_, PAR2, &par2, NULL_);
cstatus = CDFlib(GET_, zVAR_NUMBER_, PAR3, &par3, NULL_);
cstatus = CDFlib(GET_, zVAR_NUMBER_, PAR4, &par4, NULL_);
cstatus = CDFlib(GET_, zVAR_NUMBER_, PAR5, &par5, NULL_);
cstatus = CDFlib(GET_, zVAR_NUMBER_, PAR6, &par6, NULL_);
cstatus = CDFlib(GET_, zVAR_NUMBER_, PAR7, &par7, NULL_);
cstatus = CDFlib(GET_, zVAR_NUMBER_, PAR8, &par8, NULL_);
cstatus = CDFlib(GET_, zVAR_NUMBER_, PAR9, &par9, NULL_);
cstatus = CDFlib(GET_, zVAR_NUMBER_, PAR10, &par10, NULL_);
RecCount = (long)(MaxRec+1);
printf(" Max Rec %d\n", MaxRec);
if ( RecCount > 0) {
value = (double *)malloc(sizeof(double)* RecCount);
if ((cstatus = CDFlib (SELECT_,
zVAR_, varN,
zVAR_RECNUMBER_, RecStart ,
zVAR_RECCOUNT_, RecCount,
zVAR_RECINTERVAL_, RecInt,
zVAR_DIMINDICES_, indices,
zVAR_DIMCOUNTS_, countsE,
zVAR_DIMINTERVALS_, intervals,
GET_, zVAR_HYPERDATA_, value, NULL_) )
!= CDF_OK) cdf_handle_error(cstatus);
for (i = 0; i < RecCount; i++) {
UT = Double2DD_Time(value[i]);
dtm = ReadTime(UT);
if (First == 1) {
ncdefine(dtm->times);
First = 0;
}
nc_put_vara_text(ncID, TimeID, Start, TimeCount, UT);
Start[0]++;
}
nc_put_vara_text(ncID,StopTimeID, &Start[1], &TimeCount[1], UT);
// Density
if ((cstatus = CDFlib (SELECT_, zVAR_, par0,
zVAR_RECNUMBER_, RecStart,
zVAR_RECCOUNT_, RecCount,
zVAR_RECINTERVAL_, RecInt,
zVAR_DIMINDICES_, indices,
zVAR_DIMCOUNTS_, countsE,
zVAR_DIMINTERVALS_, intervals,
GET_, zVAR_HYPERDATA_, value, NULL_))
!= CDF_OK) cdf_handle_error(cstatus);
nc_put_var_double(ncID, DensID, value);
// Ion Total Pressure
if ((cstatus = CDFlib (SELECT_, zVAR_, par1,
zVAR_RECNUMBER_, RecStart,
zVAR_RECCOUNT_, RecCount,
zVAR_RECINTERVAL_, RecInt,
zVAR_DIMINDICES_, indices,
zVAR_DIMCOUNTS_, countsE,
zVAR_DIMINTERVALS_, intervals,
GET_, zVAR_HYPERDATA_, value, NULL_))
!= CDF_OK) cdf_handle_error(cstatus);
nc_put_var_double(ncID, PtotID, value);
free(value);
value = (double *)malloc(sizeof(double)* RecCount * 3);
// flux
if ((cstatus = CDFlib (SELECT_, zVAR_, par2,
zVAR_RECNUMBER_, RecStart ,
zVAR_RECCOUNT_, RecCount,
zVAR_RECINTERVAL_, RecInt,
zVAR_DIMINDICES_, indicesF,
zVAR_DIMCOUNTS_, counts,
zVAR_DIMINTERVALS_, intervals,
GET_, zVAR_HYPERDATA_, value, NULL_))
!= CDF_OK) cdf_handle_error(cstatus);
nc_put_var_double(ncID, FluxID, value);
// Energy Flux
if ((cstatus = CDFlib (SELECT_, zVAR_, par4,
zVAR_RECNUMBER_, RecStart ,
zVAR_RECCOUNT_, RecCount,
zVAR_RECINTERVAL_, RecInt,
zVAR_DIMINDICES_, indicesF,
zVAR_DIMCOUNTS_, counts,
zVAR_DIMINTERVALS_, intervals,
GET_, zVAR_HYPERDATA_, value, NULL_))
!= CDF_OK) cdf_handle_error(cstatus);
nc_put_var_double(ncID, EfluxID,value);
// Velocity GSE
if ((cstatus = CDFlib (SELECT_, zVAR_, par5,
zVAR_RECNUMBER_, RecStart ,
zVAR_RECCOUNT_, RecCount,
zVAR_RECINTERVAL_, RecInt,
zVAR_DIMINDICES_, indicesF,
zVAR_DIMCOUNTS_, counts,
zVAR_DIMINTERVALS_, intervals,
GET_, zVAR_HYPERDATA_, value, NULL_))
!= CDF_OK) cdf_handle_error(cstatus);
nc_put_var_double(ncID, VelID, value);
// Magnetic Field Vector DSL
if ((cstatus = CDFlib (SELECT_, zVAR_, par7,
zVAR_RECNUMBER_, RecStart ,
zVAR_RECCOUNT_, RecCount,
zVAR_RECINTERVAL_, RecInt,
zVAR_DIMINDICES_, indicesF,
zVAR_DIMCOUNTS_, counts,
zVAR_DIMINTERVALS_, intervals,
GET_, zVAR_HYPERDATA_, value, NULL_))
!= CDF_OK) cdf_handle_error(cstatus);
nc_put_var_double(ncID, MagID, value);
// Velocity Mag
if ((cstatus = CDFlib (SELECT_, zVAR_, par8,
zVAR_RECNUMBER_, RecStart ,
zVAR_RECCOUNT_, RecCount,
zVAR_RECINTERVAL_, RecInt,
zVAR_DIMINDICES_, indicesF,
zVAR_DIMCOUNTS_, counts,
zVAR_DIMINTERVALS_, intervals,
GET_, zVAR_HYPERDATA_, value, NULL_))
!= CDF_OK) cdf_handle_error(cstatus);
nc_put_var_double(ncID, VelMagID, value);
// Temperature Mag
if ((cstatus = CDFlib (SELECT_, zVAR_, par9,
zVAR_RECNUMBER_, RecStart ,
zVAR_RECCOUNT_, RecCount,
zVAR_RECINTERVAL_, RecInt,
zVAR_DIMINDICES_, indicesF,
zVAR_DIMCOUNTS_, counts,
zVAR_DIMINTERVALS_, intervals,
GET_, zVAR_HYPERDATA_, value, NULL_))
!= CDF_OK) cdf_handle_error(cstatus);
nc_put_var_double(ncID, TempMagID, value);
free(value);
value = (double *)malloc(sizeof(double)* RecCount * 6);
// Momentum Flux Tensor
if ((cstatus = CDFlib (SELECT_, zVAR_, par3,
zVAR_RECNUMBER_, RecStart ,
zVAR_RECCOUNT_, RecCount,
zVAR_RECINTERVAL_, RecInt,
zVAR_DIMINDICES_, indicesF2,
zVAR_DIMCOUNTS_, counts,
zVAR_DIMINTERVALS_, intervals,
GET_, zVAR_HYPERDATA_, value, NULL_))
!= CDF_OK) cdf_handle_error(cstatus);
nc_put_var_double(ncID, MfTensID, value);
// Pressure Tensor
if ((cstatus = CDFlib (SELECT_, zVAR_, par6,
zVAR_RECNUMBER_, RecStart ,
zVAR_RECCOUNT_, RecCount,
zVAR_RECINTERVAL_, RecInt,
zVAR_DIMINDICES_, indicesF2,
zVAR_DIMCOUNTS_, counts,
zVAR_DIMINTERVALS_, intervals,
GET_, zVAR_HYPERDATA_, value, NULL_))
!= CDF_OK) cdf_handle_error(cstatus);
nc_put_var_double(ncID, PtensID, value);
// Pressure Tensor Mag
if ((cstatus = CDFlib (SELECT_, zVAR_, par10,
zVAR_RECNUMBER_, RecStart ,
zVAR_RECCOUNT_, RecCount,
zVAR_RECINTERVAL_, RecInt,
zVAR_DIMINDICES_, indicesF2,
zVAR_DIMCOUNTS_, counts,
zVAR_DIMINTERVALS_, intervals,
GET_, zVAR_HYPERDATA_, value, NULL_))
!= CDF_OK) cdf_handle_error(cstatus);
nc_put_var_double(ncID, PtensMagID, value);
free(value);
/********************Close Files******************/
if ((status = nc_close(ncID)) != NC_NOERR) nc_handle_error(status);
}
if ((cstatus = CDFlib(CLOSE_, CDF_, NULL_)) != CDF_OK)
cdf_handle_error (cstatus);
}