File: interpolate.c

package info (click to toggle)
gnuastro 0.23-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 42,824 kB
  • sloc: ansic: 176,016; sh: 14,784; makefile: 1,298; cpp: 9
file content (800 lines) | stat: -rw-r--r-- 26,174 bytes parent folder | download
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
/*********************************************************************
Interpolate - Fill blank values in a dataset
This is part of GNU Astronomy Utilities (Gnuastro) package.

Original author:
     Mohammad Akhlaghi <mohammad@akhlaghi.org>
Contributing author(s):
Copyright (C) 2017-2024 Free Software Foundation, Inc.

Gnuastro is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.

Gnuastro 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
General Public License for more details.

You should have received a copy of the GNU General Public License
along with Gnuastro. If not, see <http://www.gnu.org/licenses/>.
**********************************************************************/
#include <config.h>

#include <stdio.h>
#include <errno.h>
#include <error.h>
#include <stdlib.h>
#include <string.h>

#include <gnuastro/list.h>
#include <gnuastro/fits.h>
#include <gnuastro/blank.h>
#include <gnuastro/pointer.h>
#include <gnuastro/threads.h>
#include <gnuastro/dimension.h>
#include <gnuastro/statistics.h>
#include <gnuastro/interpolate.h>
#include <gnuastro/permutation.h>

#include <gnuastro-internal/checkset.h>




/*********************************************************************/
/********************      Nearest neighbor       ********************/
/***************         (Dimension agnostic)         ****************/
/*********************************************************************/
/* These are bit-flags, so we're using hexadecimal notation. */
#define INTERPOLATE_FLAGS_NO          0
#define INTERPOLATE_FLAGS_NGB_CHECKED 0x1
#define INTERPOLATE_FLAGS_BLANK       0x2





/* Parameters for interpolation on threads. */
struct interpolate_ngb_params
{
  int                         function;
  gal_data_t                    *input;
  size_t                           num;
  gal_data_t                      *out;
  gal_data_t                   *blanks;
  size_t                  numneighbors;
  uint8_t                *thread_flags;
  int                        onlyblank;
  gal_list_void_t            *ngb_vals;
  float (*metric)(size_t *, size_t *, size_t );

  struct gal_tile_two_layer_params *tl;
};





