File: PowWCS.c

package info (click to toggle)
ftools-fv 5.5.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 7,072 kB
  • sloc: tcl: 50,774; ansic: 17,858; exp: 2,004; makefile: 172; cpp: 169; sh: 109; csh: 10
file content (1030 lines) | stat: -rw-r--r-- 36,857 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
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
#include <math.h>
#include <string.h>
#include <stdio.h>
#include "pow.h"

#define DEG2RAD 1.745329252e-2

#define NUM_WCS_TYPES 27
static char wcsProjections[NUM_WCS_TYPES][5] =
      {"-AZP", "-SZP", "-TAN", "-STG", "-SIN", "-ARC", "-ZPN", "-ZEA", "-AIR", "-CYP",
       "-CEA", "-CAR", "-MER", "-COP", "-COE", "-COD", "-COO", "-SFL", "-PAR", "-MOL",
       "-AIT", "-BON", "-PCO", "-TSC", "-CSC", "-QSC", "-HPX"};

void PowInitWCS( WCSdata *WCS, int n )
{
   int row, col;

   WCS->RaDecSwap = 0;
   WCS->nAxis     = n;
   for( row=0; row<n; row++ ) {
      WCS->refVal[row] = 0.0;
      WCS->refPix[row] = 0.0;
      for( col=0; col<n; col++ ) {
         WCS->cdFrwd[row][col] = (row==col?1:0);
         WCS->cdRvrs[row][col] = (row==col?1:0);
      }
   }
   memset (WCS->type, '\0', 6);
   memset (WCS->graphName, '\0', 1024);
   memset (WCS->curveName, '\0', 1024);
   WCS->wcs = malloc(sizeof(struct wcsprm));
   WCS->wcs->flag = -1;
   wcsini (1, n, WCS->wcs);
   WCS->haveWCSinfo = 0;
}

int FillinWCSStructure ( WCSdata *WCS )
{
   char powFitsHeader[14]="powFitsHeader";
   char powFitsHeaderCnt[17]="powFitsHeaderCnt";
   int i, relax, HDRcnt, ctrl, nreject=0, nwcs=0;
   const char *HDRstring;
   int status;
   int coordSel;
   Tcl_Obj *listObj;
   Tcl_Obj *wcsname[27];

   /* no wcs info yet */
   if ( WCS->graphName[0] != '\0' && strcmp(WCS->graphName, "NULL") != 0 ) {
      HDRstring = Tcl_GetVar2(interp,powFitsHeader,WCS->graphName,TCL_GLOBAL_ONLY);
      HDRcnt = atoi(Tcl_GetVar2(interp,powFitsHeaderCnt,WCS->graphName,TCL_GLOBAL_ONLY));
   } else if ( WCS->curveName[0] != '\0' && strcmp(WCS->curveName, "NULL") != 0 ) {
      HDRstring = Tcl_GetVar2(interp,powFitsHeader,WCS->curveName,TCL_GLOBAL_ONLY);
      HDRcnt = atoi(Tcl_GetVar2(interp,powFitsHeaderCnt,WCS->curveName,TCL_GLOBAL_ONLY));
   } else {
      Tcl_SetResult(interp, "Can't construct WCS information." ,TCL_VOLATILE);
      Tcl_SetVar(interp,"powWCSTranslation", "1" ,TCL_GLOBAL_ONLY);
      return TCL_ERROR;
   }

   relax = WCSHDR_all;

   ctrl = 2;

   if (status = wcspih(HDRstring, HDRcnt, relax, ctrl, &nreject, &nwcs, &(WCS->wcs))) {
      char errormsg[512];
      sprintf(errormsg, "Can't construct WCS information: %s", WCSpih_Message[status]);
      Tcl_SetResult(interp, errormsg ,TCL_VOLATILE);
      Tcl_SetVar(interp,"powWCSTranslation", WCSpih_Message[status] ,TCL_GLOBAL_ONLY);
      return TCL_ERROR;
   }

   listObj = Tcl_NewObj();
   for (i=0; i<nwcs; i++) {
       wcsname[i] = Tcl_NewStringObj(WCS->wcs[i].alt,-1);
   }

   Tcl_ListObjAppendElement( interp, listObj, Tcl_NewIntObj( nwcs ) );
   Tcl_ListObjAppendElement( interp, listObj, Tcl_NewListObj(nwcs,wcsname) );

   if ( WCS->graphName[0] != '\0' && strcmp(WCS->graphName, "NULL") != 0 ) {
      Tcl_SetVar2Ex(interp,"powWCSList", WCS->graphName, listObj, TCL_GLOBAL_ONLY);
   } else if ( WCS->curveName[0] != '\0' && strcmp(WCS->curveName, "NULL") != 0 ) {
      Tcl_SetVar2Ex(interp,"powWCSList", WCS->curveName, listObj, TCL_GLOBAL_ONLY);
   }

   if (nwcs > 0 ) {
      if ( WCS->graphName[0] != '\0' && strcmp(WCS->graphName, "NULL") != 0 ) {
         coordSel = atoi(Tcl_GetVar2(interp,"powWCSName",WCS->graphName,TCL_GLOBAL_ONLY));
      } else if ( WCS->curveName[0] != '\0' && strcmp(WCS->curveName, "NULL") != 0 ) {
         coordSel = atoi(Tcl_GetVar2(interp,"powWCSName",WCS->curveName,TCL_GLOBAL_ONLY));
      }

      WCS->wcs[coordSel].crpix[0] = WCS->refPix[0];
      WCS->wcs[coordSel].crpix[1] = WCS->refPix[1];
   }

   WCS->haveWCSinfo = 1;
   return TCL_OK;
}

