summaryrefslogtreecommitdiff
path: root/stwcs/wcsutil/hstwcs.py
blob: d9146d2da21e4bd4ec9c60d407a3378ef31b529e (plain) (blame)
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
from __future__ import absolute_import, division, print_function

import os
from astropy.wcs import WCS
from astropy.io import fits
from ..distortion import models, coeff_converter
import numpy as np
from stsci.tools import fileutil

from . import pc2cd
from . import getinput
from . import instruments
from .mappings import inst_mappings, ins_spec_kw

from astropy import log
default_log_level = log.getEffectiveLevel()

__all__ = ['HSTWCS']


def extract_rootname(kwvalue, suffix=""):
    """ Returns the rootname from a full reference filename

        If a non-valid value (any of ['','N/A','NONE','INDEF',None]) is input,
            simply return a string value of 'NONE'

        This function will also replace any 'suffix' specified with a blank.
    """
    # check to see whether a valid kwvalue has been provided as input
    if kwvalue.strip() in ['', 'N/A', 'NONE', 'INDEF', None]:
        return 'NONE'  # no valid value, so return 'NONE'

    # for a valid kwvalue, parse out the rootname
    # strip off any environment variable from input filename, if any are given
    if '$' in kwvalue:
        fullval = kwvalue[kwvalue.find('$') + 1:]
    else:
        fullval = kwvalue
    # Extract filename without path from kwvalue
    fname = os.path.basename(fullval).strip()

    # Now, rip out just the rootname from the full filename
    rootname = fileutil.buildNewRootname(fname)

    # Now, remove any known suffix from rootname
    rootname = rootname.replace(suffix, '')
    return rootname.strip()


def build_default_wcsname(idctab):

    idcname = extract_rootname(idctab, suffix='_idc')
    wcsname = 'IDC_' + idcname
    return wcsname


class NoConvergence(Exception):
    """
    An error class used to report non-convergence and/or divergence of
    numerical methods. It is used to report errors in the iterative solution
    used by the :py:meth:`~stwcs.hstwcs.HSTWCS.all_world2pix`\ .

    Attributes
    ----------

    best_solution : numpy.array
        Best solution achieved by the method.

    accuracy : float
        Accuracy of the :py:attr:`best_solution`\ .

    niter : int
        Number of iterations performed by the numerical method to compute
        :py:attr:`best_solution`\ .

    divergent : None, numpy.array
        Indices of the points in :py:attr:`best_solution` array for which the
        solution appears to be divergent. If the solution does not diverge,
        `divergent` will be set to `None`.

    failed2converge : None, numpy.array
        Indices of the points in :py:attr:`best_solution` array for which the
        solution failed to converge within the specified maximum number
        of iterations. If there are no non-converging poits (i.e., if
        the required accuracy has been achieved for all points) then
        `failed2converge` will be set to `None`.

    """
    def __init__(self, *args, **kwargs):
        super(NoConvergence, self).__init__(*args)

        self.best_solution  = kwargs.pop('best_solution', None)
        self.accuracy       = kwargs.pop('accuracy', None)
        self.niter          = kwargs.pop('niter', None)
        self.divergent      = kwargs.pop('divergent', None)
        self.failed2converge = kwargs.pop('failed2converge', None)


