Blame view

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


#include <stdio.h>
#include <stdlib.h>
#include <cdf.h>
#include <string.h>

#define MAX_FILE_NAME_LEN  120  

void cdf_status_handler (CDFstatus, char *); 
char * cdf_str_datatype (long);

typedef struct cdfvar {           /* CDF variable structure */
7d5742d0   Elena.Budnik   cdf attribute info
19
20
21
22
23
24
25
	char name[CDF_VAR_NAME_LEN+1];   
	long datatype;                   
	long numElements;                /* string length for CDF_CHAR, 1 otherwise */
	long dimensionality;             /* variable dimensionality                 */
	long dimSizes[CDF_MAX_DIMS];     /* variable dimension sizes                */
	long recVariance;                /* record variance                         */
	long numRecs;                    /* # of records this variable contains     */
16035364   Benjamin Renard   First commit
26
27
28
} CDFvar;

int main(int argc, char *argv[])
7d5742d0   Elena.Budnik   cdf attribute info
29
30
31
32
33
34
35
36
37
38
{  
	CDFvar var;
	CDFid    id;                     /* CDF file ID      */ 
	CDFstatus   status;              /* CDF status code */

	long nZvars, nAttrs, attrId, attrScope, varId, datatype, numElements; 
	char attrName[CDF_ATTR_NAME_LEN+1];
	char fileName[MAX_FILE_NAME_LEN], varName[CDF_VAR_NAME_LEN+1];
	char   msg[100];
	int dummy, i;
16035364   Benjamin Renard   First commit
39
40
  
  
7d5742d0   Elena.Budnik   cdf attribute info
41
42
43
44
45
46
	if (argc <= 2) 
		exit(0);                   /* CDF input file name not specified */
	else {
		strcpy(fileName, argv[1]);       /* Get the input file name */
		strcpy(varName, argv[2]);   
	}
16035364   Benjamin Renard   First commit
47
	
7d5742d0   Elena.Budnik   cdf attribute info
48
	status = CDFlib (OPEN_, CDF_, fileName, &id,  NULL_);   
16035364   Benjamin Renard   First commit
49
 		     
7d5742d0   Elena.Budnik   cdf attribute info
50
51
52
53
54
	if (status != CDF_OK)  {
		strcpy(msg, "OPEN_, CDF_, ");       
		strcat(msg, fileName);
		cdf_status_handler (status, msg);
	}
16035364   Benjamin Renard   First commit
55
56
57
58
59
60
61
62
63
64
65
66
67
 
 /*---------------------------------------------------------------------------
 *   This routine retrievs the following information:
 *
 *      nAttr - number of attributes (including global and variable)
 *      nZvars - number of zVariables
 *
 *   CDF file can have both rVariables (old style) and zVariables (new style)
 *   simultaneously.  zVariable is a superset of rVariable, and it is a lot
 *   more efficient and offers all the functionality a rVariable offers and 
 *   more.  Treat all CDF variables as zVariables.
 *--------------------------------------------------------------------------*/ 
 
7d5742d0   Elena.Budnik   cdf attribute info
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
	status = CDFlib (SELECT_, CDF_zMODE_, zMODEon2,
							GET_, CDF_NUMATTRS_,  &nAttrs, 
							CDF_NUMzVARS_,  &nZvars, 
							NULL_);
	
	if (status != CDF_OK) cdf_status_handler(status, "GET_, CDF_FILEINFO_");

	for (varId=0; varId < nZvars; varId++) 
	{
		status = CDFlib (SELECT_,zVAR_, varId,
								GET_, zVAR_NAME_, var.name,
								zVAR_DATATYPE_, &var.datatype,
								zVAR_NUMELEMS_, &var.numElements,
								zVAR_NUMDIMS_, &var.dimensionality,
								zVAR_DIMSIZES_, var.dimSizes,
								zVAR_NUMRECS_, &var.numRecs,
								zVAR_RECVARY_, &var.recVariance,
								NULL_);
		
		if (status != CDF_OK) cdf_status_handler (status, "GET_, zVARS_");
16035364   Benjamin Renard   First commit
88
   
7d5742d0   Elena.Budnik   cdf attribute info
89
90
91
92
93
94
95
96
		if (strcmp(var.name, varName) == 0) 
		{  
			printf("%s %d %d ", cdf_str_datatype(var.datatype), var.dimensionality, var.numRecs);
			for (i = 0; i < var.dimensionality; i++) printf("%d ",var.dimSizes[i]);
			
			printf("\n");      
			break;  
		}
16035364   Benjamin Renard   First commit
97
	}
7d5742d0   Elena.Budnik   cdf attribute info
98
99
100
	
	/* Close the CDF file */
	status = CDFlib (CLOSE_, CDF_, NULL_);        
16035364   Benjamin Renard   First commit
101
102
103
}


