File: dfilterx.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 (243 lines) | stat: -rw-r--r-- 6,531 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright 2018, University Corporation for Atmospheric Research
 * See netcdf/COPYRIGHT file for copying and redistribution conditions.
 */

#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef _MSC_VER
#include <io.h>
#endif

#include "netcdf.h"
#include "netcdf_filter.h"
#include "ncdispatch.h"
#include "nc4internal.h"

#include "ncjson.h"

/*
NCZarr filter API
*/

/**************************************************/
/* Per-variable filters */

/**
Find the set of filters (if any) associated with a variable.
Assumes NCZarr format using json

\param ncid NetCDF or group ID, from a previous call to nc_open(),
\param varid Variable ID
\param jsonp a JSON formatted string is returned in this argument

\returns ::NC_NOERR No error.
\returns ::NC_ENOTNC4 Not a netCDF-4 file.
\returns ::NC_EBADID Bad ncid
\returns ::NC_ENOTVAR Invalid variable ID.
\returns ::NC_EINVAL Invalid arguments
\ingroup variables
\author Dennis Heimbigner
*/
EXTERNL int
nc_inq_var_filterx_ids(int ncid, int varid, char** textp)
{
    NC* ncp;
    int stat = NC_NOERR;

    TRACE(nc_inq_var_filterx_ids);
    if((stat = NC_check_id(ncid,&ncp))) return stat;
    if((stat = ncp->dispatch->inq_var_filterx_ids(ncid,varid,textp))) goto done;

done:
    return stat;
}

/**
Find the the param info about filter (if any)
associated with a variable and with specified id.
Assumes HDF5 format using unsigned ints.

\param ncid NetCDF or group ID, from a previous call to nc_open(),
nc_create(), nc_def_grp(), or associated inquiry functions such as
nc_inq_ncid().

\param varid Variable ID
\param id The filter id of interest
\param nparamsp (Out) Storage which will get the number of parameters to the filter
\param params (Out) Storage which will get associated parameters.
Note: the caller must allocate and free.

\returns ::NC_NOERR No error.
\returns ::NC_ENOTNC4 Not a netCDF-4 file.
\returns ::NC_EBADID Bad ncid.
\returns ::NC_ENOTVAR Invalid variable ID.
\returns ::NC_ENOFILTER Specified filter not defined for this variable.
\ingroup variables
\author Dennis Heimbigner
*/
EXTERNL int
nc_inq_var_filterx_info(int ncid, int varid, const char* id, char** textp)
{
    NC* ncp;
    int stat = NC_check_id(ncid,&ncp);

    TRACE(nc_inq_var_filterx_info);
    if(stat != NC_NOERR) return stat;
    if((stat = ncp->dispatch->inq_var_filterx_info(ncid,varid,id,textp))) goto done;

done:
     return stat;
}

/**
   Define a new variable filter
   Assumes HDF5 format using unsigned ints.
   Only variables with chunked storage can use filters.

   @param ncid File and group ID.
   @param varid Variable ID.
   @param id Filter ID.
   @param nparams Number of filter parameters.
   @param parms Filter parameters.

   @return ::NC_NOERR No error.
   @return ::NC_EINVAL Variable must be chunked.
   @return ::NC_EBADID Bad ID.
   @author Dennis Heimbigner
*/

EXTERNL int
nc_def_var_filterx(int ncid, int varid, const char* json)
{
    NC* ncp;
    int stat = NC_check_id(ncid,&ncp);

    TRACE(nc_def_var_filterx);
    if(stat != NC_NOERR) return stat;
    if((stat = ncp->dispatch->def_var_filterx(ncid,varid,json))) goto done;

done:
    return stat;
}