/* Run the interpolation on many threads. */
static void *
interpolate_neighbors_on_thread(void *in_prm)
{
  /* Low-level variables that others depend on. */
  struct gal_threads_params *tprm=(struct gal_threads_params *)in_prm;
  struct interpolate_ngb_params *prm=
    (struct interpolate_ngb_params *)(tprm->params);

  /* Higher-level variables. */
  struct gal_tile_two_layer_params *tl=prm->tl;
  int correct_index=(tl && tl->totchannels>1 && !tl->workoverch);
  gal_data_t *input=prm->input;

  /* Rest of variables. */
  void *nv;
  float dist, pdist;
  uint8_t *b, *bf, *bb;
  gal_list_void_t *tvll;
  size_t ngb_counter, pind;
  gal_list_dosizet_t *lQ, *sQ;
  size_t i, index, fullind, chstart=0, ndim=input->ndim;
  gal_data_t *tin, *tout, *tnear, *value=NULL, *nearest=NULL;
  size_t size = (correct_index ? tl->tottilesinch : input->size);
  size_t *dsize = (correct_index ? tl->numtilesinch : input->dsize);
  size_t *icoord=gal_pointer_allocate(GAL_TYPE_SIZE_T, ndim, 0, __func__,
                                      "icoord");
  size_t *ncoord=gal_pointer_allocate(GAL_TYPE_SIZE_T, ndim, 0, __func__,
                                      "ncoord");
  uint8_t *flag, *fullflag=&prm->thread_flags[tprm->id*input->size];

  /* Based on the above. */
  size_t *dinc=gal_dimension_increment(ndim, dsize);


  /* Initialize the flags array. We need two flags during this processing:
     1) to see if there are blanks. 2) to see if a neighbor has been
     checked. These are both binary (0 or 1). So to avoid wasting space, we
     will use bits to store them. We start with only setting the blank flag
     once for the whole thread. Then for each interpolated pixel, we reset
     the neighbor-check flag. */
  flag=fullflag;
  bb=prm->blanks->array;
  bf=(b=fullflag)+input->size;
  do *b = *bb++ ? INTERPOLATE_FLAGS_BLANK : 0; while(++b<bf);


  /* Put the allocated space to keep the neighbor values into a structure
     for easy processing. */
  tin=input;
  for(tvll=prm->ngb_vals; tvll!=NULL; tvll=tvll->next)
    {
      nv=gal_pointer_increment(tvll->v, tprm->id*prm->numneighbors,
                               input->type);
      gal_list_data_add_alloc(&nearest, nv, tin->type, 1,
                              &prm->numneighbors, NULL, 0, -1, 1,
                              NULL, NULL, NULL);
      tin=tin->next;
    }
  gal_list_data_reverse(&nearest);


  /* Go over all the points given to this thread. */
  for(i=0; tprm->indexs[i] != GAL_BLANK_SIZE_T; ++i)
    {
      /* For easy reading. */
      fullind=tprm->indexs[i];


      /* If the caller only wanted to interpolate over blank values and
         this value is not blank (we know from the flags), then just set
         the output value at this element to the input value and go to the
         next element. */
      if(prm->onlyblank && !(fullflag[fullind] & INTERPOLATE_FLAGS_BLANK) )
        {
          tin=input;
          for(tout=prm->out; tout!=NULL; tout=tout->next)
            {
              memcpy(gal_pointer_increment(tout->array, fullind, tin->type),
                     gal_pointer_increment(tin->array,  fullind, tin->type),
                     gal_type_sizeof(tin->type));
              tin=tin->next;
            }
          continue;
        }


      /* Correct the index (if necessary). When the values come from a
         tiled dataset, the caller might want to interpolate the values of
         each channel separately (not mix values from different
         channels). In such a case, the tiles of each channel (and their
         values in 'input' are contiguous. So we need to correct
         'tprm->indexs[i]' (which is the index over the whole tessellation,
         including all channels). */
      if(correct_index)
        {
          /* Index of this tile in its channel. */
          index = fullind % tl->tottilesinch;

          /* Index of the first tile in this channel. */
          chstart = (fullind / tl->tottilesinch) * tl->tottilesinch;

          /* Set the channel's starting pointer for the flags. */
          flag = gal_pointer_increment(fullflag, chstart, GAL_TYPE_UINT8);
        }
      else
        {
          chstart=0;
          index=fullind;
        }


      /* Reset all checked bits in the flags array to 0. */
      ngb_counter=0;
      bf=(b=flag)+size;
      do *b &= ~(INTERPOLATE_FLAGS_NGB_CHECKED); while(++b<bf);


      /* Get the coordinates of this pixel (to be interpolated). */
      gal_dimension_index_to_coord(index, ndim, dsize, icoord);


      /* Start parsing the neighbors. We will use a two-way ordered linked
         list structure. To start from the nearest and go out to the
         farthest. */
      lQ=sQ=NULL;
      gal_list_dosizet_add(&lQ, &sQ, index, 0.0f);
      while(sQ)
        {
          /* Pop-out (p) an index from the queue: */
          pind=gal_list_dosizet_pop_smallest(&lQ, &sQ, &pdist);

          /* If this isn't a blank value then add its values to the list of
             neighbor values. Note that we didn't check whether the values
             were blank or not when adding this pixel to the queue. */
          if( !(flag[pind] & INTERPOLATE_FLAGS_BLANK) )
            {
              tin=input;
              for(tnear=nearest; tnear!=NULL; tnear=tnear->next)
                {
                  memcpy(gal_pointer_increment(tnear->array, ngb_counter,
                                               tin->type),
                         gal_pointer_increment(tin->array, chstart+pind,
                                               tin->type),
                         gal_type_sizeof(tin->type));
                  tin=tin->next;
                }

              /* If we have filled all the elements clean up the linked
                 list and break out. */
              if(++ngb_counter>=prm->numneighbors)
                {
                  if(lQ) gal_list_dosizet_free(lQ);
                  break;
                }
            }

          /* Go over all the neighbors of this popped pixel and add them to
             the list of neighbors to be checked. */
          GAL_DIMENSION_NEIGHBOR_OP(pind, ndim, dsize, 1, dinc,
           {
             /* Only look at neighbors that have not been checked. VERY
                IMPORTANT: we must not check for blank values here,
                otherwise we won't be able to parse over extended blank
                regions. */
             if( !(flag[nind] & INTERPOLATE_FLAGS_NGB_CHECKED) )
               {
                 /* Get the coordinates of this neighbor. */
                 gal_dimension_index_to_coord(nind, ndim, dsize, ncoord);

                 /* Distance of this neighbor to the one to be filled. */
                 dist=prm->metric(icoord, ncoord, ndim);

                 /* Add this neighbor to the list. */
                 gal_list_dosizet_add(&lQ, &sQ, nind, dist);

                 /* Flag this neighbor as checked. */
                 flag[nind] |= INTERPOLATE_FLAGS_NGB_CHECKED;
               }
           } );

          /* If there are no more meshes to add to the queue, then this
             shows, there were not enough points for
             interpolation. Normally, this loop should only be exited
             through the 'currentnum>=numnearest' check above. */
          if(sQ==NULL)
            error(EXIT_FAILURE, 0, "%s: only %zu neighbors found while "
                  "you had asked to use %zu neighbors for close neighbor "
                  "interpolation", __func__, ngb_counter,
                  prm->numneighbors);
        }

      /* Calculate the desired statistic, and write it in the output. */
      tout=prm->out;
      for(tnear=nearest; tnear!=NULL; tnear=tnear->next)
        {
          /* Find the desired statistic and copy it, but first, reset the
             flags (which remain from the last time). */
          tnear->flag &= ~(GAL_DATA_FLAG_SORT_CH | GAL_DATA_FLAG_BLANK_CH);
          switch(prm->function)
            {
            case GAL_INTERPOLATE_NEIGHBORS_FUNC_MIN:
              value=gal_statistics_minimum(tnear); break;
              break;
            case GAL_INTERPOLATE_NEIGHBORS_FUNC_MAX:
              value=gal_statistics_maximum(tnear); break;
              break;
            case GAL_INTERPOLATE_NEIGHBORS_FUNC_MEAN:
              value=gal_statistics_mean(tnear); /* Out can be a diff. type */
              value=gal_data_copy_to_new_type_free(value, tnear->type);
              break;
            case GAL_INTERPOLATE_NEIGHBORS_FUNC_MEDIAN:
              value=gal_statistics_median(tnear, 1); break;
            default:
              error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at %s "
                    "to fix the problem. The value %d is not a recognized "
                    "interpolation function identifier", __func__,
                    PACKAGE_BUGREPORT, prm->function);
            }
          memcpy(gal_pointer_increment(tout->array, fullind, tout->type),
                 value->array, gal_type_sizeof(tout->type));

          /* Clean up and go to next array. */
          gal_data_free(value);
          tout=tout->next;
        }
    }


  /* Clean up. */
  for(tnear=nearest; tnear!=NULL; tnear=tnear->next) tnear->array=NULL;
  gal_list_data_free(nearest);
  free(icoord);
  free(ncoord);
  free(dinc);


  /* Wait for all the other threads to finish and return. */
  if(tprm->b) pthread_barrier_wait(tprm->b);
  return NULL;
}