class HSTWCS(WCS):

    def __init__(self, fobj=None, ext=None, minerr=0.0, wcskey=" "):
        """
        Create a WCS object based on the instrument.

        In addition to basic WCS keywords this class provides
        instrument specific information needed in distortion computation.

        Parameters
        ----------
        fobj : str or `astropy.io.fits.HDUList` object or None
            file name, e.g j9irw4b1q_flt.fits
            fully qualified filename[EXTNAME,EXTNUM], e.g. j9irw4b1q_flt.fits[sci,1]
            `astropy.io.fits` file object, e.g fits.open('j9irw4b1q_flt.fits'), in which case the
            user is responsible for closing the file object.
        ext : int, tuple or None
            extension number
            if ext is tuple, it must be ("EXTNAME", EXTNUM), e.g. ("SCI", 2)
            if ext is None, it is assumed the data is in the primary hdu
        minerr : float
            minimum value a distortion correction must have in order to be applied.
            If CPERRja, CQERRja are smaller than minerr, the corersponding
            distortion is not applied.
        wcskey : str
            A one character A-Z or " " used to retrieve and define an
            alternate WCS description.
        """

        self.inst_kw = ins_spec_kw
        self.minerr = minerr
        self.wcskey = wcskey

        if fobj is not None:
            filename, hdr0, ehdr, phdu = getinput.parseSingleInput(f=fobj,
                                                                   ext=ext)
            self.filename = filename
            instrument_name = hdr0.get('INSTRUME', 'DEFAULT')
            if instrument_name == 'DEFAULT' or instrument_name not in list(inst_mappings.keys()):
                self.instrument = 'DEFAULT'
            else:
                self.instrument = instrument_name
            # Set the correct reference frame
            refframe = determine_refframe(hdr0)
            if refframe is not None:
                ehdr['RADESYS'] = refframe

            WCS.__init__(self, ehdr, fobj=phdu, minerr=self.minerr,
                         key=self.wcskey)
            if self.instrument == 'DEFAULT':
                self.pc2cd()
            # If input was a `astropy.io.fits.HDUList` object, it's the user's
            # responsibility to close it, otherwise, it's closed here.
            if not isinstance(fobj, fits.HDUList):
                phdu.close()
            self.setInstrSpecKw(hdr0, ehdr)
            self.readIDCCoeffs(ehdr)
            extname = ehdr.get('EXTNAME', '')
            extnum = ehdr.get('EXTVER', None)
            self.extname = (extname, extnum)
        else:
            # create a default HSTWCS object
            self.instrument = 'DEFAULT'
            WCS.__init__(self, minerr=self.minerr, key=self.wcskey)
            self.pc2cd()
            self.setInstrSpecKw()
        self.setPscale()
        self.setOrient()

    @property
    def naxis1(self):
        return self._naxis1

    @naxis1.setter
    def naxis1(self, value):
        self._naxis1 = value

    @property
    def naxis2(self):
        return self._naxis2

    @naxis2.setter
    def naxis2(self, value):
        self._naxis2 = value

    def readIDCCoeffs(self, header):
        """
        Reads in first order IDCTAB coefficients if present in the header
        """
        coeffs = ['ocx10', 'ocx11', 'ocy10', 'ocy11', 'idcscale',
                  'idcv2ref', 'idcv3ref', 'idctheta']
        for c in coeffs:
            self.__setattr__(c, header.get(c, None))

    def setInstrSpecKw(self, prim_hdr=None, ext_hdr=None):
        """
        Populate the instrument specific attributes:

        These can be in different headers but each instrument class has knowledge
        of where to look for them.

        Parameters
        ----------
        prim_hdr : `astropy.io.fits.Header`
            primary header
        ext_hdr : `astropy.io.fits.Header`
            extension header

        """
        if self.instrument in list(inst_mappings.keys()):
            inst_kl = inst_mappings[self.instrument]
            inst_kl = instruments.__dict__[inst_kl]
            insobj = inst_kl(prim_hdr, ext_hdr)

            for key in self.inst_kw:
                try:
                    self.__setattr__(key, insobj.__getattribute__(key))
                except AttributeError:
                    # Some of the instrument's attributes are recorded in the primary header and
                    # were already set, (e.g. 'DETECTOR'), the code below is a check for that case.
                    if not self.__getattribute__(key):
                        raise
                    else:
                        pass

        else:
            raise KeyError("Unsupported instrument - %s" % self.instrument)

    def setPscale(self):
        """
        Calculates the plate scale from the CD matrix
        """
        try:
            cd11 = self.wcs.cd[0][0]
            cd21 = self.wcs.cd[1][0]
            self.pscale = np.sqrt(np.power(cd11, 2) + np.power(cd21, 2)) * 3600.
        except AttributeError:
            if self.wcs.has_cd():
                print("This file has a PC matrix. You may want to convert it \n \
                to a CD matrix, if reasonable, by running pc2.cd() method.\n \
                The plate scale can be set then by calling setPscale() method.\n")
            self.pscale = None

    def setOrient(self):
        """
        Computes ORIENTAT from the CD matrix
        """
        try:
            cd12 = self.wcs.cd[0][1]
            cd22 = self.wcs.cd[1][1]
            self.orientat = np.rad2deg(np.arctan2(cd12, cd22))
        except AttributeError:
            if self.wcs.has_cd():
                print("This file has a PC matrix. You may want to convert it \n \
                to a CD matrix, if reasonable, by running pc2.cd() method.\n \
                The orientation can be set then by calling setOrient() method.\n")
            self.pscale = None

    def updatePscale(self, scale):
        """
        Updates the CD matrix with a new plate scale
        """
        self.wcs.cd = self.wcs.cd / self.pscale * scale
        self.setPscale()

    def readModel(self, update=False, header=None):
        """
        Reads distortion model from IDCTAB.

        If IDCTAB is not found ('N/A', "", or not found on disk), then
        if SIP coefficients and first order IDCTAB coefficients are present
        in the header, restore the idcmodel from the header.
        If not - assign None to self.idcmodel.

        Parameters
        ----------
        header : `astropy.io.fits.Header`
            fits extension header
        update : bool (False)
            if True - record the following IDCTAB quantities as header keywords:
            CX10, CX11, CY10, CY11, IDCSCALE, IDCTHETA, IDCXREF, IDCYREF,
            IDCV2REF, IDCV3REF
        """
        if self.idctab in [None, '', ' ', 'N/A']:
            # Keyword idctab is not present in header - check for sip coefficients
            if header is not None and 'IDCSCALE' in header:
                self._readModelFromHeader(header)
            else:
                print("Distortion model is not available: IDCTAB=None\n")
                self.idcmodel = None
        elif not os.path.exists(fileutil.osfn(self.idctab)):
            if header is not None and 'IDCSCALE' in header:
                self._readModelFromHeader(header)
            else:
                print('Distortion model is not available: IDCTAB file %s not found\n' % self.idctab)
                self.idcmodel = None
        else:
            self.readModelFromIDCTAB(header=header, update=update)

    def _readModelFromHeader(self, header):
        # Recreate idc model from SIP coefficients and header kw
        print('Restoring IDC model from SIP coefficients\n')
        model = models.GeometryModel()
        cx, cy = coeff_converter.sip2idc(self)
        model.cx = cx
        model.cy = cy
        model.name = "sip"
        model.norder = header['A_ORDER']

        refpix = {}
        refpix['XREF'] = header['IDCXREF']
        refpix['YREF'] = header['IDCYREF']
        refpix['PSCALE'] = header['IDCSCALE']
        refpix['V2REF'] = header['IDCV2REF']
        refpix['V3REF'] = header['IDCV3REF']
        refpix['THETA'] = header['IDCTHETA']
        model.refpix = refpix

        self.idcmodel = model

    def readModelFromIDCTAB(self, header=None, update=False):
        """
        Read distortion model from idc table.

        Parameters
        ----------
        header : `astropy.io.fits.Header`
            fits extension header
        update : booln (False)
            if True - save teh following as header keywords:
            CX10, CX11, CY10, CY11, IDCSCALE, IDCTHETA, IDCXREF, IDCYREF,
            IDCV2REF, IDCV3REF

        """
        if self.date_obs is None:
            print('date_obs not available\n')
            self.idcmodel = None
            return
        if self.filter1 is None and self.filter2 is None:
            'No filter information available\n'
            self.idcmodel = None
            return

        self.idcmodel = models.IDCModel(self.idctab,
                                        chip=self.chip, direction='forward',
                                        date=self.date_obs,
                                        filter1=self.filter1, filter2=self.filter2,
                                        offtab=self.offtab, binned=self.binned)

        if self.ltv1 != 0. or self.ltv2 != 0.:
            self.resetLTV()

        if update:
            if header is None:
                print('Update header with IDC model kw requested but header was not provided\n.')
            else:
                self._updatehdr(header)

    def resetLTV(self):
        """
        Reset LTV values for polarizer data

        The polarizer field is smaller than the detector field.
        The distortion coefficients are defined for the entire
        polarizer field and the LTV values are set as with subarray
        data. This may also be true for other special filters.
        This is a special case when the observation is considered
        a subarray in terms of detector field but a full frame in
        terms of distortion model.
        To avoid shifting the distortion coefficients the LTV values
        are reset to 0.
        """
        if self.naxis1 == self.idcmodel.refpix['XSIZE'] and  \
           self.naxis2 == self.idcmodel.refpix['YSIZE']:
            self.ltv1 = 0.
            self.ltv2 = 0.

    def wcs2header(self, sip2hdr=False, idc2hdr=True, wcskey=None, relax=False):
        """
        Create a `astropy.io.fits.Header` object from WCS keywords.

        If the original header had a CD matrix, return a CD matrix,
        otherwise return a PC matrix.

        Parameters
        ----------
        sip2hdr : bool
            If True - include SIP coefficients
        """
        if not relax and not sip2hdr:
            log.setLevel('WARNING')
        h = self.to_header(key=wcskey, relax=relax)
        log.setLevel(default_log_level)

        if not wcskey:
            wcskey = self.wcs.alt
        if self.wcs.has_cd():
            h = pc2cd(h, key=wcskey)

        if 'wcsname' not in h:
            if self.idctab is not None:
                wname = build_default_wcsname(self.idctab)
            else:
                wname = 'DEFAULT'
            h['wcsname{0}'.format(wcskey)] = wname

        if idc2hdr:
            for card in self._idc2hdr():
                h[card.keyword + wcskey] = (card.value, card.comment)
        try:
            del h['RESTFRQ']
            del h['RESTWAV']
        except KeyError: pass

        if sip2hdr and self.sip:
            for card in self._sip2hdr('a'):
                h[card.keyword] = (card.value, card.comment)
            for card in self._sip2hdr('b'):
                h[card.keyword] = (card.value, card.comment)

            try:
                ap = self.sip.ap
            except AssertionError:
                ap = None
            try:
                bp = self.sip.bp
            except AssertionError:
                bp = None

            if ap:
                for card in self._sip2hdr('ap'):
                    h[card.keyword] = (card.value, card.comment)
            if bp:
                for card in self._sip2hdr('bp'):
                    h[card.keyword] = (card.value, card.comment)
        return h

    def _sip2hdr(self, k):
        """
        Get a set of SIP coefficients in the form of an array
        and turn them into a `astropy.io.fits.Cardlist`.
        k - one of 'a', 'b', 'ap', 'bp'
        """

        cards = []
        korder = self.sip.__getattribute__(k + '_order')
        cards.append(fits.Card(keyword=k.upper() + '_ORDER', value=korder))
        coeffs = self.sip.__getattribute__(k)
        ind = coeffs.nonzero()
        for i in range(len(ind[0])):
            card = fits.Card(keyword=k.upper() + '_' + str(ind[0][i]) + '_' + str(ind[1][i]),
                             value=coeffs[ind[0][i], ind[1][i]])
            cards.append(card)
        return cards

    def _idc2hdr(self):
        # save some of the idc coefficients
        coeffs = ['ocx10', 'ocx11', 'ocy10', 'ocy11', 'idcscale']
        cards = []
        for c in coeffs:
            try:
                val = self.__getattribute__(c)
            except AttributeError:
                continue
            if val:
                cards.append(fits.Card(keyword=c, value=val))
        return cards

    def pc2cd(self):
        if not self.wcs.has_pc():
            self.wcs.pc = self.wcs.get_pc()
        self.wcs.cd = self.wcs.pc * self.wcs.cdelt[1]

    def all_world2pix(self, *args, **kwargs):
        """
        all_world2pix(*arg, accuracy=1.0e-4, maxiter=20, adaptive=False, \
detect_divergence=True, quiet=False)

        Performs full inverse transformation using iterative solution
        on full forward transformation with complete distortion model.

        Parameters
        ----------
        accuracy : float, optional (Default = 1.0e-4)
            Required accuracy of the solution. Iteration terminates when the
            correction to the solution found during the previous iteration
            is smaller (in the sence of the L2 norm) than `accuracy`\ .

        maxiter : int, optional (Default = 20)
            Maximum number of iterations allowed to reach the solution.

        adaptive : bool, optional (Default = False)
            Specifies whether to adaptively select only points that did not
            converge to a solution whithin the required accuracy for the
            next iteration. Default is recommended for HST as well as most
            other instruments.

            .. note::
               The :py:meth:`all_world2pix` uses a vectorized implementation
               of the method of consecutive approximations (see `Notes`
               section below) in which it iterates over *all* input poits
               *regardless* until the required accuracy has been reached for
               *all* input points. In some cases it may be possible that
               *almost all* points have reached the required accuracy but
               there are only a few of input data points left for which
               additional iterations may be needed (this depends mostly on the
               characteristics of the geometric distortions for a given
               instrument). In this situation it may be
               advantageous to set `adaptive` = `True`\ in which
               case :py:meth:`all_world2pix` will continue iterating *only* over
               the points that have not yet converged to the required
               accuracy. However, for the HST's ACS/WFC detector, which has
               the strongest distortions of all HST instruments, testing has
               shown that enabling this option would lead to a about 10-30\%
               penalty in computational time (depending on specifics of the
               image, geometric distortions, and number of input points to be
               converted). Therefore, for HST instruments,
               it is recommended to set `adaptive` = `False`\ . The only
               danger in getting this setting wrong will be a performance
               penalty.

            .. note::
               When `detect_divergence` is `True`\ , :py:meth:`all_world2pix` \
               will automatically switch to the adaptive algorithm once
               divergence has been detected.

        detect_divergence : bool, optional (Default = True)
            Specifies whether to perform a more detailed analysis of the
            convergence to a solution. Normally :py:meth:`all_world2pix`
            may not achieve the required accuracy
            if either the `tolerance` or `maxiter` arguments are too low.
            However, it may happen that for some geometric distortions
            the conditions of convergence for the the method of consecutive
            approximations used by :py:meth:`all_world2pix` may not be
            satisfied, in which case consecutive approximations to the
            solution will diverge regardless of the `tolerance` or `maxiter`
            settings.

            When `detect_divergence` is `False`\ , these divergent points
            will be detected as not having achieved the required accuracy
            (without further details). In addition, if `adaptive` is `False`
            then the algorithm will not know that the solution (for specific
            points) is diverging and will continue iterating and trying to
            "improve" diverging solutions. This may result in NaN or Inf
            values in the return results (in addition to a performance
            penalties). Even when `detect_divergence` is
            `False`\ , :py:meth:`all_world2pix`\ , at the end of the iterative
            process, will identify invalid results (NaN or Inf) as "diverging"
            solutions and will raise :py:class:`NoConvergence` unless
            the `quiet` parameter is set to `True`\ .

            When `detect_divergence` is `True`\ , :py:meth:`all_world2pix` will
            detect points for
            which current correction to the coordinates is larger than
            the correction applied during the previous iteration **if** the
            requested accuracy **has not yet been achieved**\ . In this case,
            if `adaptive` is `True`, these points will be excluded from
            further iterations and if `adaptive`
            is `False`\ , :py:meth:`all_world2pix` will automatically
            switch to the adaptive algorithm.

            .. note::
               When accuracy has been achieved, small increases in
               current corrections may be possible due to rounding errors
               (when `adaptive` is `False`\ ) and such increases
               will be ignored.

            .. note::
               Setting `detect_divergence` to `True` will incurr about 5-10\%
               performance penalty (in our testing on ACS/WFC images).
               Because the benefits of enabling this feature outweigh
               the small performance penalty, it is recommended to set
               `detect_divergence` to `True`\ , unless extensive testing
               of the distortion models for images from specific
               instruments show a good stability of the numerical method
               for a wide range of coordinates (even outside the image
               itself).

            .. note::
               Indices of the diverging inverse solutions will be reported
               in the `divergent` attribute of the
               raised :py:class:`NoConvergence` object.

        quiet : bool, optional (Default = False)
            Do not throw :py:class:`NoConvergence` exceptions when the method
            does not converge to a solution with the required accuracy
            within a specified number of maximum iterations set by `maxiter`
            parameter. Instead, simply return the found solution.

        Raises
        ------
        NoConvergence
            The method does not converge to a
            solution with the required accuracy within a specified number
            of maximum iterations set by the `maxiter` parameter.

        Notes
        -----
        Inputs can either be (RA, Dec, origin) or (RADec, origin) where RA
        and Dec are 1-D arrays/lists of coordinates and RADec is an
        array/list of pairs of coordinates.

        Using the method of consecutive approximations we iterate starting
        with the initial approximation, which is computed using the
        non-distorion-aware :py:meth:`wcs_world2pix` (or equivalent).

        The :py:meth:`all_world2pix` function uses a vectorized implementation
        of the method of consecutive approximations and therefore it is
        highly efficient (>30x) when *all* data points that need to be
        converted from sky coordinates to image coordinates are passed at
        *once*\ . Therefore, it is advisable, whenever possible, to pass
        as input a long array of all points that need to be converted
        to :py:meth:`all_world2pix` instead of calling :py:meth:`all_world2pix`
        for each data point. Also see the note to the `adaptive` parameter.

        Examples
        --------
        >>> import stwcs
        >>> from astropy.io import fits
        >>> hdulist = fits.open('j94f05bgq_flt.fits')
        >>> w = stwcs.wcsutil.HSTWCS(hdulist, ext=('sci',1))
        >>> hdulist.close()

        >>> ra, dec = w.all_pix2world([1,2,3],[1,1,1],1); print(ra); print(dec)
        [ 5.52645241  5.52649277  5.52653313]
        [-72.05171776 -72.05171295 -72.05170814]
        >>> radec = w.all_pix2world([[1,1],[2,1],[3,1]],1); print(radec)
        [[  5.52645241 -72.05171776]
         [  5.52649277 -72.05171295]
         [  5.52653313 -72.05170814]]
        >>> x, y = w.all_world2pix(ra,dec,1)
        >>> print(x)
        [ 1.00000233  2.00000232  3.00000233]
        >>> print(y)
        [ 0.99999997  0.99999997  0.99999998]
        >>> xy = w.all_world2pix(radec,1)
        >>> print(xy)
        [[ 1.00000233  0.99999997]
         [ 2.00000232  0.99999997]
         [ 3.00000233  0.99999998]]
        >>> xy = w.all_world2pix(radec,1, maxiter=3, accuracy=1.0e-10, \
quiet=False)
        NoConvergence: 'HSTWCS.all_world2pix' failed to converge to requested \
accuracy after 3 iterations.

        >>>
        Now try to use some diverging data:
        >>> divradec = w.all_pix2world([[1.0,1.0],[10000.0,50000.0],\
[3.0,1.0]],1); print(divradec)
        [[  5.52645241 -72.05171776]
         [  7.15979392 -70.81405561]
         [  5.52653313 -72.05170814]]

        >>> try:
        >>>   xy = w.all_world2pix(divradec,1, maxiter=20, accuracy=1.0e-4, \
adaptive=False, detect_divergence=True, quiet=False)
        >>> except stwcs.wcsutil.hstwcs.NoConvergence as e:
        >>>   print("Indices of diverging points: {}".format(e.divergent))
        >>>   print("Indices of poorly converging points: {}".format(e.failed2converge))
        >>>   print("Best solution: {}".format(e.best_solution))
        >>>   print("Achieved accuracy: {}".format(e.accuracy))
        >>>   raise e
        Indices of diverging points:
        [1]
        Indices of poorly converging points:
        None
        Best solution:
        [[  1.00006219e+00   9.99999288e-01]
         [ -1.99440907e+06   1.44308548e+06]
         [  3.00006257e+00   9.99999316e-01]]
        Achieved accuracy:
        [[  5.98554253e-05   6.79918148e-07]
         [  8.59514088e+11   6.61703754e+11]
         [  6.02334592e-05   6.59713067e-07]]
        Traceback (innermost last):
          File "<console>", line 8, in <module>
        NoConvergence: 'HSTWCS.all_world2pix' failed to converge to the requested accuracy.
        After 5 iterations, the solution is diverging at least for one input point.

        >>> try:
        >>>   xy = w.all_world2pix(divradec,1, maxiter=20, accuracy=1.0e-4, \
adaptive=False, detect_divergence=False, quiet=False)
        >>> except stwcs.wcsutil.hstwcs.NoConvergence as e:
        >>>   print("Indices of diverging points: {}".format(e.divergent))
        >>>   print("Indices of poorly converging points: {}".format(e.failed2converge))
        >>>   print("Best solution: {}".format(e.best_solution))
        >>>   print("Achieved accuracy: {}".format(e.accuracy))
        >>>   raise e
        Indices of diverging points:
        [1]
        Indices of poorly converging points:
        None
        Best solution:
        [[  1.   1.]
         [ nan  nan]
         [  3.   1.]]
        Achieved accuracy:
        [[  0.   0.]
         [ nan  nan]
         [  0.   0.]]
        Traceback (innermost last):
          File "<console>", line 8, in <module>
        NoConvergence: 'HSTWCS.all_world2pix' failed to converge to the requested accuracy.
        After 20 iterations, the solution is diverging at least for one input point.

        """
        #####################################################################
        ##                     PROCESS ARGUMENTS:                          ##
        #####################################################################
        nargs = len(args)

        if nargs == 3:
            try:
                ra     = np.asarray(args[0], dtype=np.float64)
                dec    = np.asarray(args[1], dtype=np.float64)
                # assert( len(ra.shape) == 1 and len(dec.shape) == 1 )
                origin = int(args[2])
                vect1D = True
            except:
                raise TypeError("When providing three arguments, they must "
                                "be (Ra, Dec, origin) where Ra and Dec are "
                                "Nx1 vectors.")
        elif nargs == 2:
            try:
                rd  = np.asarray(args[0], dtype=np.float64)
                ra  = rd[:, 0]
                dec = rd[:, 1]
                origin = int(args[1])
                vect1D = False
            except:
                raise TypeError("When providing two arguments, they must "
                                "be (RaDec, origin) where RaDec is a Nx2 array.")
        else:
            raise TypeError("Expected 2 or 3 arguments, {:d} given.".format(nargs))

        # process optional arguments:
        accuracy          = kwargs.pop('accuracy', 1.0e-4)
        maxiter           = kwargs.pop('maxiter', 20)
        adaptive          = kwargs.pop('adaptive', False)
        detect_divergence = kwargs.pop('detect_divergence', True)
        quiet             = kwargs.pop('quiet', False)

        #####################################################################
        ##                INITIALIZE ITERATIVE PROCESS:                    ##
        #####################################################################
        x0, y0 = self.wcs_world2pix(ra, dec, origin)  # <-- initial approximation
                                                      #     (WCS based only)

        # see if an iterative solution is required (when any of the
        # non-CD-matrix corrections are present). If not required
        # return initial approximation (x0, y0).
        if self.sip is None and \
           self.cpdis1 is None and self.cpdis2 is None and \
           self.det2im1 is None and self.det2im2 is None:
            # no non-WCS corrections are detected - return
            # initial approximation
            if vect1D:
                return [x0, y0]
            else:
                return np.dstack([x0, y0])[0]

        x  = x0.copy()  # 0-order solution
        y  = y0.copy()  # 0-order solution

        # initial correction:
        dx, dy = self.pix2foc(x, y, origin)
        # If pix2foc does not apply all the required distortion
        # corrections then replace the above line with:
        # r0, d0 = self.all_pix2world(x, y, origin)
        # dx, dy = self.wcs_world2pix(r0, d0, origin )
        dx -= x0
        dy -= y0

        # update initial solution:
        x -= dx
        y -= dy

        # norn (L2) squared of the correction:
        dn2prev = dx ** 2 + dy ** 2
        dn2 = dn2prev

        # prepare for iterative process
        iterlist  = list(range(1, maxiter + 1))
        accuracy2 = accuracy ** 2
        ind = None
        inddiv = None

        npts = x.shape[0]

        # turn off numpy runtime warnings for 'invalid' and 'over':
        old_invalid = np.geterr()['invalid']
        old_over = np.geterr()['over']
        np.seterr(invalid='ignore', over='ignore')

        #####################################################################
        ##                     NON-ADAPTIVE ITERATIONS:                    ##
        #####################################################################
        if not adaptive:
            for k in iterlist:
                # check convergence:
                if np.max(dn2) < accuracy2:
                    break

                # find correction to the previous solution:
                dx, dy = self.pix2foc(x, y, origin)
                # If pix2foc does not apply all the required distortion
                # corrections then replace the above line with:
                # r0, d0 = self.all_pix2world(x, y, origin)
                # dx, dy = self.wcs_world2pix(r0, d0, origin )
                dx -= x0
                dy -= y0

                # update norn (L2) squared of the correction:
                dn2 = dx ** 2 + dy ** 2

                # check for divergence (we do this in two stages
                # to optimize performance for the most common
                # scenario when succesive approximations converge):
                if detect_divergence:
                    ind, = np.where(dn2 <= dn2prev)
                    if ind.shape[0] < npts:
                        inddiv, = np.where(
                            np.logical_and(dn2 > dn2prev, dn2 >= accuracy2))
                        if inddiv.shape[0] > 0:
                            # apply correction only to the converging points:
                            x[ind] -= dx[ind]
                            y[ind] -= dy[ind]
                            # switch to adaptive iterations:
                            ind, = np.where((dn2 >= accuracy2) &
                                            (dn2 <= dn2prev) & np.isfinite(dn2))
                            iterlist = iterlist[k:]
                            adaptive = True
                            break
                    # dn2prev[ind] = dn2[ind]
                    dn2prev = dn2

                # apply correction:
                x -= dx
                y -= dy

        #####################################################################
        ##                      ADAPTIVE ITERATIONS:                       ##
        #####################################################################
        if adaptive:
            if ind is None:
                ind = np.asarray(list(range(npts)), dtype=np.int64)

            for k in iterlist:
                # check convergence:
                if ind.shape[0] == 0:
                    break

                # find correction to the previous solution:
                dx[ind], dy[ind] = self.pix2foc(x[ind], y[ind], origin)
                # If pix2foc does not apply all the required distortion
                # corrections then replace the above line with:
                # r0[ind], d0[ind] = self.all_pix2world(x[ind], y[ind], origin)
                # dx[ind], dy[ind] = self.wcs_world2pix(r0[ind], d0[ind], origin)
                dx[ind] -= x0[ind]
                dy[ind] -= y0[ind]

                # update norn (L2) squared of the correction:
                dn2 = dx ** 2 + dy ** 2

                # update indices of elements that still need correction:
                if detect_divergence:
                    ind, = np.where((dn2 >= accuracy2) & (dn2 <= dn2prev))
                    # ind = ind[np.where((dn2[ind] >= accuracy2) & (dn2[ind] <= dn2prev))]
                    dn2prev[ind] = dn2[ind]
                else:
                    ind, = np.where(dn2 >= accuracy2)
                    # ind = ind[np.where(dn2[ind] >= accuracy2)]

                # apply correction:
                x[ind] -= dx[ind]
                y[ind] -= dy[ind]

        #####################################################################
        ##         FINAL DETECTION OF INVALID, DIVERGING,                  ##
        ##         AND FAILED-TO-CONVERGE POINTS                           ##
        #####################################################################
        # Identify diverging and/or invalid points:
        invalid = (((~np.isfinite(y)) | (~np.isfinite(x)) |
                    (~np.isfinite(dn2))) &
                   (np.isfinite(ra)) & (np.isfinite(dec)))
        # When detect_divergence==False, dn2prev is outdated (it is the
        # norm^2 of the very first correction). Still better than nothing...
        inddiv, = np.where(((dn2 >= accuracy2) & (dn2 > dn2prev)) | invalid)
        if inddiv.shape[0] == 0:
            inddiv = None
        # identify points that did not converge within
        # 'maxiter' iterations:
        if k >= maxiter:
            ind, = np.where((dn2 >= accuracy2) & (dn2 <= dn2prev) & (~invalid))
            if ind.shape[0] == 0:
                ind = None
        else:
            ind = None

        #####################################################################
        ##      RAISE EXCEPTION IF DIVERGING OR TOO SLOWLY CONVERGING      ##
        ##      DATA POINTS HAVE BEEN DETECTED:                            ##
        #####################################################################
        # raise exception if diverging or too slowly converging
        if (ind is not None or inddiv is not None) and not quiet:
            if vect1D:
                sol  = [x, y]
                err  = [np.abs(dx), np.abs(dy)]
            else:
                sol  = np.dstack([x, y] )[0]
                err  = np.dstack([np.abs(dx), np.abs(dy)] )[0]

            # restore previous numpy error settings:
            np.seterr(invalid=old_invalid, over=old_over)

            if inddiv is None:
                raise NoConvergence("'HSTWCS.all_world2pix' failed to "
                                    "converge to the requested accuracy after {:d} "
                                    "iterations.".format(k), best_solution=sol,
                                    accuracy=err, niter=k, failed2converge=ind,
                                    divergent=None)
            else:
                raise NoConvergence("'HSTWCS.all_world2pix' failed to "
                                    "converge to the requested accuracy.{0:s}"
                                    "After {1:d} iterations, the solution is diverging "
                                    "at least for one input point."
                                    .format(os.linesep, k), best_solution=sol,
                                    accuracy=err, niter=k, failed2converge=ind,
                                    divergent=inddiv)

        #####################################################################
        ##             FINALIZE AND FORMAT DATA FOR RETURN:                ##
        #####################################################################
        # restore previous numpy error settings:
        np.seterr(invalid=old_invalid, over=old_over)

        if vect1D:
            return [x, y]
        else:
            return np.dstack([x, y] )[0]

    def _updatehdr(self, ext_hdr):
        # kw2add : OCX10, OCX11, OCY10, OCY11
        # record the model in the header for use by pydrizzle
        ext_hdr['OCX10'] = self.idcmodel.cx[1, 0]
        ext_hdr['OCX11'] = self.idcmodel.cx[1, 1]
        ext_hdr['OCY10'] = self.idcmodel.cy[1, 0]
        ext_hdr['OCY11'] = self.idcmodel.cy[1, 1]
        ext_hdr['IDCSCALE'] = self.idcmodel.refpix['PSCALE']
        ext_hdr['IDCTHETA'] = self.idcmodel.refpix['THETA']
        ext_hdr['IDCXREF'] = self.idcmodel.refpix['XREF']
        ext_hdr['IDCYREF'] = self.idcmodel.refpix['YREF']
        ext_hdr['IDCV2REF'] = self.idcmodel.refpix['V2REF']
        ext_hdr['IDCV3REF'] = self.idcmodel.refpix['V3REF']

    def printwcs(self):
        """
        Print the basic WCS keywords.
        """
        print('WCS Keywords\n')
        print('CD_11  CD_12: %r %r' % (self.wcs.cd[0, 0], self.wcs.cd[0, 1]))
        print('CD_21  CD_22: %r %r' % (self.wcs.cd[1, 0], self.wcs.cd[1, 1]))
        print('CRVAL    : %r %r' % (self.wcs.crval[0], self.wcs.crval[1]))
        print('CRPIX    : %r %r' % (self.wcs.crpix[0], self.wcs.crpix[1]))
        print('NAXIS    : %d %d' % (self.naxis1, self.naxis2))
        print('Plate Scale : %r' % self.pscale)
        print('ORIENTAT : %r' % self.orientat)


def determine_refframe(phdr):
    """
    Determine the reference frame in standard FITS WCS.

    This is necessary for two reasons:
    - The reference frame in HST images is stored not in RADESYS (FITS standard) but in REFFRAME.
    - REFFRAME is in the primary header, while the rest of the WCS keywords are in the
      extension header.

    The values of REFFRAME are populated from the APT template where observers are
    given three options: GSC1 (corresponds to FK5), ICRS or OTHER.
    In the case of "OTHER", we leave this to wcslib which has a default of ICRS.

    Parameters
    ----------
    phdr : `astropy.io.fits.Header`
        Primary Header of an HST observation

    Returns
    -------
    refframe : str or None
        One of the FITS WCS standard reference frames.

    """
    try:
        refframe = phdr['REFFRAME'].upper()
    except KeyError:
        refframe = None
    if refframe == "GSC1":
        refframe = "FK5"
    elif refframe != "ICRS":
        refframe = None
    return refframe