File: tst_many_writes.cpp

package info (click to toggle)
netcdf-cxx-legacy 4.2-13
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,684 kB
  • sloc: sh: 10,521; cpp: 3,134; makefile: 105
file content (122 lines) | stat: -rw-r--r-- 3,432 bytes parent folder | download | duplicates (10)
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
#include <config.h>
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <netcdf.h>

using namespace std;

#ifdef EXTRA_TESTS
#define MEGABYTE 1048576
void
get_mem_used2(int *mem_used)
{
   char buf[30];
   FILE *pf;
   size_t page_size = 4092; /* For spock... */
   unsigned size; /*       total program size */
   unsigned resident;/*   resident set size */
   unsigned share;/*      shared pages */
   unsigned text;/*       text (code) */
   unsigned lib;/*        library */
   unsigned data;/*       data/stack */
   /*unsigned dt;          dirty pages (unused in Linux 2.6)*/

   snprintf(buf, 30, "/proc/%u/statm", (unsigned)getpid());
   if ((pf = fopen(buf, "r"))) 
   {
      fscanf(pf, "%u %u %u %u %u %u", &size, &resident, &share, 
	     &text, &lib, &data);
      *mem_used = (data * page_size) / MEGABYTE;
   }
   else
      *mem_used = -1;
  fclose(pf);
}
#endif /* EXTRA_TESTS */

//Exception class
class NcErrorException : public exception
{
public:
   NcErrorException(const string& descr) throw(): exception(), _descr(descr)  {};
   ~NcErrorException() throw() {};
   
   const char* what() const throw() { ostringstream err; err << "NcErrorException: " << _descr;  return err.str().c_str();   };
   
   
private:
   string _descr;
};

void handle_error(int status) {
   if (status != NC_NOERR) {
      throw NcErrorException(nc_strerror(status));
   }
};

/******MAIN********/
int main(int argc, char** argv)
{
   int NUMVARS = 1;
   size_t NUMREC=10000;
   int fileId, dimId, varId[1];
   string filename("tst_many_writes.nc");
   
   cout << "\n*** Testing netCDF-4 file with user-provided test (thanks Marica!)\n";

   try{
      //create the netcdf-4 file
      handle_error(nc_create(filename.c_str(), NC_NETCDF4, &fileId));

      //define the unlimited dimension "rec"
      handle_error(nc_def_dim(fileId, "rec", NC_UNLIMITED, &dimId)); //--> Segmentation Fault
      //handle_error ( nc_def_dim(fileId, "rec", NUMREC, &dimId) );  //--> Good!!
      
      int dimids[1] = {dimId};
      
      //define NUMVARS variables named field_%i using a loop
      for (int v = 0; v < NUMVARS; v++)
      {
      	 size_t chunkSize[1] = {100000};
      	 ostringstream varName; varName << "field_" << v;
      	 handle_error(nc_def_var(fileId, varName.str().c_str(), NC_DOUBLE, 1, dimids , &varId[v]));
      	 handle_error(nc_def_var_chunking(fileId, varId[v], NC_CHUNKED, chunkSize));
      }
      handle_error (nc_enddef(fileId));
      
      //write data to the NUMVARS variables using nc_put_var1_double
      double data = 100;
      size_t start[1];
      size_t count[1] = {1};
      char charName[NC_MAX_NAME+1];
      
      for (int v = 0; v < NUMVARS; v++)
      {
      	 handle_error(nc_inq_varname(fileId, varId[v], charName));
	 cout << "var " << v << "\n";
      	 for (size_t start = 0; start < NUMREC; start++)
      	 {
#ifdef EXTRA_TESTS
	    if (start % 1000 == 0)
	    {
	       int mem_used;
	       get_mem_used2(&mem_used);
	       cout << mem_used << "\n";
	    }
#endif /* EXTRA_TESTS */
      	    handle_error(nc_put_vara_double(fileId, varId[v], &start, count, &data));
      	 }
      }

      //close file
      handle_error(nc_close(fileId));
      cout << "*** nctst SUCCESS!\n";      
   }
   catch(exception &ex) //exception handling
   {
      cerr << "Exception caught: " << ex.what() << endl;
      cout << "*** nctst FAILURE!\n";
      return -1;
   }
}