File: test_cdf_write.c

package info (click to toggle)
cdo 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 49,836 kB
  • sloc: cpp: 185,271; ansic: 95,766; sh: 7,192; f90: 6,147; makefile: 1,977; ruby: 1,078; csh: 1,028; python: 995; fortran: 319; pascal: 219; perl: 9
file content (91 lines) | stat: -rw-r--r-- 2,057 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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <math.h>
#include <stdlib.h>
#include <stdio.h>

#include "cdi.h"

#include "dmemory.h"

#include "simple_model_helper.h"

static double my_gamma_dist(double x);

#define missValue (-50.0)

int
main(int argc, const char **argv)
{
  // todo: handle optional arguments here to increase test coverage
  const char *fname = (argc > 1) ? argv[1] : "test.nc";

  int streamID = streamOpenWrite(fname, CDI_FILETYPE_NC);
  if (streamID < 0)
  {
    fprintf(stderr, "Open failed on %s: %s\n", fname, cdiStringError(streamID));
    return EXIT_FAILURE;
  }

  enum
  {
    sizey = 40,
    sizex = 2 * sizey,
  };

  int gridID = createLocalCurvilinearGrid(sizex, sizey);
  int zaxisID = zaxisCreate(ZAXIS_SURFACE, 1);

  int vlistID = vlistCreate();
  int varID = vlistDefVar(vlistID, gridID, zaxisID, TIME_VARYING);
  vlistDefVarMissval(vlistID, varID, missValue);
  {
    static const char creatorText[] = "CDI test_cdf_write";
    cdiDefAttTxt(vlistID, varID, "CDI Text Attribute test, created by", sizeof(creatorText) - 1, creatorText);
  }

  int taxisID = taxisCreate(TAXIS_ABSOLUTE);
  vlistDefTaxis(vlistID, taxisID);

  streamDefVlist(streamID, vlistID);

  (void) streamDefTimestep(streamID, 0);

  {
    double(*data)[sizex] = (double(*)[sizex]) Malloc(sizeof(**data) * sizex * sizey);
    for (size_t j = 0; j < sizey; ++j)
      for (size_t i = 0; i < sizex; ++i) { data[j][i] = my_gamma_dist((double) i / (double) (sizex - 1)); }
    data[sizey / 3][sizex / 2] = missValue;
    streamWriteVar(streamID, 0, (const double *) data, 1);
    Free(data);
  }

  streamClose(streamID);

  return EXIT_SUCCESS;
}

static double
my_gamma_dist(double x)
{
  enum
  {
    k = 9,
  };
  const double theta = 0.5;
  x *= 20.0;
  double pdf_x = 1.0 / (tgamma((double) k) * pow(theta, k)) * pow(x, k - 1) * exp(-x / theta);
  return pdf_x;
}

/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */