File: ReadLevels.c

package info (click to toggle)
hdf-eos4 3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,960 kB
  • sloc: ansic: 43,570; sh: 11,324; fortran: 9,649; makefile: 261
file content (270 lines) | stat: -rw-r--r-- 6,073 bytes parent folder | download | duplicates (5)
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
#include "mfhdf.h"
#include <math.h>

/* ReadLevels */

main()
{
    intn            status, i, j;
    
    int32           ptfid, n, nt, sz;
    int32           PTid, date, i32;
    float32         rain, temp, conc[4];
    float64         lon, lat, time;
    int32           fldsz, fldlevels[32], nattr;
    int32           fldtype[32], fldorder[32], recs[128];
    
    char            *pntr, *buffer, id[2], desc[16], spc[8];
    char            fldlist[128], *attrnames;
    int32           strbufsize;


    /*
     * Open the HDF swath file, "PointFile.hdf".
     */

    ptfid = PTopen("PointFile_created_with_hadeos_sample_file_writer_of_HDFEOS2_version_219_or_higher_release.hdf", DFACC_READ);
    if(ptfid == -1)
      {
	printf("\t\tError: Unable to open file PointFile.hdf\n");
	return -1;
       }


    /* Read Simple Point */
    /* ----------------- */
    PTid = PTattach(ptfid, "Simple Point");
    if (PTid == -1)
      {
	printf("\t\tError: Cannot attach point \"Simple Point\"\n");
	return -1;
      }
    strcpy(fldlist,"");
    status = PTlevelinfo(PTid, 0, fldlist, fldtype, fldorder);
    if (status == -1)
      {
	printf("\t\tError: Cannot get level info for point \"Simple Point\"\n");
	return -1;
      }
    fldsz = PTsizeof(PTid, fldlist, fldlevels);
    if (fldsz == -1)
      {
	printf("\t\tError: Cannot get field size of \"Simple Point\"\n");
	return -1;
      }

    n = PTnrecs(PTid, 0);
    if (n == -1)
      {
	printf("\t\tError: point \"Simple Point\" has no records.\n");
	return -1;
      }
    if(fldsz == 0)
      {
	printf("\t\tError: Cannot allocate mamory for buffer;  fldsz == 0\n");
	return -1;
      }

    buffer = (char *) calloc(n * fldsz, 1);
    for (i=0; i<n; i++)
    {
	recs[i] = i;
    }
    
    status = PTreadlevel(PTid, 0, fldlist, n, recs, buffer);
    if (status == -1)
      {
	printf("\t\tError: Cannot read level for point \"Simple Point\"\n");
	return -1;
      }
    pntr = buffer;

    for (i=0; i<n; i++)
    {
	memcpy(&time, pntr, 8);
	pntr += 8;
	memcpy(conc, pntr, 4*4);
	pntr += 4*4;
	memcpy(spc, pntr, 4);
	pntr += 4;
	printf("%12.1f %6.2f %6.2f %6.2f %6.2f %s\n", 
	       time,conc[0],conc[1],conc[2],conc[3],spc);
    }
    
    free(buffer);
    PTdetach(PTid);
    


    /* Read Fixed Buoy Point */
    /* --------------------- */
    PTid = PTattach(ptfid, "FixedBuoy Point");
    if (status == -1)
      {
	printf("\t\tError: Cannot get level info for point \"FixedBuoy Point\"\n");
	return -1;
      }

    /* Read First (0th) Level */
    /* ----------------------- */
    strcpy(fldlist,"");
    status = PTlevelinfo(PTid, 0, fldlist, fldtype, fldorder);
    if (status == -1)
      {
	printf("\t\tError: Cannot get level info for point \"FixedBuoy Point\"\n");
	return -1;
      }

    fldsz = PTsizeof(PTid, fldlist, fldlevels);
    if (fldsz == -1)
      {
	printf("\t\tError: Cannot get field size of \"FixedBuoy Point\"\n");
	return -1;
      }

    n = PTnrecs(PTid, 0);
    if (n == -1)
      {
	printf("\t\tError: point \"Simple Point\" has no records.\n");
	return -1;
      }
    for (i=0; i<n; i++)
    {
	recs[i] = i;
    }

    if(fldsz == 0)
      {
	printf("\t\tError: Cannot allocate mamory for buffer;  fldsz == 0\n");
	return -1;
      }

    buffer = (char *) calloc(n * fldsz, 1);
    printf("\n");
    n=1;
    recs[0]=0;
    status = PTreadlevel(PTid, 0, fldlist, n, recs, buffer);
    if (status == -1)
      {
	printf("\t\tError: Cannot read level for point \"FixedBuoy Point\"\n");
	return -1;
      }
    pntr = buffer;
    for (i=0; i<n; i++)
    {
 	memcpy(desc, pntr, 8);
 	pntr += 8;
	desc[8] = 0;
	memcpy(&lon, pntr, 8);
	pntr += 8;
	memcpy(&lat, pntr, 8);
	pntr += 8;
	memcpy(&date, pntr, 4);
	pntr += 4;
	memcpy(id, pntr, 1);
	pntr += 1;
	id[1]=0;
	printf("%-10s %12.6lf %12.6lf %10d %-4s\n", desc,lon,lat,date,id);
    }
    printf("\n");
    free(buffer);


    /* Read Second (1th) Level */
    /* ------------------------ */
    strcpy(fldlist,"");
    status = PTlevelinfo(PTid, 1, fldlist, fldtype, fldorder);
    if (status == -1)
      {
	printf("\t\tError: Cannot get level info for point \"FixedBuoy Point\"\n");
	return -1;
      }

    fldsz = PTsizeof(PTid, fldlist, fldlevels);
    if (fldsz == -1)
      {
	printf("\t\tError: Cannot get field size of \"FixedBuoy Point\"\n");
	return -1;
      }


    n = PTnrecs(PTid, 1);

    if (n == -1)
      {
	printf("\t\tError: point \"FixedBuoy Point\" has no records.\n");
	return -1;
      }

    if(fldsz == 0)
      {
	printf("\t\tError: Cannot allocate mamory for buffer;  fldsz == 0\n");
	return -1;
      }

    buffer = (char *) calloc(fldsz, 1);

    for (i=0; i<n; i++)
    {
	pntr = buffer;
	recs[0] = i;
	
	status = PTreadlevel(PTid, 1, fldlist, 1, recs, buffer);
	if (status == -1)
	  {
	    printf("\t\tError: Cannot read level for point \"FixedBuoy Point\"\n");
	    return -1;
	  }
	memcpy(&time, pntr, 8);
	pntr += 8;
	memcpy(&rain, pntr, 4);
	pntr += 4;
	memcpy(&temp, pntr, 4);
	pntr += 4;
	memcpy(id, pntr, 1);
	id[1]= 0;
	pntr += 1;
	printf("%12.1f %6.2f %6.2f %s\n", time,rain,temp,id);
    }

    free(buffer);

    nattr = PTinqattrs(PTid, NULL,&strbufsize);
    if (nattr == -1)
      {
	printf("\t\tError: Cannot inquire attributes for \"FixedBuoy Point\"\n");
	return -1;
      }
    attrnames = (char *)malloc((strbufsize+1)*sizeof(char));
    
    nattr = PTinqattrs(PTid, attrnames,&strbufsize);
    if (nattr == -1)
      {
	printf("\t\tError: Cannot inquire attributes for \"FixedBuoy Point\"\n");
	return -1;
      }

    status = PTreadattr(PTid, attrnames, &i32);
    if (status == -1)
      {
	printf("\t\tError: Cannot read attribute for \"FixedBuoy Point\"\n");
	return -1;
      }
    printf("\n");
    printf("Attribute value for %s =%d\n", attrnames, i32);
    status = PTattrinfo(PTid, "int32", &nt, &sz);
    printf("\n");
    if (status == -1)
      {
	printf("\t\tError: Cannot get attribute info for \"FixedBuoy Point\"\n");
	return -1;
      }
    printf("\n");
    PTdetach(PTid);
    free(attrnames);
    
    PTclose(ptfid);

    return 0;
}