File: vmimgresampling.c

package info (click to toggle)
cpl-plugin-vimos 2.9.15%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 18,560 kB
  • ctags: 6,341
  • sloc: ansic: 148,777; sh: 11,457; cpp: 724; makefile: 606; python: 287; perl: 10
file content (873 lines) | stat: -rw-r--r-- 22,308 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
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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
/* $Id: vmimgresampling.c,v 1.2 2013-03-25 11:43:04 cgarcia Exp $
 *
 * This file is part of the VIMOS Pipeline
 * Copyright (C) 2002-2004 European Southern Observatory
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/*
 * $Author: cgarcia $
 * $Date: 2013-03-25 11:43:04 $
 * $Revision: 1.2 $
 * $Name: not supported by cvs2svn $
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <math.h>
#include <assert.h>

#include <vimoswcs.h>

#include <piltranslator.h>
#include <pilmessages.h>
#include <cpl_msg.h>

#include "vmimage.h"
#include "vmimagearray.h"
#include "vmwcsutils.h"
#include "vmimgresampling.h"


#define BI_LINEAR  1
#define BI_CUBIC   2


/*
 * @brief
 *   Swap two double values.
 *
 * @return Nothing.
 * 
 * @param a  Pointer to first value
 * @param b  Pointer to second value
 * 
 * The function exchanges the double values stored in @em a and @em b
 * in place.
 *
 * @author P. Montegriffo
 */

/*
 * This one is no longer necessary, after fixing the getFieldOfView()
 *

static void swap(double *a, double *b)
{
  double dmp = *a;

  *a = *b;
  *b = dmp;

   return;
}

*********/
 

/*
 * @brief
 *   Compute an interpolated value.
 *
 * @return The interpolated value.
 * 
 * @param image  Image pixel buffer.
 * @param nx     Number of image pixels along x.
 * @param ny     Number of image pixels along y.
 * @param ifunc  Selection flag for the interpolation function.
 * @param x      x coordinate of point to interpolate
 * @param y      y coordinate of point to interpolate
 * 
 * The function omputes an interpolated value at a position given by @em x
 * and @em y using a bi-linear or a bi-cubic interpolation function. The
 * interpolation function is selected by the flag @em ifunc. Passing 2 for
 * @em ifunc selects the bi-cubic interpolation. Any other value selects
 * the bi-linear interpolation function.
 * 
 * @note
 *   Coefficents and formulae for the bi-cubic interpolation are taken from
 *   'Numerical Recipes', II Ed., p. 119f.
 *
 * @author P. Montegriffo
 */