int PowParseWCS( Tcl_Interp *interp, WCSdata *WCS,
                 int argc, Tcl_Obj *const argv[] )
{
   /* Known coordinate types (from worldpos below) */
   double xinc,yinc,rot;
   double refVal[MAX_WCS_DIMS],refPix[MAX_WCS_DIMS];
   double cdFrwd[MAX_WCS_DIMS][MAX_WCS_DIMS],cdRvrs[MAX_WCS_DIMS][MAX_WCS_DIMS];
   double norm;
   Tcl_Obj **listElems;
   int row, col, nElem, nDims, dim;
   char *type;
   int swap=0;

   PowInitWCS( WCS, MAX_WCS_DIMS );

   if( argc>7 ) {
      Tcl_GetDoubleFromObj(interp,argv[0],refVal+0);
      Tcl_GetDoubleFromObj(interp,argv[1],refVal+1);
      Tcl_GetDoubleFromObj(interp,argv[2],refPix+0);
      Tcl_GetDoubleFromObj(interp,argv[3],refPix+1);
      Tcl_GetDoubleFromObj(interp,argv[4],&xinc);
      Tcl_GetDoubleFromObj(interp,argv[5],&yinc);
      Tcl_GetDoubleFromObj(interp,argv[6],&rot);
      type = Tcl_GetStringFromObj(argv[7],NULL);

      if( argc>8 ) Tcl_GetBooleanFromObj(interp,argv[8],&swap);

      cdFrwd[0][0] =  xinc * cos( rot * DEG2RAD );
      cdFrwd[0][1] = -yinc * sin( rot * DEG2RAD );
      cdFrwd[1][0] =  xinc * sin( rot * DEG2RAD );
      cdFrwd[1][1] =  yinc * cos( rot * DEG2RAD );
      nDims = 2;

   } else {

      nDims = 1;

      Tcl_ListObjGetElements(interp,argv[0],&nElem,&listElems);
      if( nElem>MAX_WCS_DIMS ) nElem = MAX_WCS_DIMS;
      if( nDims < nElem ) nDims = nElem;
      for( row=0; row<nElem; row++ )
         Tcl_GetDoubleFromObj(interp, listElems[row], refVal+row);

      Tcl_ListObjGetElements(interp,argv[1],&nElem,&listElems);
      if( nElem>MAX_WCS_DIMS ) nElem = MAX_WCS_DIMS;
      if( nDims < nElem ) nDims = nElem;
      for( row=0; row<nElem; row++ )
         Tcl_GetDoubleFromObj(interp, listElems[row], refPix+row);

      Tcl_ListObjGetElements(interp,argv[2],&nElem,&listElems);
      dim = (int)(sqrt( (double)nElem )+0.5);
      nElem = ( dim>MAX_WCS_DIMS ? MAX_WCS_DIMS : dim );
      if( nDims < nElem ) nDims = nElem;
      for( row=0; row<nElem; row++ )
         for( col=0; col<nElem; col++ )
            Tcl_GetDoubleFromObj(interp, listElems[row*dim+col],
                                 &cdFrwd[row][col]);

      /*  Check if RA/Dec axes are swapped  */
      Tcl_ListObjGetElements(interp,argv[3],&nElem,&listElems);
      if( nElem ) {
         type = Tcl_GetStringFromObj(listElems[0],NULL);
         if( type[0] && (!strcmp(type, "DEC") || !strcmp(type+1, "LAT")) )
            swap = 1;
      }

      Tcl_ListObjGetElements(interp,argv[4],&nElem,&listElems);
      type = Tcl_GetStringFromObj(listElems[0],NULL);

   }

   if( swap ) {        /* Transform WCS data so that it gives RA/Dec */
      norm      = refVal[0];
      refVal[0] = refVal[1];
      refVal[1] = norm;
      for( col=0; col<nDims; col++ ) {
         norm           = cdFrwd[0][col];
         cdFrwd[0][col] = cdFrwd[1][col];
         cdFrwd[1][col] = norm;
      }
   }

   /*
    *  Test for unknown projection type.
    */

   if (strcmp(type, "none") == 0) {
      type[0] = '\0';
   } else if( nDims != 2 ) { /*  Only 2D projections allowed  */
      type[0] = '\0';
   } 

   /*
    *  Calculate the inverse transform
    */

   if( nDims == 1 ) {
      cdRvrs[0][0] = 1.0/cdFrwd[0][0];
   } else if( nDims==2 ) {
      norm = cdFrwd[0][0]*cdFrwd[1][1] - cdFrwd[0][1]*cdFrwd[1][0];
      cdRvrs[0][0] =   cdFrwd[1][1] / norm;
      cdRvrs[0][1] = - cdFrwd[0][1] / norm;
      cdRvrs[1][0] = - cdFrwd[1][0] / norm;
      cdRvrs[1][1] =   cdFrwd[0][0] / norm;
   } else {
      return TCL_ERROR;
   }
   
   /*
    *  Copy data into image's WCS structure
    */

   WCS->RaDecSwap = swap;
   WCS->nAxis     = nDims;
   if( *type && refVal[0]<0.0 ) refVal[0] += 360.0;
   for( row=0; row<nDims; row++ ) {
      WCS->refVal[row] = refVal[row];
      WCS->refPix[row] = refPix[row];
      for( col=0; col<nDims; col++ ) {
         WCS->cdFrwd[row][col] = cdFrwd[row][col];
         WCS->cdRvrs[row][col] = cdRvrs[row][col];
      }
   }

   if ( *type ) {
      strcpy(WCS->type,type);
   }

   /* PowDumpWCSstructure(WCS); */
   return TCL_OK;
}