/**
Find the first filter (if any) associated with a variable.
   
\param ncid NetCDF or group ID, from a previous call to nc_open(),
nc_create(), nc_def_grp(), or associated inquiry functions such as
nc_inq_ncid().

\param varid Variable ID

\param textp Storage which will get the filter info (id + parameters) in json format

This is redundant over the multi-filter API, so
it can be implemented in terms of those functions.

\returns ::NC_NOERR No error.
\returns ::NC_ENOTNC4 Not a netCDF-4 file.
\returns ::NC_EBADID Bad ncid.
\returns ::NC_ENOTVAR Invalid variable ID.

\ingroup variables
\author Dennis Heimbigner
*/
EXTERNL int
nc_inq_var_filterx(int ncid, int varid, char** textp)
{
    NC* ncp;
    int stat = NC_NOERR;
    char* text = NULL;
    NCjson* json = NULL;
    NCjson* jid = NULL;
    
    TRACE(nc_inq_var_filterx);
    if((stat = NC_check_id(ncid,&ncp))) goto done;

    /* Get the filters on this variable */
    if((stat = nc_inq_var_filterx_ids(ncid,varid,&text))) goto done;
    /* Parse it */
    if((stat = NCJparse(text,0,&json))) goto done;
    if(json->sort != NCJ_ARRAY)
        {stat = NC_EFILTER; goto done;}
    if(NCJlength(json) == 0 || NCJcontents(json) == NULL)
        {stat = NC_ENOFILTER; goto done;}    
    jid = NCJith(json,0);
    if(jid->sort == NCJ_DICT || jid->sort == NCJ_ARRAY)
        {stat = NC_EFILTER; goto done;}
    /* Get info about the first filter */
    if((stat = nc_inq_var_filterx_info(ncid,varid,NCJstring(jid),textp)))
        {stat = NC_ENOFILTER; goto done;}
 done:
    NCJreclaim(json);
    return stat;
}

/**************************************************/
/* Support direct user defined filters */

#ifdef ENABLE_CLIENTSIDE_FILTERS

/* Use void* to avoid having to include hdf.h*/
EXTERNL int
nc_filterx_client_register(unsigned int id, void* info)
{
    int stat = NC_NOERR;
#ifdef USE_HDF5
    NC_FILTERX_OBJ_HDF5 client;
    if(id == 0 ||info == NULL)
	return NC_EINVAL;
    memset(&client,0,sizeof(client));
    client.hdr.format = NC_FILTERX_FORMAT_HDF5;
    client.sort = NC_FILTERX_SORT_CLIENT;
    client.u.client.id = id;
    client.u.client.info = info;
    /* Note use of a global function, not part of the dispatch table */
    stat = nc4_global_filterx_action(NCFILTER_CLIENT_REG, id, &client);
#else
    stat = NC_ENOTBUILT;
#endif
    return stat;
}

EXTERNL int
nc_filterx_client_unregister(unsigned int id)
{
int stat = NC_NOERR;
#ifdef USE_HDF5
    stat = nc4_global_filterx_action(NCFILTER_CLIENT_UNREG, id, NULL);
#else
    stat = NC_ENOTBUILT;
#endif
    return stat;
}

/* Use void* to avoid having to include hdf.h*/
EXTERNL int
nc_filterx_client_inq(unsigned int id, void* infop)
{
int stat = NC_NOERR;
#ifdef USE_HDF5
    H5Z_class2_t* hct = (H5Z_class2_t*)infop;
    NC_FILTERX_OBJ_HDF5 client;
    if(id == 0 ||infop == NULL)
	return NC_EINVAL;
    memset(&client,0,sizeof(client));
    client.hdr.format = NC_FILTERX_FORMAT_HDF5;
    client.sort = NC_FILTERX_SORT_CLIENT;
    client.u.client.id = id;
    client.u.client.info = hct;
    /* Note use of a global function, not part of the dispatch table */
    stat = nc4_global_filterx_action(NCFILTER_CLIENT_INQ, id, &client);
    if(stat == NC_NOERR) {
	*hct = *(H5Z_class2_t*)client.u.client.info;
    }
#else
    stat = NC_ENOTBUILT;
#endif
    return stat;
}
#endif /*ENABLE_CLIENTSIDE_FILTERS*/