File: tst_vlenstr.c

package info (click to toggle)
netcdf-parallel 1%3A4.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 113,164 kB
  • sloc: ansic: 267,893; sh: 12,869; cpp: 5,822; yacc: 2,613; makefile: 1,813; lex: 1,216; xml: 173; awk: 2
file content (166 lines) | stat: -rw-r--r-- 4,139 bytes parent folder | download | duplicates (2)
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "netcdf.h"

#undef DEBUG

#ifdef DEBUG
static char* buf = NULL;
#endif

#define totalNumStrings 13

static const char* charPointers[totalNumStrings] = 
	{"banana", "kiwi", "apple", "grape", "apple", "cherry", "lemon", "melon", "watermelon", "strawberry", "peach", "raspberry", "pineapple"};
    
#define first_size 2
#define second_size 1
#define third_size 5
#define fourth_size 3
#define fifth_size 0
#define sixth_size 2

#define attlength 6

void
checkErrorCode(int status, const char* message)
{
    if (status != NC_NOERR){
	fprintf(stderr,"Error code: %d from %s\n",status,message);
        fprintf(stderr,"\t%s\n\n",nc_strerror(status));
    }
}


void
writeVariable(int dimlength, int ncid, nc_type vlen_typeID)
{
    int retval = NC_NOERR;
    int dimid;
    int varid;
    
    /* Setup data */
    nc_vlen_t* data = calloc(sizeof(nc_vlen_t),dimlength);

    /* create six variable length arrays of strings */
    int stringIndex = 0;

    data[0].len = first_size;
    data[0].p = charPointers+stringIndex;
    stringIndex += first_size;
    
    data[1].len = second_size;
    data[1].p = charPointers+stringIndex;
    stringIndex += second_size;
    
    data[2].len = third_size;
    data[2].p = charPointers+stringIndex;
    stringIndex += third_size;
    
    data[3].len = fourth_size;
    data[3].p = charPointers+stringIndex;
    stringIndex += fourth_size;
    
    data[4].len = fifth_size;
    data[4].p = charPointers+stringIndex;
    stringIndex += fifth_size;
    
    data[5].len = sixth_size;
    data[5].p = charPointers+stringIndex;
    stringIndex += sixth_size;
    
    /* Write vlen variable named Fruits */
    
    /* Define dimension */
    retval = nc_def_dim(ncid, "D1", dimlength, &dimid);
    checkErrorCode(retval, "nc_def_dim");
    
    /* Define variable */
    retval = nc_def_var(ncid, "Fruits", vlen_typeID, 1, &dimid, &varid);
    checkErrorCode(retval, "nc_def_var");
    
    /* Write variable */
    retval = nc_put_var(ncid, varid, data);
    checkErrorCode(retval, "nc_put_var");
    
    free(data);
}

void
writeAttribute(int len, int ncid, nc_type vlen_typeID)
{
    int retval = NC_NOERR;
    
    /* Setup data */
    nc_vlen_t* data = calloc(sizeof(nc_vlen_t),len);

    /* create six variable length arrays of strings */
    int stringIndex = 0;

    data[0].len = first_size;
    data[0].p = charPointers+stringIndex;
    stringIndex += first_size;
    
    data[1].len = second_size;
    data[1].p = charPointers+stringIndex;
    stringIndex += second_size;
    
    data[2].len = third_size;
    data[2].p = charPointers+stringIndex;
    stringIndex += third_size;
    
    data[3].len = fourth_size;
    data[3].p = charPointers+stringIndex;
    stringIndex += fourth_size;
    
    data[4].len = fifth_size;
    data[4].p = charPointers+stringIndex;
    stringIndex += fifth_size;
    
    data[5].len = sixth_size;
    data[5].p = charPointers+stringIndex;
    stringIndex += sixth_size;
        
#ifdef DEBUG
    if(buf) {free(buf); buf = NULL;}
    nc_dump_data(ncid,vlen_typeID,data,len,&buf);
    fprintf(stderr,">>> attribute = %s\n",buf);
#endif

    /* Write vlen attribute named Fruits */
    retval = nc_put_att(ncid, NC_GLOBAL, "Fruit", vlen_typeID, len, data);
    checkErrorCode(retval, "nc_put_att");
    
    free(data);
}

int
main(int argc, const char * argv[])
{
    /* Open file */
    int ncid;
    int retval;
    nc_type vlen_typeID;
    
    retval = nc_create("tst_vlenstr.nc", NC_NETCDF4, &ncid);
    checkErrorCode(retval, "nc_create");
    
    /* Define vlen type named MY_VLEN_STRING */
    retval = nc_def_vlen(ncid, "MY_VLEN_STRING", NC_STRING, &vlen_typeID);
    checkErrorCode(retval, "nc_def_vlen");
    
    /* write variable - will be able to read it back fine */
    writeVariable(attlength, ncid, vlen_typeID);
    
    /* write attribute - will read back garbled values */
    writeAttribute(attlength, ncid, vlen_typeID);
    
    retval = nc_close(ncid);
    checkErrorCode(retval, "nc_close");
    
#ifdef DEBUG
    if(buf) free(buf);
#endif
    return retval;
}