static double interpolate(float *image, int nx, int ny, int ifunc,
                          double x, double y)
{

  int  i, j, ii, kk;
  int  ind[4];

  double val;
  double v[16], t, u;
  double xx, c[16];

  static int wt[16][16] = {
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},
    {-3,0,0,3,0,0,0,0,-2,0,0,-1,0,0,0,0},
    {2,0,0,-2,0,0,0,0,1,0,0,1,0,0,0,0},
    {0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0},
    {0,0,0,0,-3,0,0,3,0,0,0,0,-2,0,0,-1},
    {0,0,0,0,2,0,0,-2,0,0,0,0,1,0,0,1},
    {-3,3,0,0,-2,-1,0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,-3,3,0,0,-2,-1,0,0},
    {9,-9,9,-9,6,3,-3,-6,6,-6,-3,3,4,2,1,2},
    {-6,6,-6,6,-4,-2,2,4,-3,3,3,-3,-2,-1,-1,-2},
    {2,-2,0,0,1,1,0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,2,-2,0,0,1,1,0,0},
    {-6,6,-6,6,-3,-3,3,3,-4,4,2,-2,-2,-2,-1,-1},
    {4,-4,4,-4,2,2,-2,-2,2,-2,-2,2,1,1,1,1}
  };


  val = 0.0;                /*** Value for undefined pixels ***/

  i = (int) floor (x);
  j = (int) floor (y);

  if (i <= 0 || j <= 0 || i >= nx - 1 || j >= ny - 1 )
    return (0.0);

  /* Indexes for 4 closest neighbors positions */

  ind[0] = nx * j + i;
  ind[1] = nx * j + i + 1;
  ind[2] = nx * (j + 1) + i + 1;
  ind[3] = nx * (j + 1) + i;


  /* Values for 4 closest neighbors pixels */

  for (ii = 0; ii < 4; ii++) {
    v[ii] = *(image + ind[ii]);

    /* FIXME:
     *   What's the use of this print statment, especially since the 
     *   hardcoded values for x and y do not correspond to the size
     *   of a VIMOS image. Did I miss something. (RP)
     *
     *   Temporarily disabled, since not present in the original version. (RP)
     */
    /*
      if ((y >= 2015.) && (y <= 2016.) && (x >= 2041.0)) {
        printf("ii= %d, ind[ii]=%d, v[ii]=%f\n", ii, ind[ii], 
               *(image + ind[ii]));
      }
    */
  }

  t = x - (double) i;
  u = y - (double) j;

  switch (ifunc) {
      case BI_CUBIC:
        if ( i <= 1 || j <= 1 || i >= nx - 2 || j >= ny - 2 )
          return (0.0);

        /* Compute the gradients along x,y and the cross derivative */
     
        for (ii = 0; ii < 4; ii++) {
          v[ii + 4] = (*(image + ind[ii] + 1) -
                       *(image +ind[ii] - 1)) / 2.0;

          v[ii + 8] = (*(image + ind[ii] + nx) -
                       *(image + ind[ii] - nx)) / 2.0;

          v[ii + 12] = (*(image + ind[ii] + nx + 1) -
                        *(image + ind[ii] - nx + 1) - 
                        *(image+ind[ii]+nx-1) +
                        *(image+ind[ii]-nx-1)) / 4.0;
        }
     
        /* Get coefficents for bicubic interpolation */
     
        for (ii = 0; ii < 16; ii++) {
          xx = 0.0;
          for (kk=0; kk<16; kk++)
            xx += wt[ii][kk]*v[kk];
          c[ii] = xx;
        }         

        /* Compute interpolated value */
     
        for (ii = 3; ii >= 0; ii--)
          val = t * val + ((c[ii * 4 + 3] * u + c[ii * 4 + 2]) * u +
                           c[ii * 4 + 1]) * u + c[ii * 4];
     
        break;

      default:
        val = (double)( (1.0 - t) * (1.0 - u) * v[0] 
                       +       t  * (1.0 - u) * v[1] 
                       +       t  *        u  * v[2] 
                       +(1.0 - t) *        u  * v[3] );
        
        /* FIXME:
         *   What's the use of this print statment, especially since the 
         *   hardcoded values for x and y do not correspond to the size
         *   of a VIMOS image. Did I miss something. (RP)
         *
         *   Temporarily disabled, since not present in the original
         *   version. (RP)
         */
        /*
          if (y >= 2015. && y <= 2016. && x >= 2041.0) {
            printf("bi_linear: i=%d, j=%d, t=%f, u=%f, val=%f, v[0]=%f, "
                   "v[1]=%f, v[2]=%f, v[3]=%f\n", i, j, t, u, val, v[0],
                   v[1], v[2], v[3]);
           }
        */
        break;
  }

  return (val);

}


/**
 * @brief
 *   Determine the total field of view of a set of images.
 *
 * @return The function returns a pointer to the WCS structure of the
 *   reference image if no error occurred, otherwise @c NULL is returned.
 *
 * @param images  Set of images.
 * @param ra1     Start right ascension of the field of view.
 * @param dec1    Start declination  of the field of view.
 * @param ra2     End right ascension of the field of view.
 * @param dec2    End declination  of the field of view.
 *
 * The function determines the union of the sky regions covered by the
 * images in the input array @em images. The first image in the input
 * array is, arbitrarily choosen, as reference, and is returned if
 * the field of view determination was successful.
 *
 * @author P. Montegriffo, R. Palsa
 */