/* When no interpolation is needed, then we can just copy the input into
   the output. */
static gal_data_t *
interpolate_copy_input(gal_data_t *input, int aslinkedlist)
{
  gal_data_t *tin, *tout;

  /* Make a copy of the first input. */
  tout=gal_data_copy(input);
  tout->next=NULL;

  /* If we have a linked list, copy each element. */
  if(aslinkedlist)
    {
      /* Note that we have already copied the first input. */
      for(tin=input->next; tin!=NULL; tin=tin->next)
        {
          /* Copy this dataset (will also copy flags). */
          tout->next=gal_data_copy(tin);
          tout=tout->next;
        }

      /* Output is the reverse of the input, so reverse it. */
      gal_list_data_reverse(&tout);
    }

  /* Return the copied list. */
  return tout;
}





/* Interpolate blank values in an array. If the 'tl!=NULL', then it is
   assumed that the tile values correspond to given tessellation. Such that
   'input[i]' corresponds to 'tiles[i]' in the tessellation. */
gal_data_t *
gal_interpolate_neighbors(gal_data_t *input,
                          struct gal_tile_two_layer_params *tl,
                          uint8_t metric, size_t numneighbors,
                          size_t numthreads, int onlyblank,
                          int aslinkedlist, int function)
{
  gal_data_t *tin, *tout;
  struct interpolate_ngb_params prm;
  size_t ngbvnum=numthreads*numneighbors;
  int permute=(tl && tl->totchannels>1 && tl->workoverch);


  /* If there are no blank values in the array, AND we should only fill
     blank values, then simply copy the input and abort. */
  if( (input->flag | GAL_DATA_FLAG_BLANK_CH)   /* Zero bit is meaningful.*/
      && !(input->flag | GAL_DATA_FLAG_HASBLANK)/* There are no blanks.  */
      && onlyblank )                           /* Only interpolate blank.*/
    return interpolate_copy_input(input, aslinkedlist);


  /* Initialize the constant parameters. */
  prm.tl           = tl;
  prm.ngb_vals     = NULL;
  prm.input        = input;
  prm.function     = function;
  prm.onlyblank    = onlyblank;
  prm.numneighbors = numneighbors;
  prm.num          = aslinkedlist ? gal_list_data_number(input) : 1;


  /* Set the metric. */
  switch(metric)
    {
    case GAL_INTERPOLATE_NEIGHBORS_METRIC_RADIAL:
      prm.metric=gal_dimension_dist_radial;
      break;
    case GAL_INTERPOLATE_NEIGHBORS_METRIC_MANHATTAN:
      prm.metric=gal_dimension_dist_manhattan;
      break;
    default:
      error(EXIT_FAILURE, 0, "%s: %d is not a valid metric identifier",
            __func__, metric);
    }


  /* Flag the blank values. */
  prm.blanks=gal_blank_flag(input);


  /* If the input is from a tile structure and the user has asked to ignore
     channels, then re-order the values. */
  if(permute)
    {
      /* Prepare the permutation (if necessary/not already defined). */
      gal_tile_full_permutation(tl);

      /* Re-order values to ignore channels (if necessary). */
      gal_permutation_apply(input, tl->permutation);
      gal_permutation_apply(prm.blanks, tl->permutation);

      /* If this is a linked list, then permute remaining nodes. */
      if(aslinkedlist)
        for(tin=input->next; tin!=NULL; tin=tin->next)
          gal_permutation_apply(tin, tl->permutation);
    }


  /* Allocate space for the (first) output. */
  prm.out=gal_data_alloc(NULL, input->type, input->ndim, input->dsize,
                         input->wcs, 0, input->minmapsize,
                         input->quietmmap, NULL, input->unit, NULL);
  gal_list_void_add(&prm.ngb_vals,
                    gal_pointer_allocate(input->type, ngbvnum, 0, __func__,
                                         "prm.ngb_vals"));


  /* If we are given a list of datasets, make the necessary
     allocations. The reason we are doing this after a check of
     'aslinkedlist' is that the 'input' might have a 'next' element, but
     the caller might not have called 'aslinkedlist'. */
  prm.out->next=NULL;
  if(aslinkedlist)
    for(tin=input->next; tin!=NULL; tin=tin->next)
      {
        /* A small sanity check. */
        if( gal_dimension_is_different(input, tin) )
          error(EXIT_FAILURE, 0, "%s: all datasets in the list must have "
                "the same dimension and size", __func__);

        /* Allocate the output array for this node. */
        gal_list_data_add_alloc(&prm.out, NULL, tin->type, tin->ndim,
                                tin->dsize, tin->wcs, 0, tin->minmapsize,
                                tin->quietmmap, NULL, tin->unit, NULL);

        /* Allocate the space for the neighbor values of this input. */
        gal_list_void_add(&prm.ngb_vals,
                          gal_pointer_allocate(tin->type, ngbvnum, 0,
                                               __func__, "prm.ngb_vals"));
      }
  gal_list_data_reverse(&prm.out);
  gal_list_void_reverse(&prm.ngb_vals);


  /* Allocate space for all the flag values of all the threads here (memory
     in each thread is limited) and this is cleaner. */
  prm.thread_flags=gal_pointer_allocate(GAL_TYPE_UINT8,
                                        numthreads*input->size, 0, __func__,
                                        "prm.thread_flags");


  /* Spin-off the threads. */
  gal_threads_spin_off(interpolate_neighbors_on_thread, &prm,
                       input->size, numthreads, input->minmapsize,
                       input->quietmmap);


  /* If the values were permuted for the interpolation, then re-order the
     values back to their original location (so they correspond to their
     tile indexs. */
  if(permute)
    {
      gal_permutation_apply_inverse(input, tl->permutation);
      for(tout=prm.out; tout!=NULL; tout=tout->next)
        gal_permutation_apply_inverse(tout, tl->permutation);
    }


  /* The interpolated array doesn't have blank values. So set the blank
     flag to 0 and set the use-zero to 1. */
  for(tout=prm.out; tout!=NULL; tout=tout->next)
    {
      tout->flag |= GAL_DATA_FLAG_BLANK_CH;
      tout->flag &= ~GAL_DATA_FLAG_HASBLANK;
    }


  /* Clean up and return. */
  free(prm.thread_flags);
  gal_data_free(prm.blanks);
  gal_list_void_free(prm.ngb_vals, 1);
  return prm.out;
}




















