Blame view

php/src/cdfsamplingfromdata.c 5.38 KB
7d539a8c   Elena.Budnik   server side modifs
1
/* $Id: cdf constant samplig from data $ */
16035364   Benjamin Renard   First commit
2

16035364   Benjamin Renard   First commit
3
4
5
6
7
8
9
#include <stdio.h>
#include <stdlib.h>
#include <cdf.h>
#include <string.h>
#include <time.h>
#include <math.h>

23fba7a9   Elena.Budnik   no temp file for ...
10
#define MAX_FILE_NAME_LEN  120
16035364   Benjamin Renard   First commit
11
12
13
14
15
#define badFile -100

/*************************************
   Global variables and structures
**************************************/
cdb91ea6   Elena.Budnik   redmine #5568
16
17
18
19
20
21
22
23
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
//   CDFdatatype;
16035364   Benjamin Renard   First commit
24
25

struct cdfvar {                              // CDF variable structure
cdb91ea6   Elena.Budnik   redmine #5568
26
27
28
29
30
31
	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
32
33
} cdfVar;

16035364   Benjamin Renard   First commit
34
35
36
37
38
/*--------------------------------------------------------------------------
 *  Handles a CDF error.
 *--------------------------------------------------------------------------*/
void cdf_handle_error(CDFstatus status)
{
cdb91ea6   Elena.Budnik   redmine #5568
39
	char  message[CDF_STATUSTEXT_LEN+1];
16035364   Benjamin Renard   First commit
40

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

16035364   Benjamin Renard   First commit
46
47
int main(int argc, char *argv[])
{
23fba7a9   Elena.Budnik   no temp file for ...
48
	char fileName[MAX_FILE_NAME_LEN];
7d539a8c   Elena.Budnik   server side modifs
49
	long RecStart = 0, RecCount = 2, RecInt = 1;
cdb91ea6   Elena.Budnik   redmine #5568
50
51
52
53
54
55
	long indices[1] = {0}, intervals[1] = {1}, counts[1] = {1};
	long varN = -1;  
	long datatype = 0, attrN, maxEntry;
	CDFstatus   cstatus;           // CDF status code
	CDFid id;
	double *value;
cdb91ea6   Elena.Budnik   redmine #5568
56
	int i, minFalse = 0, numEnough;
cdb91ea6   Elena.Budnik   redmine #5568
57
	char attrValue[300];
7d539a8c   Elena.Budnik   server side modifs
58
	int sampling;
cdb91ea6   Elena.Budnik   redmine #5568
59
60
61
	double epoch16[2], dbl_value, dbl_value_;
	long year, month, day, hour, minute, sec, msec, mksec, nsec, psec; 
	long long *int_value;
23fba7a9   Elena.Budnik   no temp file for ...
62
63
64
65
66
	
	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
67
68
    
/*********************** Open CDF file *****************************/
cdb91ea6   Elena.Budnik   redmine #5568
69
70
	if ((cstatus = CDFopen(fileName, &id))  != CDF_OK)
										cdf_handle_error(cstatus);
16035364   Benjamin Renard   First commit
71
72
73
 
/*********** treat all vars as zVars with eliminated false dimensionality and get Nrecs **********/
    
cdb91ea6   Elena.Budnik   redmine #5568
74
75
76
77
78
79
80
81
	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);  
cdb91ea6   Elena.Budnik   redmine #5568
82
	}
7d539a8c   Elena.Budnik   server side modifs
83
	
cdb91ea6   Elena.Budnik   redmine #5568
84
85
86
87
88
89
	fprintf(stdout,"DATATYPE %d %d\n", varN, datatype);
	if ((cstatus = CDFlib(SELECT_, zVAR_, varN,
								GET_, zVAR_MAXREC_, &CDFmaxRec, NULL_)) != CDF_OK)
										cdf_handle_error (cstatus);

	fprintf(stdout,"Max Recs %d\n", CDFmaxRec+1);
