Debian Codegraph @

MontageLib/MakeHdr/montageMakeHdr.c

raw symbols SCIP
1 /* Module: mMakeHdr.c
2
3 Version Developer Date Change
4 ------- --------------- ------- -----------------------
5 3.1 John Good 22Sep15 Add full cube info to template header
6 3.0 John Good 29Jul15 Check for cube columns and do a little analysis
7 2.7 John Good 27Sep12 Add max pixel size capability
8 2.6 John Good 20Aug07 Add 'table of tables' capability
9 2.5 John Good 07Aug07 Added check for "allsky" (large area) data
10 2.4 John Good 09Jul06 Exit properly when a bad WCS encountered
11 2.3 John Good 22Feb06 Removed extra (repeated) header lines
12 2.2 John Good 17Nov05 Added flag (-p) to externally set pixel scale
13 2.1 John Good 30Nov04 Forgot to check for pixel scale when
14 using four corners (or lat,lon).
15 2.0 John Good 16Aug04 Added code to alternately check:
16 four corners (equatorial) or four corners
17 (arbitrary with system in header) or
18 just a set of ra, dec or just a set of
19 lon, lat (with system in header)
20 1.9 John Good 10Aug04 Added four corners of region to output
21 1.8 John Good 09Jan04 Fixed realloc() bug for 'lats'
22 1.7 John Good 25Nov03 Added extern optarg references
23 1.6 John Good 01Oct03 Add check for naxis1, naxis2 columns
24 in addition to ns, nl
25 1.5 John Good 25Aug03 Added status file processing
26 1.4 John Good 27Jun03 Added a few comments for clarity
27 1.3 John Good 09Apr03 Removed unused variable offscl
28 1.2 John Good 22Mar03 Renamed wcsCheck to checkWCS
29 for consistency. Checking system
30 and equinox strings on command line
31 for validity
32 1.1 John Good 13Mar03 Added WCS header check and
33 modified command-line processing
34 to use getopt() library. Check for
35 missing/invalid images.tbl. Check
36 for valid equinox and propogate to
37 output header.
38 1.0 John Good 29Jan03 Baseline code
39
40 */
41
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <string.h>
46 #include <strings.h>
47 #include <ctype.h>
48 #include <math.h>
49 #include <mtbl.h>
50 #include <fitsio.h>
51 #include <coord.h>
52 #include <wcs.h>
53 #include <boundaries.h>
54
55 #include <mMakeHdr.h>
56 #include <montage.h>
57
58 #define MAXSTR 4096
59 #define MAXFILES 16
60 #define MAXCOORD 4096
61
62 #define UNKNOWN 0
63 #define FOURCORNERS 1
64 #define WCS 2
65 #define LONLAT 3
66
67
68 struct WorldCoor *outwcs;
69
70
71 /* Basic image WCS information */
72 /* (from the FITS header and as */
73 /* returned from the WCS library) */
74
75 struct ImgInfo
76 {
77 struct WorldCoor *wcs;
78 int sys;
79 int equinox;
80 double epoch;
81 char ctype1[16];
82 char ctype2[16];
83 int naxis1;
84 int naxis2;
85 double crpix1;
86 double crpix2;
87 double crval1;
88 double crval2;
89 double cdelt1;
90 double cdelt2;
91 double crota2;
92 }
93 input;
94
95 int mMakeHdr_debug;
96
97
98 static char montage_msgstr[1024];
99 static char montage_json [1024];
100
101
102 /*-***********************************************************************/
103 /* */
104 /* mMakeHdr */
105 /* */
106 /* Create the best header 'bounding' a table or set of tables (each */
107 /* with image metadata or point sources). */
108 /* */
109 /* char *tblfile Input image metadata table or source table */
110 /* or table of tables */
111 /* */
112 /* char *template Output image header template */
113 /* */
114 /* char *csys Coordinate system (e.g. 'EquJ', 'Galactic'). */
115 /* Fairly forgiving */
116 /* */
117 /* double equinox Coordinate system equinox (e.g. 2000.0) */
118 /* */
119 /* double pixelScale Pixel scale in degrees */
120 /* */
121 /* int northAligned Defaults to minimum bounding box around */
122 /* input images. This forces template to be */
123 /* north-aligned */
124 /* */
125 /* double pad Optional extra padding around output template */
126 /* */
127 /* int isPercentage Pad is in pixels by default. This changes */
128 /* that to a percentage of the image size */
129 /* */
130 /* int maxPixel Setting the pixel scale can result in really */
131 /* big images. This forces a maximum number */
132 /* of pixels in NAXIS1, NAXIS2 */
133 /* */
134 /* int debug Debugging output level */
135 /* */
136 /*************************************************************************/
137
138 struct mMakeHdrReturn *mMakeHdr(char *tblfile, char *template, char *csysin, double equinox, double pixelScale,
139 int northAligned, double pad, int isPercentage, int maxPixel, int debugin)
140 {
141 int i, stat, ncols, nimages, ntables, maxfiles;
142 int naxis1, naxis2, datatype, nCube;
143 int have3d, have4d;
144
145 int sys, ifiles;
146 double val;
147
148 int itable;
149
150 int inaxis;
151
152 int ictype1;
153 int ictype2;
154 int iequinox;
155 int inl;
156 int ins;
157 int icrval1;
158 int icrval2;
159 int icrpix1;
160 int icrpix2;
161 int icdelt1;
162 int icdelt2;
163 int icrota2;
164
165 int ilon1, ilat1;
166 int ilon2, ilat2;
167 int ilon3, ilat3;
168 int ilon4, ilat4;
169
170 int inaxis3, inaxis4;
171 int icrval3, icrval4;
172 int icrpix3, icrpix4;
173 int icdelt3, icdelt4;
174
175 char epochStr [MAXSTR];
176 char csysStr [MAXSTR];
177 char csys [MAXSTR];
178 char msg [MAXSTR];
179 char tfile [MAXSTR];
180
181 char *checkWCS;
182
183 double xpos, ypos;
184 double lon, lat;
185
186 double x, y, z;
187 double xmin, ymin, zmin;
188 double xmax, ymax, zmax;
189
190 int naxis, colNaxis;
191
192 int naxis3, colNaxis3;
193 double crval3, colCrval3;
194 double crpix3, colCrpix3;
195 double cdelt3, colCdelt3;
196
197 int naxis4, colNaxis4;
198 double crval4, colCrval4;
199 double crpix4, colCrpix4;
200 double cdelt4, colCdelt4;
201
202 double minpix, maxpix;
203 double colMinpix, colMaxpix;
204
205 double minCdelt = 360.;
206
207 char header[1600];
208 char temp[80];
209
210 char *keyval;
211 char refsys;
212
213 double *lons, *lats;
214 int maxcoords, ncoords;
215
216 double lonc[4], latc[4];
217
218 struct bndInfo *box = (struct bndInfo *)NULL;
219
220 FILE *fout;
221
222 char **fnames;
223
224 double dtr;
225
226 struct mMakeHdrReturn *returnStruct;
227
228
229 dtr = atan(1.)/45.;
230
231 maxfiles = MAXFILES;
232
233
234 /*******************************/
235 /* Initialize return structure */
236 /*******************************/
237
238 returnStruct = (struct mMakeHdrReturn *)malloc(sizeof(struct mMakeHdrReturn));
239
240 memset((void *)returnStruct, 0, sizeof(returnStruct));
241
242
243 returnStruct->status = 1;
244
245 strcpy(returnStruct->msg, "");
246
247
248 /**************************/
249 /* Initialize data arrays */
250 /**************************/
251
252 fnames = (char **)malloc(maxfiles * sizeof(char *));
253
254 for(i=0; i<maxfiles; ++i)
255 fnames[i] = (char *)malloc(MAXSTR * sizeof(char));
256
257 lons = (double *)malloc(MAXCOORD * sizeof(double));
258 lats = (double *)malloc(MAXCOORD * sizeof(double));
259
260 if(lats == (double *)NULL)
261 {
262 sprintf(returnStruct->msg, "Memory allocation failure.");
263 return returnStruct;
264 }
265
266 maxcoords = MAXCOORD;
267 ncoords = 0;
268
269
270 /***************************************/
271 /* Process the command-line parameters */
272 /***************************************/
273
274 mMakeHdr_debug = debugin;
275
276 strcpy(csys, csysin);
277
278 if(strcasecmp(csys, "eq") == 0) sys = EQUJ;
279 else if(strcasecmp(csys, "equ") == 0) sys = EQUJ;
280 else if(strcasecmp(csys, "equatorial") == 0) sys = EQUJ;
281 else if(strcasecmp(csys, "equj") == 0) sys = EQUJ;
282 else if(strcasecmp(csys, "equb") == 0) sys = EQUB;
283 else if(strcasecmp(csys, "ec") == 0) sys = ECLJ;
284 else if(strcasecmp(csys, "ecl") == 0) sys = ECLJ;
285 else if(strcasecmp(csys, "ecliptic") == 0) sys = ECLJ;
286 else if(strcasecmp(csys, "eclj") == 0) sys = ECLJ;
287 else if(strcasecmp(csys, "eclb") == 0) sys = ECLB;
288 else if(strcasecmp(csys, "ga") == 0) sys = GAL;
289 else if(strcasecmp(csys, "gal") == 0) sys = GAL;
290 else if(strcasecmp(csys, "galactic") == 0) sys = GAL;
291 else if(strcasecmp(csys, "sgal") == 0) sys = SGAL;
292 else if(strcasecmp(csys, "supergalactic") == 0) sys = SGAL;
293 else
294 {
295 sprintf(returnStruct->msg, "Invalid system string. Must be EQUJ|EQUB|ECLJ|ECLB|GAL|SGAL");
296 return returnStruct;
297 }
298
299 if(equinox == 0. && (sys == EQUB || sys == ECLB))
300 equinox = 1950.;
301
302 fout = fopen(template, "w+");
303
304 if(fout == (FILE *)NULL)
305 {
306 sprintf(returnStruct->msg, "Can't open output header file.");
307 return returnStruct;
308 }
309
310
311 /****************************/
312 /* Open the list table file */
313 /* This may be a list of */
314 /* tables, in which case */
315 /* read them in. */
316 /****************************/
317
318 ntables = 0;
319
320 ncols = topen(tblfile);
321
322 if(ncols <= 0)
323 {
324 sprintf(returnStruct->msg, "Invalid table file: %s", tblfile);
325 return returnStruct;
326 }
327
328 itable = tcol("table");
329
330 if(itable < 0)
331 {
332 strcpy(fnames[0], tblfile);
333
334 ntables = 1;
335 }
336
337 else
338 {
339 while(1)
340 {
341 stat = tread();
342
343 if(stat < 0)
344 break;
345
346 strcpy(fnames[ntables], tval(itable));
347
348 ++ntables;
349
350 if(ntables >= maxfiles)
351 {
352 maxfiles += MAXFILES;
353
354 fnames = (char **)malloc(maxfiles * sizeof(char *));
355
356 for(i=maxfiles-MAXFILES; i<maxfiles; ++i)
357 fnames[i] = (char *)malloc(MAXSTR * sizeof(char));
358 }
359 }
360 }
361
362 tclose();
363
364
365 /*********************************************/
366 /* Loop over the set of image metadata files */
367 /*********************************************/
368
369 nimages = 0;
370 nCube = 0;
371
372 naxis = 2;
373
374 naxis3 = 0;
375 crval3 = 0.;
376 crpix3 = 0.;
377
378 naxis4 = 0;
379 crval4 = 0.;
380 crpix4 = 0.;
381
382 have3d = 1;
383 have4d = 1;
384
385 for(ifiles=0; ifiles<ntables; ++ifiles)
386 {
387 strcpy(tfile, fnames[ifiles]);
388
389 if(mMakeHdr_debug >= 1)
390 {
391 printf("Table file %d: [%s]\n", ifiles, tfile);
392 fflush(stdout);
393 }
394
395 /**********************************/
396 /* Open the image list table file */
397 /**********************************/
398 ncols = topen(tfile);
399
400 if(ncols <= 0)
401 {
402 for(ifiles=0; ifiles<ntables; ++ifiles)
403 free(fnames[ifiles]);
404
405 free(fnames);
406
407 sprintf(returnStruct->msg, "Invalid image metadata file: %s", tfile);
408 return returnStruct;
409 }
410
411 inaxis = tcol("naxis");
412
413
414 /* Check for "cube" parameters */
415
416 inaxis3 = tcol("naxis3");
417 icrval3 = tcol("crval3");
418 icrpix3 = tcol("crpix3");
419 icdelt3 = tcol("cdelt3");
420
421 inaxis4 = tcol("naxis4");
422 icrval4 = tcol("crval4");
423 icrpix4 = tcol("crpix4");
424 icdelt4 = tcol("cdelt4");
425
426
427
428 /* First check to see if we have four equatorial corners */
429
430 icdelt1 = tcol("cdelt1");
431 icdelt2 = tcol("cdelt2");
432
433 datatype = UNKNOWN;
434
435 ilon1 = tcol("ra1");
436 ilon2 = tcol("ra2");
437 ilon3 = tcol("ra3");
438 ilon4 = tcol("ra4");
439
440 ilat1 = tcol("dec1");
441 ilat2 = tcol("dec2");
442 ilat3 = tcol("dec3");
443 ilat4 = tcol("dec4");
444
445 if(ilon1 >= 0
446 && ilon2 >= 0
447 && ilon3 >= 0
448 && ilon4 >= 0
449 && ilat1 >= 0
450 && ilat2 >= 0
451 && ilat3 >= 0
452 && ilat4 >= 0)
453 datatype = FOURCORNERS;
454
455
456 /* Then check for generic lon, lat corners */
457
458 if(datatype == UNKNOWN)
459 {
460 ilon1 = tcol("lon1");
461 ilon2 = tcol("lon2");
462 ilon3 = tcol("lon3");
463 ilon4 = tcol("lon4");
464
465 ilat1 = tcol("lat1");
466 ilat2 = tcol("lat2");
467 ilat3 = tcol("lat3");
468 ilat4 = tcol("lat4");
469
470 if(ilon1 >= 0
471 && ilon2 >= 0
472 && ilon3 >= 0
473 && ilon4 >= 0
474 && ilat1 >= 0
475 && ilat2 >= 0
476 && ilat3 >= 0
477 && ilat4 >= 0)
478 datatype = FOURCORNERS;
479 }
480
481
482 /* Then check for full WCS */
483
484 if(datatype == UNKNOWN)
485 {
486 ictype1 = tcol("ctype1");
487 ictype2 = tcol("ctype2");
488 iequinox = tcol("equinox");
489 inl = tcol("nl");
490 ins = tcol("ns");
491 icrval1 = tcol("crval1");
492 icrval2 = tcol("crval2");
493 icrpix1 = tcol("crpix1");
494 icrpix2 = tcol("crpix2");
495 icdelt1 = tcol("cdelt1");
496 icdelt2 = tcol("cdelt2");
497 icrota2 = tcol("crota2");
498
499 if(ins < 0)
500 ins = tcol("naxis1");
501
502 if(inl < 0)
503 inl = tcol("naxis2");
504
505 if(ictype1 >= 0
506 && ictype2 >= 0
507 && inl >= 0
508 && ins >= 0
509 && icrval1 >= 0
510 && icrval2 >= 0
511 && icrpix1 >= 0
512 && icrpix2 >= 0
513 && icdelt1 >= 0
514 && icdelt2 >= 0
515 && icrota2 >= 0)
516 datatype = WCS;
517 }
518
519
520 /* And finally settle for just (ra,dec) or (lon,lat) columns */
521
522 if(datatype == UNKNOWN)
523 {
524 ilon1 = tcol("ra");
525 ilat1 = tcol("dec");
526
527 if(ilon1 >= 0
528 && ilat1 >= 0)
529 datatype = LONLAT;
530 }
531
532 if(datatype == UNKNOWN)
533 {
534 ilon1 = tcol("lon");
535 ilat1 = tcol("lat");
536
537 if(ilon1 >= 0
538 && ilat1 >= 0)
539 datatype = LONLAT;
540 }
541
542 if(datatype == UNKNOWN)
543 {
544 ilon1 = tcol("crval1");
545 ilat1 = tcol("crval2");
546
547 if(ilon1 >= 0
548 && ilat1 >= 0)
549 datatype = LONLAT;
550 }
551
552
553 /* If we found none of the above, give up */
554
555 if(datatype == UNKNOWN)
556 {
557 for(ifiles=0; ifiles<ntables; ++ifiles)
558 free(fnames[ifiles]);
559
560 free(fnames);
561
562 sprintf(returnStruct->msg, "Need columns: ctype1 ctype2 nl ns crval1 crval2 crpix1 crpix2 cdelt1 cdelt2 crota2 (equinox optional). Four corners (equatorial) will be used if they exist or even just a single set of coordinates");
563 return returnStruct;
564 }
565
566
567
568 /**************************************************/
569 /* Try to determine coordinate system and equinox */
570 /**************************************************/
571
572 input.sys = EQUJ;
573 input.epoch = 2000.;
574
575
576 /* Equinox */
577
578 keyval = tfindkey("EQUINOX");
579
580 if(keyval != (char *)NULL)
581 strcpy(epochStr, keyval);
582
583 keyval = tfindkey("EPOCH");
584
585 if(keyval != (char *)NULL)
586 strcpy(epochStr, keyval);
587
588 keyval = tfindkey("equinox");
589
590 if(keyval != (char *)NULL)
591 strcpy(epochStr, keyval);
592
593 keyval = tfindkey("epoch");
594
595 if(keyval != (char *)NULL)
596 strcpy(epochStr, keyval);
597
598
599 /* Coordinate system */
600
601 keyval = tfindkey("CSYS");
602
603 if(keyval != (char *)NULL)
604 strcpy(csysStr, keyval);
605
606 keyval = tfindkey("SYSTEM");
607
608 if(keyval != (char *)NULL)
609 strcpy(csysStr, keyval);
610
611 keyval = tfindkey("SYS");
612
613 if(keyval != (char *)NULL)
614 strcpy(csysStr, keyval);
615
616 keyval = tfindkey("COORD");
617
618 if(keyval != (char *)NULL)
619 strcpy(csysStr, keyval);
620
621 keyval = tfindkey("COORDSYS");
622
623 if(keyval != (char *)NULL)
624 strcpy(csysStr, keyval);
625
626 keyval = tfindkey("csys");
627
628 if(keyval != (char *)NULL)
629 strcpy(csysStr, keyval);
630
631 keyval = tfindkey("system");
632
633 if(keyval != (char *)NULL)
634 strcpy(csysStr, keyval);
635
636 keyval = tfindkey("sys");
637
638 if(keyval != (char *)NULL)
639 strcpy(csysStr, keyval);
640
641 keyval = tfindkey("coord");
642
643 if(keyval != (char *)NULL)
644 strcpy(csysStr, keyval);
645
646 keyval = tfindkey("coordsys");
647
648 if(keyval != (char *)NULL)
649 strcpy(csysStr, keyval);
650
651 for(i=0; i<strlen(csysStr); ++i)
652 csysStr[i] = tolower(csysStr[i]);
653
654 if(epochStr[0] == 'j'
655 || epochStr[0] == 'J')
656 {
657 refsys = 'j';
658 input.epoch = atof(epochStr+1);
659 }
660 else if(epochStr[0] == 'b'
661 || epochStr[0] == 'B')
662 {
663 refsys = 'b';
664 input.epoch = atof(epochStr+1);
665 }
666 else
667 {
668 refsys = 'j';
669 input.epoch = atof(epochStr);
670
671 if(input.epoch == 0.)
672 input.epoch = 2000.;
673 }
674
675 if(csysStr[strlen(csysStr)-1] == 'j')
676 refsys = 'j';
677 else if(csysStr[strlen(csysStr)-1] == 'j')
678 refsys = 'b';
679
680 if(strncmp(csysStr, "eq", 2) == 0 && refsys == 'j') input.sys = EQUJ;
681 if(strncmp(csysStr, "ec", 2) == 0 && refsys == 'j') input.sys = ECLJ;
682 if(strncmp(csysStr, "eq", 2) == 0 && refsys == 'b') input.sys = EQUB;
683 if(strncmp(csysStr, "ec", 2) == 0 && refsys == 'b') input.sys = ECLB;
684
685 if(strncmp(csysStr, "ga", 2) == 0) input.sys = GAL;
686 if(strncmp(csysStr, "sg", 2) == 0) input.sys = SGAL;
687 if(strncmp(csysStr, "su", 2) == 0) input.sys = SGAL;
688
689
690
691 /**************************************************/
692 /* Read the records and collect the image corners */
693 /**************************************************/
694
695 if(inaxis3 < 0
696 || icrval3 < 0
697 || icrpix3 < 0
698 || icdelt3 < 0)
699 have3d = 0;
700
701 if(inaxis4 < 0
702 || icrval4 < 0
703 || icrpix4 < 0
704 || icdelt4 < 0)
705 have4d = 0;
706
707
708 while(1)
709 {
710 stat = tread();
711
712 if(stat < 0)
713 break;
714
715 ++nimages;
716
717 if(inaxis >= 0)
718 {
719 if(tnull(inaxis))
720 naxis = -1; // Empty naxis value means we can't do cube
721 else
722 {
723 colNaxis = atoi(tval(inaxis));
724
725 if(nimages == 1)
726 naxis = colNaxis; // First naxis value becomes our reference
727 else
728 {
729 if(colNaxis == 2)
730 naxis = -1; // An naxis value of 2 means not a cube
731 else if(colNaxis != naxis)
732 naxis = -1; // If we don't match reference, we can't do cube
733 }
734
735 if(colNaxis > 2)
736 {
737 ++nCube;
738
739 if(naxis > 2 && have3d) // Now we check for cube parameter consistency
740 {
741 if(tnull(inaxis3)
742 || tnull(icrval3)
743 || tnull(icrpix3)
744 || tnull(icdelt3))
745 naxis = -1;
746
747 else
748 {
749 colNaxis3 = atoi(tval(inaxis3));
750 colCrval3 = atof(tval(icrval3));
751 colCrpix3 = atof(tval(icrpix3));
752 colCdelt3 = atof(tval(icdelt3));
753 }
754 }
755
756 if(naxis > 3 && have4d)
757 {
758 if(tnull(inaxis4)
759 || tnull(icrval4)
760 || tnull(icrpix4)
761 || tnull(icdelt4))
762 naxis = -1;
763
764 else
765 {
766 colNaxis4 = atoi(tval(inaxis4));
767 colCrval4 = atof(tval(icrval4));
768 colCrpix4 = atof(tval(icrpix4));
769 colCdelt4 = atof(tval(icdelt4));
770 }
771 }
772
773 if(nimages == 1)
774 {
775 naxis3 = colNaxis3; // Taking the first values as reference
776 crval3 = colCrval3;
777 crpix3 = colCrpix3;
778 cdelt3 = colCdelt3;
779
780 naxis4 = colNaxis4;
781 crval4 = colCrval4;
782 crpix4 = colCrpix4;
783 cdelt4 = colCdelt4;
784 }
785
786 if(naxis > 2 && !have3d) // If it says there are 3 dimensions but
787 naxis = -1; // we don't have the columns, give up
788
789 if(have3d)
790 {
791 if(colCrval3 != crval3) // CRVAL and CDELT must agree
792 naxis = -1;
793
794 if(colCdelt3 != cdelt3)
795 naxis = -1;
796
797 minpix = -crpix3; // We use the columns CRPIX3 and NAXIS3
798 maxpix = -crpix3 + naxis3; // to adjust the overall range (global
799 // CRPIX4 and NAXIS3)
800 colMinpix = -colCrpix3;
801 colMaxpix = -colCrpix3 + colNaxis3;
802
803 if(colMinpix < minpix) minpix = colMinpix;
804 if(colMaxpix > maxpix) maxpix = colMaxpix;
805
806 crpix3 = -minpix;
807 naxis3 = (int)(maxpix + crpix3 + 0.00001);
808 }
809
810 if(naxis > 3 && !have4d) // Ditto for the fourth dimension
811 naxis = -1;
812
813 if(naxis > 3 && have4d)
814 {
815 if(colCrval4 != crval4)
816 naxis = -1;
817
818 if(colCdelt4 != cdelt4)
819 naxis = -1;
820
821 minpix = -crpix4;
822 maxpix = -crpix4 + naxis4;
823
824 colMinpix = -colCrpix4;
825 colMaxpix = -colCrpix4 + colNaxis4;
826
827 if(colMinpix < minpix) minpix = colMinpix;
828 if(colMaxpix > maxpix) maxpix = colMaxpix;
829
830 crpix4 = -minpix;
831 naxis4 = (int)(maxpix + crpix4 + 0.00001);
832 }
833 }
834 }
835 }
836
837 if(datatype == LONLAT)
838 {
839 xpos = atof(tval(ilon1));
840 ypos = atof(tval(ilat1));
841
842 convertCoordinates(input.sys, input.epoch, xpos, ypos,
843 sys, equinox, &lon, &lat, 0.0);
844
845 lons[ncoords] = lon;
846 lats[ncoords] = lat;
847
848 ++ncoords;
849
850 if(icdelt1 >= 0)
851 {
852 val = fabs(atof(tval(icdelt1)));
853
854 if(val < minCdelt)
855 minCdelt = val;
856 }
857
858 if(icdelt2 >= 0)
859 {
860 val = fabs(atof(tval(icdelt2)));
861
862 if(val < minCdelt)
863 minCdelt = val;
864 }
865 }
866 if(datatype == FOURCORNERS)
867 {
868 xpos = atof(tval(ilon1));
869 ypos = atof(tval(ilat1));
870
871 convertCoordinates(input.sys, input.epoch, xpos, ypos,
872 sys, equinox, &lon, &lat, 0.0);
873
874 lons[ncoords] = lon;
875 lats[ncoords] = lat;
876
877 ++ncoords;
878
879 xpos = atof(tval(ilon2));
880 ypos = atof(tval(ilat2));
881
882 convertCoordinates(input.sys, input.epoch, xpos, ypos,
883 sys, equinox, &lon, &lat, 0.0);
884
885 lons[ncoords] = lon;
886 lats[ncoords] = lat;
887
888 ++ncoords;
889
890 xpos = atof(tval(ilon3));
891 ypos = atof(tval(ilat3));
892
893 convertCoordinates(input.sys, input.epoch, xpos, ypos,
894 sys, equinox, &lon, &lat, 0.0);
895
896 lons[ncoords] = lon;
897 lats[ncoords] = lat;
898
899 ++ncoords;
900
901 xpos = atof(tval(ilon4));
902 ypos = atof(tval(ilat4));
903
904 convertCoordinates(input.sys, input.epoch, xpos, ypos,
905 sys, equinox, &lon, &lat, 0.0);
906
907 lons[ncoords] = lon;
908 lats[ncoords] = lat;
909
910 ++ncoords;
911
912 if(icdelt1 >= 0)
913 {
914 val = fabs(atof(tval(icdelt1)));
915
916 if(val < minCdelt)
917 minCdelt = val;
918 }
919
920 if(icdelt2 >= 0)
921 {
922 val = fabs(atof(tval(icdelt2)));
923
924 if(val < minCdelt)
925 minCdelt = val;
926 }
927 }
928 else if(datatype == WCS)
929 {
930 strcpy(input.ctype1, tval(ictype1));
931 strcpy(input.ctype2, tval(ictype2));
932
933 input.naxis1 = atoi(tval(ins));
934 input.naxis2 = atoi(tval(inl));
935 input.crpix1 = atof(tval(icrpix1));
936 input.crpix2 = atof(tval(icrpix2));
937 input.crval1 = atof(tval(icrval1));
938 input.crval2 = atof(tval(icrval2));
939 input.cdelt1 = atof(tval(icdelt1));
940 input.cdelt2 = atof(tval(icdelt2));
941 input.crota2 = atof(tval(icrota2));
942 input.equinox = 2000;
943
944 if(iequinox >= 0)
945 input.equinox = atoi(tval(iequinox));
946
947 if(fabs(input.cdelt1) < minCdelt) minCdelt = fabs(input.cdelt1);
948 if(fabs(input.cdelt2) < minCdelt) minCdelt = fabs(input.cdelt2);
949
950 strcpy(header, "");
951 sprintf(temp, "SIMPLE = T" ); mMakeHdr_stradd(header, temp);
952 sprintf(temp, "BITPIX = -64" ); mMakeHdr_stradd(header, temp);
953 sprintf(temp, "NAXIS = 2" ); mMakeHdr_stradd(header, temp);
954 sprintf(temp, "NAXIS1 = %d", input.naxis1 ); mMakeHdr_stradd(header, temp);
955 sprintf(temp, "NAXIS2 = %d", input.naxis2 ); mMakeHdr_stradd(header, temp);
956 sprintf(temp, "CTYPE1 = '%s'", input.ctype1 ); mMakeHdr_stradd(header, temp);
957 sprintf(temp, "CTYPE2 = '%s'", input.ctype2 ); mMakeHdr_stradd(header, temp);
958 sprintf(temp, "CRVAL1 = %14.9f", input.crval1 ); mMakeHdr_stradd(header, temp);
959 sprintf(temp, "CRVAL2 = %14.9f", input.crval2 ); mMakeHdr_stradd(header, temp);
960 sprintf(temp, "CRPIX1 = %14.9f", input.crpix1 ); mMakeHdr_stradd(header, temp);
961 sprintf(temp, "CRPIX2 = %14.9f", input.crpix2 ); mMakeHdr_stradd(header, temp);
962 sprintf(temp, "CDELT1 = %14.9f", input.cdelt1 ); mMakeHdr_stradd(header, temp);
963 sprintf(temp, "CDELT2 = %14.9f", input.cdelt2 ); mMakeHdr_stradd(header, temp);
964 sprintf(temp, "CROTA2 = %14.9f", input.crota2 ); mMakeHdr_stradd(header, temp);
965 sprintf(temp, "EQUINOX = %d", input.equinox); mMakeHdr_stradd(header, temp);
966 sprintf(temp, "END" ); mMakeHdr_stradd(header, temp);
967
968 input.wcs = wcsinit(header);
969
970 if(input.wcs == (struct WorldCoor *)NULL)
971 {
972 for(ifiles=0; ifiles<ntables; ++ifiles)
973 free(fnames[ifiles]);
974
975 free(fnames);
976
977 sprintf(returnStruct->msg, "Bad WCS for image %d", nimages);
978 return returnStruct;
979 }
980
981 checkWCS = montage_checkWCS(input.wcs);
982
983 if(checkWCS)
984 {
985 for(ifiles=0; ifiles<ntables; ++ifiles)
986 free(fnames[ifiles]);
987
988 free(fnames);
989
990 strcpy(returnStruct->msg, montage_msgstr);
991 return returnStruct;
992 }
993
994
995 /* Get the coordinate system and epoch in a */
996 /* form compatible with the conversion library */
997
998 if(input.wcs->syswcs == WCS_J2000)
999 {
1000 input.sys = EQUJ;
1001 input.epoch = 2000.;
1002
1003 if(input.wcs->equinox == 1950)
1004 input.epoch = 1950.;
1005 }
1006 else if(input.wcs->syswcs == WCS_B1950)
1007 {
1008 input.sys = EQUB;
1009 input.epoch = 1950.;
1010
1011 if(input.wcs->equinox == 2000)
1012 input.epoch = 2000;
1013 }
1014 else if(input.wcs->syswcs == WCS_GALACTIC)
1015 {
1016 input.sys = GAL;
1017 input.epoch = 2000.;
1018 }
1019 else if(input.wcs->syswcs == WCS_ECLIPTIC)
1020 {
1021 input.sys = ECLJ;
1022 input.epoch = 2000.;
1023
1024 if(input.wcs->equinox == 1950)
1025 {
1026 input.sys = ECLB;
1027 input.epoch = 1950.;
1028 }
1029 }
1030 else
1031 {
1032 input.sys = EQUJ;
1033 input.epoch = 2000.;
1034 }
1035
1036
1037
1038 /* Collect the locations of the corners of the images */
1039
1040 pix2wcs(input.wcs, 0.5, 0.5, &xpos, &ypos);
1041
1042 convertCoordinates(input.sys, input.epoch, xpos, ypos,
1043 sys, equinox, &lon, &lat, 0.0);
1044
1045 lons[ncoords] = lon;
1046 lats[ncoords] = lat;
1047
1048 ++ncoords;
1049
1050
1051 pix2wcs(input.wcs, input.naxis1+0.5, 0.5, &xpos, &ypos);
1052
1053 convertCoordinates(input.sys, input.epoch, xpos, ypos,
1054 sys, equinox, &lon, &lat, 0.0);
1055
1056 lons[ncoords] = lon;
1057 lats[ncoords] = lat;
1058
1059 ++ncoords;
1060
1061
1062 pix2wcs(input.wcs, input.naxis1+0.5, input.naxis2+0.5,
1063 &xpos, &ypos);
1064
1065 convertCoordinates(input.sys, input.epoch, xpos, ypos,
1066 sys, equinox, &lon, &lat, 0.0);
1067
1068 lons[ncoords] = lon;
1069 lats[ncoords] = lat;
1070
1071 ++ncoords;
1072
1073
1074 pix2wcs(input.wcs, 0.5, input.naxis2+0.5, &xpos, &ypos);
1075
1076 convertCoordinates(input.sys, input.epoch, xpos, ypos,
1077 sys, equinox, &lon, &lat, 0.0);
1078
1079 lons[ncoords] = lon;
1080 lats[ncoords] = lat;
1081
1082 ++ncoords;
1083 }
1084
1085 if(pixelScale > 0.)
1086 minCdelt = pixelScale;
1087
1088 if(ncoords >= maxcoords)
1089 {
1090 maxcoords += MAXCOORD;
1091
1092 lons = (double *)realloc(lons, maxcoords * sizeof(double));
1093 lats = (double *)realloc(lats, maxcoords * sizeof(double));
1094
1095 if(lats == (double *)NULL)
1096 {
1097 fclose(fout);
1098
1099 for(ifiles=0; ifiles<ntables; ++ifiles)
1100 free(fnames[ifiles]);
1101
1102 free(fnames);
1103
1104 sprintf(returnStruct->msg, "Memory allocation failure.");
1105 return returnStruct;
1106 }
1107 }
1108 }
1109
1110 tclose();
1111 }
1112
1113 for(ifiles=0; ifiles<ntables; ++ifiles)
1114 free(fnames[ifiles]);
1115
1116 free(fnames);
1117
1118
1119 /**********************************************/
1120 /* Check for very large region (e.g. all-sky) */
1121 /**********************************************/
1122
1123 xmin = 1.;
1124 xmax = -1.;
1125 ymin = 1.;
1126 ymax = -1.;
1127 zmin = 1.;
1128 zmax = -1.;
1129
1130 for(i=0; i<ncoords; ++i)
1131 {
1132 x = cos(lons[i] * dtr) * cos(lats[i] * dtr);
1133 y = sin(lons[i] * dtr) * cos(lats[i] * dtr);
1134 z = sin(lats[i] * dtr);
1135
1136 if(x < xmin) xmin = x;
1137 if(x > xmax) xmax = x;
1138 if(y < ymin) ymin = y;
1139 if(y > ymax) ymax = y;
1140 if(z < zmin) zmin = z;
1141 if(z > zmax) zmax = z;
1142 }
1143
1144 if(xmax - xmin > 1
1145 || ymax - ymin > 1
1146 || zmax - zmin > 1)
1147 {
1148 naxis1 = 360. / minCdelt;
1149 naxis2 = 180. / minCdelt;
1150
1151 fprintf(fout, "SIMPLE = T\n");
1152 fprintf(fout, "BITPIX = -64\n");
1153
1154 if(naxis == -1)
1155 fprintf(fout, "NAXIS = 2\n");
1156 else
1157 fprintf(fout, "NAXIS = %d\n", naxis);
1158
1159 fprintf(fout, "NAXIS1 = %d\n", naxis1);
1160 fprintf(fout, "NAXIS2 = %d\n", naxis2);
1161
1162 if(naxis > 2)
1163 fprintf(fout, "NAXIS3 = %d\n", naxis3);
1164
1165 if(naxis > 3)
1166 fprintf(fout, "NAXIS4 = %d\n", naxis4);
1167
1168 if(sys == EQUJ)
1169 {
1170 fprintf(fout, "CTYPE1 = 'RA---AIT'\n");
1171 fprintf(fout, "CTYPE2 = 'DEC--AIT'\n");
1172 fprintf(fout, "EQUINOX = %-g\n", equinox);
1173 }
1174 if(sys == EQUB)
1175 {
1176 fprintf(fout, "CTYPE1 = 'RA---AIT'\n");
1177 fprintf(fout, "CTYPE2 = 'DEC--AIT'\n");
1178 fprintf(fout, "EQUINOX = %-g\n", equinox);
1179 }
1180 if(sys == ECLJ)
1181 {
1182 fprintf(fout, "CTYPE1 = 'ELON-AIT'\n");
1183 fprintf(fout, "CTYPE2 = 'ELAT-AIT'\n");
1184 fprintf(fout, "EQUINOX = %-g\n", equinox);
1185 }
1186 if(sys == ECLB)
1187 {
1188 fprintf(fout, "CTYPE1 = 'ELON-AIT'\n");
1189 fprintf(fout, "CTYPE2 = 'ELAT-AIT'\n");
1190 fprintf(fout, "EQUINOX = %-g\n", equinox);
1191 }
1192 if(sys == GAL)
1193 {
1194 fprintf(fout, "CTYPE1 = 'GLON-AIT'\n");
1195 fprintf(fout, "CTYPE2 = 'GLAT-AIT'\n");
1196 }
1197 if(sys == SGAL)
1198 {
1199 fprintf(fout, "CTYPE1 = 'SLON-AIT'\n");
1200 fprintf(fout, "CTYPE2 = 'SLAT-AIT'\n");
1201 }
1202
1203 fprintf(fout, "CRVAL1 = %14.9f\n", 0.);
1204 fprintf(fout, "CRVAL2 = %14.9f\n", 0.);
1205
1206 if(naxis > 2)
1207 fprintf(fout, "CRVAL3 = %14.9f\n", crval3);
1208
1209 if(naxis > 3)
1210 fprintf(fout, "CRVAL4 = %14.9f\n", crval4);
1211
1212 fprintf(fout, "CRPIX1 = %14.4f\n", ((double)naxis1 + 1.)/2.);
1213 fprintf(fout, "CRPIX2 = %14.4f\n", ((double)naxis2 + 1.)/2.);
1214
1215 if(naxis > 2)
1216 fprintf(fout, "CRPIX3 = %14.9f\n", crpix3);
1217
1218 if(naxis > 3)
1219 fprintf(fout, "CRPIX4 = %14.9f\n", crpix4);
1220
1221 fprintf(fout, "CDELT1 = %14.9f\n", -minCdelt);
1222 fprintf(fout, "CDELT2 = %14.9f\n", minCdelt);
1223
1224 if(naxis > 2)
1225 fprintf(fout, "CDELT3 = %14.9f\n", cdelt3);
1226
1227 if(naxis > 3)
1228 fprintf(fout, "CDELT4 = %14.9f\n", cdelt4);
1229
1230 fprintf(fout, "CROTA2 = %14.9f\n", 0.);
1231 fprintf(fout, "END\n");
1232 fflush(fout);
1233 fclose(fout);
1234
1235 strcpy(msg, "");
1236
1237 if(naxis < 0)
1238 strcpy(msg, " Cube columns exist but are either blank or inconsistent; outputting 2D only.");
1239
1240 free(lons);
1241 free(lats);
1242
1243 returnStruct->status = 0;
1244
1245 sprintf(returnStruct->msg, "msg=\"Large area; defaulting to AITOFF projection.%s\", count=%d, ncube=%d, naxis1=%d, naxis2=%d",
1246 msg, nimages, nCube, naxis1, naxis2);
1247
1248 sprintf(returnStruct->json, "{\"msg\":\"Large area; defaulting to AITOFF projection.%s\", \"count\":%d, \"ncube\":%d, \"naxis1\":%d, \"naxis2\":%d}",
1249 msg, nimages, nCube, naxis1, naxis2);
1250
1251 strcpy(returnStruct->note, msg);
1252
1253 returnStruct->count = nimages;
1254 returnStruct->ncube = nCube;
1255 returnStruct->naxis1 = naxis1;
1256 returnStruct->naxis2 = naxis2;
1257 returnStruct->clon = 0.;
1258 returnStruct->clat = 0.;
1259 returnStruct->lonsize = 360.;
1260 returnStruct->latsize = 180.;
1261 returnStruct->posang = 0.;
1262 returnStruct->lon1 = 0.;
1263 returnStruct->lat1 = 0.;
1264 returnStruct->lon2 = 0.;
1265 returnStruct->lat2 = 0.;
1266 returnStruct->lon3 = 0.;
1267 returnStruct->lat3 = 0.;
1268 returnStruct->lon4 = 0.;
1269 returnStruct->lat4 = 0.;
1270
1271 return returnStruct;
1272 }
1273
1274
1275 /************************************/
1276 /* Get the bounding box information */
1277 /************************************/
1278
1279 if(northAligned)
1280 box = bndVerticalBoundingBox(ncoords, lons, lats);
1281 else
1282 box = bndBoundingBox(ncoords, lons, lats);
1283
1284 free(lons);
1285 free(lats);
1286
1287 if(box == (struct bndInfo *)NULL)
1288 {
1289 sprintf(montage_msgstr, "Error computing boundaries.");
1290 return returnStruct;
1291 }
1292
1293 if(minCdelt <= 0. || minCdelt >= 360.)
1294 minCdelt = 1./3600.;
1295
1296 if(maxPixel > 0)
1297 {
1298 if(box->latSize > box->lonSize)
1299 minCdelt = box->latSize / maxPixel;
1300 else
1301 minCdelt = box->lonSize / maxPixel;
1302 }
1303
1304 if(isPercentage)
1305 {
1306 naxis1 = box->lonSize / minCdelt;
1307 naxis2 = box->latSize / minCdelt;
1308
1309 if(naxis1 > naxis2)
1310 pad = pad / 100. * naxis1;
1311 else
1312 pad = pad / 100. * naxis2;
1313 }
1314
1315 if(mMakeHdr_debug >= 1)
1316 {
1317 printf("pad = %-g (isPercentage = %d)\n", pad, isPercentage);
1318 fflush(stdout);
1319 }
1320
1321 naxis1 = box->lonSize / minCdelt + 2 * pad;
1322 if(naxis1 * minCdelt < box->lonSize) naxis1 += 2;
1323
1324 naxis2 = box->latSize / minCdelt + 2 * pad;
1325 if(naxis2 * minCdelt < box->lonSize) naxis2 += 2;
1326
1327 fprintf(fout, "SIMPLE = T\n");
1328 fprintf(fout, "BITPIX = -64\n");
1329
1330 if(naxis == -1)
1331 fprintf(fout, "NAXIS = 2\n");
1332 else
1333 fprintf(fout, "NAXIS = %d\n", naxis);
1334
1335 fprintf(fout, "NAXIS1 = %d\n", naxis1);
1336 fprintf(fout, "NAXIS2 = %d\n", naxis2);
1337
1338 if(naxis > 2)
1339 fprintf(fout, "NAXIS3 = %d\n", naxis3);
1340
1341 if(naxis > 3)
1342 fprintf(fout, "NAXIS4 = %d\n", naxis4);
1343
1344 if(sys == EQUJ)
1345 {
1346 fprintf(fout, "CTYPE1 = 'RA---TAN'\n");
1347 fprintf(fout, "CTYPE2 = 'DEC--TAN'\n");
1348 fprintf(fout, "EQUINOX = %-g\n", equinox);
1349 }
1350 if(sys == EQUB)
1351 {
1352 fprintf(fout, "CTYPE1 = 'RA---TAN'\n");
1353 fprintf(fout, "CTYPE2 = 'DEC--TAN'\n");
1354 fprintf(fout, "EQUINOX = %-g\n", equinox);
1355 }
1356 if(sys == ECLJ)
1357 {
1358 fprintf(fout, "CTYPE1 = 'ELON-TAN'\n");
1359 fprintf(fout, "CTYPE2 = 'ELAT-TAN'\n");
1360 fprintf(fout, "EQUINOX = %-g\n", equinox);
1361 }
1362 if(sys == ECLB)
1363 {
1364 fprintf(fout, "CTYPE1 = 'ELON-TAN'\n");
1365 fprintf(fout, "CTYPE2 = 'ELAT-TAN'\n");
1366 fprintf(fout, "EQUINOX = %-g\n", equinox);
1367 }
1368 if(sys == GAL)
1369 {
1370 fprintf(fout, "CTYPE1 = 'GLON-TAN'\n");
1371 fprintf(fout, "CTYPE2 = 'GLAT-TAN'\n");
1372 }
1373 if(sys == SGAL)
1374 {
1375 fprintf(fout, "CTYPE1 = 'SLON-TAN'\n");
1376 fprintf(fout, "CTYPE2 = 'SLAT-TAN'\n");
1377 }
1378
1379 fprintf(fout, "CRVAL1 = %14.9f\n", box->centerLon);
1380 fprintf(fout, "CRVAL2 = %14.9f\n", box->centerLat);
1381
1382 if(naxis > 2)
1383 fprintf(fout, "CRVAL3 = %14.9f\n", crval3);
1384
1385 if(naxis > 3)
1386 fprintf(fout, "CRVAL4 = %14.9f\n", crval4);
1387
1388 fprintf(fout, "CRPIX1 = %14.4f\n", ((double)naxis1 + 1.)/2.);
1389 fprintf(fout, "CRPIX2 = %14.4f\n", ((double)naxis2 + 1.)/2.);
1390
1391 if(naxis > 2)
1392 fprintf(fout, "CRPIX3 = %14.9f\n", crpix3);
1393
1394 if(naxis > 3)
1395 fprintf(fout, "CRPIX4 = %14.9f\n", crpix4);
1396
1397 fprintf(fout, "CDELT1 = %14.9f\n", -minCdelt);
1398 fprintf(fout, "CDELT2 = %14.9f\n", minCdelt);
1399
1400 if(naxis > 2)
1401 fprintf(fout, "CDELT3 = %14.9f\n", cdelt3);
1402
1403 if(naxis > 3)
1404 fprintf(fout, "CDELT4 = %14.9f\n", cdelt4);
1405
1406 fprintf(fout, "CROTA2 = %14.9f\n", box->posAngle);
1407 fprintf(fout, "END\n");
1408 fflush(fout);
1409 fclose(fout);
1410
1411
1412 /* Collect the locations of the corners of the images */
1413
1414 if(mMakeHdr_readTemplate(template))
1415 {
1416 strcpy(returnStruct->msg, montage_msgstr);
1417 return returnStruct;
1418 }
1419
1420 ncoords = 0;
1421
1422 pix2wcs(outwcs, 0.5, 0.5, &lon, &lat);
1423
1424 lonc[ncoords] = lon;
1425 latc[ncoords] = lat;
1426
1427 ++ncoords;
1428
1429
1430 pix2wcs(outwcs, naxis1+0.5, 0.5, &lon, &lat);
1431
1432 lonc[ncoords] = lon;
1433 latc[ncoords] = lat;
1434
1435 ++ncoords;
1436
1437
1438 pix2wcs(outwcs, naxis1+0.5, naxis2+0.5, &lon, &lat);
1439
1440 lonc[ncoords] = lon;
1441 latc[ncoords] = lat;
1442
1443 ++ncoords;
1444
1445
1446 pix2wcs(outwcs, 0.5, naxis2+0.5, &lon, &lat);
1447
1448 lonc[ncoords] = lon;
1449 latc[ncoords] = lat;
1450
1451 ++ncoords;
1452
1453
1454 if(mMakeHdr_debug != 1)
1455 {
1456 strcpy(msg, "");
1457
1458 if(naxis < 0)
1459 strcpy(msg, "Cube columns exist but are either blank or inconsistent. Outputting 2D only.");
1460
1461 if(strlen(msg) == 0)
1462 {
1463 sprintf(montage_msgstr, "count=%d, ncube=%d, naxis1=%d, naxis2=%d, clon=%.6f, clat=%.6f, loncize=%.6f, latsize=%.6f, posang=%.6f, lon1=%.6f, lat1=%.6f, lon2=%.6f, lat2=%.6f, lon3=%.6f, lat3=%.6f, lon4=%.6f, lat4=%.6f",
1464 nimages,
1465 nCube,
1466 naxis1, naxis2,
1467 box->centerLon, box->centerLat,
1468 minCdelt*naxis1, minCdelt*naxis2,
1469 box->posAngle,
1470 lonc[0], latc[0],
1471 lonc[1], latc[1],
1472 lonc[2], latc[2],
1473 lonc[3], latc[3]);
1474
1475 sprintf(montage_json, "{\"count\"=%d, \"ncube\"=%d, \"naxis1\"=%d, \"naxis2\"=%d, \"clon\"=%.6f, \"clat\"=%.6f, \"lonsize\"=%.6f, \"latsize\"=%.6f, \"posang\"=%.6f, \"lon1\"=%.6f, \"lat1\"=%.6f, \"lon2\"=%.6f, \"lat2\"=%.6f, \"lon3\"=%.6f, \"lat3\"=%.6f, \"lon4\"=%.6f, \"lat4\"=%.6f}",
1476 nimages,
1477 nCube,
1478 naxis1, naxis2,
1479 box->centerLon, box->centerLat,
1480 minCdelt*naxis1, minCdelt*naxis2,
1481 box->posAngle,
1482 lonc[0], latc[0],
1483 lonc[1], latc[1],
1484 lonc[2], latc[2],
1485 lonc[3], latc[3]);
1486 }
1487 else
1488 {
1489 sprintf(montage_msgstr, "msg=\"%s\", count=%d, ncube=%d, naxis1=%d, naxis2=%d, clon=%.6f, clat=%.6f, lonsize=%.6f, latsize=%.6f, posang=%.6f, lon1=%.6f, lat1=%.6f, lon2=%.6f, lat2=%.6f, lon3=%.6f, lat3=%.6f, lon4=%.6f, lat4=%.6f",
1490 msg,
1491 nimages,
1492 nCube,
1493 naxis1, naxis2,
1494 box->centerLon, box->centerLat,
1495 minCdelt*naxis1, minCdelt*naxis2,
1496 box->posAngle,
1497 lonc[0], latc[0],
1498 lonc[1], latc[1],
1499 lonc[2], latc[2],
1500 lonc[3], latc[3]);
1501
1502 sprintf(montage_json, "{msg=\"%s\", \"count\"=%d, \"ncube\"=%d, \"naxis1\"=%d, \"naxis2\"=%d, \"clon\"=%.6f, \"clat\"=%.6f, \"lonsize\"=%.6f, \"latsize\"=%.6f, \"posang\"=%.6f, \"lon1\"=%.6f, \"lat1\"=%.6f, \"lon2\"=%.6f, \"lat2\"=%.6f, \"lon3\"=%.6f, \"lat3\"=%.6f, \"lon4\"=%.6f, \"lat4\"=%.6f}",
1503 msg,
1504 nimages,
1505 nCube,
1506 naxis1, naxis2,
1507 box->centerLon, box->centerLat,
1508 minCdelt*naxis1, minCdelt*naxis2,
1509 box->posAngle,
1510 lonc[0], latc[0],
1511 lonc[1], latc[1],
1512 lonc[2], latc[2],
1513 lonc[3], latc[3]);
1514 }
1515 }
1516
1517 returnStruct->status = 0;
1518
1519 strcpy(returnStruct->msg, montage_msgstr);
1520 strcpy(returnStruct->json, montage_json);
1521
1522 strcpy(returnStruct->note, msg);
1523
1524 returnStruct->count = nimages;
1525 returnStruct->ncube = nCube;
1526 returnStruct->naxis1 = naxis1;
1527 returnStruct->naxis2 = naxis2;
1528 returnStruct->clon = box->centerLon;
1529 returnStruct->clat = box->centerLat;
1530 returnStruct->lonsize = minCdelt*naxis1;
1531 returnStruct->latsize = minCdelt*naxis2;
1532 returnStruct->posang = box->posAngle;
1533 returnStruct->lon1 = lonc[0];
1534 returnStruct->lat1 = latc[0];
1535 returnStruct->lon2 = lonc[1];
1536 returnStruct->lat2 = latc[1];
1537 returnStruct->lon3 = lonc[2];
1538 returnStruct->lat3 = latc[2];
1539 returnStruct->lon4 = lonc[3];
1540 returnStruct->lat4 = latc[3];
1541
1542 return returnStruct;
1543 }
1544
1545
1546 int mMakeHdr_stradd(char *header, char *card)
1547 {
1548 int i;
1549
1550 int hlen = strlen(header);
1551 int clen = strlen(card);
1552
1553 for(i=0; i<clen; ++i)
1554 header[hlen+i] = card[i];
1555
1556 if(clen < 80)
1557 for(i=clen; i<80; ++i)
1558 header[hlen+i] = ' ';
1559
1560 header[hlen+80] = '\0';
1561
1562 return(strlen(header));
1563 }
1564
1565
1566
1567 /**************************************************/
1568 /* */
1569 /* Read the output header template file. */
1570 /* Specifically extract the image size info. */
1571 /* Also, create a single-string version of the */
1572 /* header data and use it to initialize the */
1573 /* output WCS transform. */
1574 /* */
1575 /**************************************************/
1576
1577 int mMakeHdr_readTemplate(char *filename)
1578 {
1579 int j;
1580
1581 FILE *fp;
1582
1583 char line[MAXSTR];
1584
1585 char header[80000];
1586
1587
1588 /********************************************************/
1589 /* Open the template file, read and parse all the lines */
1590 /********************************************************/
1591
1592 fp = fopen(filename, "r");
1593
1594 if(fp == (FILE *)NULL)
1595 {
1596 sprintf(montage_msgstr, "Template file not found.");
1597 return 1;
1598 }
1599
1600 strcpy(header, "");
1601
1602 for(j=0; j<1000; ++j)
1603 {
1604 if(fgets(line, MAXSTR, fp) == (char *)NULL)
1605 break;
1606
1607 if(line[strlen(line)-1] == '\n')
1608 line[strlen(line)-1] = '\0';
1609
1610 if(line[strlen(line)-1] == '\r')
1611 line[strlen(line)-1] = '\0';
1612
1613 mMakeHdr_stradd(header, line);
1614 }
1615
1616 fclose(fp);
1617
1618
1619 /****************************************/
1620 /* Initialize the WCS transform library */
1621 /****************************************/
1622
1623 outwcs = wcsinit(header);
1624
1625 if(outwcs == (struct WorldCoor *)NULL)
1626 {
1627 sprintf(montage_msgstr, "Output wcsinit() failed.");
1628 return 1;
1629 }
1630
1631 return 0;
1632 }