/*********************************************************************/
/********************          1D on grid         ********************/
/*********************************************************************/
gsl_spline *
gal_interpolate_1d_make_gsl_spline(gal_data_t *X, gal_data_t *Y, int type_1d)
{
  size_t i, c;
  double *x, *y;
  gal_data_t *Xd, *Yd;
  gsl_spline *spline=NULL;
  const gsl_interp_type *itype=NULL;
  int Yhasblank=gal_blank_present(Y, 0);

  /* A small sanity check. */
  if(Y->ndim!=1)
    error(EXIT_FAILURE, 0, "%s: input dataset is not 1D (it is %zuD)",
          __func__, Y->ndim);
  if(X)
    {
      if( gal_dimension_is_different(X, Y) )
        error(EXIT_FAILURE, 0, "%s: when two inputs are given, they must "
              "have the same dimensions. X has %zu elements, while Y has "
              "%zu", __func__, X->size, Y->size);
      if(gal_blank_present(X, 0))
        error(EXIT_FAILURE, 0, "%s: the X dataset has blank elements",
              __func__);
    }

  /* Set the interpolation type. */
  switch(type_1d)
    {
    case GAL_INTERPOLATE_1D_LINEAR:
      itype=gsl_interp_linear;           break;
    case GAL_INTERPOLATE_1D_POLYNOMIAL:
      itype=gsl_interp_polynomial;       break;
    case GAL_INTERPOLATE_1D_CSPLINE:
      itype=gsl_interp_cspline;          break;
    case GAL_INTERPOLATE_1D_CSPLINE_PERIODIC:
      itype=gsl_interp_cspline_periodic; break;
    case GAL_INTERPOLATE_1D_AKIMA:
      itype=gsl_interp_akima;            break;
    case GAL_INTERPOLATE_1D_AKIMA_PERIODIC:
      itype=gsl_interp_akima_periodic;   break;
    case GAL_INTERPOLATE_1D_STEFFEN:
#if GAL_CONFIG_HAVE_GSL_INTERP_STEFFEN
      itype=gsl_interp_steffen;          break;
#else
      error(EXIT_FAILURE, 0, "%s: Steffen interpolation isn't available "
            "in the system's GNU Scientific Library (GSL). Please install "
            "a more recent GSL (version >= 2.0, released in October 2015) "
            "and rebuild Gnuastro", __func__);
#endif
    default:
      error(EXIT_FAILURE, 0, "%s: code %d not recognizable for the GSL "
            "interpolation type", __func__, type_1d);
    }

  /* Initializations. Note that if Y doesn't have any blank elements and is
     already in 'double' type, then we don't need to make a copy. */
  Yd = ( (Yhasblank || Y->type!=GAL_TYPE_FLOAT64)
         ? gal_data_copy_to_new_type(Y, GAL_TYPE_FLOAT64)
         : Y );
  Xd = ( X
         /* Has to be 'Yhasblank', we KNOW X doesn't have blank values. */
         ? ( (Yhasblank || X->type!=GAL_TYPE_FLOAT64)
             ? gal_data_copy_to_new_type(X, GAL_TYPE_FLOAT64)
             : X )
         : gal_data_alloc(NULL, GAL_TYPE_FLOAT64, 1, Y->dsize, NULL,
                          0, -1, 1, NULL, NULL, NULL) );

  /* Fill in the X axis values while also removing NaN/blank elements. */
  c=0;
  x=Xd->array;
  y=Yd->array;
  for(i=0;i<Yd->size;++i)
    if( !isnan(y[i]) )
      {
        y[ c   ] = y[i];
	x[ c++ ] = X ? x[i] : i;
      }

  /* Make sure we have enough valid points for interpolation. */
  if( c>=gsl_interp_type_min_size(itype) )
    {
      spline=gsl_spline_alloc(itype, c);
      gsl_spline_init(spline, x, y, c);
    }
  else
    spline=NULL;

  /* Clean up and return. */
  if(Xd!=X) gal_data_free(Xd);
  if(Yd!=Y) gal_data_free(Yd);
  return spline;
}