/* Remove, because of wrongly computed FOV: the pixel having
 * simultaneously the max observed RA and Dec is not necessarily
 * at the corner of the union of all images (think of the case
 * when the field is rotated); the same is true for the pixel
 * having simultaneously the min observed RA and Dec.
 * 

static struct WorldCoor *getFieldOfView(VimosImageArray *images,
                                        double *ra1, double *dec1,
                                        double *ra2, double *dec2)
{

  int i;

  double wra1, wra2, wdec1, wdec2;

  struct  WorldCoor *wcs_r = 0L;
  struct  WorldCoor *wcs_w = 0L;

  VimosImage *image = (VimosImage *)imageArrayGet(images, 0);


   *
   * Read reference WCS from the first (arbitrarily choosen) image
   * in the list.
   * 

  if (!(wcs_r = rdimage(image->descs)))
    return 0L;

  vimoswcsrange(wcs_r, ra1, ra2, dec1, dec2);

  for (i = 1; i < imageArraySize(images); i++) {
    image = (VimosImage *)imageArrayGet(images, i);

    if (!(wcs_w = rdimage(image->descs))) {
      vimoswcsfree(wcs_r);
      return 0L;
    }

    vimoswcsrange (wcs_w, &wra1, &wra2, &wdec1, &wdec2);

    *ra1 = wra1 < *ra1 ? wra1 : *ra1;
    *ra2 = wra2 > *ra2 ? wra2 : *ra2;

    *dec1 = wdec1 < *dec1 ? wdec1 : *dec1;
    *dec2 = wdec2 > *dec2 ? wdec2 : *dec2;

    vimoswcsfree(wcs_w);
  }

  return wcs_r;

}

**********/

/*
 * This is the new version to compute the FOV. That is, this is
 * the one that really finds it.
 */

static struct WorldCoor *getFieldOfViewNew(VimosImageArray *images,
                                        double *xpix1, double *ypix1,
                                        double *xpix2, double *ypix2)
{

  int offscl;
  int i;

  double ra1, ra2, dec1, dec2;
  double xmin, ymin, xmax, ymax;
  double x, y;

  struct  WorldCoor *wcs_r = 0L;
  struct  WorldCoor *wcs_w = 0L;


  VimosImage *image = (VimosImage *)imageArrayGet(images, 0);


  /*
   * Read reference WCS from the first (arbitrarily choosen) image
   * in the list.
   */

  if (!(wcs_r = rdimage(image->descs)))
    return 0L;

  xmin = 1.0;
  ymin = 1.0;
  xmax = wcs_r->nxpix;
  ymax = wcs_r->nypix;

  for (i = 1; i < imageArraySize(images); i++) {
    image = (VimosImage *)imageArrayGet(images, i);

    if (!(wcs_w = rdimage(image->descs))) {
      vimoswcsfree(wcs_r);
      return 0L;
    }

    pix2vimoswcs(wcs_w, 1., 1., &ra1, &dec1);
    pix2vimoswcs(wcs_w, wcs_w->nxpix, wcs_w->nypix, &ra2, &dec2);

    vimoswcs2pix(wcs_r, ra1, dec1, &x, &y, &offscl);
    if (x < xmin)
      xmin = x;
    if (y < ymin)
      ymin = y;

    vimoswcs2pix(wcs_r, ra2, dec2, &x, &y, &offscl);
    if (x > xmax)
      xmax = x;
    if (y > ymax)
      ymax = y;

    vimoswcsfree(wcs_w);
  }

  *xpix1 = xmin;
  *ypix1 = ymin;
  *xpix2 = xmax;
  *ypix2 = ymax;

  return wcs_r;

}


/**
 * @brief
 *   Setup a new world coordinate system.
 *
 * @return The function returns a reference to the newly created WCS
 *   structure if no error occurred, otherwise the return value is
 *   @c NULL.
 *
 * @param wcs   Reference world coordinate system.
 * @param ra1   Start right ascension of the field of view.
 * @param dec1  Start declination  of the field of view.
 * @param ra2   End right ascension of the field of view.
 * @param dec2  End declination  of the field of view.
 *
 * The functions computes the pixel position of corners of the field of
 * view given by @em ra1, @em dec1 and @em ra2, @em dec2 with respect
 * to the coordinate system defined by @em wcs. The pixel coordinates
 * of the reference pixel defined by @em wcs are adjusted accordingly.
 *
 * @author P. Montegriffo
 */

