Changeset: 5a8ae035ff05 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=5a8ae035ff05
Modified Files:
sql/backends/monet5/vaults/netcdf/netcdf.c
sql/scripts/74_netcdf.sql
Branch: SciQL-2-NetCDF
Log Message:
Added definition of sql procedures for NetCDF operations. Code identation.
diffs (truncated from 513 to 300 lines):
diff --git a/sql/backends/monet5/vaults/netcdf/netcdf.c
b/sql/backends/monet5/vaults/netcdf/netcdf.c
--- a/sql/backends/monet5/vaults/netcdf/netcdf.c
+++ b/sql/backends/monet5/vaults/netcdf/netcdf.c
@@ -26,25 +26,26 @@
#include "array.h"
#include "netcdf_vault.h"
+/* SQL statements for population of NetCDF catalog */
#define INSFILE \
"INSERT INTO netcdf_files(file_id,location) \
VALUES(%d, '%s');"
#define INSDIM \
- "INSERT INTO netcdf_dims(dim_id,file_id,name,length) \
-VALUES(%d, %d, '%s', %d);"
+ "INSERT INTO netcdf_dims(dim_id,file_id,name,length) \
+ VALUES(%d, %d, '%s', %d);"
#define INSVAR \
- "INSERT INTO
netcdf_vars(var_id,file_id,name,vartype,ndim,coord_dim_id) \
-VALUES(%d, %d, '%s', '%s', %d, %d);"
+ "INSERT INTO netcdf_vars(var_id,file_id,name,vartype,ndim,coord_dim_id) \
+ VALUES(%d, %d, '%s', '%s', %d, %d);"
#define INSVARDIM \
- "INSERT INTO netcdf_vardim(var_id,dim_id,file_id,dimpos) \
-VALUES(%d, %d, %d, %d);"
+ "INSERT INTO netcdf_vardim(var_id,dim_id,file_id,dimpos) \
+ VALUES(%d, %d, %d, %d);"
#define INSATTR \
- "INSERT INTO
netcdf_attrs(obj_name,att_name,att_type,value,file_id,gr_name) \
-VALUES('%s', '%s', '%s', '%s', %d, '%s');"
+ "INSERT INTO
netcdf_attrs(obj_name,att_name,att_type,value,file_id,gr_name) \
+ VALUES('%s', '%s', '%s', '%s', %d, '%s');"
#define LOAD_NCDF_VAR(tpe,ncdftpe) \
{ \
@@ -60,30 +61,31 @@ VALUES('%s', '%s', '%s', '%s', %d, '%s')
varid, nc_strerror(retval));
\
}
+/* simple test for netcdf library */
str
NCDFtest(int *vars, str *fname)
{
- int ncid; /* dataset id */
- int dims, ngatts, unlimdim;
- int retval;
+ int ncid; /* dataset id */
+ int dims, ngatts, unlimdim;
+ int retval;
- str msg = MAL_SUCCEED;
+ str msg = MAL_SUCCEED;
/* Open NetCDF file */
- if ((retval = nc_open(*fname, NC_NOWRITE, &ncid)))
- return createException(MAL, "netcdf.test", "Cannot open NetCDF file %s:
%s", *fname, nc_strerror(retval));
+ if ((retval = nc_open(*fname, NC_NOWRITE, &ncid)))
+ return createException(MAL, "netcdf.test", "Cannot open NetCDF file
%s: %s", *fname, nc_strerror(retval));
- if ((retval = nc_inq(ncid, &dims, vars, &ngatts, &unlimdim)))
- return createException(MAL, "netcdf.test", "Cannot read NetCDF header:
%s", nc_strerror(retval));
+ if ((retval = nc_inq(ncid, &dims, vars, &ngatts, &unlimdim)))
+ return createException(MAL, "netcdf.test", "Cannot read NetCDF
header: %s", nc_strerror(retval));
- if ((retval = nc_close(ncid)))
+ if ((retval = nc_close(ncid)))
return createException(MAL, "netcdf.test", "Cannot close file %s: \
%s", *fname, nc_strerror(retval));
- return msg;
+ return msg;
}
-/* use function from ncdump utility: NetCDF type number to name */
+/* the following function is from ncdump utility: NetCDF type number to name */
static const char *
prim_type_name(nc_type type)
{
@@ -157,7 +159,7 @@ NCDF2SQL(nc_type type)
}
}
-
+/* create and populate a dimension bat */
static str
NCDFARRAYseries(bat *bid, bte start, bte step, int stop, int group, int series)
{
@@ -204,11 +206,11 @@ NCDFattach(Client cntxt, MalBlkPtr mb, M
msg = getSQLContext(cntxt, mb, &m, NULL);
if (msg)
- return msg;
+ return msg;
sch = mvc_bind_schema(m, "sys");
if ( !sch )
- return createException(MAL, "netcdf.attach", "Cannot get schema
sys\n");
+ return createException(MAL, "netcdf.attach", "Cannot get schema
sys\n");
tfiles = mvc_bind_table(m, sch, "netcdf_files");
tdims = mvc_bind_table(m, sch, "netcdf_dims");
@@ -218,23 +220,21 @@ NCDFattach(Client cntxt, MalBlkPtr mb, M
if (tfiles == NULL || tdims == NULL || tvars == NULL ||
tvardim == NULL || tattrs == NULL)
- return createException(MAL, "netcdf.attach", "Catalog table mi\
-ssing\n");
+ return createException(MAL, "netcdf.attach", "Catalog table
missing\n");
/* check if the file is already attached */
col = mvc_bind_column(m, tfiles, "location");
rid = table_funcs.column_find_row(m->session->tr, col, fname, NULL);
if (rid != oid_nil)
- return createException(SQL, "netcdf.attach", "File %s is already a\
-ttached\n", fname);
+ return createException(SQL, "netcdf.attach", "File %s is already
attached\n", fname);
/* Open NetCDF file */
if ((retval = nc_open(fname, NC_NOWRITE, &ncid)))
- return createException(MAL, "netcdf.test", "Cannot open NetCDF \
+ return createException(MAL, "netcdf.test", "Cannot open NetCDF \
file %s: %s", fname, nc_strerror(retval));
if ((retval = nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdim)))
- return createException(MAL, "netcdf.test", "Cannot read NetCDF \
+ return createException(MAL, "netcdf.test", "Cannot read NetCDF \
header: %s", nc_strerror(retval));
/* Insert row into netcdf_files table */
@@ -243,191 +243,191 @@ header: %s", nc_strerror(retval));
snprintf(buf, BUFSIZ, INSFILE, (int)fid, fname);
if ( ( msg = SQLstatementIntern(cntxt,&s,"netcdf.attach",TRUE,FALSE))
- != MAL_SUCCEED )
- goto finish;
+ != MAL_SUCCEED )
+ goto finish;
/* Read dimensions from NetCDF header and insert a row for each one
into netcdf_dims table */
dims = (char **)GDKzalloc(sizeof(char *) * ndims);
for (didx = 0; didx < ndims; didx++){
- if ((retval = nc_inq_dim(ncid, didx, dname, &dlen)))
- return createException(MAL, "netcdf.attach", "Cannot read dimension
%d : %s", didx, nc_strerror(retval));
+ if ((retval = nc_inq_dim(ncid, didx, dname, &dlen)))
+ return createException(MAL, "netcdf.attach", "Cannot read
dimension %d : %s", didx, nc_strerror(retval));
- snprintf(buf, BUFSIZ, INSDIM, didx, (int)fid, dname, (int)dlen);
- if ( ( msg = SQLstatementIntern(cntxt,&s,"netcdf.attach",TRUE,FALSE))
- != MAL_SUCCEED )
- goto finish;
+ snprintf(buf, BUFSIZ, INSDIM, didx, (int)fid, dname, (int)dlen);
+ if ( ( msg =
SQLstatementIntern(cntxt,&s,"netcdf.attach",TRUE,FALSE))
+ != MAL_SUCCEED )
+ goto finish;
- dims[didx] = GDKstrdup(dname);
-
+ dims[didx] = GDKstrdup(dname);
}
/* Read variables and attributes from the header and insert rows in
netcdf_vars, netcdf_vardims, and netcdf_attrs tables */
for (vidx = 0; vidx < nvars; vidx++){
- if ( (retval = nc_inq_var(ncid, vidx, vname, &vtype, &vndims, vdims,
&vnatts)))
- return createException(MAL, "netcdf.attach",
+ if ( (retval = nc_inq_var(ncid, vidx, vname, &vtype, &vndims,
vdims, &vnatts)))
+ return createException(MAL, "netcdf.attach",
"Cannot read variable %d : %s",
vidx, nc_strerror(retval));
- /* Check if this is coordinate variable */
- if ( (vndims == 1) && ( strcmp(vname, dims[vdims[0]]) == 0 ))
- coord_dim_id = vdims[0];
- else coord_dim_id = -1;
+ /* Check if this is coordinate variable */
+ if ( (vndims == 1) && ( strcmp(vname, dims[vdims[0]]) == 0 ))
+ coord_dim_id = vdims[0];
+ else coord_dim_id = -1;
- snprintf(buf, BUFSIZ, INSVAR, vidx, (int)fid, vname,
prim_type_name(vtype), vndims, coord_dim_id);
- if ( ( msg = SQLstatementIntern(cntxt,&s,"netcdf.attach",TRUE,FALSE))
- != MAL_SUCCEED )
- goto finish;
+ snprintf(buf, BUFSIZ, INSVAR, vidx, (int)fid, vname,
prim_type_name(vtype), vndims, coord_dim_id);
+ if ( ( msg =
SQLstatementIntern(cntxt,&s,"netcdf.attach",TRUE,FALSE))
+ != MAL_SUCCEED )
+ goto finish;
- if ( coord_dim_id < 0 ){
- for (i = 0; i < vndims; i++){
- snprintf(buf, BUFSIZ, INSVARDIM, vidx, vdims[i], (int)fid, i);
- if ( ( msg = SQLstatementIntern(cntxt,&s,"netcdf.attach",TRUE,FALSE))
- != MAL_SUCCEED )
- goto finish;
+ if ( coord_dim_id < 0 ){
+ for (i = 0; i < vndims; i++){
+ snprintf(buf, BUFSIZ, INSVARDIM, vidx, vdims[i], (int)fid, i);
+ if ( ( msg =
SQLstatementIntern(cntxt,&s,"netcdf.attach",TRUE,FALSE))
+ != MAL_SUCCEED )
+ goto finish;
+ }
}
- }
- if ( vnatts > 0 ) { /* fill in netcdf_attrs table */
+ if ( vnatts > 0 ) { /* fill in netcdf_attrs table */
- for (aidx = 0; aidx < vnatts; aidx++){
- if ((retval = nc_inq_attname(ncid,vidx,aidx,aname)))
- return createException(MAL, "netcdf.attach",
- "Cannot read attribute %d of variable %d: %s",
- aidx, vidx, nc_strerror(retval));
+ for (aidx = 0; aidx < vnatts; aidx++){
+ if ((retval = nc_inq_attname(ncid,vidx,aidx,aname)))
+ return createException(MAL, "netcdf.attach",
+ "Cannot read attribute %d of variable %d: %s",
+ aidx, vidx, nc_strerror(retval));
- if ((retval = nc_inq_att(ncid,vidx,aname,&atype,&alen)))
- return createException(MAL, "netcdf.attach",
- "Cannot read attribute %s type and
length: %s",
- aname, nc_strerror(retval));
+ if ((retval = nc_inq_att(ncid,vidx,aname,&atype,&alen)))
+ return createException(MAL, "netcdf.attach",
+ "Cannot read attribute %s type and length: %s",
+ aname, nc_strerror(retval));
- switch ( atype ) {
- case NC_CHAR:
- aval = (char *) GDKzalloc(alen + 1);
- if ((retval = nc_get_att_text(ncid,vidx,aname,aval)))
- return createException(MAL, "netcdf.attach",
- "Cannot read attribute %s value: %s",
- aname, nc_strerror(retval));
- aval[alen] = '\0';
- snprintf(buf, BUFSIZ, INSATTR, vname, aname, "string", aval,
(int)fid, "root");
- GDKfree(aval);
- break;
+ switch ( atype ) {
+ case NC_CHAR:
+ aval = (char *) GDKzalloc(alen + 1);
+ if ((retval = nc_get_att_text(ncid,vidx,aname,aval)))
+ return createException(MAL, "netcdf.attach",
+ "Cannot read attribute %s value: %s",
+ aname, nc_strerror(retval));
+ aval[alen] = '\0';
+ snprintf(buf, BUFSIZ, INSATTR, vname, aname, "string", aval,
(int)fid, "root");
+ GDKfree(aval);
+ break;
- case NC_INT:
- if ((retval = nc_get_att_int(ncid,vidx,aname,&avalint)))
- return createException(MAL, "netcdf.attach",
- "Cannot read attribute %s value: %s",
- aname, nc_strerror(retval));
- snprintf(abuf,80,"%d",avalint);
- snprintf(buf, BUFSIZ, INSATTR, vname, aname, prim_type_name(atype),
abuf, (int)fid, "root");
+ case NC_INT:
+ if ((retval = nc_get_att_int(ncid,vidx,aname,&avalint)))
+ return createException(MAL, "netcdf.attach",
+ "Cannot read attribute %s value: %s",
+ aname, nc_strerror(retval));
+ snprintf(abuf,80,"%d",avalint);
+ snprintf(buf, BUFSIZ, INSATTR, vname, aname,
prim_type_name(atype), abuf, (int)fid, "root");
break;
- case NC_FLOAT:
- if ((retval = nc_get_att_float(ncid,vidx,aname,&avalfl)))
- return createException(MAL, "netcdf.attach",
- "Cannot read attribute %s value: %s",
- aname, nc_strerror(retval));
- snprintf(abuf,80,"%7.2f",avalfl);
- snprintf(buf, BUFSIZ, INSATTR, vname, aname, prim_type_name(atype),
abuf, (int)fid, "root");
- break;
-
- case NC_DOUBLE:
- if ((retval = nc_get_att_double(ncid,vidx,aname,&avaldbl)))
- return createException(MAL, "netcdf.attach",
- "Cannot read attribute %s value: %s",
- aname, nc_strerror(retval));
- snprintf(abuf,80,"%7.2e",avaldbl);
- snprintf(buf, BUFSIZ, INSATTR, vname, aname, prim_type_name(atype),
abuf, (int)fid, "root");
+ case NC_FLOAT:
+ if ((retval = nc_get_att_float(ncid,vidx,aname,&avalfl)))
+ return createException(MAL, "netcdf.attach",
+ "Cannot read attribute %s value: %s",
+ aname, nc_strerror(retval));
+ snprintf(abuf,80,"%7.2f",avalfl);
+ snprintf(buf, BUFSIZ, INSATTR, vname, aname,
prim_type_name(atype), abuf, (int)fid, "root");
break;
- default: continue; /* next attribute */
- }
+ case NC_DOUBLE:
+ if ((retval = nc_get_att_double(ncid,vidx,aname,&avaldbl)))
+ return createException(MAL, "netcdf.attach",
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list