/* Return 0 if all blanks were filled. */
static int
interpolate_1d_blank_write(gal_data_t *in, gsl_spline *spline,
			   gsl_interp_accel *acc)
{
  double tmp;
  int hasblank=0;
  uint8_t  *su8 =in->array, *u8 =in->array, *u8f =u8 +in->size;
  int8_t   *si8 =in->array, *i8 =in->array, *i8f =i8 +in->size;
  uint16_t *su16=in->array, *u16=in->array, *u16f=u16+in->size;
  int16_t  *si16=in->array, *i16=in->array, *i16f=i16+in->size;
  uint32_t *su32=in->array, *u32=in->array, *u32f=u32+in->size;
  int32_t  *si32=in->array, *i32=in->array, *i32f=i32+in->size;
  uint64_t *su64=in->array, *u64=in->array, *u64f=u64+in->size;
  int64_t  *si64=in->array, *i64=in->array, *i64f=i64+in->size;
  float    *sf32=in->array, *f32=in->array, *f32f=f32+in->size;
  double   *sf64=in->array, *f64=in->array, *f64f=f64+in->size;

  switch(in->type)
    {
    case GAL_TYPE_UINT8:
      do
        if(*u8==GAL_BLANK_UINT8)
          {
            /* If the evaluation is good, this function will return 0. */
            if( gsl_spline_eval_e(spline, u8-su8, acc, &tmp)==0 )
              *u8=tmp;
            else hasblank=1;
          }
      while(++u8<u8f);
      break;
    case GAL_TYPE_INT8:
      do
        if(*i8==GAL_BLANK_INT8)
          {
            if( gsl_spline_eval_e(spline, i8-si8, acc, &tmp)==0 )
              *u16=tmp;
            else hasblank=1;
          }
      while(++i8<i8f);
      break;
    case GAL_TYPE_UINT16:
      do
        if(*u16==GAL_BLANK_UINT16)
          {
            if( gsl_spline_eval_e(spline, u16-su16, acc, &tmp)==0 )
              *u16=tmp;
            else hasblank=1;
          }
      while(++u16<u16f);
      break;
    case GAL_TYPE_INT16:
      do
        if(*i16==GAL_BLANK_INT16)
          {
            if( gsl_spline_eval_e(spline, i16-si16, acc, &tmp)==0 )
              *i16=tmp;
            else hasblank=1;
          }
      while(++i16<i16f);
      break;
    case GAL_TYPE_UINT32:
      do
        if(*u32==GAL_BLANK_UINT32)
          {
            if( gsl_spline_eval_e(spline, u32-su32, acc, &tmp)==0 )
              *u32=tmp;
            else hasblank=1;
          }
      while(++u32<u32f);
      break;
    case GAL_TYPE_INT32:
      do
        if(*i32==GAL_BLANK_INT32)
          {
            if( gsl_spline_eval_e(spline, i32-si32, acc, &tmp)==0 )
              *i32=tmp;
            else hasblank=1;
          }
      while(++i32<i32f);
      break;
    case GAL_TYPE_UINT64:
      do
        if(*u64==GAL_BLANK_UINT64)
          {
            if( gsl_spline_eval_e(spline, u64-su64, acc, &tmp)==0 )
              *u64=tmp;
            else hasblank=1;
          }
      while(++u64<u64f);
      break;
    case GAL_TYPE_INT64:
      do
        if(*i64==GAL_BLANK_INT64)
          {
            if( gsl_spline_eval_e(spline, i64-si64, acc, &tmp)==0 )
              *i64=tmp;
            else hasblank=1;
          }
      while(++i64<i64f);
      break;
    case GAL_TYPE_FLOAT32:
      do
        if(isnan(*f32))
          {
            if( gsl_spline_eval_e(spline, f32-sf32, acc, &tmp)==0 )
              *f32=tmp;
            else hasblank=1;
          }
      while(++f32<f32f);
      break;
    case GAL_TYPE_FLOAT64:
      do
        if(isnan(*f64))
          {
            if( gsl_spline_eval_e(spline, f64-sf64, acc, f64) )
              hasblank=1;
          }
      while(++f64<f64f);
      break;
    default:
      error(EXIT_FAILURE, 0, "%s: code %d is not a recognized data type",
	    __func__, in->type);
    }
  return hasblank;
}





