File: tst_nofill3.c

package info (click to toggle)
netcdf-parallel 1%3A4.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 101,668 kB
  • sloc: ansic: 200,241; sh: 10,807; yacc: 2,522; makefile: 1,306; lex: 1,153; xml: 173; awk: 2
file content (113 lines) | stat: -rw-r--r-- 3,402 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
/*
  Copyright 2011, UCAR/Unidata
  See COPYRIGHT file for copying and redistribution conditions.

  This is part of netCDF.

  This program tests for a bug discovered with nofill mode that failed
  only on file systems with block size in a particular range.  This version
  of the test showed failure using a normal nc_create() call rather tha
  a special nc__create() call to change the blksize.
*/

#include <config.h>
#include <nc_tests.h>
#include "err_macros.h"
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <errno.h>
#include <netcdf.h>

#define FILE_NAME "tst_nofill3.nc"
#define TIME_LEN 1
/* Change any of these values and the bug doesn't happen or requires a
 * larger disk block size (set with the "chunksize" parameter to
 * nc__create()) to see. Similarly, adding attributes or additional
 * variables will either make the bug go away or require a larger disk
 * block size to observe.  But not just any larger disk block size; it
 * will have to be in a relatively narrow range. */
#define LON_LEN 11
#define LAT_LEN 11
#define LVL_LEN 34

int
create_file(char *file_name, int fill_mode)
{
   int ncid;
   int lon_dim, lat_dim, lvl_dim, time_dim;
   int time_id, zonal_wnd_id;
   int i;

   /* rank (number of dimensions) for each variable */
#  define RANK_time 1
#  define RANK_zonal_wnd 3

   /* variable shapes */
   int zonal_wnd_dims[RANK_zonal_wnd];
   size_t zonal_wnd_start[RANK_zonal_wnd];
   size_t zonal_wnd_count[RANK_zonal_wnd];
   float zonal_wnd[LON_LEN*LAT_LEN];
   int time[TIME_LEN];

   for(i = 0; i < TIME_LEN; i++) {
       time[i] = 1;
   }
   if (nc_create(file_name, NC_CLOBBER, &ncid)) ERR;
   if (nc_set_fill(ncid, fill_mode, NULL)) ERR;

   /* define dimensions */
   if (nc_def_dim(ncid, "lon", LON_LEN, &lon_dim)) ERR;
   if (nc_def_dim(ncid, "lat", LAT_LEN, &lat_dim)) ERR;
   if (nc_def_dim(ncid, "lvl", LVL_LEN, &lvl_dim)) ERR;
   if (nc_def_dim(ncid, "time", TIME_LEN, &time_dim)) ERR;

   /* define variables */
   if (nc_def_var(ncid, "time", NC_INT, RANK_time, &time_dim, &time_id)) ERR;

   zonal_wnd_dims[0] = lvl_dim;
   zonal_wnd_dims[1] = lat_dim;
   zonal_wnd_dims[2] = lon_dim;
   if (nc_def_var(ncid, "zonal_wnd", NC_FLOAT, RANK_zonal_wnd, zonal_wnd_dims, &zonal_wnd_id)) ERR;

   if (nc_enddef (ncid)) ERR;
   if (nc_put_var_int(ncid, time_id, time)) ERR;
   /* Bug exposed when last value written. */
   {
      int izw;

      i = LVL_LEN - 1;
      for(izw = 0; izw < LAT_LEN * LON_LEN; izw++) {
	 zonal_wnd[izw] = 100 + i;
      }
      zonal_wnd_start[0] = i;
      zonal_wnd_start[1] = 0;
      zonal_wnd_start[2] = 0;
      zonal_wnd_count[0] = 1;
      zonal_wnd_count[1] = LAT_LEN;
      zonal_wnd_count[2] = LON_LEN;
      if (nc_put_vara_float(ncid, zonal_wnd_id, zonal_wnd_start, zonal_wnd_count, zonal_wnd)) ERR;
   }
   if (nc_close(ncid)) ERR;
   return 0;
}

int
main(int argc, char **argv)
{
   printf("\n*** Testing nofill mode.\n");
   printf("*** Create file in nofill mode using nc_create()...");
   {
       int ncid, varid;
      int data[TIME_LEN];

      if (create_file(FILE_NAME, NC_NOFILL)) ERR;
      if (nc_open(FILE_NAME, 0, &ncid)) ERR;
      if (nc_inq_varid(ncid, "time", &varid)) ERR;
      if (nc_get_var(ncid, varid, data)) ERR;
      if (data[0] != 1) ERR; /* value overwritten by zero due to NOFILL bug */
      if (nc_close(ncid)) ERR;
   }
   SUMMARIZE_ERR;
   FINAL_RESULTS;
}