Blame view

php/src/cdfstartstopfromdata.c 5.13 KB
16035364   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* $Id: StartStopFromData.c,v 1.1 2010/08/31 10:32:55 budnik Exp $ */

/** @file StartStopFromData.c
*   @brief  
*/
#include <stdio.h>
#include <stdlib.h>
#include <cdf.h>
#include <string.h>
#include <time.h>
#include <math.h>

#define MAX_FILE_NAME_LEN  120
#define TIMESHIFT  62167219200.0 
/*************************************
   Global variables and structures
**************************************/
long  CDFDims,                                         // Number of dimensions in a CDF file
cdb91ea6   Elena.Budnik   redmine #5568
19
20
21
22
23
24
25
	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
//   CDFdatatype;
16035364   Benjamin Renard   First commit
26
27

struct cdfvar {                              // CDF variable structure
cdb91ea6   Elena.Budnik   redmine #5568
28
29
30
31
32
33
	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
16035364   Benjamin Renard   First commit
34
35
36
37
38
39
40
41
42
} cdfVar;



/*--------------------------------------------------------------------------
 *  Handles a CDF error.
 *--------------------------------------------------------------------------*/
void cdf_handle_error(CDFstatus status)
{
cdb91ea6   Elena.Budnik   redmine #5568
43
	char  message[CDF_STATUSTEXT_LEN+1];
16035364   Benjamin Renard   First commit
44

cdb91ea6   Elena.Budnik   redmine #5568
45
46
47
	CDFerror (status, message);              /* Get the appropriate message */
	fprintf  (stderr, "%s\n", message);
	exit(1);
16035364   Benjamin Renard   First commit
48
49
}

16035364   Benjamin Renard   First commit
50
51
int main(int argc, char *argv[])
{
cdb91ea6   Elena.Budnik   redmine #5568
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
	char fileName[MAX_FILE_NAME_LEN];
	long RecStart = 0, RecCount, RecInt = 1;
	long indices[1] = {0}, intervals[1] = {1}, counts[1] = {1};
	long varN = -1;  
	long datatype = 0;
	CDFstatus   cstatus;           // CDF status code
	CDFid id;
	double *value;
	int i;
	double epoch16[2], dbl_value, dbl_value_;
	long year, month, day, hour, min, sec, msec, mksec, nsec, psec;    
	long long *int_value;
		
	if (argc <= 1) 
		exit(0);                               /* CDF input file name not specified */
	else
		strcpy(fileName, argv[argc-1]);       /* Get the input file name */ 
16035364   Benjamin Renard   First commit
69
70

/*********************** Open CDF file *****************************/
cdb91ea6   Elena.Budnik   redmine #5568
71
72
	if ((cstatus = CDFopen(fileName, &id))  != CDF_OK)
										cdf_handle_error(cstatus);
16035364   Benjamin Renard   First commit
73
74
75
 
/*********** treat all vars as zVars with eliminated false dimensionality and get Nrecs **********/
    
cdb91ea6   Elena.Budnik   redmine #5568
76
77
78
79
80
81
82
83
84
	if ((cstatus = CDFlib(SELECT_, CDF_zMODE_, zMODEon2, NULL_)) != CDF_OK)
																cdf_handle_error (cstatus);
	
	while (datatype != CDF_EPOCH && datatype != CDF_EPOCH16 && datatype != CDF_TIME_TT2000) {
		varN++;
		if ((cstatus = CDFlib(SELECT_, zVAR_, varN,
									GET_, zVAR_DATATYPE_, &datatype, NULL_)) != CDF_OK)
																cdf_handle_error (cstatus);
	}
16035364   Benjamin Renard   First commit
85
   
cdb91ea6   Elena.Budnik   redmine #5568
86
87
88
	if ((cstatus = CDFlib(SELECT_, zVAR_, varN,
								GET_, zVAR_MAXREC_, &CDFmaxRec, NULL_)) != CDF_OK)
																cdf_handle_error (cstatus);
16035364   Benjamin Renard   First commit
89

cdb91ea6   Elena.Budnik   redmine #5568
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
	//   fprintf(stdout,"Max Recs %d\n", CDFmaxRec+1);
	if (CDFmaxRec < 1) exit (0);
	
   if ( datatype == CDF_TIME_TT2000 ) {
		int_value = (long long *) malloc (sizeof(long long) * (CDFmaxRec+1));
		/******************* Get Epoch  *************************/
		if ((cstatus = CDFlib (SELECT_,
								zVAR_, varN,
								zVAR_RECNUMBER_, RecStart,
								zVAR_RECCOUNT_, CDFmaxRec+1,
								zVAR_RECINTERVAL_, RecInt,
								zVAR_DIMINDICES_, indices,
								zVAR_DIMCOUNTS_, counts,
								zVAR_DIMINTERVALS_, intervals,
								GET_, zVAR_HYPERDATA_, int_value,  NULL_)) != CDF_OK) 
															cdf_handle_error (cstatus);
		
		dbl_value_ = CDF_TT2000_to_UTC_EPOCH(int_value[0]);
		dbl_value = CDF_TT2000_to_UTC_EPOCH(int_value[CDFmaxRec]);	
		printf("%f %f\n", dbl_value_/1000.0-TIMESHIFT,dbl_value/1000.0-TIMESHIFT);
		free(int_value); 
	}
	else {
		value = (double *)malloc(sizeof(double)* (CDFmaxRec+1)*((datatype == CDF_EPOCH16)+1));
16035364   Benjamin Renard   First commit
114

cdb91ea6   Elena.Budnik   redmine #5568
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
		/******************* Get Epoch  *************************/
		if ((cstatus = CDFlib (SELECT_,
								zVAR_, varN,
								zVAR_RECNUMBER_, RecStart,
								zVAR_RECCOUNT_, CDFmaxRec+1,
								zVAR_RECINTERVAL_, RecInt,
								zVAR_DIMINDICES_, indices,
								zVAR_DIMCOUNTS_, counts,
								zVAR_DIMINTERVALS_, intervals,
								GET_, zVAR_HYPERDATA_, value,  NULL_)) != CDF_OK) 
															cdf_handle_error (cstatus);
	 
		if (datatype == CDF_EPOCH16) {
			epoch16[0] = value[0];
			epoch16[1] = value[1];
			EPOCH16breakdown(epoch16, &year, &month, &day, &hour, &min, &sec, &msec, &mksec, &nsec, &psec);
			value[0] = computeEPOCH(year, month, day, hour, min, sec, msec);
			epoch16[0] = value[CDFmaxRec*2];
			epoch16[1] = value[CDFmaxRec*2+1];
			EPOCH16breakdown(epoch16, &year, &month, &day, &hour, &min, &sec, &msec, &mksec, &nsec, &psec);
			value[CDFmaxRec] = computeEPOCH(year, month, day, hour, min, sec, msec);
		}
		
		printf("%f %f\n", value[0]/1000.0-TIMESHIFT, value[CDFmaxRec]/1000.0-TIMESHIFT);
		free(value); 
	}
16035364   Benjamin Renard   First commit
141
   
cdb91ea6   Elena.Budnik   redmine #5568
142
143
    	if ((cstatus = CDFlib(CLOSE_, CDF_, NULL_)) != CDF_OK)
										cdf_handle_error (cstatus);   
16035364   Benjamin Renard   First commit
144
}