void
gal_interpolate_1d_blank(gal_data_t *in, int type_1d)
{
  int hasblank;
  gsl_spline *spline;
  gsl_interp_accel *acc;

  /* If there are no blank elements, just return. */
  if(!gal_blank_present(in, 1)) return;

  /* Initialize the necessary structures. */
  spline=gal_interpolate_1d_make_gsl_spline(NULL, in, type_1d);

  /* If any interpolation structure was actually made. */
  if(spline)
    {
      /* Write the values in the blank elements. */
      acc=gsl_interp_accel_alloc();
      hasblank=interpolate_1d_blank_write(in, spline, acc);

      /* For a check.
      {
        size_t i;
        double *d;
        gal_data_t *check=gal_data_copy_to_new_type(in, GAL_TYPE_FLOAT64);
        d=check->array;
        for(i=0;i<check->size;++i)
          printf("%-10zu%f\n", i, d[i]);
        gal_data_free(check);
      }
      */

      /* Set the blank flags, note that 'GAL_DATA_FLAG_BLANK_CH' is already set
         by the top call to 'gal_blank_present'. */
      if(hasblank)
        in->flag |=  GAL_DATA_FLAG_HASBLANK;
      else
        in->flag &= ~GAL_DATA_FLAG_HASBLANK;

      /* Clean up. */
      gsl_spline_free(spline);
      gsl_interp_accel_free(acc);
    }
}