void PowDumpWCSstructure ( WCSdata *WCS ) {
   fprintf(stdout, "**********************************\n");
   fprintf(stdout, "WCS->graphName  : <%s>\n", WCS->graphName);
   fprintf(stdout, "WCS->curveName  : <%s>\n", WCS->curveName);
   fprintf(stdout, "WCS->type       : <%s>\n", WCS->type);
   fprintf(stdout, "WCS->RaDecSwap  : <%d>\n", WCS->RaDecSwap);
   fprintf(stdout, "WCS->nAxis      : <%d>\n", WCS->nAxis);
   fprintf(stdout, "WCS->refVal[0]  : <%20.15f>\n", WCS->refVal[0]);
   fprintf(stdout, "WCS->refVal[1]  : <%20.15f>\n", WCS->refVal[1]);
   fprintf(stdout, "WCS->refPix[0]  : <%20.15f>\n", WCS->refPix[0]);
   fprintf(stdout, "WCS->refPix[1]  : <%20.15f>\n", WCS->refPix[1]);
   fprintf(stdout, "WCS->cdFrwd[0]  : <%20.15f,%20.15f>\n", WCS->cdFrwd[0][0], WCS->cdFrwd[0][1]);
   fprintf(stdout, "WCS->cdFrwd[1]  : <%20.15f,%20.15f>\n", WCS->cdFrwd[1][0], WCS->cdFrwd[1][1]);
   fprintf(stdout, "WCS->cdRvrs[0]  : <%20.15f,%20.15f>\n", WCS->cdRvrs[0][0], WCS->cdRvrs[0][1]);
   fprintf(stdout, "WCS->cdRvrs[1]  : <%20.15f,%20.15f>\n", WCS->cdRvrs[1][0], WCS->cdRvrs[1][1]);
   fprintf(stdout, "WCS->rot        : <%20.15f>\n", WCS->rot);
   fprintf(stdout, "WCS->haveWCSinfo: <%d>\n", WCS->haveWCSinfo);
   fprintf(stdout, "**********************************\n");
   fflush(stdout);
}

int PowWCSInitImage( ClientData clientData, Tcl_Interp *interp, 
                     int argc, Tcl_Obj *const argv[] )
{
   /* Fills in the origin, increment, and otherend fields in the 
      specified image using the WCS info provided.  This is mainly
      used by powCreateImage and by the callback proc for the trace
      on powWCS */
   double xpos,ypos;
   char *imageName;
   PowImage *image_ptr;
   int n;
  
   if( argc < 6 || argc > 11 ) {
      Tcl_SetResult( interp,
                     "usage: powWCSInitImage image xref yref xrefpix "
                     "yrefpix xinc yinc rot type ?swap?\n"
                     "   or: powWCSInitImage image {refVal} {refPix} "
                     "{matrix} {type} {proj}",
                     TCL_VOLATILE );
      return TCL_ERROR;
   }

   imageName = Tcl_GetStringFromObj(argv[1],NULL);
   image_ptr = PowFindImage( imageName );
   if (image_ptr == (PowImage *) NULL) {
      Tcl_SetResult( interp, "Couldn't find image.", TCL_VOLATILE );
      return TCL_ERROR;
   }

   PowParseWCS(interp, &image_ptr->WCS, argc-2, argv+2);

   /* add image name to WCS structure */
   strcpy (image_ptr->WCS.graphName, imageName);
   image_ptr->WCS.haveWCSinfo = 0;

   for( n=0; n<image_ptr->WCS.nAxis; ) {
      image_ptr->WCS.refPix[n++]--; /*  Makes pixels zero-indexed  */
   }

   if( !image_ptr->WCS.type[0]) {
     /* Tcl_SetVar2(interp,"powWCS",imageName,"",TCL_GLOBAL_ONLY); */
   }

   /*
   image_ptr->WCS.xref      = (swap?yref:xref);
   image_ptr->WCS.yref      = (swap?xref:yref);
   image_ptr->WCS.xrefpix   = xrefpix - 1; 
   image_ptr->WCS.yrefpix   = yrefpix - 1; 
   image_ptr->WCS.xinc      = xinc;
   image_ptr->WCS.yinc      = (swap?-yinc:yinc);
   image_ptr->WCS.rot       = (swap?90-rot:rot);
   */

   if( PowPixToPos( -0.5, -0.5, &image_ptr->WCS, &xpos, &ypos ) ) {
      Tcl_SetResult( interp,
                     "Couldn't translate pixels to WCS coords for image "
                     "initialization", TCL_VOLATILE );
      return TCL_ERROR;
   }
    
   image_ptr->xorigin = xpos;
   image_ptr->yorigin = ypos;

   if( PowPixToPos( image_ptr->width-0.5, image_ptr->height-0.5,
                    &image_ptr->WCS, &xpos, &ypos )  ) {
      Tcl_SetResult( interp,
                     "Couldn't translate pixels to WCS coords for "
                     "image initialization", TCL_VOLATILE );
      return TCL_ERROR;
   }

   image_ptr->xotherend = xpos;
   image_ptr->yotherend = ypos;

   image_ptr->xinc = ( xpos - image_ptr->xorigin ) / image_ptr->width;
   image_ptr->yinc = ( ypos - image_ptr->yorigin ) / image_ptr->height;

   return TCL_OK;
}