16035364   Benjamin Renard   First commit
104
105
void cdf_status_handler (CDFstatus status, char *source)
{
7d5742d0   Elena.Budnik   cdf attribute info
106
	char  message[CDF_STATUSTEXT_LEN+1];
16035364   Benjamin Renard   First commit
107

7d5742d0   Elena.Budnik   cdf attribute info
108
	CDFerror (status, message);              /* Get the appropriate message */
16035364   Benjamin Renard   First commit
109

7d5742d0   Elena.Budnik   cdf attribute info
110
111
112
113
114
115
116
117
118
119
120
121
122
123
	if (status < CDF_WARN) {
			printf ("An error has occurred, halting...\n");
			printf ("%s\n", message);
			printf ("** Error source: %s\n", source);
			exit (status);
	}
	else if (status < CDF_OK) {
			printf ("Warning, function may not have compeleted as expected...\n");
			printf ("%s\n", message);
	}
	else if (status > CDF_OK) {
			printf ("Function compeleted successfully, but be advised that...\n");
			printf ("%s\n", message);
	}   
16035364   Benjamin Renard   First commit
124
}
7d5742d0   Elena.Budnik   cdf attribute info
125

16035364   Benjamin Renard   First commit
126
127
128
129
130
131
/*--------------------------------------------------------------------------
 *  This routine returns the string representation of the given CDF 
 *  datatype.
 *--------------------------------------------------------------------------*/
char *  cdf_str_datatype (long type)
{
7d5742d0   Elena.Budnik   cdf attribute info
132
133
134
	switch (type) {
	case CDF_BYTE:
		return "CDF_BYTE";
16035364   Benjamin Renard   First commit
135

7d5742d0   Elena.Budnik   cdf attribute info
136
137
	case CDF_INT1:
		return "CDF_INT1";
16035364   Benjamin Renard   First commit
138

7d5742d0   Elena.Budnik   cdf attribute info
139
140
	case CDF_CHAR:
		return "CDF_CHAR";
16035364   Benjamin Renard   First commit
141

7d5742d0   Elena.Budnik   cdf attribute info
142
143
	case CDF_INT2:
		return "CDF_INT2";
16035364   Benjamin Renard   First commit
144

7d5742d0   Elena.Budnik   cdf attribute info
145
146
	case CDF_UCHAR:
		return "CDF_UCHAR";
16035364   Benjamin Renard   First commit
147

7d5742d0   Elena.Budnik   cdf attribute info
148
149
	case CDF_UINT1:
		return "CDF_UINT1";
16035364   Benjamin Renard   First commit
150

7d5742d0   Elena.Budnik   cdf attribute info
151
152
	case CDF_INT4:
		return "CDF_INT4";
16035364   Benjamin Renard   First commit
153

7d5742d0   Elena.Budnik   cdf attribute info
154
155
	case CDF_UINT2:
		return "CDF_UINT2";
16035364   Benjamin Renard   First commit
156

7d5742d0   Elena.Budnik   cdf attribute info
157
158
	case CDF_FLOAT:
		return "CDF_FLOAT";
16035364   Benjamin Renard   First commit
159

7d5742d0   Elena.Budnik   cdf attribute info
160
161
	case CDF_REAL4:
		return "CDF_REAL4";
16035364   Benjamin Renard   First commit
162

7d5742d0   Elena.Budnik   cdf attribute info
163
164
	case CDF_DOUBLE:
		return "CDF_DOUBLE";
16035364   Benjamin Renard   First commit
165

7d5742d0   Elena.Budnik   cdf attribute info
166
167
	case CDF_REAL8:
		return "CDF_REAL8";
16035364   Benjamin Renard   First commit
168

7d5742d0   Elena.Budnik   cdf attribute info
169
170
	case CDF_UINT4:
		return "CDF_UINT4";
16035364   Benjamin Renard   First commit
171

7d5742d0   Elena.Budnik   cdf attribute info
172
173
	case CDF_EPOCH:
		return "CDF_EPOCH";
16035364   Benjamin Renard   First commit
174

7d5742d0   Elena.Budnik   cdf attribute info
175
176
177
178
	case CDF_EPOCH16:
		return "CDF_EPOCH16";
	}
}