File: WriteFields.c

package info (click to toggle)
hdf-eos4 2.20v1.00-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, trixie
  • size: 34,028 kB
  • sloc: ansic: 46,955; sh: 23,547; fortran: 15,467; csh: 1,098; makefile: 633
file content (149 lines) | stat: -rw-r--r-- 3,256 bytes parent folder | download | duplicates (4)
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
#include "hdf.h"


/*
 * In this example we will (1) open the "SwathFile" HDF file, (2) attach to
 * the "Swath1" swath, and (3) write data to the "Longitude", "Latitude",
 * and "Spectra" fields.
 */


main()
{

    intn            i, j, k, status, track, xtrack, start[3], count[3];

    int32           swfid, SWid, attr[4]={3, 5, 7, 11};

    float32         lng[10] = {0.0, 1.0, 2.0, 3.0, 4.0,
    5.0, 6.0, 7.0, 8.0, 9.0}, lat[10];
    /* Define longitude values along the cross track */

    float64         plane[40*20], tme[20];
    
    /*
     * Open the HDF swath file, "SwathFile.hdf".
     */

    swfid = SWopen("SwathFile_created_with_hadeos_sample_file_writer_of_HDFEOS2_version_219_or_higher_release.hdf", DFACC_RDWR);
    if (swfid == -1)
      {
	printf("\t\tError: Cannot open file \"SwathFile.hdf\"\n");
	return -1;
      }

    if (swfid != -1)
    {

	/*
	 * Attach the "Swath1" swath.
	 */

	SWid = SWattach(swfid, "Swath1");
	if (SWid == -1)
	  {
	    printf("\t\tError: Cannot attach swath \"Swath1\"\n");
	    return -1;
	  }
	if (SWid != -1)
	{

	    /* Write data starting at the beginning of each cross track */
	    start[1] = 0;
	    count[0] = 1;
	    count[1] = 10;
	    

	    /*
	     * Loop through all the tracks, incrementing the track starting
	     * position by one each time.
	     */

	    for (track = 0; track < 20; track++)
	    {
		start[0] = track;
		status = SWwritefield(SWid, "Longitude", start, NULL,
				      count, lng);
		if (status == -1)
		  {
		    printf("\t\tError: Cannot write field \"Longitude\"\n");
		    return -1;
		  }
                for (xtrack = 0; xtrack < 10; xtrack++)
                    lat[xtrack] = track;
		
                status = SWwritefield(SWid, "Latitude", start, NULL,
                                      count, lat);
		if (status == -1)
		  {
		    printf("\t\tError: Cannot write field \"Latitude\"\n");
		    return -1;
		  }
	    }



	    /*
	     *  Write Time Field 
             */
	    for (i=0;i<20;i++) tme[i] = 34574087.3 + 84893.2*i;
	    status = SWwritefield(SWid, "Time", NULL, NULL,
				  NULL, tme);
	    if (status == -1)
	      {
		printf("\t\tError: Cannot write field \"Time\"\n");
		return -1;
	      }
	    

	    /*
	     * Write Spectra one plane at a time 
	     * Value is 100 * track index + band index
	     */

	    start[1] = 0;
	    start[2] = 0;
	    count[0] = 1;
	    count[1] = 40;
	    count[2] = 20;
	    for (i = 0; i < 15; i++)
	    {
		start[0] = i;
		for (j=0; j<40; j++)
		    for (k= 0; k<20; k++)
			plane[j*20+k] = j*100 + i;
		
		status = SWwritefield(SWid, "Spectra", start, NULL,
				      count, plane);
		if (status == -1)
		  {
		    printf("\t\tError: Cannot write field \"Spectra\"\n");
		    return -1;
		  }
	    }


	    /* Write User Attribute */
	    status = SWwriteattr(SWid, "TestAttr", DFNT_INT32, 4, attr);
	    if (status == -1)
	      {
		printf("\t\tError: Cannot write attribute \"TestAttr\"\n");
		return -1;
	      }
	}
    }

    status = SWdetach(SWid);
    if (status == -1)
      {
	printf("\t\tError: Cannot detach SWid\n");
	return -1;
      }
    status = SWclose(swfid);
    if (status == -1)
      {
	printf("\t\tError: Cannot close hdf file.\n");
	return -1;
      }
    return 0;
}