int PowWCSInitCurve(ClientData clientData, Tcl_Interp *interp, 
		    int argc, Tcl_Obj *const argv[])
{
   /* Fills in the WCS structure if info exists.  This is mainly
      used by powCreateCurve and by the callback proc for the trace
      on powWCS */
   PowCurve *curve_ptr;
   char *curveName;
   int str_len;
   char *p;
  
   if( argc < 7 || argc > 11 ) {
      Tcl_SetResult( interp,
                     "usage: powWCSInitCurve curve xref yref xrefpix "
                     "yrefpix xinc yinc rot type ?swap?\n"
                     "   or: powWCSInitCurve curve {refVal} {refPix} "
                     "{matrix} {type} {proj}",
                     TCL_VOLATILE );
      return TCL_ERROR;
   }

   curveName = Tcl_GetStringFromObj( argv[1], NULL );
   curve_ptr = PowFindCurve( curveName );
   if (curve_ptr == (PowCurve *) NULL) {
      Tcl_SetResult( interp, "Couldn't find curve.", TCL_VOLATILE );
      return TCL_ERROR;
   }

   PowParseWCS( interp, &curve_ptr->WCS, argc-2, argv+2 );

   /* add curve name to WCS structure */
   strcpy (curve_ptr->WCS.curveName, curveName);

   p = strstr(curveName, "_contour");
   if ( p != (char *)NULL ) {
      /* input is contour curve, grab its graph handler */
      str_len = strlen(curve_ptr->WCS.curveName) - strlen(p);
      strncpy(curve_ptr->WCS.graphName, curve_ptr->WCS.curveName, str_len);
      curve_ptr->WCS.graphName[str_len] = '\0';
   }

   if ( curve_ptr->WCS.type[0] == '\0' ) {
      /* for some reason, this has to be done for Windows. */
/*
      curve_ptr->WCS.refPix[0] = 0.0;
      curve_ptr->WCS.refPix[1] = 0.0;
*/
   }

   FillinWCSStructure(&curve_ptr->WCS);

   if ( curve_ptr->WCS.type[0] == '\0' ) {
      const char *WCSstring;

      WCSstring = Tcl_GetVar2(interp, "powWCS", curveName,TCL_GLOBAL_ONLY);
      /* Tcl_SetVar2(interp,"powWCS", curveName, "", TCL_GLOBAL_ONLY); */
   }
   return TCL_OK;
}

int PowWCSInitGraph( PowGraph *graph, char *curves, char *images,
		     int x_points_right, int y_points_up)
{
   PowCurve *current_curve;
   PowImage *current_image;
   int index,Argc;
   const char **Argv;
   char *p;

   graph->WCS.type[0] = '\0';
   graph->xoff = 0.0;
   graph->yoff = 0.0;
   if(images != NULL && strstr(images,"NULL") == NULL ) {

      if(Tcl_SplitList(interp,images,&Argc,&Argv) != TCL_OK) {
	 return TCL_ERROR;
      }
      for( index=0; index<Argc; index++ ) {
	 current_image = PowFindImage( Argv[index] );
	 if( current_image->WCS.type[0] ) {
	    graph->WCS = current_image->WCS;
	    ckfree( (char *)Argv );
	    return TCL_OK;
	 }
      }

      /*  Failed to find a WCS image.  Grab first image's WCS structure
          anyway... It could still contain linear scaling.              */

      graph->WCS = PowFindImage( Argv[0] )->WCS;
      /* wcsini (1, 2, graph->WCS.wcs); */
      p = strstr (images, "imgobj_");
      if ( p != (char *)NULL ) {
         p += strlen("imgobj_");
         strcpy(graph->WCS.graphName, p);
      } else {
         strcpy(graph->WCS.graphName, images);
      }
      strcpy(graph->WCS.curveName, "\0");
      ckfree( (char *)Argv );
      return TCL_OK;
   }

   if(curves != NULL && strstr(curves,"NULL") == NULL ) {

      if(Tcl_SplitList(interp,curves,&Argc,&Argv) != TCL_OK) {
	 return TCL_ERROR;
      }
      for( index=0; index<Argc; index++ ) {
	 current_curve = PowFindCurve( Argv[index] );
	 if( current_curve->WCS.type[0] ) {
	    graph->WCS = current_curve->WCS;
            strcpy(graph->WCS.graphName, "\0");
            strcpy(graph->WCS.curveName, curves);
	    ckfree( (char *)Argv );
	    return TCL_OK;
	 }
      }
      ckfree( (char *)Argv );
   }

   PowInitWCS( &graph->WCS, 2 );
   if( !x_points_right ) {
      graph->WCS.cdFrwd[0][0] = -1.0;
   }
   if( !y_points_up ) {
      graph->WCS.cdFrwd[1][1] = -1.0;
   }

   return TCL_OK;
}

int PowXYPx(ClientData clientData, Tcl_Interp *interp, 
            int argc, Tcl_Obj *const argv[])
{
   /* Calls the pow_xypx WCS routine, returns list of 2 image pixels*/
   double xpix,ypix,xpos,ypos;
   Tcl_Obj *res[2];
   WCSdata WCS;
  
   if(argc < 11 ) {
     Tcl_SetResult( interp,
                    "usage: powXYPx xpos ypos xref yref xrefpix yrefpix "
                    "xinc yinc rot type", TCL_VOLATILE );
     return TCL_ERROR;
   }

   Tcl_GetDoubleFromObj(interp,argv[1],&xpos);
   Tcl_GetDoubleFromObj(interp,argv[2],&ypos);

   PowParseWCS( interp, &WCS, argc-3, argv+3 );

   if( PowPosToPix(xpos,ypos,&WCS,&xpix,&ypix) != 0 ) {
     Tcl_SetResult( interp, "Couldn't translate WCS coords to pixels",
                    TCL_VOLATILE );
     return TCL_ERROR;
   }

   res[0] = Tcl_NewDoubleObj( xpix );
   res[1] = Tcl_NewDoubleObj( ypix );

   Tcl_SetObjResult(interp, Tcl_NewListObj(2, res) );

   return TCL_OK;
}