/*
 * This function is outdated, after fixing the getFieldOfView().
 *

static struct WorldCoor *setupWcs(struct WorldCoor *wcs, double ra1,
                                  double dec1, double ra2, double dec2)
{

   int offscl;

   double xpix1, ypix1, xpix2, ypix2;
   double crpix1, crpix2;

   struct WorldCoor *wcs_r = 0L;
  

    * Initialize returning structure * 

    * FIXME:
    *  The original code sets the CD matrix to NULL, so that wcskinit()
    *  ignores it and uses the values from CDELTi and CROTAi instead
    *  to setup the WCS structure. But note that the original code
    *  used a hardcoded 0.0 for the rotation angle.
    *
    *  Check if this was done on purpose. There was no hint about that
    *  in the original code. (RP)
    *
    *  Original code follows:
    *
    *  double * cd = NULL;
    *  double   crota = 0.0;
    *
    *  .
    *  .
    *  .
    *
    *  wcs_r = wcskinit((int)wcs->nxpix, (int)wcs->nypix,
    *                   "RA---TAN", "DEC--TAN",
    *                   wcs->crpix[0], wcs->crpix[1],
    *                   wcs->crval[0], wcs->crval[1], cd,
    *                   wcs->cdelt[0], wcs->cdelt[1], crota,
    *                   (int)wcs->equinox, wcs->epoch);
    *
    * 

   wcs_r = wcskinit((int)wcs->nxpix, (int)wcs->nypix,
                    "RA---TAN", "DEC--TAN",
                    wcs->crpix[0], wcs->crpix[1],
                    wcs->crval[0], wcs->crval[1], wcs->cd,
                    0., 0., 0., (int)wcs->equinox, wcs->epoch);

   if (!wcs_r)
     return 0L;


    * Compute corner pixels coordinates in the new framework * 

   wcs2pix(wcs_r, ra1, dec1, &xpix1, &ypix1, &offscl);
   wcs2pix(wcs_r, ra2, dec2, &xpix2, &ypix2, &offscl);

   if (xpix1 > xpix2)
     swap(&xpix1, &xpix2);

   if (ypix1 > ypix2)
     swap(&ypix1, &ypix2);


    * Set new image dimensions and reference pixel * 

    * FIXME:
    *   The original code had the following 2 line just before
    *   the 2 floor() calls below.
    *
    *   wcs_r->nxpix = nint(xpix2) - nint(xpix1) + 1.0;
    *   wcs_r->nypix = nint(ypix2) - nint(ypix1) + 1.0;
    *
    *   They had no effect since their result was overwritten by the
    *   two calls to floor(). Anyway the function nint() is not available
    *   on every system, it belongs to the sunmath library available
    *   on SUNs. So if this alternative should be used it can be emulated by
    *
    *   wcs_r->nxpix = (int)(xpix2 + 0.5) - (int)(xpix1 + 0.5) + 1.0;
    *   wcs_r->nypix = (int)(ypix2 + 0.5) - (int)(ypix1 + 0.5) + 1.0;
    * 

   wcs_r->nxpix = floor(xpix2 - xpix1 + 1.0);
   wcs_r->nypix = floor(ypix2 - ypix1 + 1.0);

   crpix1 = wcs_r->crpix[0] - floor(xpix1);
   crpix2 = wcs_r->crpix[1] - floor(ypix1);

   if (wcsreset(wcs_r, crpix1, crpix2, wcs_r->crval[0], wcs_r->crval[1],
                wcs_r->cdelt[0], wcs_r->cdelt[1], 0., wcs_r->cd)) {
     wcsfree(wcs_r);
     return 0L;
   }

   return wcs_r;

}

****************/

/*
 * This is the new function replacing the above one - note how
 * simple it is...
 */

static struct WorldCoor *setupWcsNew(struct WorldCoor *wcs, double xpix1,
                                  double ypix1, double xpix2, double ypix2)
{

   double crpix1, crpix2;

   wcs->nxpix = floor(xpix2 - xpix1 + 1.0);
   wcs->nypix = floor(ypix2 - ypix1 + 1.0);

   crpix1 = wcs->crpix[0] - floor(xpix1);
   crpix2 = wcs->crpix[1] - floor(ypix1);

   if (vimoswcsreset(wcs, crpix1, crpix2, wcs->crval[0], wcs->crval[1],
                wcs->cdelt[0], wcs->cdelt[1], 0., wcs->cd)) {
     return 0L;
   }

   return wcs;

}

