-
Notifications
You must be signed in to change notification settings - Fork 2
/
imageops.py
704 lines (591 loc) · 22.7 KB
/
imageops.py
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
"""
2010
S.Rodney
Simple image operations.
"""
import exceptions
from pyfits import getheader
from math import sqrt
def getpixscale(image, returntuple=False, ext=None):
""" Compute the pixel scale of the reference pixel in arcsec/pix in
each direction from the fits header cd matrix.
:param image: any valid input to getheader(), namely:
a string giving a fits filename, a pyfits hdulist or hdu, a pyfits
header object, a tuple or list giving [hdr,data]
:param returntuple: bool. When True, return the two pixel scale values
along the x and y axes. When False, return the average of the two.
:param ext: (optional) extension number for science array
:return: float value or tuple giving the pixel scale in arcseconds/pixel.
"""
hdr = getheader(image, ext=ext)
if 'CD1_1' in hdr:
cd11 = hdr['CD1_1']
cd12 = hdr['CD1_2']
cd21 = hdr['CD2_1']
cd22 = hdr['CD2_2']
# define the sign based on determinant
det = cd11 * cd22 - cd12 * cd21
if det < 0:
sgn = -1
else:
sgn = 1
if cd12 == 0 and cd21 == 0:
# no rotation: x=RA, y=Dec
cdelt1 = cd11
cdelt2 = cd22
else:
cdelt1 = sgn * sqrt(cd11 ** 2 + cd12 ** 2)
cdelt2 = sqrt(cd22 ** 2 + cd21 ** 2)
elif 'CDELT1' in hdr.keys() and \
(hdr['CDELT1'] != 1 and hdr['CDELT2'] != 1):
cdelt1 = hdr['CDELT1']
cdelt2 = hdr['CDELT2']
else:
raise exceptions.RuntimeError(
"Cannot identify CD matrix in %s" % image)
cdelt1 *= 3600.
cdelt2 *= 3600.
if returntuple:
return cdelt1, cdelt2
else:
return (abs(cdelt1) + abs(cdelt2)) / 2.
def imstamp( image, xc, yc, nx, ny, ext=0, fillpix=0, saveas=None, clobber=False ):
"""
cut a postage stamp out of the given image
centered on (xc,yc) with pixel size nx x ny.
the 'image' may be the filename of a fits file
or a numpy array.
TODO: If the stamp extends beyond the image boundary
then it is filled with pixels of value 'fillpix'.
TODO : update the wcs keywords to correct values
TODO : mef extension handling
"""
from numpy import ndarray
import pyfits
import os
# read in the image data
if isinstance( image, str ) :
imdat = pyfits.getdata( image, ext=ext )
elif isinstance( image, ndarray ):
imdat = image
naxis1,naxis2 = imdat.shape
# TODO:
# check if stamp will overrun the boundary
# make a blank stamp with value fillpix
# paste in good pixels from image
# extract the stamp and return it
stamp = imdat[ (yc-ny/2):(yc+ny/2), (xc-nx/2):(xc+nx/2)]
if not saveas :
return( stamp )
if os.path.exists( saveas ) and not clobber :
print("%s exists. Not clobbering.")
return( stamp )
elif os.path.exists( saveas ) and clobber :
os.remove( saveas )
hdr = pyfits.getheader( image, ext=ext )
hdu = pyfits.PrimaryHDU( data=stamp, header=hdr )
hdulist = pyfits.HDUList( hdus=[hdu])
hdulist.writeto( saveas )
return( saveas )
def imsec( image, sec, ext=0 ):
"""
cut a rectangular section out of the
given image defined by the
pixel coordinates sec=[x1,x2,y1,y2]
TODO : mef extension handling
TODO : error handling
"""
from numpy import ndarray
from pyfits import getdata
x1,x2,y1,y2 = sec
# read in the image data
if isinstance( image, str ) :
imdat = getdata( image, ext=ext )
elif isinstance( image, ndarray ):
imdat = image
# extract the stamp and return it
sec = imdat[ y1:y2, x1:x2]
return( sec )
def drz2sciwht( drzfile, scifile=None, whtfile=None,
verbose=False ):
""" split up a MEF file produced by multidrizzle
into the _sci and _wht components """
import pyfits
import os
from util import naming
drzfile = os.path.abspath(drzfile)
if not scifile : scifile = naming.chsuffix(drzfile, 'sci')
if not whtfile : whtfile = naming.chsuffix(drzfile, 'wht')
if verbose:
print("Splitting up mef drizzled image %s "%drzfile)
print("into single-extension drizzle products:")
print(" %s\n %s"%(scifile, whtfile) )
drz = pyfits.open( drzfile )
sci = pyfits.PrimaryHDU( data = drz['SCI'].data,
header= drz['SCI'].header )
if 'D001PIXF' in drz[0].header.keys():
sci.header.update('PIXFRAC', drz[0].header['D001PIXF'],
comment="multidrizzle 'drop' parameter")
if 'FILTER' in drz[0].header.keys():
sci.header.update(
'FILTER', drz[0].header['FILTER'],
comment="element selected in filter wheel")
if 'EXPTIME' in drz[0].header.keys():
sci.header.update(
'EXPTIME', drz[0].header['EXPTIME'],
comment="exposure time")
if 'INSTRUME' in drz[0].header.keys():
sci.header.update(
'INSTRUME', drz[0].header['INSTRUME'],
comment="instrument")
if 'DETECTOR' in drz[0].header.keys():
sci.header.update(
'DETECTOR', drz[0].header['DETECTOR'],
comment="detector")
sci.writeto( scifile, clobber=True )
wht = pyfits.PrimaryHDU( data = drz['WHT'].data,
header= drz['WHT'].header )
if 'D001PIXF' in drz[0].header.keys():
wht.header.update('PIXFRAC', drz[0].header['D001PIXF'],
comment="multidrizzle 'drop' parameter")
if 'FILTER' in drz[0].header.keys():
wht.header.update(
'FILTER', drz[0].header['FILTER'],
comment="element selected in filter wheel")
if 'EXPTIME' in drz[0].header.keys():
wht.header.update(
'EXPTIME', drz[0].header['EXPTIME'],
comment="exposure time")
if 'INSTRUME' in drz[0].header.keys():
wht.header.update(
'INSTRUME', drz[0].header['INSTRUME'],
comment="instrument")
if 'DETECTOR' in drz[0].header.keys():
wht.header.update(
'DETECTOR', drz[0].header['DETECTOR'],
comment="detector")
wht.writeto( whtfile, clobber=True )
return( scifile, whtfile )
def imsubtract( image1, image2, outfile=None,
clobber=False, verbose=False, debug=False):
"""
construct a simple subtraction: image2 - image1
guards against different sized data arrays by assuming
that the lower left pixel (0,0) is the anchor point.
"""
import os
import pyfits
from numpy import ndarray
import exceptions
if debug : import pdb; pdb.set_trace()
if outfile :
if os.path.isfile( outfile ) and not clobber :
print("%s exists. Not clobbering."%outfile)
return( outfile )
# read in the images
if not os.path.isfile( image1 ) :
raise exceptions.RuntimeError(
"The image file %s is not valid."%image1 )
im1head = pyfits.getheader( image1 )
im1data = pyfits.getdata( image1 )
if not os.path.isfile( image2 ) :
raise exceptions.RuntimeError(
"The image file %s is not valid."%image2 )
im2head = pyfits.getheader( image2 )
im2data = pyfits.getdata( image2 )
# sometimes multidrizzle drops a pixel. Unpredictable.
nx2,ny2 = im2data.shape
nx1,ny1 = im1data.shape
if nx2>nx1 or ny2>ny1 :
im2data = im2data[:min(nx1,nx2),:min(ny1,ny2)]
im1data = im1data[:min(nx1,nx2),:min(ny1,ny2)]
elif nx2<nx1 or ny2<ny1 :
im1data = im1data[:min(nx1,nx2),:min(ny1,ny2)]
im2data = im2data[:min(nx1,nx2),:min(ny1,ny2)]
diffim = im2data - im1data
if not outfile :
return( diffim )
else :
im2head.update("SRCIM1",image1,"First source image = template for subtraction")
im2head.update("SRCIM2",image2,"Second source image = search epoch image")
outdir = os.path.split( outfile )[0]
if outdir and not os.path.isdir(outdir):
os.makedirs( outdir )
pyfits.writeto( outfile, diffim,
header=im2head,
clobber=clobber )
return( outfile )
def imWeightedAve( image1, image2, weight1, weight2, outfile, clobber=False, verbose=False):
"""
construct a weighted average of image1 and image2:
(weight1*image1 + weight2*image2) / (weight1+weight2)
Mean image is written to outfile.
"""
import os
import pyfits
from numpy import ndarray, nan_to_num
import exceptions
if os.path.isfile(outfile) :
if clobber :
os.unlink( outfile )
else :
print( "%s exists. Not clobbering."%outfile )
return( outfile )
# read in the sci and wht images
im1hdr = pyfits.getheader( image1 )
im1 = pyfits.getdata( image1 )
im2 = pyfits.getdata( image2 )
wht1 = pyfits.getdata( weight1 )
wht2 = pyfits.getdata( weight2 )
meanim = nan_to_num( (wht1*im1 + wht2*im2)/(wht1+wht2) )
# TODO : make a useful header
outdir = os.path.dirname( outfile )
if not os.path.isdir(outdir):
os.makedirs( outdir )
pyfits.writeto( outfile, meanim, header=im1hdr )
return( outfile )
def imaverage( imagelist, outfile,
clobber=False, verbose=False):
"""
construct a simple average of all images in the list.
Assumes all input images have identical dimensions
Returns name of outfile
"""
import os
import pyfits
from numpy import where, ones, zeros, array, ndarray, nan_to_num,float32
import exceptions
if os.path.exists(outfile) :
if clobber :
os.unlink( outfile )
else :
print( "%s exists. Not clobbering."%outfile )
return( outfile )
# make empty arrays for components of the weighted average
naxis1 = pyfits.getval( imagelist[0], 'NAXIS1')
naxis2 = pyfits.getval( imagelist[0], 'NAXIS2')
sumarray = zeros( [naxis2,naxis1], dtype=float32 )
ncombinearray = zeros( [naxis2,naxis1], dtype=float32 )
# construct the weighted average and update header keywords
outhdr = pyfits.getheader( imagelist[0] )
i = 1
# import pdb; pdb.set_trace()
for imfile in imagelist :
imdat = pyfits.getdata( imfile )
sumarray += imdat
ncombinearray += where( imdat != 0 , ones(imdat.shape), zeros(imdat.shape) )
outhdr.update("SRCIM%02i"%i,imfile,"source image %i, used in average "%i )
i+= 1
outscidat = where( ncombinearray > 0 , sumarray/ncombinearray, zeros(sumarray.shape) )
outdir = os.path.dirname( outfile )
if outdir :
if not os.path.isdir(outdir):
os.makedirs( outdir )
pyfits.writeto( outfile, outscidat, header=outhdr )
return( outfile )
def weightedAverage( imagelist, whtlist, outfile, outwht,
clobber=False, verbose=False):
"""
construct a weighted average :
(weight1*image1 + weight2*image2 + ...) / (weight1+weight2+...)
And a composite weight map :
(weight1 + weight2 + ...) / Nweight
Mean image is written to outfile.
Assumes all input images have identical dimensions
Returns name of outfile and whtfile
"""
import os
import pyfits
from numpy import where, ones, zeros, array, ndarray, nan_to_num,float32
import exceptions
if os.path.exists(outfile) :
if clobber :
os.unlink( outfile )
else :
print( "%s exists. Not clobbering."%outfile )
return( outfile, outwht )
if os.path.exists(outwht) :
if clobber :
os.unlink( outwht )
else :
print( "%s exists. Not clobbering."%outwht )
return( outfile, outwht )
# make empty arrays for components of the weighted average
naxis1 = pyfits.getval( imagelist[0], 'NAXIS1')
naxis2 = pyfits.getval( imagelist[0], 'NAXIS2')
sumarray = zeros( [naxis2,naxis1], dtype=float32 )
ncombinearray = zeros( [naxis2,naxis1], dtype=float32 )
whtarray = ones( [naxis2,naxis1], dtype=float32 )
# construct the weighted average and update header keywords
outhdr = pyfits.getheader( imagelist[0] )
i = 1
# import pdb; pdb.set_trace()
for imfile, whtfile in zip( imagelist, whtlist ) :
imdat = pyfits.getdata( imfile )
whtdat = pyfits.getdata( whtfile )
sumarray += whtdat * imdat
whtarray += whtdat
ncombinearray += where( whtdat > 0 , ones(whtdat.shape), zeros(whtdat.shape) )
outhdr.update("SRCIM%02i"%i,imfile,"source image %i, used in weighted average "%i )
i+= 1
# outscidat = nan_to_num( sumarray / whtarray )
# outscidat = sumarray / whtarray
outscidat = where( ncombinearray > 0 , sumarray / whtarray, zeros(sumarray.shape) )
outwhtdat = where( ncombinearray > 0 , whtarray/ncombinearray, zeros(whtarray.shape) )
outdir = os.path.dirname( outfile )
if outdir :
if not os.path.isdir(outdir):
os.makedirs( outdir )
pyfits.writeto( outfile, outscidat, header=outhdr )
pyfits.writeto( outwht, outwhtdat, header=outhdr )
return( outfile, outwht )
def minCombine( imagelist, whtlist, outfile, outwht,
clobber=False, verbose=False):
"""
combined images in image list by taking the minimum value for all
pixels with weight > 0.
Min-Combined image is written to outfile.
Assumes all input images have identical dimensions
Returns name of outfile and whtfile
import os
import pyfits
from numpy import where, ones, zeros, array, ndarray, nan_to_num,float32, min
import exceptions
if os.path.exists(outfile) :
if clobber :
os.unlink( outfile )
else :
print( "%s exists. Not clobbering."%outfile )
return( outfile, outwht )
if os.path.exists(outwht) :
if clobber :
os.unlink( outwht )
else :
print( "%s exists. Not clobbering."%outwht )
return( outfile, outwht )
# TODO : how to take the minimum only when wht>0 ??
# make empty arrays for components of the weighted average
naxis1 = pyfits.getval( imagelist[0], 'NAXIS1')
naxis2 = pyfits.getval( imagelist[0], 'NAXIS2')
ncombinearray = zeros( [naxis2,naxis1], dtype=float32 )
# minarray = zeros( [naxis2,naxis1], dtype=float32 )
# whtarray = zeros( [naxis2,naxis1], dtype=float32 )
# construct the weighted average and update header keywords
outhdr = pyfits.getheader( imagelist[0] )
i = 1
# import pdb; pdb.set_trace()
for imfile, whtfile in zip( imagelist, whtlist ) :
imdat = pyfits.getdata( imfile )
whtdat = pyfits.getdata( whtfile )
if not minarray :
minarray = imdat
whtarray = whtdat
continue
minarray = where( whtdat > 0 , min(minarray,imdat), zeros(whtdat.shape) )
whtdat * imdat
whtarray += whtdat
ncombinearray += where( whtdat > 0 , ones(whtdat.shape), zeros(whtdat.shape) )
outhdr.update("SRCIM%02i"%i,imfile,"source image %i, used in weighted average "%i )
i+= 1
# outscidat = nan_to_num( sumarray / whtarray )
# outscidat = sumarray / whtarray
outscidat = where( ncombinearray > 0 , sumarray / whtarray, zeros(sumarray.shape) )
outwhtdat = where( ncombinearray > 0 , whtarray/ncombinearray, zeros(whtarray.shape) )
outdir = os.path.dirname( outfile )
if outdir :
if not os.path.isdir(outdir):
os.makedirs( outdir )
pyfits.writeto( outfile, outscidat, header=outhdr )
pyfits.writeto( outwht, outwhtdat, header=outhdr )
return( outfile, outwht )
"""
def imaverage2( image1, image2,
outfile=None, clobber=False, verbose=False):
"""
construct a simple average of image1 and image2:
(image1 + image2) / 2.0
Mean image is written to outfile.
"""
import os
import pyfits
from numpy import ndarray
import exceptions
if os.path.isfile(outfile) :
if clobber :
os.unlink( outfile )
else :
print( "%s exists. Not clobbering."%outfile )
return( outfile )
im1data = pyfits.getdata( image1 )
im1head = pyfits.getheader( image1 )
im2data = pyfits.getdata( image2 )
# sometimes multidrizzle drops a pixel. Unpredictable.
nx2,ny2 = im2data.shape
nx1,ny1 = im1data.shape
if nx2>nx1 or ny2>ny1 :
im2data = im2data[:min(nx1,nx2),:min(ny1,ny2)]
im1data = im1data[:min(nx1,nx2),:min(ny1,ny2)]
elif nx2<nx1 or ny2<ny1 :
im1data = im1data[:min(nx1,nx2),:min(ny1,ny2)]
im2data = im2data[:min(nx1,nx2),:min(ny1,ny2)]
meanim = (im2data + im1data)/2.
# TODO : make a useful header
im1head.update("SRCIM1",image1,"First source image for imaverage")
im1head.update("SRCIM2",image2,"Second source image for imaverage")
outdir = os.path.dirname( outfile )
if outdir :
if not os.path.isdir(outdir):
os.makedirs( outdir )
pyfits.writeto( outfile, meanim, header=im1head )
return( outfile )
def imsum( image1, image2, outfile=None, clobber=False, verbose=False):
"""
add together image1 and image2
"""
import os
import pyfits
from numpy import ndarray
import exceptions
# read in the images
if isinstance( image1, str ):
if not os.path.isfile( image1 ) :
raise exceptions.RuntimeError(
"The image file %s is not valid."%image1 )
im1head = pyfits.getheader( image1 )
im1data = pyfits.getdata( image1 )
im1head.update("SRCIM1",image1,"First source image for imsum")
elif isinstance( image1, ndarray ):
im1data = image1
im1head = None
else :
raise exceptions.RuntimeError(
"Provide a fits file name or numpy array for each image.")
if isinstance( image2, str ):
if not os.path.isfile( image2 ) :
raise exceptions.RuntimeError(
"The image file %s is not valid."%image2 )
im2data = pyfits.getdata( image2 )
im1head.update("SRCIM2",image2,"Second source image for imsum")
elif isinstance( image2, ndarray ):
im2data = image2
else :
raise exceptions.RuntimeError(
"Provide a fits file name or numpy array for each image.")
# sometimes multidrizzle drops a pixel. Unpredictable.
nx2,ny2 = im2data.shape
nx1,ny1 = im1data.shape
if nx2>nx1 or ny2>ny1 :
im2data = im2data[:min(nx1,nx2),:min(ny1,ny2)]
im1data = im1data[:min(nx1,nx2),:min(ny1,ny2)]
elif nx2<nx1 or ny2<ny1 :
im1data = im1data[:min(nx1,nx2),:min(ny1,ny2)]
im2data = im2data[:min(nx1,nx2),:min(ny1,ny2)]
sumim = im2data + im1data
if outfile :
# TODO : make a useful header
outdir = os.path.split( outfile )[0]
if outdir :
if not os.path.isdir(outdir):
os.makedirs( outdir )
pyfits.writeto( outfile, sumim,
header=im1head,
clobber=clobber )
return( outfile )
else :
return( sumim )
def imscaleflux( image1, scalefactor, outfile=None, clobber=False, verbose=False):
"""
multiply the flux in image by scalefactor and save to outfile
"""
import os
import pyfits
from numpy import ndarray
import exceptions
# read in the image
if isinstance( image1, str ):
if not os.path.isfile( image1 ) :
raise exceptions.RuntimeError(
"The image file %s is not valid."%image1 )
im1head = pyfits.getheader( image1 )
im1data = pyfits.getdata( image1 )
im1head.update("FLXSCALE",scalefactor,"Flux scaling factor")
elif isinstance( image1, ndarray ):
im1data = image1
im1head = None
else :
raise exceptions.RuntimeError("Provide a fits file name or numpy array for each image.")
scaledim = scalefactor * im1data
if outfile :
outdir = os.path.split( outfile )[0]
if outdir :
if not os.path.isdir(outdir):
os.makedirs( outdir )
pyfits.writeto( outfile, scaledim, header=im1head,clobber=clobber )
return( outfile )
else :
return( scaledim )
def int2bin( n, returntype='bitnumbers', bits=16 ) :
"""
convert the integer n into a binary representation.
The return value is either a list or a string according
to the 'returntype' setting:
'bitnumbers' : list of bit numbers
'bitvalues' : list of bit values
'string' : string of 1's and 0's
(formatted in quartets,
most significant bit on the left)
examples:
>> int2bitflags( 2304, returntype='bitvalues' )
[256,2048]
>> int2bitflags( 2304, returntype='bitnumbers' )
[9,12]
>> int2bitflags( 2304, returntype='string' )
'0000
"""
import exceptions
if returntype not in ['bitnumbers','bitvalues','string']:
raise exceptions.RuntimeError(
"returntype must be one of ['bitnumbers','bitvalues','string']")
bitlist = []
vallist = []
bitstr = ''
for y in range(bits-1, -1, -1):
if (n >> y & 1) :
bitlist.append( y+1 )
vallist.append( 2**y )
bitstr += '1'
else :
bitstr += '0'
if not y%4 :
bitstr += ' '
if returntype=='bitnumbers': return(sorted(bitlist))
elif returntype=='bitvalues': return(sorted(vallist))
elif returntype=='string': return( bitstr )
def wcscopy( donor, recipient, dext=1, rext=1, verbose=False ):
""" Copy the WCS header keywords from the donor
fits file into the header of the recipient fits file.
dext and rext specify the fits extensions to use for
the donor and recipient, respectively.
"""
import pyfits
donfits = pyfits.open( donor, mode='readonly' )
recfits = pyfits.open( recipient, mode='update' )
donhdr = donfits[dext].header
donfits.close()
rechdr = recfits[rext].header
wcskeylist = [ 'WCSAXES','CRPIX1 ','CRPIX2 ','CRVAL1 ','CRVAL2 ','CTYPE1 ','CTYPE2 ','CD1_1 ','CD1_2 ','CD2_1 ','CD2_2 ','LTV1 ','LTV2 ','LTM1_1 ','LTM2_2 ','ORIENTAT','RA_APER','DEC_APER','PA_APER','VAFACTOR' ]
Nupdated = 0
after = 'NAXIS2'
for key in wcskeylist :
if key in donhdr :
value = donhdr.get( key )
if key in rechdr :
rechdr[key] = value
else :
rechdr.update( key, value, after=after )
after = key
Nupdated += 1
recfits.flush()
recfits.close()
if verbose : print( 'Updated %i WCS keywords in %s'%(Nupdated, recipient) )
return( None )