int PowWorldPos(ClientData clientData, Tcl_Interp *interp, 
                int argc, Tcl_Obj *const argv[])
{
   /* Calls the pow_worldpos WCS routine, returns list of 2 graph coords*/
   double xpix,ypix,xpos,ypos;
   Tcl_Obj *res[2];
   WCSdata WCS;
  
   if(argc < 11 ) {
     Tcl_SetResult( interp,
                    "usage: powWorldPos xpix ypix xref yref xrefpix "
                    "yrefpix xinc yinc rot type", TCL_VOLATILE );
     return TCL_ERROR;
   }

   Tcl_GetDoubleFromObj(interp,argv[1],&xpix);
   Tcl_GetDoubleFromObj(interp,argv[2],&ypix);

   PowParseWCS( interp, &WCS, argc-3, argv+3 );

   if( PowPixToPos(xpix,ypix,&WCS,&xpos,&ypos) != 0 ) {
     Tcl_SetResult( interp, "Couldn't translate pixels to WCS coords",
                    TCL_VOLATILE );
     return TCL_ERROR;
   }

   res[0] = Tcl_NewDoubleObj( xpos );
   res[1] = Tcl_NewDoubleObj( ypos );

   Tcl_SetObjResult(interp, Tcl_NewListObj(2, res) );

   return TCL_OK;
}

/*--------------------------------------------------------------------------*/
int pow_worldpos(double xpix, double ypix,
                 double refVal[], double refPix[],
                 double matrix[][MAX_WCS_DIMS],
                 char *type, double *xpos, double *ypos)

/* PDW 03/00: Add -CAR projection support                                  */
/* PDW 02/00: Change interface to use more general matrix/vector notation  */
/* LEB 11/97: change the name of the routine from 'ffwldp' to 'pow_worldpos' 
              rexmoved 'status' argument, convert to (0,0) based images to
              match POW convention. */
/* WDP 1/97: change the name of the routine from 'worldpos' to 'ffwldp' */

/*  worldpos.c -- WCS Algorithms from Classic AIPS.
    Copyright (C) 1994
    Associated Universities, Inc. Washington DC, USA.
   
    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.
   
    This library 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 Library General Public
    License for more details.
   
    You should have received a copy of the GNU Library General Public License
    along with this library; if not, write to the Free Software Foundation,
    Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
   
    Correspondence concerning AIPS should be addressed as follows:
           Internet email: aipsmail@nrao.edu
           Postal address: AIPS Group
                           National Radio Astronomy Observatory
                           520 Edgemont Road
                           Charlottesville, VA 22903-2475 USA

                 -=-=-=-=-=-=-

    These two ANSI C functions, worldpos() and xypix(), perform
    forward and reverse WCS computations for 8 types of projective
    geometries ("-SIN", "-TAN", "-ARC", "-NCP", "-GLS", "-MER", "-AIT"
    and "-STG"):

        worldpos() converts from pixel location to RA,Dec 
        xypix()    converts from RA,Dec         to pixel location   

    where "(RA,Dec)" are more generically (long,lat). These functions
    are based on the WCS implementation of Classic AIPS, an
    implementation which has been in production use for more than ten
    years. See the two memos by Eric Greisen

        ftp://fits.cv.nrao.edu/fits/documents/wcs/aips27.ps.Z
	ftp://fits.cv.nrao.edu/fits/documents/wcs/aips46.ps.Z

    for descriptions of the 8 projective geometries and the
    algorithms.  Footnotes in these two documents describe the
    differences between these algorithms and the 1993-94 WCS draft
    proposal (see URL below). In particular, these algorithms support
    ordinary field rotation, but not skew geometries (CD or PC matrix
    cases). Also, the MER and AIT algorithms work correctly only for
    CRVALi=(0,0). Users should note that GLS projections with yref!=0
    will behave differently in this code than in the draft WCS
    proposal.  The NCP projection is now obsolete (it is a special
    case of SIN).  WCS syntax and semantics for various advanced
    features is discussed in the draft WCS proposal by Greisen and
    Calabretta at:
    
        ftp://fits.cv.nrao.edu/fits/documents/wcs/wcs.all.ps.Z
    
                -=-=-=-

    The original version of this code was Emailed to D.Wells on
    Friday, 23 September by Bill Cotton <bcotton@gorilla.cv.nrao.edu>,
    who described it as a "..more or less.. exact translation from the
    AIPSish..". Changes were made by Don Wells <dwells@nrao.edu>
    during the period October 11-13, 1994:
    1) added GNU license and header comments
    2) added testpos.c program to perform extensive circularity tests
    3) changed float-->double to get more than 7 significant figures
    4) testpos.c circularity test failed on MER and AIT. B.Cotton
       found that "..there were a couple of lines of code [in] the wrong
       place as a result of merging several Fortran routines." 
    5) testpos.c found 0h wraparound in xypix() and worldpos().
    6) E.Greisen recommended removal of various redundant if-statements,
       and addition of a 360d difference test to MER case of worldpos(). 
*/