/**
 * @brief
 *   Update an image keyword list with a new world coordinate system.
 *
 * @return The function returns 0 if the keyword list was updated
 *   successfully, otherwise 1 is returned.
 *
 * @param image  Image object.
 * @param wcs    WCS information.
 *
 * The function updates the world coordinate system related keywords
 * @li CRPIX1,
 * @li CRPIX2,
 * @li CRVAL1,
 * @li CRVAL2,
 * and the CD matrix coefficients of the image @em image, i.e. it's keyword
 * list, with the new values taken from @em wcs. If the keyword do not
 * exist they are created.
 *
 * @author P. Sartoretti
 */

static int updateWcsKeywords(VimosImage *image, struct WorldCoor *wcs)
{

  register int  i, j, k;


  if (writeDoubleDescriptor(&image->descs,
                            pilTrnGetKeyword("Crpix", 1), wcs->xrefpix,
                            pilTrnGetComment("Crpix")) == VM_FALSE) {
    return 1;
  }

  if (writeDoubleDescriptor(&image->descs,
                            pilTrnGetKeyword("Crpix", 2), wcs->yrefpix,
                            pilTrnGetComment("Crpix")) == VM_FALSE) {
    return 1;
  }

  if (writeDoubleDescriptor(&image->descs,
                            pilTrnGetKeyword("Crval", 1), wcs->xref,
                            pilTrnGetComment("Crval")) == VM_FALSE) {
    return 1;
  }

  if (writeDoubleDescriptor(&image->descs,
                            pilTrnGetKeyword("Crval", 2), wcs->yref,
                            pilTrnGetComment("Crval")) == VM_FALSE) {
    return 1;
  }


  k = 0;

  for(i = 1; i <= 2; i++) {
    for(j = 1; j <= 2; j++) {
      if (writeDoubleDescriptor(&image->descs,
                                pilTrnGetKeyword("CD", i, j), wcs->cd[k],
                                pilTrnGetComment("CD")) == VM_FALSE) {
	return 1;
      }

      k++;
    }
  }
      
  return 1;

}


/**
 * @brief
 *   Resample a set of images to a common coordinate grid.
 *
 * @return The function returns TBD.
 *
 * @param set     Set of images.
 * @param method  Interpolation method.
 *
 * TBD
 *
 * @author P. Montegriffo, R. Palsa
 */

