test_cdf_write.c 4.85 KB
Newer Older
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
#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

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

#include "cdi.h"

struct cart_coord {
  double lat, lon;
};

static double
my_gamma_dist(double x);

static void
compute_curvilinear(double *coords_,
                    const struct cart_coord a[4], size_t sizex, size_t sizey);

#define DEG2RAD(phi) ((M_PI / 180.0) * (phi))
#define RAD2DEG(phi) ((180./M_PI) * (phi))
#define missValue (-50.0)

int main(int argc, const char **argv)
{
  /* todo: handle optional arguments here to increase test coverage */
  const char *fname = "test.nc";
30
  if (argc > 1)
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
    fname = argv[1];
  int streamID = streamOpenWrite(fname, 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,
  };

  size_t datasize = (size_t)sizex * (size_t)sizey;
  int gridID = gridCreate(GRID_CURVILINEAR, (int)datasize);
  gridDefXsize(gridID, sizex);
  gridDefYsize(gridID, sizey);
  {
    /* anti-clockwise coordinates around Amazonia */
    static struct cart_coord region[4]
      = { { .lon = DEG2RAD(-85.0), .lat = DEG2RAD(-25.0) },
          { .lon = DEG2RAD(-44.0), .lat = DEG2RAD(-18.0) },
          { .lon = DEG2RAD(-50.0), .lat = DEG2RAD(7.0) },
          { .lon = DEG2RAD(-80.0), .lat = DEG2RAD(10.0) } };
    double (*gridCoords)[sizey][sizex]
      = (double (*)[sizey][sizex])
      malloc(sizeof (*gridCoords) * sizey * sizex * 2);
    if (gridCoords == NULL)
      {
        perror("grid coordinate memory allocation failed");
        return EXIT_FAILURE;
      }
    compute_curvilinear((double *)gridCoords, region, sizex, sizey);
    gridDefXvals(gridID, (double *)(gridCoords[1]));
    gridDefYvals(gridID, (double *)(gridCoords[0]));
    free(gridCoords);
  }

  int zaxisID = zaxisCreate(ZAXIS_SURFACE, 1);

  int vlistID = vlistCreate();
  int varID = vlistDefVar(vlistID, gridID, zaxisID, TSTEP_INSTANT);
  vlistDefVarMissval(vlistID, varID, missValue);
76
77
78
79
80
  {
    static const char creatorText[] = "CDI test_cdf_write";
    vlistDefAttTxt(vlistID, varID, "CDI Text Attribute test, created by",
                   sizeof (creatorText) - 1, creatorText);
  }
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

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

  streamDefVlist(streamID, vlistID);

  (void)streamDefTimestep(streamID, 0);

  {
    double (*data)[sizex] = malloc(sizeof (**data) * sizex * sizey);
    if (!data)
      {
        perror("data values memory allocation failed");
        return EXIT_FAILURE;
      }
    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 inline double
cart_distance(struct cart_coord p1, struct cart_coord p2)
{
  double d_lat = sin((p1.lat-p2.lat)/2.0),
    d_lon = sin((p1.lon - p2.lon)/2.0),
    d = 2.0 * asin(sqrt(d_lat * d_lat +
                        cos(p1.lat)*cos(p2.lat) * (d_lon * d_lon)));
  return d;
}

static inline struct cart_coord
intermediate_coord(struct cart_coord p1, struct cart_coord p2, double f)
{
  double d = cart_distance(p1, p2),
    sine_of_d = sin(d),
    A = sin((1 - f) * d) / sine_of_d,
    B = sin(f * d) / sine_of_d,
    x = A * cos(p1.lat) * cos(p1.lon) + B * cos(p2.lat) * cos(p2.lon),
    y = A * cos(p1.lat) * sin(p1.lon) + B * cos(p2.lat) * sin(p2.lon),
    z = A * sin(p1.lat) + B * sin(p2.lat);
  struct cart_coord ic = { .lat = atan2(z, sqrt(x * x + y * y)),
                           .lon = atan2(y, x) };
  return ic;
}

static void
compute_curvilinear(double *coords_,
                    const struct cart_coord a[4], size_t sizex, size_t sizey)
{
  double (*coords)[sizey][sizex] = (double (*)[sizey][sizex])coords_;
  for (size_t j = 0; j < sizey; ++j)
    {
      double g = (double)j / (double)(sizey - 1);
      /* compute start/end coordinates of great circle in x direction */
      struct cart_coord gc_left = intermediate_coord(a[0], a[3], g),
        gc_right = intermediate_coord(a[1], a[2], g);
      for (size_t i = 0; i < sizex; ++i)
        {
          double f = (double)i / (double)(sizex - 1);
          struct cart_coord pij = intermediate_coord(gc_left, gc_right, f);
          coords[0][j][i] = RAD2DEG(pij.lat);
          coords[1][j][i] = RAD2DEG(pij.lon);
        }
    }
}

static double
my_gamma_dist(double x)
{
  enum {
    k = 9,
  };
  const double theta = 0.5;
  x *= 20.0;
  double pdf_x = 1.0 / ( tgamma(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:
 */