/*-----------------------------------------------------------------------*/
/* routine to determine accurate position for pixel coordinates          */
/* returns 0 if successful otherwise:                                    */
/* 1 = angle too large for projection;                                   */
/* (WDP 1/97: changed the return value to 501 instead of 1)              */
/* does: -SIN, -TAN, -ARC, -NCP, -GLS, -MER, -AIT projections            */
/* anything else is linear                                               */
/* Input:                                                                */
/*   f   xpix    x pixel number  (RA or long without rotation)           */
/*   f   ypiy    y pixel number  (dec or lat without rotation)           */
/*   d   xref    x reference coordinate value (deg)                      */
/*   d   yref    y reference coordinate value (deg)                      */
/*   f   xrefpix x reference pixel                                       */
/*   f   yrefpix y reference pixel                                       */
/*   f   xinc    x coordinate increment (deg)                            */
/*   f   yinc    y coordinate increment (deg)                            */
/*   f   rot     rotation (deg)  (from N through E)                      */
/*   c  *type    projection type code e.g. "-SIN";                       */
/* Output:                                                               */
/*   d   *xpos   x (RA) coordinate (deg)                                 */
/*   d   *ypos   y (dec) coordinate (deg)                                */
/*-----------------------------------------------------------------------*/
{
   double dx, dy, dz, x, y, z;
   double sins, coss, dect, rat, dt, l, m, mg, da, dd, cos0, sin0;
   double dec0, ra0;
   double geo1, geo2, geo3;
   double pi    = 3.14159265358979323846;
   double cond2r= pi/180.0;
   double deps = 1.0e-5;
   int itype;

   /*  Apply Transform Matrix  */

   dx = matrix[0][0] * (xpix-refPix[0]) + matrix[0][1] * (ypix-refPix[1]);
   dy = matrix[1][0] * (xpix-refPix[0]) + matrix[1][1] * (ypix-refPix[1]);

   /*  find type  */
   /* WDP 1/97: removed support for default type for better error checking */
   for( itype=0; itype<NUM_WCS_TYPES; itype++ )
      if( !strncmp(type, wcsProjections[itype], 4) ) break;

   /* convert to radians  */
   ra0  = refVal[0] * cond2r;
   dec0 = refVal[1] * cond2r;
   l    =  dx  * cond2r;
   m    =  dy  * cond2r;
   sins = l*l + m*m;
   cos0 = cos(dec0);
   sin0 = sin(dec0);

   /* process by case  */
   switch (itype) {
    case 0:   /* -SIN sin*/ 
      if (sins>1.0) return( 501);
      coss = sqrt (1.0 - sins);
      dt = sin0 * coss + cos0 * m;
      if ((dt>1.0) || (dt<-1.0)) return(501);
      dect = asin(dt);
      rat = cos0 * coss - sin0 * m;
      if ((rat==0.0) && (l==0.0)) return(501);
      rat = atan2(l, rat) + ra0;
      break;
    case 1:   /* -TAN tan */
      x = cos0*cos(ra0) - l*sin(ra0) - m*cos(ra0)*sin0;
      y = cos0*sin(ra0) + l*cos(ra0) - m*sin(ra0)*sin0;
      z = sin0                       + m*         cos0;
      rat  = atan2( y, x );
      dect = atan( z / sqrt(x*x+y*y) );
      break;
    case 2:   /* -ARC Arc*/
      if (sins>=pi*pi) return(501);
      sins = sqrt(sins);
      coss = cos(sins);
      if (sins!=0.0) sins = sin(sins) / sins;
      else
	sins = 1.0;
      dt = m * cos0 * sins + sin0 * coss;
      if ((dt>1.0) || (dt<-1.0)) return(501);
      dect = asin(dt);
      da = coss - dt * sin0;
      dt = l * sins * cos0;
      if ((da==0.0) && (dt==0.0)) return(501);
      rat = ra0 + atan2(dt, da);
      break;
    case 3:   /* -NCP North celestial pole*/
      dect = cos0 - m * sin0;
      if (dect==0.0) return(501);
      rat = ra0 + atan2(l, dect);
      dt = cos(rat-ra0);
      if (dt==0.0) return(501);
      dect = dect / dt;
      if ((dect>1.0) || (dect<-1.0)) return(501);
      dect = acos(dect);
      if (dec0<0.0) dect = -dect;
      break;
    case 4:   /* -GLS global sinusoid */
      dect = dec0 + m;
      if (fabs(dect)>pi/2.0) return(501);
      coss = cos(dect);
      if (fabs(l)>pi*coss) return(501);
      rat = ra0;
      if (coss>deps) rat = rat + l / coss;
      break;
    case 5:   /* -MER mercator */
      /*  dt = yinc * cosr + xinc * sinr;  */
      /* Calculate the declination change for a (1,1) offset from refpix */
      dt = matrix[1][0] + matrix[1][1];
      if (dt==0.0) dt = 1.0;
      dy = (refVal[1]*0.5 + 45.0) * cond2r;
      dx = dy + dt / 2.0 * cond2r;
      dy = log (tan(dy));
      dx = log (tan(dx));
      geo2 = dt * cond2r / (dx - dy);
      geo3 = geo2 * dy;
      geo1 = cos(refVal[1]*cond2r);
      if (geo1<=0.0) geo1 = 1.0;
      rat = l / geo1 + ra0;
      if (fabs(rat - ra0) > pi+pi) return(501);
      dt = 0.0;
      if (geo2!=0.0) dt = (m + geo3) / geo2;
      dt = exp (dt);
      dect = 2.0 * atan(dt) - pi / 2.0;
      break;
    case 6:   /* -AIT Aitoff */
      /*  dt = yinc * cosr + xinc * sinr;  */
      /* Calculate the declination change for a (1,1) offset from refpix */
      dt = matrix[1][0] + matrix[1][1];
      if (dt==0.0) dt = 1.0;
      dt = dt * cond2r;
      dy = dec0;
      dx = sin(dy+dt)/sqrt((1.0+cos(dy+dt))/2.0) -
	  sin(dy)/sqrt((1.0+cos(dy))/2.0);
      if (dx==0.0) dx = 1.0;
      geo2 = dt / dx;
      /* Calculate the right ascension change for a (1,1) offset from refpix */
      /* dt = xinc*cosr - yinc* sinr; */
      dt = matrix[0][0] + matrix[0][1];
      if (dt==0.0) dt = 1.0;
      dt = dt * cond2r;
      dx = 2.0 * cos(dy) * sin(dt/2.0);
      if (dx==0.0) dx = 1.0;
      geo1 = dt * sqrt((1.0+cos(dy)*cos(dt/2.0))/2.0) / dx;
      geo3 = geo2 * sin(dy) / sqrt((1.0+cos(dy))/2.0);
      rat = ra0;
      dect = dec0;
      if ((l==0.0) && (m==0.0)) break;
      dz = 4.0 - l*l/(4.0*geo1*geo1) - ((m+geo3)/geo2)*((m+geo3)/geo2);
      if ((dz>4.0) || (dz<2.0)) return(501);
      dz = 0.5 * sqrt (dz);
      dd = (m+geo3) * dz / geo2;
      if (fabs(dd)>1.0) return(501);
      dd = asin(dd);
      if (fabs(cos(dd))<deps) return(501);
      da = l * dz / (2.0 * geo1 * cos(dd));
      if (fabs(da)>1.0) return(501);
      da = asin(da);
      rat = ra0 + 2.0 * da;
      dect = dd;
      break;
    case 7:   /* -STG Sterographic*/
      dz = (4.0 - sins) / (4.0 + sins);
      if (fabs(dz)>1.0) return(501);
      dect = dz * sin0 + m * cos0 * (1.0+dz) / 2.0;
      if (fabs(dect)>1.0) return(501);
      dect = asin(dect);
      rat = cos(dect);
      if (fabs(rat)<deps) return(501);
      rat = l * (1.0+dz) / (2.0 * rat);
      if (fabs(rat)>1.0) return(501);
      rat = asin(rat);
      mg = 1.0 + sin(dect) * sin0 + cos(dect) * cos0 * cos(rat);
      if (fabs(mg)<deps) return(501);
      mg = 2.0 * (sin(dect) * cos0 - cos(dect) * sin0 * cos(rat)) / mg;
      if (fabs(mg-m)>deps) rat = pi - rat;
      rat = ra0 + rat;
      break;
   case 8:    /*  -CAR Cartesian  */
      rat  = ra0  + l;
      dect = dec0 + m;
      /*  Should do some sperical wrapping, but can't get it to work, yet
      if( dect >  0.5*pi ) { dect =  pi - dect; rat += pi; }
      if( dect < -0.5*pi ) { dect = -pi - dect; rat += pi; }
      */ 
      break;

    default:
      /* fall through to here on error */
      return(504);
   }

   /*  return ra in range  */
   /*  Oh let's not.  LEB */
   /*
     if (rat-ra0> pi) rat -= pi + pi;
     if (rat-ra0<-pi) rat += pi + pi;
     if (rat < 0.0)   rat += pi + pi; // added by DCW 10/12/94
   */

   /*  correct units back to degrees  */
   *xpos  = rat  / cond2r;
   *ypos  = dect / cond2r;

   /*  Do bounds check in degree space since values are exact  */
   if     ( *xpos <    0.0 )
      *xpos += 360.0;
   else if( *xpos >= 360.0 )
      *xpos -= 360.0;

   return 0;
}  /* End of worldpos */