7d539a8c   Elena.Budnik   server side modifs
90
91
	
	if (CDFmaxRec < 3) {
cdb91ea6   Elena.Budnik   redmine #5568
92
93
94
95
96
		fprintf(stdout," %d\n", badFile);
		exit (0);
	}
	
	if ( datatype == CDF_TIME_TT2000 ) {
7d539a8c   Elena.Budnik   server side modifs
97
		int_value = (long long *) malloc (sizeof(long long) * (RecCount+1));
cdb91ea6   Elena.Budnik   redmine #5568
98
99
100
101
		/******************* Get Epoch  *************************/
		if ((cstatus = CDFlib (SELECT_,
								zVAR_, varN,
								zVAR_RECNUMBER_, RecStart,
7d539a8c   Elena.Budnik   server side modifs
102
								zVAR_RECCOUNT_, RecCount + 1,
cdb91ea6   Elena.Budnik   redmine #5568
103
104
105
106
107
108
109
110
								zVAR_RECINTERVAL_, RecInt,
								zVAR_DIMINDICES_, indices,
								zVAR_DIMCOUNTS_, counts,
								zVAR_DIMINTERVALS_, intervals,
								GET_, zVAR_HYPERDATA_, int_value,  NULL_)) != CDF_OK) 
															cdf_handle_error (cstatus);
	}
	else {
7d539a8c   Elena.Budnik   server side modifs
111
		value = (double *)malloc(sizeof(double)* (RecCount + 1)*((datatype == CDF_EPOCH16)+1)); 
cdb91ea6   Elena.Budnik   redmine #5568
112
113
114
115
		/******************* Get Epoch  *************************/
		if ((cstatus = CDFlib (SELECT_,
								zVAR_, varN,
								zVAR_RECNUMBER_, RecStart,
7d539a8c   Elena.Budnik   server side modifs
116
								zVAR_RECCOUNT_, RecCount + 1,
cdb91ea6   Elena.Budnik   redmine #5568
117
118
119
120
121
122
123
								zVAR_RECINTERVAL_, RecInt,
								zVAR_DIMINDICES_, indices,
								zVAR_DIMCOUNTS_, counts,
								zVAR_DIMINTERVALS_, intervals,
								GET_, zVAR_HYPERDATA_, value,  NULL_)) != CDF_OK) 
															cdf_handle_error (cstatus);
	}
16035364   Benjamin Renard   First commit
124

7d539a8c   Elena.Budnik   server side modifs
125
	
cdb91ea6   Elena.Budnik   redmine #5568
126
		if (datatype == CDF_TIME_TT2000) {
7d539a8c   Elena.Budnik   server side modifs
127
128
129
			dbl_value_ = CDF_TT2000_to_UTC_EPOCH(int_value[0]);
			dbl_value = CDF_TT2000_to_UTC_EPOCH(int_value[1]);
			sampling = (int)(dbl_value - dbl_value_ + 50)/100;
cdb91ea6   Elena.Budnik   redmine #5568
130
131
132
		}
		else {
			if (datatype == CDF_EPOCH16) {
7d539a8c   Elena.Budnik   server side modifs
133
134
				epoch16[0] = value[0];
				epoch16[1] = value[1];
cdb91ea6   Elena.Budnik   redmine #5568
135
				EPOCH16breakdown(epoch16, &year, &month, &day, &hour, &minute, &sec, &msec, &mksec, &nsec, &psec);
7d539a8c   Elena.Budnik   server side modifs
136
137
138
				value[0] = computeEPOCH(year, month, day, hour, minute, sec, msec);
				epoch16[0] = value[2];
				epoch16[1] = value[3];
cdb91ea6   Elena.Budnik   redmine #5568
139
				EPOCH16breakdown(epoch16, &year, &month, &day, &hour, &minute, &sec, &msec, &mksec, &nsec, &psec);
7d539a8c   Elena.Budnik   server side modifs
140
				value[1] = computeEPOCH(year, month, day, hour, minute, sec, msec);
cdb91ea6   Elena.Budnik   redmine #5568
141
			}
7d539a8c   Elena.Budnik   server side modifs
142
			sampling = (int)(value[1] - value[0] + 50)/100;
cdb91ea6   Elena.Budnik   redmine #5568
143
		}
7d539a8c   Elena.Budnik   server side modifs
144

cdb91ea6   Elena.Budnik   redmine #5568
145
146
147
	// fprintf(stdout,"value %f\n", value[11]-value[10]);
	if (datatype == CDF_TIME_TT2000) free(int_value); 
	else free(value);
7d539a8c   Elena.Budnik   server side modifs
148

16035364   Benjamin Renard   First commit
149
     
cdb91ea6   Elena.Budnik   redmine #5568
150
151
	if ((cstatus = CDFlib(CLOSE_, CDF_, NULL_)) != CDF_OK)
										cdf_handle_error (cstatus);
16035364   Benjamin Renard   First commit
152

cdb91ea6   Elena.Budnik   redmine #5568
153
	//   fprintf(stdout,"%d %f\n",min[1], (float)min[0]/100.0);
7d539a8c   Elena.Budnik   server side modifs
154
	fprintf(stdout,"%f\n",(float)sampling/10.0); 
16035364   Benjamin Renard   First commit
155
}