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 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
|
/* Copyright 2003-2018, University Corporation for Atmospheric
* Research. See COPYRIGHT file for copying and redistribution
* conditions. */
/**
* @file
* @internal The netCDF-4 file functions.
*
* This file is part of netcdf-4, a netCDF-like interface for NCZ, or
* a ZARR backend for netCDF, depending on your point of view.
*
* @author Dennis Heimbigner, Ed Hartnett
*/
#include "zincludes.h"
#include "zfilter.h"
/* Forward */
static int NCZ_enddef(NC_FILE_INFO_T* h5);
static int ncz_sync_netcdf4_file(NC_FILE_INFO_T* file, int isclose);
/**
* @internal Put the file back in redef mode. This is done
* automatically for netcdf-4 files, if the user forgets.
*
* @param ncid File and group ID.
*
* @return ::NC_NOERR No error.
* @author Dennis Heimbigner, Ed Hartnett
*/
int
NCZ_redef(int ncid)
{
NC_FILE_INFO_T* zinfo = NULL;
int stat = NC_NOERR;
ZTRACE(0,"NCZ_redef(ncid)");
/* Find this file's metadata. */
if ((stat = nc4_find_grp_h5(ncid, NULL, &zinfo)))
goto done;
assert(zinfo);
/* If we're already in define mode, return an error. */
if (zinfo->flags & NC_INDEF)
{stat = NC_EINDEFINE; goto done;}
/* If the file is read-only, return an error. */
if (zinfo->no_write)
{stat = NC_EPERM; goto done;}
/* Set define mode. */
zinfo->flags |= NC_INDEF;
/* For nc_abort, we need to remember if we're in define mode as a
redef. */
zinfo->redef = NC_TRUE;
done:
return ZUNTRACE(stat);
}
/**
* @internal For netcdf-4 files, this just calls nc_enddef, ignoring
* the extra parameters.
*
* @param ncid File and group ID.
* @param h_minfree Ignored for netCDF-4 files.
* @param v_align Ignored for netCDF-4 files.
* @param v_minfree Ignored for netCDF-4 files.
* @param r_align Ignored for netCDF-4 files.
*
* @return ::NC_NOERR No error.
* @author Dennis Heimbigner, Ed Hartnett
*/
int
NCZ__enddef(int ncid, size_t h_minfree, size_t v_align,
size_t v_minfree, size_t r_align)
{
int stat = NC_NOERR;
NC_FILE_INFO_T* h5 = NULL;
NC_GRP_INFO_T* grp = NULL;
ZTRACE(0,"ncid=%d",ncid);
if ((stat = nc4_find_grp_h5(ncid, &grp, &h5)))
goto done;
stat = NCZ_enddef(h5);
done:
return ZUNTRACE(stat);
}
/**
* @internal Take the file out of define mode. This is called
* automatically for netcdf-4 files, if the user forgets.
*
* @param h5 File object
*
* @return ::NC_NOERR No error.
* @return ::NC_EBADID Bad ncid.
* @return ::NC_EBADGRPID Bad group ID.
* @author Dennis Heimbigner, Ed Hartnett
*/
static int
NCZ_enddef(NC_FILE_INFO_T* h5)
{
NC_VAR_INFO_T *var;
int i,j;
int stat = NC_NOERR;
ZTRACE(1,"h5=%s",h5->hdr.name);
/* When exiting define mode, process all variables */
for (i = 0; i < nclistlength(h5->allgroups); i++) {
NC_GRP_INFO_T* g = nclistget(h5->allgroups,i);
for (j = 0; j < ncindexsize(g->vars); j++) {
var = (NC_VAR_INFO_T *)ncindexith(g->vars, j);
assert(var);
var->written_to = NC_TRUE; /* mark it written */
var->created = 1;
#if 0
/* set the fill value and _FillValue attribute */
if((stat = NCZ_ensure_fill_value(var))) goto done; /* ensure var->fill_value is set */
assert(var->no_fill || var->fill_value != NULL);
/* rebuild the fill chunk */
if((stat = NCZ_adjust_var_cache(var))) goto done;
#ifdef ENABLE_NCZARR_FILTERS
/* Build the filter working parameters for any filters */
if((stat = NCZ_filter_setup(var))) goto done;
#endif
#endif /*0|1*/
}
}
if((stat = ncz_enddef_netcdf4_file(h5))) goto done;
done:
return ZUNTRACE(stat);
}
/**
* @internal Flushes all buffers associated with the file, after
* writing all changed metadata. This may only be called in data mode.
*
* @param ncid File and group ID.
*
* @return ::NC_NOERR No error.
* @return ::NC_EBADID Bad ncid.
* @return ::NC_EINDEFINE Classic model file is in define mode.
* @author Dennis Heimbigner, Ed Hartnett
*/
int
NCZ_sync(int ncid)
{
int stat = NC_NOERR;
NC_FILE_INFO_T* file = NULL;
ZTRACE(0,"ncid=%d",ncid);
LOG((2, "%s: ncid 0x%x", __func__, ncid));
if ((stat = nc4_find_grp_h5(ncid, NULL, &file)))
return stat;
assert(file);
/* If we're in define mode, we can't sync. */
if (file->flags & NC_INDEF)
{
if (file->cmode & NC_CLASSIC_MODEL)
return NC_EINDEFINE;
if ((stat = NCZ_enddef(file)))
return stat;
}
/* do not do this if file is writeonce */
stat = ncz_sync_netcdf4_file(file,!ZCLOSE);
return stat;
}
/**
* @internal From the netcdf-3 docs: The function nc_abort just closes
* the netCDF dataset, if not in define mode. If the dataset is being
* created and is still in define mode, the dataset is deleted. If
* define mode was entered by a call to nc_redef, the netCDF dataset
* is restored to its state before definition mode was entered and the
* dataset is closed.
*
* @param ncid File and group ID.
*
* @return ::NC_NOERR No error.
* @author Dennis Heimbigner, Ed Hartnett
*/
int
NCZ_abort(int ncid)
{
int stat = NC_NOERR;
NC_FILE_INFO_T* h5 = NULL;
ZTRACE(0,"ncid=%d",ncid);
LOG((2, "%s: ncid 0x%x", __func__, ncid));
/* Find metadata for this file. */
if ((stat = nc4_find_grp_h5(ncid, NULL, &h5)))
return stat;
assert(h5);
stat = ncz_closeorabort(h5, NULL, 1);
return ZUNTRACE(stat);
}
/**
* @internal Close the netcdf file, writing any changes first.
*
* @param ncid File and group ID.
* @param params any extra parameters in/out of close
*
* @return ::NC_NOERR No error.
* @author Dennis Heimbigner, Ed Hartnett
*/
int
NCZ_close(int ncid, void* params)
{
int stat = NC_NOERR;
NC_FILE_INFO_T* h5 = NULL;
ZTRACE(0,"ncid=%d",ncid);
LOG((1, "%s: ncid 0x%x", __func__, ncid));
/* Find metadata for this file. */
if ((stat = nc4_find_grp_h5(ncid, NULL, &h5)))
return stat;
assert(h5);
return ncz_closeorabort(h5, params, 0);
}
/**
* @internal From the netcdf-3 docs: The function nc_abort just closes
* the netCDF dataset, if not in define mode. If the dataset is being
* created and is still in define mode, the dataset is deleted. If
* define mode was entered by a call to nc_redef, the netCDF dataset
* is restored to its state before definition mode was entered and the
* dataset is closed.
*
* @param ncid File and group ID.
*
* @return ::NC_NOERR No error.
* @author Dennis Heimbigner, Ed Hartnett
*/
int
ncz_closeorabort(NC_FILE_INFO_T* h5, void* params, int abort)
{
int stat = NC_NOERR;
assert(h5);
NC_UNUSED(params);
ZTRACE(3,"file=%s abort=%d",h5->hdr.name,abort);
LOG((2, "%s: file: %p", __func__, h5));
/* If we're in define mode, but not redefing the file, delete it. */
if(!abort) {
/* Invoke enddef if needed, which includes sync first */
if(h5->flags & NC_INDEF) h5->flags ^= NC_INDEF;
/* Sync the file unless this is a read-only file. */
if(!h5->no_write) {
if((stat = ncz_sync_netcdf4_file(h5,ZCLOSE)))
goto done;
}
}
/* Reclaim memory */
/* Free any zarr-related data, including the map */
if ((stat = ncz_close_file(h5, abort)))
goto done;
/* Reclaim provenance info */
NCZ_clear_provenance(&h5->provenance);
/* Free the NC_FILE_INFO_T struct. */
if ((stat = nc4_nc4f_list_del(h5)))
return stat;
done:
return ZUNTRACE(stat);
}
/**************************************************/
/**
* @internal Learn number of dimensions, variables, global attributes,
* and the ID of the first unlimited dimension (if any).
*
* @note It's possible for any of these pointers to be NULL, in which
* case don't try to figure out that value.
*
* @param ncid File and group ID.
* @param ndimsp Pointer that gets number of dimensions.
* @param nvarsp Pointer that gets number of variables.
* @param nattsp Pointer that gets number of global attributes.
* @param unlimdimidp Pointer that gets first unlimited dimension ID,
* or -1 if there are no unlimied dimensions.
*
* @return ::NC_NOERR No error.
* @author Dennis Heimbigner, Ed Hartnett
*/
int
NCZ_inq(int ncid, int *ndimsp, int *nvarsp, int *nattsp, int *unlimdimidp)
{
NC *nc;
NC_FILE_INFO_T* file;
NC_GRP_INFO_T *grp;
int stat = NC_NOERR;
int i;
LOG((2, "%s: ncid 0x%x", __func__, ncid));
/* Find file metadata. */
if ((stat = nc4_find_nc_grp_h5(ncid, &nc, &grp, &file)))
return stat;
assert(file && grp && nc);
/* Count the number of dims, vars, and global atts; need to iterate
* because of possible nulls. */
if (ndimsp)
{
*ndimsp = ncindexcount(grp->dim);
}
if (nvarsp)
{
*nvarsp = ncindexcount(grp->vars);
}
if (nattsp)
{
/* Do we need to read the atts? */
if (!grp->atts_read)
if ((stat = ncz_read_atts(file,(NC_OBJ*)grp)))
return stat;
*nattsp = ncindexcount(grp->att);
}
if (unlimdimidp)
{
/* Default, no unlimited dimension */
*unlimdimidp = -1;
/* If there's more than one unlimited dim, which was not possible
with netcdf-3, then only the last unlimited one will be reported
back in xtendimp. */
/* Note that this code is inconsistent with nc_inq_unlimid() */
for(i=0;i<ncindexsize(grp->dim);i++) {
NC_DIM_INFO_T* d = (NC_DIM_INFO_T*)ncindexith(grp->dim,i);
if(d == NULL) continue;
if(d->unlimited) {
*unlimdimidp = d->hdr.id;
break;
}
}
}
return NC_NOERR;
}
/**
* @internal This function will write all changed metadata and flush
* ZARR file to disk.
*
* @param file Pointer to file info struct.
*
* @return ::NC_NOERR No error.
* @return ::NC_EINDEFINE Classic model file in define mode.
* @return ::NC_EHDFERR ZARR error.
* @author Dennis Heimbigner, Ed Hartnett
*/
static int
ncz_sync_netcdf4_file(NC_FILE_INFO_T* file, int isclose)
{
int stat = NC_NOERR;
assert(file && file->format_file_info);
LOG((3, "%s", __func__));
ZTRACE(2,"file=%s",file->hdr.name);
/* If we're in define mode, that's an error, for strict nc3 rules,
* otherwise, end define mode. */
if (file->flags & NC_INDEF)
{
if (file->cmode & NC_CLASSIC_MODEL)
return NC_EINDEFINE;
/* Turn define mode off. */
file->flags ^= NC_INDEF;
/* Redef mode needs to be tracked separately for nc_abort. */
file->redef = NC_FALSE;
}
#ifdef LOGGING
/* This will print out the names, types, lens, etc of the vars and
atts in the file, if the logging level is 2 or greater. */
log_metadata_nc(file);
#endif
/* Write any metadata that has changed. */
if (!file->no_write)
{
/* Write out provenance; will create _NCProperties */
if((stat = NCZ_write_provenance(file)))
goto done;
/* Write all the metadata. */
if((stat = ncz_sync_file(file,isclose)))
goto done;
}
done:
return ZUNTRACE(stat);
}
/**
* @internal This function will do the enddef stuff for an nczarr file.
*
* @param file Pointer to ZARR file info struct.
*
* @return ::NC_NOERR No error.
* @return ::NC_ENOTINDEFINE Not in define mode.
* @author Dennis Heimbigner, Ed Hartnett
*/
int
ncz_enddef_netcdf4_file(NC_FILE_INFO_T* file)
{
assert(file);
LOG((3, "%s", __func__));
/* If we're not in define mode, return an error. */
if (!(file->flags & NC_INDEF))
return NC_ENOTINDEFINE;
/* Turn define mode off. */
file->flags ^= NC_INDEF;
/* Redef mode needs to be tracked separately for nc_abort. */
file->redef = NC_FALSE;
return ncz_sync_netcdf4_file(file,!ZCLOSE);
}
/**
* @internal IN netcdf, you first create
* the variable and then (optionally) specify the fill value.
*
* @param ncid File and group ID.
* @param fillmode File mode.
* @param old_modep Pointer that gets old mode. Ignored if NULL.
*
* @return ::NC_NOERR No error.
* @author Dennis Heimbigner
*/
int
NCZ_set_fill(int ncid, int fillmode, int *old_modep)
{
NC_FILE_INFO_T* h5 = NULL;
int stat = NC_NOERR;
ZTRACE(0,"NCZ_set_fill(ncid,fillmode,old)");
/* Get pointer to file info. */
if ((stat = nc4_find_grp_h5(ncid, NULL, &h5)))
goto done;
assert(h5);
/* Trying to set fill on a read-only file? You sicken me! */
if (h5->no_write)
{stat = NC_EPERM; goto done;}
/* Did you pass me some weird fillmode? */
if (fillmode != NC_FILL && fillmode != NC_NOFILL)
{stat = NC_EINVAL; goto done;}
/* If the user wants to know, tell him what the old mode was. */
if (old_modep)
*old_modep = h5->fill_mode;
h5->fill_mode = fillmode;
done:
return ZUNTRACE(stat);
}
|