VimosImageArray *VmImResampleImages(VimosImageArray *set,
                                    ResamplingMethod method)
{

  const char *fctid = "VmImResampleImages";


  int i;
  int nx, ny;

  size_t k;
  size_t npixel;

/*  double ra1, ra2, dec1, dec2;  */

  double xpix1, ypix1, xpix2, ypix2;

  VimosImage *srcImage, *dstImage;

  struct WorldCoor *wcs_r, *wcs;


  /*
   * Get the full field of view, i.e. the ranges of right ascension
   * and declination, covered by the union of the fields of view of
   * all images in the input set.
   */

  cpl_msg_info(fctid, "Computing sky area covered by image set ...");

/*
 * The original code was incorrectly defining the new image corners
 * from the ranges in RA and Dec... Instead, the new image corners
 * are simply the min-max range of all image corners, computed in
 * the WCS of the reference frame!
 *
 *  if (!(wcs_r = getFieldOfView(set, &ra1, &dec1, &ra2, &dec2))) {
 *    cpl_msg_error(fctid, "Sky area computation failed!");
 *    return 0L;
 *  }
 *
 *  cpl_msg_info(fctid, "R.A. covered range: %10.6f --> %10.6f", ra1, ra2);
 *  cpl_msg_info(fctid, "Dec. covered range: %10.6f --> %10.6f", dec1, dec2);
 */

  if (!(wcs_r = getFieldOfViewNew(set, &xpix1, &ypix1, &xpix2, &ypix2))) {
    cpl_msg_error(fctid, "Sky area computation failed!");
    return 0L;
  }

  /*
   * Setup new WCS for the full field of view
   */

/*
 * See note above about incorrectly computed FOV.
 *
 *  if (!(wcs = setupWcs(wcs_r, ra1, dec1, ra2, dec2))) {
 *    cpl_msg_error(fctid, "Field of view coordinate system creation failed!");
 *
 *    vimoswcsfree(wcs_r);
 *    return 0L;
 *  }
 */

  if (!(wcs = setupWcsNew(wcs_r, xpix1, ypix1, xpix2, ypix2))) {
    cpl_msg_error(fctid, "Field of view coordinate system creation failed!");

    vimoswcsfree(wcs_r);
    return 0L;
  }
  else {
    cpl_msg_info(fctid, "New image dimensions: NAXIS1=%d, NAXIS2=%d",
                (int)wcs->nxpix, (int)wcs->nypix);
    cpl_msg_debug(fctid, "New reference pixel:  CRPIX1=%10.3f, CRPIX2=%10.3f",
                wcs->crpix[0], wcs->crpix[1]);
  }

  /*
   * No need to free memory, it's the input wcs_r the it's output to
   * from setupWcsNew(), i.e., wcs_r = wcs (same pointer).
   * 
   *
   *  vimoswcsfree(wcs_r);
   *
   *****/


  /*
   * Remap imput images to the new coordinate grid
   */

  nx = (int)wcs->nxpix;
  ny = (int)wcs->nypix;
  
  npixel = nx * ny;


  for (i = 0; i < imageArraySize(set); i++) {

    cpl_msg_info(fctid, "Resampling image %d.", i);


    /*
     * Get source image from the input set and retrieve its world
     * coordinate system information.
     */

    srcImage = (VimosImage *)imageArrayGet(set, i);

    if (!(wcs_r = rdimage(srcImage->descs))) {
      cpl_msg_error(fctid, "Retrieving coordinate system information from "
                  "image %d failed!", i);

      vimoswcsfree(wcs);
      return 0L;
    }


    if (!(dstImage = newImageAndAlloc(nx, ny))) {
      cpl_msg_error(fctid, "Not enough memory!");

      vimoswcsfree(wcs_r);
      vimoswcsfree(wcs);

      return 0L;
    }


    /*
     * Copy the image header to the resampled image and update 
     * the WCS related keywords.
     */

    if (copyAllDescriptors(srcImage->descs, &dstImage->descs) == VM_FALSE) {
      cpl_msg_error(fctid, "Copying header keywords failed!");
      
      deleteImage(dstImage);

      vimoswcsfree(wcs_r);
      vimoswcsfree(wcs);

      return 0L;
    }

    if (!updateWcsKeywords(dstImage, wcs)) {
      cpl_msg_error(fctid, "Updating world coordinate system information "
                  "failed!");
      deleteImage(dstImage);

      vimoswcsfree(wcs_r);
      vimoswcsfree(wcs);

      return 0L;
    }


    /*
     * Loop over pixels. For each pixel position of the resampled image get
     * its position on the sky and use this position to compute its pixel
     * value, by interpolation, from the input image pixels.
     */

    /* FIXME:
     *  For performance reasons this loop over all pixels of the result
     *  image should be done inside the resampling function. Calling
     *  interpolate() for each pixel is not very efficient. (RP)
     */

    for (k = 0; k < npixel; k++) {

      int offscl;

      double xpix, ypix, x, y;
      double ra, dec;
      

      /*
       * Compute world coordinates for pixel center
       */

      xpix = (double)(k % nx) + 1.0;
      ypix = (double)(k / nx) + 1.0;

      pix2vimoswcs(wcs, xpix, ypix, &ra, &dec);

      /*
       * Compute pixel coordinates of the source image corresponding to
       * the just computed ra and dec.
       */

      vimoswcs2pix(wcs_r, ra, dec, &x, &y, &offscl);

      x = x - 1.0 ;
      y = y - 1.0 ;

/*
if (k % 64 == 0) {
printf("XXXX %d %d %f %f\n", (int)xpix - 1, (int)ypix - 1, x, y);
}
*/

      /*
       * Compute interpolated value
       */

      /* FIXME:
       *   Instead of DOUBLENULLVALUE (which comes from cfitsio) a simple
       *   0. does the same job. To be checked! (RP)
       */

      if (!offscl)
        dstImage->data[k] = interpolate(srcImage->data, wcs_r->nxpix,
                                        wcs_r->nypix, method, x, y);
      else
        dstImage->data[k] = DOUBLENULLVALUE;

    }


    /*
     * Replace the source image in the input set with the resampled
     * result. The source is deallocated.
     */

    deleteImage((VimosImage *)imageArrayRemove(set, i));
    imageArraySet(set, i, dstImage);

    vimoswcsfree(wcs_r);
  }


  /*
   * Cleanup
   */

  vimoswcsfree(wcs);
    
  return set;

}