File: index.c

package info (click to toggle)
libgetdata 0.10.0-5%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 16,260 kB
  • sloc: ansic: 99,855; cpp: 4,820; fortran: 4,524; sh: 4,178; f90: 2,543; python: 2,378; perl: 1,893; php: 1,447; makefile: 1,418
file content (285 lines) | stat: -rw-r--r-- 8,035 bytes parent folder | download | duplicates (5)
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
/* Copyright (C) 2009-2013, 2015, 2016 D. V. Wiebe
 *
 ***************************************************************************
 *
 * This file is part of the GetData project.
 *
 * GetData is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 *
 * GetData is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with GetData; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
#include "internal.h"

static double _GD_Extrapolate(DIRFILE *D, gd_entry_t *E, int repr, double value,
    off64_t limit, int eof)
{
  off64_t n;
  double sample = NAN;
  double data[2];

  dtrace("%p, %p, %i, %g, %" PRId64 ", %i", D, E, repr, value, (int64_t)limit,
      eof);

  /* load data */
  n = _GD_DoField(D, E, repr, limit - eof, 2, GD_FLOAT64, data);

  if (D->error) {
    dreturn("%.15g", sample);
    return sample;
  } else if (n < 2) {
    _GD_SetError(D, GD_E_DOMAIN, GD_E_DOMAIN_EMPTY, NULL, 0, NULL);
    dreturn("%.15g", sample);
    return sample;
  }

  sample = limit + (value - data[eof]) / (data[1] - data[0]);
  dreturn("%.15g", sample);
  return sample;
}

/* find the (fractional) frame number based on a monotonic look-up */
static double _GD_GetIndex(DIRFILE* D, gd_entry_t *E, int repr, double value,
    off64_t field_start, off64_t field_end)
{
  double sample = NAN;
  int dir = -1; /* -1 = unknown; 0 = ascending; 1 = descending */
  off64_t low = field_start;
  off64_t high = field_end;
  off64_t c;
  double low_v, high_v, field_start_v, c_v;
  size_t n;

  dtrace("%p, %p, %i, %g, %" PRId64 ", %" PRId64, D, E, repr, value,
      (int64_t)field_start, (int64_t)field_end);

  /* find the end-points */
  n = _GD_DoField(D, E, repr, field_start, 1, GD_FLOAT64, &low_v);
  field_start_v = low_v;

  if (D->error) {
    dreturn("%.15g", sample);
    return sample;
  }

  if (n == 0) {
    _GD_SetError(D, GD_E_DOMAIN, GD_E_DOMAIN_EMPTY, NULL, 0, NULL);
    dreturn("%.15g", sample);
    return sample;
  }

  n = _GD_DoField(D, E, repr, field_end - 1, 1, GD_FLOAT64, &high_v);

  if (D->error) {
    dreturn("%.15g", sample);
    return sample;
  }

  if (n > 0) {
    if (high_v == low_v) {
      _GD_SetError(D, GD_E_RANGE, GD_E_SINGULAR_RANGE, NULL, 0, NULL);
      dreturn("%.15g", sample);
      return sample;
    }

    dir = (high_v < low_v) ? 1 : 0;

    /* extrapolate, if necessary */
    if ((!dir && low_v > value) || (dir && low_v < value)) {
      sample = _GD_Extrapolate(D, E, repr, value, low, 0);
      dreturn("%.15g", sample);
      return sample;
    } else if ((!dir && high_v < value) || (dir && high_v > value)) {
      sample = _GD_Extrapolate(D, E, repr, value, high - 1, 1);
      dreturn("%.15g", sample);
      return sample;
    }
  } else {
    /* binary search until either we find the end or we find a subdomain in
     * which our value lies */
    for (;;) {
      c = (high + low) / 2;
      n = _GD_DoField(D, E, repr, c, 1, GD_FLOAT64, &c_v);

      if (D->error) {
        dreturn("%.15g", sample);
        return sample;
      }

      if (n == 0) {
        if (c - low == 1) {
          if (low == field_start) {
            _GD_SetError(D, GD_E_DOMAIN, GD_E_DOMAIN_EMPTY, NULL, 0, NULL);
            dreturn("%.15g", sample);
            return sample;
          }

          /* low is the EOF -- so, extrapolate */
          sample = _GD_Extrapolate(D, E, repr, value, low, 1);
          dreturn("%.15g", sample);
          return sample;
        } else {
          /* haven't found the EOF yet */
          high = c;
        }
      } else {
        if (dir == -1) {
          if (c_v == low_v) {
            /* in this case, the range may not be singular, so use our guess
             * as the new lower limit and keep looking */
            low = c;
            continue;
          }

          dir = (c_v < field_start_v) ? 1 : 0;

          if ((!dir && field_start_v > value) ||
              (dir && field_start_v < value))
          {
            /* extrapolate BOF */
            sample = _GD_Extrapolate(D, E, repr, value, low, 0);
            dreturn("%.15g", sample);
            return sample;
          }
        }
        if ((!dir && c_v > value) || (dir && c_v < value)) {
          /* below our guess -- finding the end is no longer relevant */
          high = c;
          high_v = c_v;
          break;
        } else if ((!dir && c_v < value) || (dir && c_v > value)) {
          /* above our guess -- still need to look for the end */
          low = c;
          low_v = c_v;
        }
      }
    }
  }

  /* Step 2: binary search until we find the value */
  for (;high - low > 1;) {
    /* load data */
    c = (high + low) / 2;
    n = _GD_DoField(D, E, repr, c, 1, GD_FLOAT64, &c_v);

    if (D->error) {
      dreturn("%.15g", sample);
      return sample;
    }

    if (n == 0) {
      /* someone's been stealing our data.  How Rude! */
      _GD_SetError(D, GD_E_DOMAIN, GD_E_DOMAIN_EMPTY, NULL, 0, NULL);
      dreturn("%.15g", sample);
      return sample;
    }

    if ((!dir && c_v > value) || (dir && c_v < value)) {
      /* before our guess */
      high_v = c_v;
      high = c;
    } else if ((!dir && c_v < value) || (dir && c_v > value)) {
      /* after our guess */
      low_v = c_v;
      low = c;
    } else {
      /* our guess was unexpectedly correct */
      sample = (double)c;
      dreturn("%.15g", sample);
      return sample;
    }
  }

  sample = low + (value - low_v) / (high_v - low_v);
  dreturn("%.15g", sample);
  return sample;
}