/*--------------------------------------------------------------------------*/
int pow_xypx(double xpos, double ypos,
             double refVal[], double refPix[],
             double matrixF[][MAX_WCS_DIMS],
             double matrixR[][MAX_WCS_DIMS],
             char *type, double *xpix, double *ypix)
/* PDW 03/00: Add -CAR projection support                                  */
/* PDW 02/00: Change interface to use more general matrix/vector notation  */
/* LEB 11/97: change the name of the routine from 'ffxypx' to 'pow_xypx'
   removed 'status' argument, convert to (0,0) based images to match POW 
   convention. */
/* WDP  1/97: changed name of routine from xypix to ffxypx    */
/*-----------------------------------------------------------------------*/
/* routine to determine accurate pixel coordinates for an RA and Dec     */
/* returns 0 if successful otherwise:                                    */
/* 1 = angle too large for projection;                                   */
/* 2 = bad values                                                        */
/* WDP 1/97: changed the return values to 501 and 502 instead of 1 and 2 */
/* does: -SIN, -TAN, -ARC, -NCP, -GLS, -MER, -AIT projections            */
/* anything else is linear                                               */
/* Input:                                                                */
/*   d   xpos    x (RA) coordinate (deg)                                 */
/*   d   ypos    y (dec) coordinate (deg)                                */
/*   d   xref    x reference coordinate value (deg)                      */
/*   d   yref    y reference coordinate value (deg)                      */
/*   f   xrefpix x reference pixel                                       */
/*   f   yrefpix y reference pixel                                       */
/*   f   xinc    x coordinate increment (deg)                            */
/*   f   yinc    y coordinate increment (deg)                            */
/*   f   rot     rotation (deg)  (from N through E)                      */
/*   c  *type    projection type code e.g. "-SIN";                       */
/* Output:                                                               */
/*   f  *xpix    x pixel number  (RA or long without rotation)           */
/*   f  *ypiy    y pixel number  (dec or lat without rotation)           */
/*-----------------------------------------------------------------------*/
{
   double dx, dy, ra0, dec0, ra, dec, coss, sins, dt, da, dd, sint;
   double cos0, sin0, dRA;
   double l, m, geo1, geo2, geo3;
   double deps=1.0e-5;
   double pi    = 3.14159265358979323846;
   double cond2r= pi/180.0;
   int itype;

   /*  find type  */
   /* WDP 1/97: removed support for default type for better error checking */
   for( itype=0; itype<NUM_WCS_TYPES; itype++ )
      if (!strncmp(type, wcsProjections[itype], 4)) break;

   if ( xpos < 0.0 )
      xpos += 360.0;

   /* Non linear position */
   ra0  = refVal[0] * cond2r;
   dec0 = refVal[1] * cond2r;
   ra   = xpos * cond2r;
   dec  = ypos * cond2r;

   dRA  = ra-ra0;
   if(      dRA >   pi ) dRA -= pi + pi;
   else if( dRA <= -pi ) dRA += pi + pi;

   /* compute direction cosine */
   coss = cos(dec);
   sins = sin(dec);
   cos0 = cos(dec0);
   sin0 = sin(dec0);
   l = sin(dRA) * coss;
   sint = sins * sin0 + coss * cos0 * cos(dRA);

   /* process by case  */
   switch (itype) {
    case 0:   /* -SIN sin*/ 
         if (sint<0.0) return(501);
         m = sins * cos(dec0) - coss * sin(dec0) * cos(dRA);
      break;
    case 1:   /* -TAN tan */
         if (sint<=0.0) return(501);
         if( cos0<0.001 ) {
            /* Do a first order expansion around pole */
            m = (coss * cos(dRA)) / (sins * sin0);
            m = (-m + cos0 * (1.0 + m*m)) / sin0;
         } else {
            m = ( sins/sint - sin0 ) / cos0;
         }
	 if( fabs(sin(ra0)) < 0.3 ) {
	    l  = coss*sin(ra)/sint - cos0*sin(ra0) + m*sin(ra0)*sin0;
	    l /= cos(ra0);
	 } else {
	    l  = coss*cos(ra)/sint - cos0*cos(ra0) + m*cos(ra0)*sin0;
	    l /= -sin(ra0);
	 }
      break;
    case 2:   /* -ARC Arc*/
         m = sins * sin(dec0) + coss * cos(dec0) * cos(dRA);
         if (m<-1.0) m = -1.0;
         if (m>1.0) m = 1.0;
         m = acos(m);
         if (m!=0) 
            m = m / sin(m);
         else
            m = 1.0;
         l = l * m;
         m = (sins * cos(dec0) - coss * sin(dec0) * cos(dRA)) * m;
      break;
    case 3:   /* -NCP North celestial pole*/
         if (dec0==0.0) 
	     return(501);  /* can't stand the equator */
         else
	   m = (cos(dec0) - coss * cos(dRA)) / sin(dec0);
      break;
    case 4:   /* -GLS global sinusoid */
         if (fabs(dec) >pi*0.5) return(501);
         if (fabs(dec0)>pi*0.5) return(501);
         m = dec - dec0;
         l = dRA * coss;
      break;
    case 5:   /* -MER mercator */
         /*  dt = yinc * cosr + xinc * sinr;  */
         /* Calculate the declination change for a (1,1) offset from refpix */
         dt = matrixF[1][0] + matrixF[1][1];
         if (dt==0.0) dt = 1.0;
         dy = (dec0 + 90.0 * cond2r) * 0.5;
         dx = dy + (dt * 0.5) * cond2r;
         dy = log (tan(dy));
         dx = log (tan(dx));
         geo2 = dt * cond2r / (dx - dy);
         geo3 = geo2 * dy;
         l  = dRA * cos(dec0);
         dt = dec * 0.5 + pi * 0.25;
         dt = tan(dt);
         if (dt<deps) return(502);
         m = geo2 * log (dt) - geo3;
         break;
    case 6:   /* -AIT Aitoff */
         da = 0.5 * dRA;
         if (fabs(dRA)>pi) return(501);
         /* Calculate the declination change for a (1,1) offset from refpix */
         /*  dt = yinc * cosr + xinc * sinr;  */
         dt = matrixF[1][0] + matrixF[1][1];
         if (dt==0.0) dt = 1.0;
         dt = dt * cond2r;
         dy = dec0;
         dx = sin(dy+dt)/sqrt((1.0+cos(dy+dt))/2.0) -
             sin(dy)/sqrt((1.0+cos(dy))/2.0);
         if (dx==0.0) dx = 1.0;
         geo2 = dt / dx;
         /* Calculate the RA change for a (1,1) offset from refpix */
         /* dt = xinc*cosr - yinc* sinr; */
         dt = matrixF[0][0] + matrixF[0][1];
         if (dt==0.0) dt = 1.0;
         dt = dt * cond2r;
         dx = 2.0 * cos(dy) * sin(dt/2.0);
         if (dx==0.0) dx = 1.0;
         geo1 = dt * sqrt((1.0+cos(dy)*cos(dt/2.0))/2.0) / dx;
         geo3 = geo2 * sin(dy) / sqrt((1.0+cos(dy))/2.0);
         dt = sqrt ((1.0 + cos(dec) * cos(da))/2.0);
         if (fabs(dt)<deps) return(503);
         l = 2.0 * geo1 * cos(dec) * sin(da) / dt;
         m = geo2 * sin(dec) / dt - geo3;
      break;
    case 7:   /* -STG Sterographic*/
         if (fabs(dec)>0.5*pi) return(501);
         dd = 1.0 + sins * sin(dec0) + coss * cos(dec0) * cos(dRA);
         if (fabs(dd)<deps) return(501);
         dd = 2.0 / dd;
         l = l * dd;
         m = dd * (sins * cos(dec0) - coss * sin(dec0) * cos(dRA));
      break;
   case 8:    /* -CAR Cartesian  */
      /* l = ra - ra0; */
      l = dRA;
      m = dec - dec0;
      break;

    default:
      /* fall through to here on error */
      return(504);

   }  /* end of itype switch */

   /*   back to degrees  */
   dx = l / cond2r;
   dy = m / cond2r;

   /*  Apply Transform Matrix  */

   *xpix = matrixR[0][0] * dx + matrixR[0][1] * dy + refPix[0];
   *ypix = matrixR[1][0] * dx + matrixR[1][1] * dy + refPix[1];

   return 0;
}  /* end xypix */