double gd_framenum_subset64(DIRFILE* D, const char* field_code, double value,
    off64_t field_start, off64_t field_end)
{
  double frame = NAN;
  gd_entry_t* entry;
  int repr = GD_REPR_NONE;
  unsigned int spf;

  dtrace("%p, \"%s\", %g, %" PRId64 ", %" PRId64, D, field_code, value,
      (int64_t)field_start, (int64_t)field_end);

  GD_RETURN_IF_INVALID(D, "%.15g", frame);

  entry = _GD_FindFieldAndRepr(D, field_code, &repr, NULL, 1);

  if (D->error) {
    dreturn("%.15g", frame);
    return frame;
  }

  if (_GD_NativeType(D, entry, repr) & GD_COMPLEX)
    _GD_SetError(D, GD_E_DOMAIN, GD_E_DOMAIN_COMPLEX, NULL, 0, NULL);
  else if (entry->field_type & GD_SCALAR_ENTRY_BIT)
    _GD_SetError(D, GD_E_DIMENSION, GD_E_DIM_CALLER, NULL, 0, field_code);

  if (D->error) {
    dreturn("%.15g", frame);
    return frame;
  }

  spf = _GD_GetSPF(D, entry);
  if (field_start == 0)
    field_start = D->fragment[entry->fragment_index].frame_offset * spf;
  else
    field_start *= spf;

  if (field_end == 0)
    field_end = (gd_nframes64(D) + 1) * spf - 1;
  else
    field_end = (field_end + 1) * spf - 1;

  if (field_end - field_start < 2)
    _GD_SetError(D, GD_E_DOMAIN, GD_E_DOMAIN_EMPTY, NULL, 0, NULL);

  if (!D->error)
    frame = _GD_GetIndex(D, entry, repr, value, field_start, field_end) / spf;

  dreturn("%.15g", frame);
  return frame;
}

double gd_framenum_subset(DIRFILE* D, const char* field_code, double value,
    off_t field_start, off_t field_end)
{
  double frame;

  dtrace("%p, \"%s\", %g, %" PRId64 ", %" PRId64, D, field_code, value,
      (int64_t)field_start, (int64_t)field_end);

  frame = gd_framenum_subset64(D, field_code,  value, (off64_t)field_start,
      (off64_t)field_end);

  dreturn("%.15g", frame);
  return frame;
}

double gd_framenum(DIRFILE* D, const char* field_code, double value)
{
  double frame;

  dtrace("%p, \"%s\", %g", D, field_code, value);

  frame = gd_framenum_subset64(D, field_code,  value, 0, 0);

  dreturn("%.15g", frame);
  return frame;
}
/* vim: ts=2 sw=2 et tw=80
*/