cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c                                                                c
c     This file contains 9 subroutines and 6 functions:          c
c                                                                c
c     SUBROUTINES:                                               c
c                                                                c
c     1) hawki_gc_Ks(xr,yr,chip,xc,yc)                           c
c     2) hawki_gc_H(xr,yr,chip,xc,yc)                            c
c     3) hawki_gc_J(xr,yr,chip,xc,yc)                            c
c     4) tab_HAWKI_J(xt,yt,chip,xc,yc)                           c
c     5) poly_HAWKI_J(xr,yr,chip,xc,yc)                          c
c     6) tab_HAWKI_H(xt,yt,chip,xc,yc)                           c
c     7) poly_HAWKI_H(xr,yr,chip,xc,yc)                          c
c     8) tab_HAWKI_Ks(xt,yt,chip,xc,yc)                          c
c     9) poly_HAWKI_Ks(xr,yr,chip,xc,yc)                         c
c                                                                c
c                                                                c
c     FUNCTIONS:                                                 c
c                                                                c
c     1) function step_J(x)                                      c
c     2) function adj_J(xr,cp)                                   c
c     3) function step_H(x)                                      c
c     4) function adj_H(xr,cp)                                   c
c     5) function step_Ks(x)                                     c
c     6) function adj_Ks(xr,cp)                                  c
c                                                                c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc


c This subroutine applies the full Ks-filter geometric-distortion
c correction.
c Inputs: (x,y) raw coordinates 
c Outputs: (x,y) corrected coordinates

      subroutine hawki_gc_Ks(xr,yr,chip,xc,yc)
      implicit none
      
      real*8 xr,yr ! Raw coordinates
      real*8 xs ! x coordinate corrected only for periodicity
      real*8 xt,yt ! Coordinates corrected for periodicity and table of residuals
      real*8 xc,yc ! Final corrected coordinates
      real*8 step_Ks ! Periodicity correction 1
      real*8 adj_Ks ! Periodicity correction 2
      integer chip ! Chip number

      ! Periodicity correction 1
      xs=xr-step_Ks(xr)*1.0d0
      ! Periodicity correction 2
      xs=xs-adj_Ks(xs,chip)

      ! Table of residuals correction
      call tab_HAWKI_Ks(xs,yr,chip,xt,yt)
           
      ! Polynomial correction
      call poly_HAWKI_Ks(xt,yt,chip,xc,yc)
            
      return

      end


c This subroutine applies the full H-filter geometric-distortion
c correction.
c Inputs: (x,y) raw coordinates 
c Outputs: (x,y) corrected coordinates

      subroutine hawki_gc_H(xr,yr,chip,xc,yc)
      implicit none
      
      real*8 xr,yr ! Raw coordinates
      real*8 xs ! x coordinate corrected only for periodicity
      real*8 xt,yt ! Coordinates corrected for periodicity and table of residuals
      real*8 xc,yc ! Final corrected coordinates
      real*8 step_H ! Periodicity correction 1
      real*8 adj_H ! Periodicity correction 2
      integer chip ! Chip nummber

      ! Periodicity correction 1
      xs=xr-step_H(xr)*1.0d0
      ! Periodicity correction 2
      xs=xs-adj_H(xs,chip)

      ! Table of residuals correction
      call tab_HAWKI_H(xs,yr,chip,xt,yt)
           
      ! Polynomial correction
      call poly_HAWKI_H(xt,yt,chip,xc,yc)

      return

      end


c This subroutine applies the full J-filter geometric-distortion
c correction.
c Inputs: (x,y) raw coordinates 
c Outputs: (x,y) corrected coordinates

      subroutine hawki_gc_J(xr,yr,chip,xc,yc)
      implicit none
      
      real*8 xr,yr ! Raw coordinates
      real*8 xs ! x coordinate corrected only for periodicity
      real*8 xt,yt ! Coordinates corrected for periodicity and table of residuals
      real*8 xc,yc ! Final corrected coordinates
      real*8 step_J ! Periodicity correction 1
      real*8 adj_J ! Periodicity correction 2
      integer chip ! Chip number

      ! Periodicity correction 1
      xs=xr-step_J(xr)*1.0d0
      ! Periodicity correction 2
      xs=xs-adj_J(xs,chip)

      ! Table of residuals correction
      call tab_HAWKI_J(xs,yr,chip,xt,yt)
           
      ! Polynomial correction
      call poly_HAWKI_J(xt,yt,chip,xc,yc)
            
      return

      end


c This subroutine applies the table-of-residuals correction (J
c filter). It performes a bi-linear interpolation between the grid
c points.

      subroutine tab_HAWKI_J(xt,yt,chip,xc,yc)
      implicit none

      real*8 m_tab(12) ! Cell boundaries
      real*8 tab1cor(12,12,4) ! Table of residuals (12x12)
      real*8 dx_1(144),dy_1(144),dx_2(144),dy_2(144) ! Grid-point values
      real*8 dx_3(144),dy_3(144),dx_4(144),dy_4(144) ! Grid-point values
      integer i,j,jj ! Indexes
      integer chip ! Chip number
      real*8 xt,yt ! Uncorrected coordinates
      real*8 xc,yc ! Corrected coordinates
      ! Coefficients used to perform the bi-linear interpolation
      real*8 f1,f2,f3,f4
      real*8 c1x,c2x,c3x,c4x
      real*8 c1y,c2y,c3y,c4y
      real*8 area
      real*8 dx,dy ! Corrections to apply

      data m_tab/0.0, 256.0, 426.6667, 597.3333, 768.0,    
     .     938.6667, 1109.3333, 1280.0, 1450.6667, 1621.3333, 
     .     1792.0, 2048.0/

      data dx_1/-0.3726,0.0355,-0.0347,-0.071,-0.0581,0.0545,
     .     -0.0245,-0.0729,0.0649,0.0109,-0.0592,-0.0163,
     .     -0.0558,0.0416,-0.0361,0.0026,-0.0347,-0.0345,
     .     -0.0042,-0.0008,-0.0422,-0.0374,-0.0366,0.0085,
     .     -0.0475,0.0654,-0.0053,-0.062,-0.0502,-0.0679,
     .     -0.049,-0.0068,-0.027,0.0187,0.0438,-0.001,
     .     -0.0365,0.0576,-0.0312,-0.0802,-0.0581,-0.044,
     .     -0.0198,-0.0066,0.015,-0.0111,-0.0197,0.044,
     .     0.0357,-0.0091,-0.0096,-0.0832,-0.0937,-0.0382,
     .     0.0084,-0.0358,0.0203,0.0377,0.025,0.0263,
     .     -0.0072,-0.0079,-0.0189,-0.0382,-0.0153,-0.0607,
     .     -0.0334,-0.0088,0.0247,0.0309,0.0071,0.0485,
     .     -0.0899,-0.0518,-0.0136,-0.008,0.0001,0.0188,
     .     -0.0413,0.0439,-0.0249,0.0105,-0.0354,0.0428,
     .     -0.036,-0.027,-0.0383,-0.0603,-0.0133,-0.0275,
     .     0.0163,0.0266,0.0543,0.0547,-0.0034,0.0445,
     .     -0.0026,-0.0061,-0.0479,-0.049,0.0005,0.0304,
     .     0.0509,0.0264,0.019,0.0099,-0.007,0.0019,
     .     -0.027,-0.0312,0.0128,-0.0688,0.0338,0.02,
     .     0.0063,0.0584,0.0212,-0.0179,-0.0351,-0.0695,
     .     -0.0187,-0.0277,-0.0246,-0.0128,-0.0145,0.005,
     .     -0.0096,0.0479,0.0203,0.0036,-0.0549,0.0702,
     .     -0.0546,0.0167,-0.0456,-0.0219,-0.0541,-0.0339,
     .     0.0308,-0.0049,-0.0043,-0.0445,0.065,0.1842/
      data dy_1/-0.0177,-0.0618,-0.016,0.0936,-0.02,0.0249,
     .     0.0362,0.0699,-0.0255,0.0665,-0.0177,0.1444,
     .     -0.0032,0.0295,-0.0349,-0.0159,0.004,-0.0402,
     .     0.0566,0.0049,0.0234,0.0162,0.0645,0.0175,
     .     -0.0488,0.0205,-0.0146,0.0128,0.0391,-0.0483,
     .     0.0066,0,0.0453,0.054,-0.0031,0.0669,
     .     -0.0136,-0.0179,-0.0626,0.0189,-0.0263,-0.0071,
     .     0.0324,0.0397,0.0507,0.0306,0.0352,0.0676,
     .     0.0092,-0.011,0.0027,0.0355,0.0583,0.0056,
     .     0.0132,0.0832,0.0491,0.0057,0.0289,0.0702,
     .     -0.0546,-0.022,0.016,-0.0027,0.0558,0.0382,
     .     0.0241,0.0179,0.0317,0.0294,0.0509,0.0433,
     .     0.0289,-0.0222,-0.0311,0.0021,0.0025,0.091,
     .     0.0086,0.0009,0.0317,0.0265,0.0283,0.0209,
     .     0.0522,0.0033,0.0109,0.0199,-0.0001,-0.0313,
     .     0.006,0.0633,-0.0256,0.0117,-0.0055,0.0601,
     .     -0.0025,0.0078,-0.0395,-0.0095,0.0101,0.0322,
     .     0.0372,0.0147,0.0448,0.0109,0.0325,0.0349,
     .     -0.0642,0.0199,-0.0204,0.0109,-0.0217,-0.0152,
     .     0.0182,-0.0156,0.0541,0.0291,0.0011,0.0825,
     .     -0.0393,0.0212,0.0024,-0.0199,0.013,-0.0046,
     .     -0.0181,0.0254,-0.0088,0.0487,0.0049,-0.0063,
     .     0.0245,-0.039,-0.048,0.0229,-0.0335,0.0402,
     .     -0.0129,-0.0113,-0.0182,-0.0293,0.0283,0.0137/
      data dx_2/0.0322,-0.0021,-0.0183,0.045,-0.0016,0.0281,
     .     0.0093,-0.0156,0.0137,-0.0047,-0.0086,-0.0366,
     .     0.0604,0.0082,0.0108,0.0066,0.0222,0.0067,
     .     0.0068,-0.0027,-0.0168,0.0003,-0.0462,0.0156,
     .     -0.0117,0.0263,-0.0455,0.0062,-0.0182,-0.0257,
     .     -0.0009,-0.0032,-0.0085,-0.0293,-0.0013,0.003,
     .     0.0428,-0.0201,0.0015,0.0085,-0.0275,-0.0037,
     .     -0.0217,0.0003,0.0054,0.0144,0.0014,-0.0413,
     .     -0.0299,0.0077,0.0045,0.0004,-0.0077,-0.0156,
     .     -0.0489,-0.0043,-0.0032,0.0048,-0.0263,-0.0039,
     .     -0.0107,-0.0198,0.0452,-0.0123,0.04,-0.0462,
     .     -0.0229,0.0045,0.0216,-0.0038,-0.0335,-0.0723,
     .     -0.0044,-0.0369,0.0002,0.0164,-0.03,-0.0233,
     .     -0.0481,-0.0055,0.0156,-0.0055,-0.004,-0.0147,
     .     -0.0387,-0.0317,0.0089,-0.0123,-0.0382,0.0238,
     .     0.0014,-0.0201,-0.0633,-0.0318,-0.0132,-0.0381,
     .     0.0168,0.0227,0.0127,-0.0321,0.0186,-0.0063,
     .     -0.0184,-0.0358,-0.0103,-0.0183,-0.0524,-0.0158,
     .     0.0315,-0.0386,-0.0105,-0.0027,-0.0177,-0.0151,
     .     0.0044,-0.0473,0.0026,-0.0426,-0.0456,0.0081,
     .     -0.0441,0.0078,-0.062,-0.0003,-0.0417,-0.0207,
     .     0.0073,-0.001,-0.0252,-0.0173,-0.0628,0.0218,
     .     0.0159,0.0314,0.0267,-0.051,-0.0021,-0.0039,
     .     0.0053,-0.0188,-0.0794,-0.0276,0.0235,-0.0903/
      data dy_2/-0.0166,-0.0053,-0.001,-0.0347,-0.0187,0.0348,
     .     0.0186,0.0155,0.004,0.0002,0.0246,-0.0506,
     .     0.039,0.0326,0.0148,-0.011,0.0163,0.0297,
     .     0.0288,0.0289,-0.0004,-0.0108,0.0237,-0.0187,
     .     -0.0057,0.0385,0.0001,0.0115,0.0327,0.004,
     .     -0.0038,-0.0182,-0.0307,0.0116,-0.0282,-0.0072,
     .     0.0288,-0.0167,0.0117,-0.0128,0.0297,-0.022,
     .     -0.0102,0.0126,0.0275,-0.0086,0.0144,0.0162,
     .     -0.0633,0.0315,-0.0326,0.0426,-0.0005,0.0183,
     .     -0.0089,-0.0053,-0.0041,0.0207,-0.0085,0.0006,
     .     0.0021,0.0093,-0.0242,0.0495,0.0294,-0.0002,
     .     0.0118,-0.0116,0.0065,0.0337,0.0075,-0.0058,
     .     -0.0092,-0.0074,-0.0097,0.0074,0.0392,0.0302,
     .     0.0426,0.0039,-0.0011,-0.0068,0.0181,-0.0091,
     .     0.0034,0.0183,-0.013,0.0151,0.0173,0.0085,
     .     -0.0038,0.0279,0.0301,0.0063,-0.0338,0.0118,
     .     0.0279,-0.0129,-0.0334,0.0285,-0.0313,-0.003,
     .     -0.0174,0.0213,-0.0083,-0.0019,-0.0158,0.0271,
     .     -0.055,0.0426,-0.0207,-0.0134,-0.0121,-0.0052,
     .     -0.0228,0.0456,0.0195,0.0066,0.0074,-0.0198,
     .     -0.0105,0.0089,-0.0074,-0.0479,-0.0063,0.0041,
     .     -0.0333,0.0059,0.0204,-0.0116,-0.0105,0.0618,
     .     -0.085,-0.0129,-0.0088,0.0467,0.026,0.0565,
     .     0.0012,-0.0083,0.0121,-0.0019,-0.0196,0.0694/
      data dx_3/0.0708,-0.0407,-0.0101,0.0246,0.055,0.0166,
     .     -0.0247,0.0398,0.0335,0.0148,0.0584,-0.0504,
     .     0.0675,-0.0202,0.033,0.0073,0.0178,-0.0038,
     .     0.0187,-0.0385,-0.0082,0.04,-0.0654,0.0264,
     .     0.0556,-0.0554,-0.0143,0.0247,0.0099,0.0006,
     .     -0.0186,0.0148,0.0112,-0.0185,0.0835,-0.0284,
     .     -0.0323,0.0063,-0.0149,0.0439,0.0218,0.0446,
     .     -0.039,-0.0004,0.0042,0.0461,0.0325,-0.0059,
     .     0.028,-0.0279,0.0249,0.0337,0.0168,-0.0131,
     .     0.0076,-0.0174,-0.0133,-0.0086,-0.0066,0.0979,
     .     0.0079,-0.0202,-0.0133,0.0558,0.0086,-0.0002,
     .     0.0524,-0.0058,0.001,-0.0342,0.0185,0.0012,
     .     -0.0389,0.0356,-0.0024,0.0326,0.0408,0.0201,
     .     -0.017,0.001,-0.0177,-0.0265,-0.0012,-0.0377,
     .     0.0217,-0.0171,0.0313,0.0119,-0.0096,-0.0113,
     .     -0.0337,-0.0387,-0.0706,-0.0446,0.0273,-0.0567,
     .     -0.0057,-0.0337,0.0114,-0.0526,-0.0135,-0.0022,
     .     -0.0074,0.021,-0.0204,0.0361,0.0301,-0.0208,
     .     0.0037,-0.0156,-0.0121,0.001,0.0088,-0.0009,
     .     -0.0515,-0.0135,0.0188,-0.0258,0.0559,-0.0268,
     .     0.0087,-0.0551,-0.0124,-0.0031,-0.0242,0.0375,
     .     0.0055,-0.0174,-0.0228,0.0179,-0.0084,0.0023,
     .     0.0685,-0.0059,-0.0038,0.0214,0.0365,-0.0668,
     .     -0.0155,-0.058,-0.038,-0.01,-0.0136,-0.0784/
      data dy_3/0.0075,-0.0076,0.0329,-0.0414,-0.0685,0.0712,
     .     0.0137,0.0093,-0.0207,0.0419,-0.0339,0.0694,
     .     -0.0108,0.004,0.0135,-0.0239,0.0238,0.0245,
     .     0.0534,0.0163,-0.0098,0.0138,0.064,0.0202,
     .     0.0016,0.0348,0.0007,0.0113,0.0429,0.0618,
     .     -0.0263,0.0093,0.005,0.0256,0.0051,0.0321,
     .     0.0981,-0.0329,-0.0015,0.0541,-0.0526,-0.0121,
     .     0.0045,0.0298,0.0153,0.011,0.0297,0.0064,
     .     0.0223,0.0092,-0.0039,-0.0098,0.0097,-0.0483,
     .     0.0161,-0.058,-0.002,0.0204,0.0191,-0.0713,
     .     -0.0327,0.0227,-0.009,0.0132,-0.0112,0.0408,
     .     0.0049,0.0163,0.0341,0.0407,0.0106,-0.013,
     .     -0.0008,0.0039,-0.0205,0.0328,0.0312,0.0163,
     .     0.0166,-0.036,0.0177,0.0459,0.0005,-0.0436,
     .     0.0156,0.0212,-0.0459,0.0419,0.026,-0.0032,
     .     -0.0074,0.0271,0.0043,-0.0215,0.0375,0.0029,
     .     0.0001,0.0137,0.0108,-0.013,0.0179,0.0127,
     .     -0.0149,-0.0312,-0.0361,-0.0384,0.0173,0.0103,
     .     -0.0166,0.0563,-0.0177,-0.0074,0.0002,0.038,
     .     -0.0246,-0.0085,-0.0055,-0.0375,-0.0213,-0.004,
     .     0.0145,-0.015,0.0014,-0.0092,-0.0199,0.0071,
     .     -0.0012,0.028,-0.0035,-0.0166,-0.0005,-0.0343,
     .     -0.0443,0.0424,0.0227,-0.0208,-0.024,0.0158,
     .     -0.0461,-0.0061,0.023,-0.0329,0.0161,-0.0079/
      data dx_4/-0.0936,-0.0127,-0.0684,0.0016,0.006,0.004,
     .     0.0225,0.0018,0.022,0.0342,-0.0386,0.011,
     .     0.0036,0.0209,0.0423,0.0106,0.0117,-0.0156,
     .     0.0022,-0.0158,0.0065,-0.0241,0.0125,-0.0254,
     .     -0.0032,0.0604,0.0102,-0.0136,0.0174,-0.0405,
     .     -0.0109,-0.0089,-0.0475,-0.0089,0.0191,-0.0109,
     .     -0.0124,0.0255,0.0015,-0.0096,-0.008,0.0273,
     .     -0.0196,0.0033,0.0004,0.0185,0.016,0.0006,
     .     0.057,-0.0104,-0.0051,0.0043,-0.0057,0.0018,
     .     0.0103,-0.033,-0.0216,-0.0004,-0.0054,0.002,
     .     0.049,0.002,-0.0031,0.0247,-0.0217,0.0151,
     .     -0.0197,-0.0038,0.0455,-0.0439,-0.0171,0.0123,
     .     -0.0167,0.0086,0.0243,0.0129,0.0324,0.0009,
     .     0.0411,-0.0048,0.023,-0.0179,-0.0513,-0.0484,
     .     0.0043,-0.0176,-0.0137,0.0277,-0.0251,0.0444,
     .     -0.0204,-0.0223,0.0031,0.0162,-0.0361,0.05,
     .     0.021,-0.0074,-0.0174,-0.0005,0.03,-0.0064,
     .     -0.0002,-0.0058,0.0139,0.0461,-0.0371,0.0418,
     .     0.0368,0.0015,-0.0077,0.0317,-0.0061,0.009,
     .     0.0062,0.019,0.0278,-0.0542,-0.0545,0.009,
     .     -0.0039,-0.0108,0.0294,-0.0059,0.0126,0.0016,
     .     0.0075,0.0005,-0.0416,-0.0149,-0.0213,-0.0222,
     .     0.0489,-0.018,0.0228,-0.01,-0.0145,-0.0085,
     .     -0.0267,0.027,0.0165,0.0106,-0.0387,0.1281/
      data dy_4/-0.0329,0.0158,-0.0333,0.0165,-0.0336,0.0145,
     .     0.0181,-0.0061,0.0615,-0.0092,0.0387,-0.0001,
     .     -0.0471,0.0031,0.0057,-0.0478,0.0299,0.0222,
     .     -0.0151,-0.0183,-0.018,0.0195,0.0009,0.0036,
     .     -0.0022,-0.0263,-0.0198,-0.0523,-0.0069,-0.003,
     .     -0.0119,0.0039,0.0129,0.0324,0.0202,0.0313,
     .     -0.0165,0.0201,-0.0049,0.0111,0.013,-0.0223,
     .     0.007,-0.0261,-0.0014,0.0269,0.015,-0.0187,
     .     -0.0732,0.0094,0.0141,-0.0126,-0.0155,-0.0031,
     .     0.0369,-0.0017,0.0091,0.0152,0.0316,0.0277,
     .     -0.0228,-0.0299,-0.0093,-0.011,0.001,0.0429,
     .     0.0236,-0.0385,0.0005,-0.0196,0.0059,-0.0183,
     .     0.0057,-0.0255,-0.0278,-0.0372,-0.0127,0.0396,
     .     -0.0593,-0.017,0.0238,-0.0269,0.011,-0.0022,
     .     0.0336,-0.0449,-0.0263,-0.0166,-0.0183,0.0033,
     .     -0.0084,0.0273,-0.0298,0.0011,-0.0031,-0.0051,
     .     -0.0259,-0.0042,0.0058,-0.0305,-0.0301,-0.0064,
     .     -0.0572,0.012,0.0618,0.0354,0.0051,0.0753,
     .     -0.0315,-0.0276,-0.005,0.0238,-0.01,-0.0407,
     .     0.0182,-0.0045,-0.0056,0.0268,0.0009,-0.0248,
     .     -0.0151,-0.0143,0.0341,-0.008,-0.0127,-0.0039,
     .     -0.0502,0.0306,0.0273,-0.0276,0.0069,-0.01,
     .     -0.0154,-0.0803,-0.0641,-0.0184,0.0225,0.0146,
     .     -0.069,0.0114,0.0031,-0.0037,0.0265,0.0685/

      ! Building the table of residuals
      do i=1,12
         do j=1,12
            jj=(i-1)*12+(j)
            if (chip.eq.1) then
               tab1cor(i,j,3)=dx_1(jj)
               tab1cor(i,j,4)=dy_1(jj)
            endif
            if (chip.eq.2) then
               tab1cor(i,j,3)=dx_2(jj)
               tab1cor(i,j,4)=dy_2(jj)
            endif
            if (chip.eq.3) then
               tab1cor(i,j,3)=dx_3(jj)
               tab1cor(i,j,4)=dy_3(jj)
            endif
            if (chip.eq.4) then
               tab1cor(i,j,3)=dx_4(jj)
               tab1cor(i,j,4)=dy_4(jj)
            endif
            tab1cor(i,j,1)=m_tab(i)
            tab1cor(i,j,2)=m_tab(j)
         enddo
      enddo
      
      ! Bi-linear interpolation
      do i=1,11
         do j=1,11
            if (xt.gt.tab1cor(i,j,1)
     .           .and.xt.le.tab1cor((i+1),j,1)
     .           .and.yt.gt.tab1cor(i,j,2)
     .           .and.yt.le.tab1cor(i,(j+1),2)) then
               c1x=tab1cor(i,j,3)
               c1y=tab1cor(i,j,4)
               c2x=tab1cor((i+1),j,3)
               c2y=tab1cor((i+1),j,4)
               c3x=tab1cor((i+1),(j+1),3)
               c3y=tab1cor((i+1),(j+1),4)
               c4x=tab1cor(i,(j+1),3)
               c4y=tab1cor(i,(j+1),4)
               area=((tab1cor((i+1),j,1)-tab1cor(i,j,1))*
     .              (tab1cor(i,(j+1),2)-tab1cor(i,j,2)))
               f1=((tab1cor((i+1),j,1)-xt)*
     .              (tab1cor(i,(j+1),2)-yt))/area
               f2=((xt-tab1cor(i,j,1))*
     .              (tab1cor(i,(j+1),2)-yt))/area
               f3=((xt-tab1cor(i,j,1))*
     .              (yt-tab1cor(i,j,2)))/area
               f4=((tab1cor((i+1),j,1)-xt)*
     .              (yt-tab1cor(i,j,2)))/area
               
               dx=(f1*c1x + f2*c2x + f3*c3x + f4*c4x)*1.0d0
               dy=(f1*c1y + f2*c2y + f3*c3y + f4*c4y)*1.0d0
               goto 19871
            else
               dx=0
               dy=0
            endif
         enddo               
      enddo

19871 continue
         
      ! Applying the corrections (xt,yt) -> (xc,yc)
      xc=xt-1*dx
      yc=yt-1*dy
      
      return

      end


c This subroutine computes the average periodicity correction (J
c filter).

      real*8 function step_J(x)
      implicit none

      real*8 x ! x coordinate  
      integer i,iu ! Indexes
      real fx ! x coordinate modulus 128
      real fxlist(6) ! Nodes of the periodic trend
      real dxlist(6) ! Median value of the periodicity in each node
      real ff ! Multiplying factor
      
      data fxlist/-1.7926, 2.8191, 62.2074, 66.8191, 126.2074, 130.8191/ 
      data dxlist/-0.0354,  0.0294,  0.0294, -0.0354, -0.0354,  0.0294/  
      
      fx = x-int(x/128)*128

      ! Locating surrounding nodes
      iu = 1
      do i = 1, 5
         if (fx.ge.fxlist(i).and.
     .        fx.le.fxlist(i+1)) iu = i
      enddo 
      
      ! Computing the periodicity correction step_J
      ff  = (fx-fxlist(iu)) /(fxlist(iu+1)-fxlist(iu))
      step_J = dxlist(iu) + ff*(dxlist(iu+1)-dxlist(iu))
      
      return
      
      end
      

c This subroutine computes the fine-tuned periodicity correction for
c each chip (J filter).

      real*8 function adj_J(xr,cp)
      implicit none
      
      real*8 xr ! x coordinate
      real*8 aux ! Auxiliary variable
      integer i ! Index
      integer cp ! Chip number
      real*8 xf(33) ! Bins along the x axis
      real*8 df1(32),df2(32),df3(32),df4(32) ! Correction to apply in each bin
      data xf/0, 64, 128, 192, 256, 320, 384, 448, 512, 576, 640, 704,
     .     768, 832, 896, 960, 1024, 1088, 1152, 1216,1280, 1344, 
     .     1408, 1472, 1536, 1600, 1664, 1728, 1792, 1856, 1920, 1984,
     .     2048/
      data df1/-0.09229340,-0.01842250,0.08280690, 0.03377256,
     .     0.04801692, 0.01465257, 0.01323956,-0.04396537,-0.03375305,
     .     -0.05436793,-0.06196414,-0.05479639,-0.04657959,
     .     -0.02233258,-0.02039378,-0.00228406, 0.02147058,
     .     0.01468316, 0.03328425, 0.01909325, 0.04728674,
     .     0.02567947, 0.05253608, 0.00141833, 0.02891462,
     .     -0.01449756,-0.00015640,-0.03086354,-0.01538987,
     .     -0.03449348, 0.01520985, 0.04795376/
      data df2/-0.00057079,-0.00938317, 0.03910401, 0.00558897,
     .     0.01883318, 0.00968799, 0.02107634,-0.00283864,
     .     -0.00501769,-0.00111341,-0.01410563,-0.00253396,
     .     -0.01504978, 0.00733814,-0.00289078, 0.01462664,
     .     0.01051573, 0.00167924, 0.00862662,-0.01249793,
     .     -0.00420715,-0.00533898,-0.01359294,-0.01092856,
     .     0.00312047, 0.00764240,-0.00426377,-0.00472352,
     .     -0.01559434,-0.00561567,-0.02214873, 0.02180588/
      data df3/-0.01970432, 0.02884372,-0.00229708, 0.00692760,
     .     -0.01550447,-0.00570215,-0.00329345, 0.00664539,
     .     -0.00873392, 0.00734182, 0.00354078, 0.01972933,
     .     0.00737355, 0.01954487,-0.00104754, 0.00476784,
     .     -0.00350267, 0.01813762,-0.00254369, 0.01301167,
     .     -0.00999486, 0.00714465,-0.01685801,-0.00206452,
     .     -0.01019183,-0.01216047,-0.00662063,-0.00445498,
     .     0.00113138,-0.01083973, 0.00146945,-0.00985205/
      data df4/-0.02667851, 0.00742215, 0.00581334, 0.02662513,
     .     0.00825098, 0.02106631, 0.00659175, 0.00391510,
     .     -0.00935439,-0.01187671,-0.02663978,-0.02271541,
     .     -0.01644189,-0.01635498,-0.00226596,-0.00510062,
     .     -0.00015288, 0.02140232, 0.00927640, 0.01873252,
     .     0.00266790, 0.01953825,-0.00115431, 0.00147618,
     .     -0.01115331,-0.02372904,-0.00903966,-0.02259492,
     .     0.00447458,-0.03234343,-0.00275219, 0.01778092/

      ! Computing the correction in each bin
      do i=1,32
         if (xr.gt.xf(i).and.xr.le.xf(i+1)) then
            if (cp.eq.1) then
               aux=df1(i)
            endif
            if (cp.eq.2) then
               aux=df2(i)
            endif
            if (cp.eq.3) then
               aux=df3(i)
            endif
            if (cp.eq.4) then
               aux=df4(i)
            endif
            goto 9876
         endif
      enddo
      
 9876 continue
      
      adj_J=aux
      
      return
      
      end


c This subroutine performes the polynomial correction (J filter).

      subroutine poly_HAWKI_J(xr,yr,chip,xc,yc)

      implicit none
 
      integer i ! Index
      integer chip ! Chip number
      real*8 xr, yr ! Uncorrected coordinates
      real*8 xc, yc ! Corrected coordinates

      real*8 a(20) ! Polynomial coefficients
      real*8 b(20) ! Polynomial coefficients
      real*8 x_cen,y_cen ! Reference positions (1024,1024)

      real*8 dx, dy ! Corrections to apply
      real*8 rx, ry ! Normalized positions

      x_cen=1024
      y_cen=1024

      ! Choosing the polynomial coefficients

      a(1)=0
      a(2)=0

      if (chip.eq.1) then

         a(3)=  -1.0512897278d0
         a(4)=  -0.6044576452d0
         a(5)=  -0.3826869792d0
         a(6)=  +1.5505919087d0
         a(7)=  +0.3702019608d0
         a(8)=  +0.6291491413d0
         a(9)=  +0.6024497727d0
         a(10)= +0.1633212895d0
         a(11)= +0.0223658700d0
         a(12)= +0.0227871580d0
         a(13)= +0.0152826747d0
         a(14)= -0.0002487749d0
         a(15)= -1.0324884219d0
         a(16)= -0.2159877407d0
         a(17)= -0.3707914178d0
         a(18)= -0.2320280591d0
         a(19)= -0.1810933901d0
         a(20)= -0.4372275311d0
         b(1)=  +1.0142887079d0
         b(2)=  +0.3876406029d0
         b(3)=  -0.1284859527d0
         b(4)=  -0.6059178963d0
         b(5)=  -0.7079452709d0
         b(6)=  -0.0533213639d0
         b(7)=  +0.2251032727d0
         b(8)=  -0.0133963579d0
         b(9)=  +0.4682751824d0
         b(10)= -0.1089860215d0
         b(11)= -0.0578542063d0
         b(12)= -0.0062182046d0
         b(13)= +0.1011121742d0
         b(14)= +0.0030390234d0
         b(15)= +0.0578591239d0
         b(16)= +0.0671913777d0
         b(17)= +0.0566183334d0
         b(18)= -0.0815181945d0
         b(19)= +0.0030106818d0
         b(20)= -0.1904097200d0
  
      endif

      if (chip.eq.2) then

         a(3)= +0.8286224060d0
         a(4)= -0.6571529649d0
         a(5)= +0.2783767635d0
         a(6)= +0.8367972983d0
         a(7)= -0.1447417545d0
         a(8)= +0.2790942887d0
         a(9)= +0.0326254484d0
         a(10)=+0.0653440017d0
         a(11)=+0.0068546739d0
         a(12)=+0.0120480342d0
         a(13)=-0.0341362518d0
         a(14)=-0.0194050411d0
         a(15)=-0.3554291670d0
         a(16)=+0.1271942794d0
         a(17)=+0.0134796429d0
         a(18)=-0.0491591987d0
         a(19)=+0.0240978201d0
         a(20)=-0.0352610796d0
         b(1)= -0.9879004294d0
         b(2)= +0.6367412187d0
         b(3)= -0.2503351135d0
         b(4)= +0.5421550617d0
         b(5)= -0.8230635855d0
         b(6)= -0.0370429862d0
         b(7)= +0.2440315142d0
         b(8)= -0.0125214021d0
         b(9)= +0.3585912356d0
         b(10)=+0.0031425771d0
         b(11)=+0.0388916531d0
         b(12)=+0.0035849849d0
         b(13)=-0.1168321089d0
         b(14)=+0.0627178983d0
         b(15)=+0.0067860838d0
         b(16)=+0.0369731854d0
         b(17)=-0.0644525334d0
         b(18)=-0.0193269138d0
         b(19)=-0.0787185781d0
         b(20)=-0.0996896386d0       

      endif
     
      if (chip.eq.3) then

         a(3)= -0.8466109064d0
         a(4)= +0.4279204287d0
         a(5)= -0.3773148190d0
         a(6)= +0.3042183650d0
         a(7)= -0.0600027858d0
         a(8)= +0.2367041229d0
         a(9)= -0.4981033628d0
         a(10)=-0.0303747876d0
         a(11)=+0.0685422272d0
         a(12)=-0.0023207762d0
         a(13)=-0.0124762622d0
         a(14)=+0.0223772150d0
         a(15)=-0.0042178116d0
         a(16)=+0.0342531873d0
         a(17)=-0.0234227030d0
         a(18)=+0.1229066691d0
         a(19)=-0.0098351436d0
         a(20)=+0.3995710266d0
         b(1)= -1.2082760451d0
         b(2)= +0.1061868991d0
         b(3)= +0.1648701926d0
         b(4)= -0.5029514894d0
         b(5)= +0.7087854888d0
         b(6)= +0.0434558882d0
         b(7)= +0.1781563788d0
         b(8)= -0.0930159786d0
         b(9)= +0.3451223238d0
         b(10)=+0.0516348235d0
         b(11)=+0.0129975517d0
         b(12)=-0.0253233320d0
         b(13)=-0.0129298789d0
         b(14)=+0.0828496936d0
         b(15)=-0.0093137008d0
         b(16)=+0.0244176051d0
         b(17)=-0.0395666437d0
         b(18)=-0.0722792232d0
         b(19)=+0.0935092473d0
         b(20)=-0.0437351581d0
            
      endif

      if (chip.eq.4) then

         a(3)= +0.9166983576d0
         a(4)= +0.6709887466d0
         a(5)= +0.2876473515d0
         a(6)= +1.1652934906d0
         a(7)= +0.2401481899d0
         a(8)= +0.5319443561d0
         a(9)= -0.0095827994d0
         a(10)=+0.0053040693d0
         a(11)=+0.0085236383d0
         a(12)=+0.0834829477d0
         a(13)=+0.0153670924d0
         a(14)=-0.0284720879d0
         a(15)=-0.5668749074d0
         a(16)=-0.2435273026d0
         a(17)=-0.1487373201d0
         a(18)=+0.0481917794d0
         a(19)=-0.1242152535d0
         a(20)=-0.0059744627d0
         b(1)= +0.9418744129d0
         b(2)= +0.6642030014d0
         b(3)= +0.2049945864d0
         b(4)= +0.4871479061d0
         b(5)= +0.7421879336d0
         b(6)= -0.1657323887d0
         b(7)= +0.2088982277d0
         b(8)= +0.0911028705d0
         b(9)= +0.1299004057d0
         b(10)=+0.0599277209d0
         b(11)=+0.0482673098d0
         b(12)=+0.0909703201d0
         b(13)=-0.0079432053d0
         b(14)=-0.0039440715d0
         b(15)=+0.1269841933d0
         b(16)=+0.0776265256d0
         b(17)=+0.1247751537d0
         b(18)=+0.0488426952d0
         b(19)=-0.1300584891d0
         b(20)=+0.1496173183d0
           
      endif
      
      ! Computing the corrections
      rx=((xr)-x_cen)/x_cen
      ry=((yr)-y_cen)/y_cen
      dx=(a(1)*rx)+
     .     (a(2)*ry)+
     .     (a(3)*rx*rx)+
     .     (a(4)*rx*ry)+
     .     (a(5)*ry*ry)+
     .     (a(6)*rx*rx*rx)+
     .     (a(7)*rx*rx*ry)+
     .     (a(8)*rx*ry*ry)+
     .     (a(9)*ry*ry*ry)+
     .     (a(10)*rx*rx*rx*rx)+
     .     (a(11)*rx*rx*rx*ry)+
     .     (a(12)*rx*rx*ry*ry)+
     .     (a(13)*rx*ry*ry*ry)+
     .     (a(14)*ry*ry*ry*ry)+
     .     (a(15)*rx*rx*rx*rx*rx)+
     .     (a(16)*rx*rx*rx*rx*ry)+
     .     (a(17)*rx*rx*rx*ry*ry)+
     .     (a(18)*rx*rx*ry*ry*ry)+
     .     (a(19)*rx*ry*ry*ry*ry)+
     .     (a(20)*ry*ry*ry*ry*ry)
      dy=(b(1)*rx)+
     .     (b(2)*ry)+
     .     (b(3)*rx*rx)+
     .     (b(4)*rx*ry)+
     .     (b(5)*ry*ry)+
     .     (b(6)*rx*rx*rx)+
     .     (b(7)*rx*rx*ry)+
     .     (b(8)*rx*ry*ry)+
     .     (b(9)*ry*ry*ry)+
     .     (b(10)*rx*rx*rx*rx)+
     .     (b(11)*rx*rx*rx*ry)+
     .     (b(12)*rx*rx*ry*ry)+
     .     (b(13)*rx*ry*ry*ry)+
     .     (b(14)*ry*ry*ry*ry)+
     .     (b(15)*rx*rx*rx*rx*rx)+
     .     (b(16)*rx*rx*rx*rx*ry)+
     .     (b(17)*rx*rx*rx*ry*ry)+
     .     (b(18)*rx*rx*ry*ry*ry)+
     .     (b(19)*rx*ry*ry*ry*ry)+
     .     (b(20)*ry*ry*ry*ry*ry)

      ! Applying the corrections (xr,yr) -> (xc,yc)
      xc=xr-dx
      yc=yr-dy
   
      return
      
      end


c This subroutine applies the table-of-residuals correction (H
c filter). It performes a bi-linear interpolation between the grid
c points.

      subroutine tab_HAWKI_H(xt,yt,chip,xc,yc)
      implicit none

      real*8 m_tab(12) !  Cell boundaries
      real*8 tab1cor(12,12,4) ! Table of residuals (12x12)
      real*8 dx_1(144),dy_1(144),dx_2(144),dy_2(144) ! Grid-point values
      real*8 dx_3(144),dy_3(144),dx_4(144),dy_4(144) ! Grid-point values
      integer i,j,jj ! Indexes
      integer chip ! Chip number
      real*8 xt,yt ! Uncorrected coordinates
      real*8 xc,yc ! Corrected coordinates
      ! Coefficients used to perform the bi-linear interpolation
      real*8 f1,f2,f3,f4
      real*8 c1x,c2x,c3x,c4x
      real*8 c1y,c2y,c3y,c4y
      real*8 area
      real*8 dx,dy ! Corrections to apply

      data m_tab/0.0, 256.0, 426.6667, 597.3333, 768.0,    
     .     938.6667, 1109.3333, 1280.0, 1450.6667, 1621.3333, 
     .     1792.0, 2048.0/

      data dx_1/-0.084,-0.0172, 0.0282, 0.0502, -0.005, 0.021,-0.0224,
     .     -0.0004,-0.0264, 0.0206, 0.0393, 0.0042, 0.0233,  0.023,
     .      0.0131, 0.0018, 0.0214, 0.0287, -0.002,  0.024,-0.0095,
     .      -0.007,-0.0024, 0.0032, 0.0526, -0.006, 0.0006,-0.0227,
     .     -0.0063,-0.0146, 0.0066,-0.0083, 0.0043, 0.0048,-0.0021,
     .      0.0318,  0.041, 0.0144,-0.0321, -0.013,-0.0165,-0.0085,
     .      0.0193,-0.0041, 0.0054, 0.0005,-0.0021,-0.0073, 0.0175,
     .      0.0114,-0.0296, 0.0158,-0.0243,-0.0147, 0.0076, 0.0127,
     .      0.0085, 0.0065, 0.0159, 0.0263,-0.0457,-0.0007, 0.0533,
     .      0.0044,  0.035,-0.0267,-0.0046,-0.0176,-0.0029, 0.0168,
     .     -0.0047,-0.0081, -0.025,-0.0113,-0.0023, 0.0263, 0.0427,
     .      0.0091,-0.0211,-0.0189, 0.0014, 0.0068,-0.0094,  0.016,
     .      -0.033,-0.0058, -0.029,-0.0177, 0.0116, 0.0025,-0.0375,
     .      0.0183,-0.0005,-0.0027, 0.0277,-0.0232, 0.0015,-0.0394,
     .     -0.0084,-0.0312,  0.007,-0.0058,-0.0019, 0.0106, 0.0061,
     .     -0.0053,-0.0059, 0.0265, 0.0102,-0.0179,-0.0194,-0.0168,
     .     -0.0127, 0.0312, 0.0065, -0.004,-0.0068, 0.0109,-0.0248,
     .     -0.0004,-0.0107,-0.0061,-0.0161, -0.002, 0.0028,-0.0168,
     .      0.0082,  0.009, 0.0131,-0.0458, 0.0161, 0.0066, -0.021,
     .     -0.0014,-0.0295, 0.0086,-0.0483, 0.0069,-0.0193,-0.0169,
     .     -0.0051, 0.0357, -0.046, 0.0643/
      data dy_1/-0.0549,-0.0112,-0.0117, 0.0508,-0.0426,-0.0063, 0.0149,
     .     -0.0189, 0.0208,  0.004,-0.0066,-0.0137, 0.0432,-0.0058,
     .      0.0042, 0.0069,-0.0095,-0.0261,-0.0085, 0.0244,-0.0182, 
     .     0.0041, 0.0017,  0.011, -0.042,-0.0011, 0.0109,-0.0157,
     .     -0.0084,-0.0188, 0.0259,-0.0522, 0.0184,-0.0089,-0.0078,
     .     -0.0158, 0.0105, 0.0302,-0.0212, 0.0012,-0.0171, 0.0011,
     .      0.0016, 0.0153,-0.0021, 0.0038, 0.0003, 0.0324,-0.0125,
     .     -0.0115,-0.0131, 0.0162, 0.0023, 0.0172, 0.0124, 0.0017,
     .      0.0144,-0.0124,-0.0027, 0.0082,-0.0074, 0.0225,-0.0104,
     .     -0.0122, 0.0156, 0.0082,-0.0055, 0.0223, 0.0075,-0.0055,
     .     -0.0268, 0.0246, -0.003, 0.0004, 0.0101, 0.0105,-0.0205,
     .     -0.0113,  0.014,-0.0089,-0.0087, 0.0203, 0.0187,-0.0166,
     .     -0.0203, 0.0241, 0.0047, 0.0059,  0.015, 0.0373, -0.016,
     .     -0.0016, 0.0146,-0.0072,-0.0084, 0.0126, 0.0262,-0.0059,
     .     -0.0024,-0.0226, 0.0113,-0.0213, 0.0011, 0.0092,-0.0002,
     .       0.024, -0.006, 0.0156,-0.0265, 0.0181,-0.0036, 0.0065,
     .      0.0249,-0.0141, 0.0011,-0.0079, 0.0009, 0.0155,-0.0005,
     .      0.0068,  0.015, 0.0079,-0.0117, 0.0051, 0.0119,   0.01,
     .     -0.0033, 0.0135,-0.0136,-0.0244, 0.0399,-0.0063, 0.0001,
     .     -0.0069, 0.0052, -0.011,-0.0015, 0.0066, 0.0186,-0.0417,
     .      0.0253, 0.0395,-0.0339,-0.0115/
      data dx_2/-0.0223, 0.0069, 0.0105, 0.0151, 0.0494, -0.027, 0.0311,
     .      0.0053, 0.0218, 0.0085, 0.0099,-0.0324, -0.013, 0.0154,
     .     -0.0288, 0.0209,  0.004, 0.0089, 0.0047, 0.0155, 0.0137,
     .       0.002, 0.0261, 0.0003, 0.0135,-0.0082, 0.0246, 0.0124,
     .     -0.0019,-0.0061,  0.014, 0.0194,-0.0214, 0.0013,-0.0021,
     .       0.005, 0.0501,-0.0261,  0.004,-0.0168, 0.0011,-0.0013,
     .     -0.0035, 0.0091, 0.0095,-0.0073, 0.0195, 0.0115,-0.0209,
     .      0.0073, 0.0109,-0.0129, 0.0161,  0.001, 0.0095,-0.0007,
     .     -0.0014,-0.0125, 0.0246,-0.0044,-0.0108,-0.0232, 0.0037,
     .      0.0482,-0.0172, 0.0183,-0.0106, 0.0021, 0.0015, 0.0033,
     .     -0.0119,-0.0087,  0.011, -0.009,-0.0077,-0.0027,-0.0101,
     .      -0.006, 0.0091,-0.0088, 0.0037,-0.0095, 0.0162,-0.0005,
     .     -0.0136,-0.0059, 0.0164, 0.0235,-0.0036, 0.0029, 0.0273,
     .     -0.0362,-0.0117,-0.0039,-0.0152,-0.0218,-0.0404, 0.0309,
     .     -0.0236, 0.0059,-0.0049,-0.0033,-0.0079,-0.0158,-0.0291,
     .      0.0057,-0.0101, 0.0065, 0.0169,-0.0397,-0.0044,-0.0001,
     .     -0.0162,-0.0129, 0.0054, 0.0079,-0.0181, 0.0363,-0.0099,
     .     -0.0004,-0.0234, 0.0042, 0.0006,-0.0576, 0.0228, 0.0137,
     .     -0.0381,-0.0009,-0.0011,-0.0154, 0.0272,-0.0329, 0.0381,
     .     -0.0367,-0.0041, 0.0119, 0.0056,-0.0221, 0.0316,-0.0092,
     .     -0.0418,-0.0144,-0.0439, 0.0172/
      data dy_2/-0.0213, 0.0324, -0.004,-0.0129, 0.0239, 0.0049,-0.0043,
     .      0.0233, 0.0004,-0.0178,-0.0012,-0.0138, 0.0128,-0.0057,
     .     -0.0027, 0.0113,-0.0225, 0.0185,-0.0141, 0.0028, 0.0054,
     .     -0.0061, 0.0098, 0.0021,-0.0054, 0.0053,  0.021,-0.0064,
     .      0.0167,-0.0044, 0.0152, 0.0012,-0.0308, 0.0289, 0.0024,
     .     -0.0012, 0.0311, 0.0076,-0.0089,-0.0111, 0.0307, 0.0218,
     .     -0.0122, 0.0277,-0.0109,-0.0172,-0.0062,-0.0249,-0.0239,
     .      0.0231, 0.0034,-0.0109, 0.0195, 0.0241,-0.0079,-0.0002,
     .      0.0091, -0.013, 0.0072, 0.0056, 0.0217,-0.0238,-0.0042,
     .      -0.002,-0.0006, 0.0305,-0.0105,-0.0075,-0.0048, 0.0027,
     .     -0.0131, 0.0197,-0.0043, 0.0129, 0.0038,-0.0173, 0.0443,
     .      0.0115, 0.0247,-0.0016, 0.0024, 0.0094, 0.0072,-0.0211,
     .     -0.0326, 0.0374,-0.0217, 0.0007, 0.0243, 0.0251, 0.0037,
     .     -0.0096, 0.0116, 0.0103, 0.0076, -0.003, 0.0066, 0.0082,
     .      0.0069, 0.0003, 0.0212, 0.0081,  0.004,-0.0066,  0.007,
     .      0.0146,-0.0429, 0.0283,-0.0079, 0.0208,-0.0098,-0.0151,
     .      -0.023,-0.0142,-0.0323, 0.0193, 0.0105, 0.0053,-0.0085,
     .      0.0055,-0.0475, 0.0051, 0.0092,-0.0361, 0.0316, 0.0216,
     .     -0.0082,-0.0139, 0.0025, 0.0112,-0.0123, 0.0082, 0.0436,
     .     -0.0231, 0.0046, 0.0203, 0.0004, 0.0057, 0.0148,-0.0067,
     .      0.0135, 0.0005,-0.0143, 0.0506/
      data dx_3/0.0099,-0.0061,-0.0267, 0.0305,-0.0114, 0.0237,-0.0128,
     .      0.0074, 0.0189, 0.0112, 0.0201,-0.0771, 0.0533,-0.0137,
     .      0.0156,-0.0011, 0.0178,-0.0102,  0.011,-0.0039, 0.0046,
     .      0.0158, 0.0255, -0.002, 0.0239,-0.0029, 0.0107, 0.0136,
     .      0.0041,  0.016,-0.0116,-0.0113,-0.0276, 0.0056, 0.0344,
     .      0.0024,  0.024,-0.0247, 0.0166,-0.0074, 0.0457,-0.0042,
     .     -0.0042,  0.006,-0.0167, 0.0274, 0.0096, 0.0577,-0.0129,
     .      0.0107, 0.0036, 0.0359,-0.0027, 0.0262,-0.0007,-0.0061,
     .     -0.0201,-0.0388, 0.0294,-0.0042, 0.0146,-0.0045, 0.0156,
     .      0.0398, 0.0296, 0.0173, 0.0014,-0.0099,-0.0333,-0.0105,
     .     -0.0076, 0.0269, 0.0095,-0.0043, 0.0361, 0.0109, 0.0072,
     .       0.013,-0.0135,-0.0103,-0.0133,-0.0278, 0.0171,-0.0173,
     .      0.0225, -0.022, 0.0166, 0.0311,   0.01,-0.0038, 0.0209,
     .      -0.019,-0.0215,-0.0059,-0.0008, 0.0202,-0.0083,-0.0053,
     .     -0.0109, 0.0187,-0.0021, 0.0163,-0.0043,-0.0062,-0.0073,
     .      0.0122, 0.0075,-0.0019, 0.0219,-0.0322, 0.0011, -0.005,
     .      0.0405,-0.0007, 0.0263,-0.0124, 0.0022, 0.0079, 0.0185,
     .      0.0053, 0.0059,-0.0042,-0.0237, 0.0469,-0.0158, 0.0067,
     .      0.0094,-0.0229, -0.003, 0.0086, 0.0237,-0.0167, 0.0924,
     .     -0.0175,-0.0106,-0.0256, 0.0475,-0.0188, 0.0082, 0.0036,
     .     -0.0436,-0.0003, 0.0313,-0.0416/
      data dy_3/-0.0425, 0.0013, 0.0037, 0.0056,-0.0086,-0.0179,-0.0161,
     .     -0.0125,  0.005, -0.005, 0.0021, 0.0627,-0.0326, 0.0098,
     .     -0.0011,-0.0205, 0.0097, 0.0019, 0.0141,-0.0073, 0.0196,
     .      0.0135, 0.0057,-0.0082,  0.004,-0.0079,-0.0371, 0.0159,
     .     -0.0151, 0.0085, -0.003, 0.0143, 0.0024,-0.0077, 0.0226,
     .      0.0172, 0.0332,-0.0136, 0.0021, -0.013, 0.0104, 0.0108,
     .      0.0021,  0.006, 0.0093, 0.0215,-0.0269, 0.0331,-0.0166,
     .      0.0061,-0.0155, 0.0055, 0.0037,-0.0096, -0.006,   0.03,
     .     -0.0063,-0.0096, 0.0055,-0.0177, 0.0184,-0.0048, 0.0065,
     .       0.002,-0.0115, 0.0362,-0.0099,  0.005,-0.0174, 0.0172,
     .      0.0216,-0.0085, 0.0001,-0.0168, 0.0023,-0.0018, 0.0245,
     .      0.0144, 0.0018,-0.0054, 0.0069,-0.0023, 0.0004, 0.0063,
     .      0.0033, 0.0053, 0.0015,-0.0138, 0.0165, 0.0154, 0.0032,
     .     -0.0268, 0.0103, 0.0021, 0.0134,-0.0067,-0.0023,-0.0026,
     .     -0.0096,  0.005, 0.0106, 0.0051, 0.0037,-0.0143, 0.0007,
     .       0.009, 0.0151,  0.027,  0.017,-0.0119,-0.0059,-0.0106,
     .     -0.0013, 0.0163, 0.0247,-0.0166,-0.0112,-0.0106, -0.036,
     .      0.0161,-0.0305, 0.0359,-0.0065,-0.0226,-0.0017,-0.0061,
     .      0.0108,-0.0169,  0.001, 0.0057,-0.0007,-0.0259,-0.0441,
     .       0.012, 0.0006, 0.0095,-0.0206, 0.0198, -0.007,-0.0298,
     .      0.0154, 0.0113,-0.0142, 0.0035/      
      data dx_4/-0.0155,-0.0146,-0.0087,-0.0043, 0.0286, 0.0055,-0.0177,
     .     -0.0057,  0.017, 0.0019, 0.0277,-0.1152,-0.0227, 0.0236,
     .      0.0082, 0.0027,-0.0093, 0.0065,-0.0094, -0.004,  0.007,
     .     -0.0161, 0.0249,-0.0297, 0.0388, 0.0095, 0.0307,-0.0023,
     .     -0.0067,-0.0035,-0.0224,-0.0271,-0.0102, 0.0076, 0.0204,
     .      0.0287, 0.0403,-0.0104, 0.0066,-0.0066,-0.0015, 0.0124,
     .     -0.0106,-0.0231, 0.0136,-0.0026, 0.0149, 0.0131, 0.0186,
     .       0.035,-0.0149, 0.0204, 0.0013, 0.0266, 0.0152,-0.0107,
     .      0.0061, 0.0145,-0.0545, 0.0291, 0.0321, 0.0054, 0.0271,
     .      0.0278,-0.0033,-0.0135,-0.0021,-0.0074,-0.0034, -0.003,
     .     -0.0208,-0.0041,-0.0233,-0.0088, 0.0081, 0.0042, 0.0467,
     .      0.0185,-0.0038,  0.017, 0.0182,-0.0324,-0.0221, -0.014,
     .     -0.0033,-0.0115, 0.0263, 0.0052,  0.027, 0.0158, 0.0132,
     .      0.0407,-0.0027, 0.0032,-0.0327, 0.0089,-0.0165, 0.0194,
     .      0.0231, 0.0041, 0.0091,-0.0151,-0.0218, 0.0002, 0.0552,
     .      0.0304,-0.0079, -0.048, 0.0371,-0.0111,-0.0041, 0.0229,
     .      0.0298, 0.0154, 0.0158, 0.0147, 0.0177,-0.0439,-0.0253,
     .     -0.0328, 0.0478, 0.0058, 0.0299, 0.0325,-0.0038,-0.0015,
     .     -0.0158, 0.0103,-0.0196,-0.0089,-0.0363, 0.0222, 0.0331,
     .     -0.0117,-0.0171,-0.0108,  0.003, 0.0388,-0.0229,  0.002,
     .      0.0536,-0.0452,-0.0093, 0.1084/
      data dy_4/-0.0205,-0.0248, 0.0404,-0.0114, 0.0038, 0.0158,-0.0109,
     .      0.0055, 0.0165, 0.0141, 0.0213,-0.0317,-0.0509,-0.0007,
     .     -0.0282, -0.003,-0.0007,-0.0004,-0.0049, -0.022, 0.0341,
     .      0.0023, 0.0108,-0.0068,  0.012,-0.0119, -0.009,-0.0155,
     .     -0.0097, 0.0078,-0.0126, 0.0019,  0.024, 0.0243, 0.0277,
     .      0.0152,-0.0397, 0.0178,-0.0328,-0.0087,-0.0016,-0.0023,
     .     -0.0344, -0.016, 0.0157, 0.0114,-0.0042, 0.0256,-0.0243,
     .     -0.0206,-0.0029,-0.0018, 0.0081, 0.0045, 0.0305,-0.0211,
     .      0.0065, 0.0062, 0.0023, 0.0225, 0.0027,-0.0152,-0.0145,
     .      0.0057,-0.0088,  0.006,-0.0034, 0.0437,-0.0031, 0.0114,
     .      0.0004,  0.005,-0.0181,-0.0162,-0.0207, 0.0051, 0.0268,
     .      0.0061,-0.0014,-0.0038, 0.0114,-0.0441,-0.0082, -0.013,
     .      -0.018,-0.0178,-0.0043,  -0.03,  0.016, 0.0146,-0.0161,
     .      0.0136, 0.0097, 0.0312, -0.043, 0.0538,-0.0054,-0.0178,
     .      0.0039, 0.0022,-0.0069,-0.0398,-0.0295, 0.0139, 0.0254,
     .      0.0525, 0.0346, 0.0091, -0.023, 0.0156,-0.0086,-0.0022,
     .     -0.0124,-0.0019, 0.0057,-0.0051,-0.0219, 0.0052,-0.0199,
     .      0.0517,-0.0019,  0.001,-0.0034, 0.0347,-0.0006,-0.0071,
     .      0.0323, 0.0044, 0.0338, 0.0052, 0.0162,-0.0157,-0.0397,
     .     -0.0084, 0.0046,-0.0042, 0.0163,-0.0127,-0.0147, 0.0209,
     .     -0.0158,-0.0098,-0.0036, 0.0131/

      ! Building the table of residuals
      do i=1,12
         do j=1,12
            jj=(i-1)*12+(j)
            if (chip.eq.1) then
               tab1cor(i,j,3)=dx_1(jj)
               tab1cor(i,j,4)=dy_1(jj)
            endif
            if (chip.eq.2) then
               tab1cor(i,j,3)=dx_2(jj)
               tab1cor(i,j,4)=dy_2(jj)
            endif
            if (chip.eq.3) then
               tab1cor(i,j,3)=dx_3(jj)
               tab1cor(i,j,4)=dy_3(jj)
            endif
            if (chip.eq.4) then
               tab1cor(i,j,3)=dx_4(jj)
               tab1cor(i,j,4)=dy_4(jj)
            endif
            tab1cor(i,j,1)=m_tab(i)
            tab1cor(i,j,2)=m_tab(j)
         enddo
      enddo
      
      ! Bi-linear interpolation
      do i=1,11
         do j=1,11
            if (xt.gt.tab1cor(i,j,1)
     .           .and.xt.le.tab1cor((i+1),j,1)
     .           .and.yt.gt.tab1cor(i,j,2)
     .           .and.yt.le.tab1cor(i,(j+1),2)) then
               c1x=tab1cor(i,j,3)
               c1y=tab1cor(i,j,4)
               c2x=tab1cor((i+1),j,3)
               c2y=tab1cor((i+1),j,4)
               c3x=tab1cor((i+1),(j+1),3)
               c3y=tab1cor((i+1),(j+1),4)
               c4x=tab1cor(i,(j+1),3)
               c4y=tab1cor(i,(j+1),4)
               area=((tab1cor((i+1),j,1)-tab1cor(i,j,1))*
     .              (tab1cor(i,(j+1),2)-tab1cor(i,j,2)))
               f1=((tab1cor((i+1),j,1)-xt)*
     .              (tab1cor(i,(j+1),2)-yt))/area
               f2=((xt-tab1cor(i,j,1))*
     .              (tab1cor(i,(j+1),2)-yt))/area
               f3=((xt-tab1cor(i,j,1))*
     .              (yt-tab1cor(i,j,2)))/area
               f4=((tab1cor((i+1),j,1)-xt)*
     .              (yt-tab1cor(i,j,2)))/area
               
               dx=(f1*c1x + f2*c2x + f3*c3x + f4*c4x)*1.0d0
               dy=(f1*c1y + f2*c2y + f3*c3y + f4*c4y)*1.0d0
               goto 19872
            else
               dx=0
               dy=0
            endif
         enddo               
      enddo

19872 continue
      
      ! Applying the corrections (xt,yt) -> (xc,yc)
      xc=xt-1*dx
      yc=yt-1*dy
      
      return

      end


c This subroutine computes the average periodicity correction (H
c filter).

      real*8 function step_H(x)
      implicit none
 
      real*8 x ! x coordinate      
      integer i,iu ! Indexes
      real fx ! x coordinate modulus 128
      real fxlist(6) ! Nodes of the periodic trend
      real dxlist(6) ! Median value of the periodicity in each node
      real ff ! Multiplying factor

      data fxlist/-1.7926, 2.8191, 62.2074, 66.8191, 126.2074, 130.8191/
      data dxlist/-0.0307,  0.0308,  0.0308, -0.0307, -0.0307,  0.0308/
      
      fx = x-int(x/128)*128

      ! Locating surrounding nodes
      iu = 1
      do i = 1, 5
         if (fx.ge.fxlist(i).and.
     .        fx.le.fxlist(i+1)) iu = i
      enddo 

      ! Computing the periodicity correction step_H
      ff  = (fx-fxlist(iu)) /(fxlist(iu+1)-fxlist(iu))
      step_H = dxlist(iu) + ff*(dxlist(iu+1)-dxlist(iu))
      
      return

      end


c This subroutine computes the fine-tuned periodicity correction for
c each chip (H filter).

      real*8 function adj_H(xr,cp)
      implicit none
      
      real*8 xr ! x coordinate
      real*8 aux ! Auxiliary variable
      integer i ! Index
      integer cp ! Chip number
      real*8 xf(33) ! Bins along the x axis
      real*8 df1(32),df2(32),df3(32),df4(32) ! Correction to apply in each bin
      data xf/0, 64, 128, 192, 256, 320, 384, 448, 512, 576, 640, 704,
     .     768, 832, 896, 960, 1024, 1088, 1152, 1216,1280, 1344, 
     .     1408, 1472, 1536, 1600, 1664, 1728, 1792, 1856, 1920, 1984,
     .     2048/
      data df1/-0.03653597,-0.01683692, 0.05469833, 0.01006390,
     .     0.02803087, 0.00144951, 0.02238706,-0.00970597,-0.01184281,
     .     -0.00800890,-0.01909435,-0.01470695,-0.01626401,-0.00784520,
     .     -0.00279102, 0.00089938, 0.01827749, 0.00644721, 0.01714099,
     .     -0.01396326, 0.01600293,-0.00629425, 0.01591781,-0.02024854,
     .     0.00993935,-0.02734480, 0.00118882,-0.02597595,-0.00572776,
     .     -0.02385361, 0.00282210, 0.01681508/
      data df2/-0.00573018,-0.01312266, 0.02455122, 0.00072540, 
     .     0.01207572, 0.00601698, 0.01785646, 0.00204755, 0.00229564,
     .     -0.00183026,-0.00376177,-0.00396094,-0.00668423, 0.00600069,
     .     -0.00233289, 0.00355879, 0.00794038,-0.00235126,-0.00363628,
     .     -0.01372475,-0.01696049,-0.00278456,-0.00230774,-0.00599730,
     .     0.00576759,-0.00034831,-0.00862713,-0.00692392,-0.01881527,
     .     -0.01018521,-0.01296909, 0.01117231/
      data df3/-0.03282133, 0.02015376,-0.00387097, 0.01158585,
     .     -0.00720655, 0.00969601,-0.00723553, 0.00281318,-0.00167665,
     .     0.00107689,-0.00486912, 0.00717943,-0.00335068, 0.00496140,
     .     -0.00929029, 0.00618596,-0.00418500, 0.01609246,-0.00100757,
     .     0.02221010, 0.00563842, 0.02569409, 0.00661268, 0.01119825,
     .     0.00381316,-0.00905178,-0.00683216,-0.00536052,-0.00219385,
     .     -0.01637549, 0.00737898, 0.01945867/
      data df4/-0.02578115, 0.00178316,-0.00094977, 0.02079139,
     .     0.00496320, 0.02571332, 0.00362911, 0.00398121,-0.00286118,
     .     -0.01073459,-0.02181914,-0.02408306,-0.01248392,-0.01472242,
     .     -0.00687808,-0.00484756, 0.00370996, 0.01856303, 0.01388201,
     .     0.03052538, 0.01518193, 0.02201300, 0.00846662, 0.01366063,
     .     0.00624949,-0.02019684,-0.00632956,-0.02824873, 0.00248207,
     .     -0.02980169, 0.01453919, 0.03945113/

      ! Computing the correction in each bin
      do i=1,32
         if (xr.gt.xf(i).and.xr.le.xf(i+1)) then
            if (cp.eq.1) then
               aux=df1(i)
            endif
            if (cp.eq.2) then
               aux=df2(i)
            endif
            if (cp.eq.3) then
               aux=df3(i)
            endif
            if (cp.eq.4) then
               aux=df4(i)
            endif
            goto 9876
         endif
      enddo
      
 9876 continue
      
      adj_H=aux
      
      return
      
      end


c This subroutine performes the polynomial correction (H filter).

      subroutine poly_HAWKI_H(xr,yr,chip,xc,yc)

      implicit none
 
      integer i ! Index
      integer chip ! Chip number
      real*8 xr, yr ! Uncorrected coordinates
      real*8 xc, yc ! Corrected coordinates

      real*8 a(20) ! Polynomial coefficients
      real*8 b(20) ! Polynomial coefficients
      real*8 x_cen,y_cen ! Reference positions (1024,1024)

      real*8 dx, dy ! Corrections to apply
      real*8 rx, ry ! Normalized positions

      x_cen=1024
      y_cen=1024

      ! Choosing the polynomial coefficients

      a(1)=0
      a(2)=0

      if (chip.eq.1) then

         a(3)=  -0.9262873453d0 
         a(4)=  -0.5395914856d0 
         a(5)=  -0.3677583773d0 
         a(6)=  +0.7507383632d0 
         a(7)=  +0.2075459339d0 
         a(8)=  +0.3588722245d0 
         a(9)=  +0.1200359368d0 
         a(10)= +0.1013012391d0 
         a(11)= +0.0489178163d0 
         a(12)= +0.0057044710d0 
         a(13)= +0.0006056042d0 
         a(14)= -0.0148946464d0 
         a(15)= -0.4113565474d0 
         a(16)= -0.1111300564d0 
         a(17)= -0.1112292252d0 
         a(18)= -0.1027690400d0 
         a(19)= -0.0623514002d0 
         a(20)= -0.0592246266d0 
         b(1)=  +0.8333560351d0 
         b(2)=  +0.1072553865d0 
         b(3)=  -0.1884914978d0 
         b(4)=  -0.4845687370d0 
         b(5)=  -0.6665861646d0 
         b(6)=  -0.0107377700d0 
         b(7)=  +0.2479572369d0 
         b(8)=  +0.0732608131d0 
         b(9)=  +0.3826908050d0 
         b(10)= -0.0620166482d0 
         b(11)= -0.0119326032d0 
         b(12)= +0.0237800576d0 
         b(13)= +0.0639446289d0 
         b(14)= +0.0080081405d0 
         b(15)= +0.0361449814d0 
         b(16)= +0.0009919634d0 
         b(17)= -0.0126362180d0 
         b(18)= -0.0324511888d0 
         b(19)= -0.0335687040d0 
         b(20)= -0.1120143808d0   

      endif

      if (chip.eq.2) then

         a(3)= +0.8672302657d0 
         a(4)= -0.6007508980d0 
         a(5)= +0.2354920932d0 
         a(6)= +0.6426184930d0 
         a(7)= -0.1293972518d0 
         a(8)= +0.2867055878d0 
         a(9)= -0.1640009020d0 
         a(10)=+0.0478220115d0 
         a(11)=+0.0372482526d0 
         a(12)=+0.0563785988d0 
         a(13)=-0.0465517402d0 
         a(14)=-0.0076826317d0 
         a(15)=-0.2203116674d0 
         a(16)=+0.0979498991d0 
         a(17)=-0.0066495719d0 
         a(18)=-0.0367651817d0 
         a(19)=+0.0173223814d0 
         a(20)=+0.1187331988d0 
         b(1)= -1.1888958042d0 
         b(2)= +0.4693547758d0 
         b(3)= -0.2446243551d0 
         b(4)= +0.6234216938d0 
         b(5)= -0.6587555453d0 
         b(6)= -0.0545513131d0 
         b(7)= +0.2346728924d0 
         b(8)= -0.0120076656d0 
         b(9)= +0.4239546095d0 
         b(10)=-0.0262152999d0 
         b(11)=+0.0376471043d0 
         b(12)=-0.0470307971d0 
         b(13)=-0.0464156644d0 
         b(14)=-0.0394992397d0 
         b(15)=+0.0205538780d0 
         b(16)=+0.0509055739d0 
         b(17)=-0.0312931868d0 
         b(18)=+0.0028310290d0 
         b(19)=-0.0701136235d0 
         b(20)=-0.1392902800d0 

      endif
         
      if (chip.eq.3) then
         
         a(3)= -0.8154979339d0
         a(4)= +0.4987005078d0
         a(5)= -0.4159453150d0
         a(6)= +0.6093351065d0
         a(7)= -0.0423500147d0
         a(8)= +0.3090972505d0
         a(9)= -0.3937622744d0
         a(10)=-0.0025169563d0
         a(11)=+0.0260678380d0
         a(12)=-0.0196011963d0
         a(13)=+0.0138516488d0
         a(14)=+0.0111667128d0
         a(15)=-0.2683516828d0
         a(16)=+0.0483011727d0
         a(17)=-0.0803344520d0
         a(18)=+0.0689614866d0
         a(19)=-0.0592254427d0
         a(20)=+0.3142619161d0
         b(1)= -1.5201158333d0
         b(2)= +0.1666912958d0
         b(3)= +0.1466777201d0
         b(4)= -0.3726904502d0
         b(5)= +0.7083811452d0
         b(6)= +0.0076704214d0
         b(7)= +0.1861076243d0
         b(8)= -0.1285834098d0
         b(9)= +0.3094454341d0
         b(10)=+0.0288082485d0
         b(11)=+0.0041682894d0
         b(12)=+0.0268069672d0
         b(13)=-0.0285040671d0
         b(14)=+0.0458994867d0
         b(15)=-0.0091811143d0
         b(16)=+0.0181577854d0
         b(17)=-0.0232364071d0
         b(18)=-0.0395335409d0
         b(19)=+0.1159338398d0
         b(20)=-0.0510279366d0
           
      endif

      if (chip.eq.4) then

         a(3)= +0.9634977715d0 
         a(4)= +0.7549536369d0 
         a(5)= +0.1584014036d0 
         a(6)= +0.9933182603d0 
         a(7)= +0.0922119133d0 
         a(8)= +0.5665027774d0 
         a(9)= -0.0334085922d0 
         a(10)=-0.0224352374d0 
         a(11)=+0.0025752254d0 
         a(12)=+0.1048300478d0 
         a(13)=+0.0283734762d0 
         a(14)=+0.0124766506d0 
         a(15)=-0.4594955652d0 
         a(16)=-0.1154489475d0 
         a(17)=-0.1699383427d0 
         a(18)=+0.1329683644d0 
         a(19)=-0.1417696520d0 
         a(20)=-0.0007888783d0 
         b(1)= +0.6672535663d0 
         b(2)= +0.5729028225d0 
         b(3)= +0.1742929344d0 
         b(4)= +0.5988786906d0 
         b(5)= +0.7704535106d0 
         b(6)= -0.0388703262d0 
         b(7)= +0.2296763456d0 
         b(8)= +0.0702752435d0 
         b(9)= +0.2385016874d0 
         b(10)=+0.0618116743d0 
         b(11)=+0.0218875462d0 
         b(12)=+0.0841264855d0 
         b(13)=+0.0104554778d0 
         b(14)=+0.0079329199d0 
         b(15)=+0.0423575242d0 
         b(16)=+0.0635303332d0 
         b(17)=+0.0771428653d0 
         b(18)=-0.0046003100d0 
         b(19)=-0.0743949388d0 
         b(20)=+0.0040929118d0
         
      endif

      ! Computing the corrections
      rx=((xr)-x_cen)/x_cen
      ry=((yr)-y_cen)/y_cen
      dx=(a(1)*rx)+
     .     (a(2)*ry)+
     .     (a(3)*rx*rx)+
     .     (a(4)*rx*ry)+
     .     (a(5)*ry*ry)+
     .     (a(6)*rx*rx*rx)+
     .     (a(7)*rx*rx*ry)+
     .     (a(8)*rx*ry*ry)+
     .     (a(9)*ry*ry*ry)+
     .     (a(10)*rx*rx*rx*rx)+
     .     (a(11)*rx*rx*rx*ry)+
     .     (a(12)*rx*rx*ry*ry)+
     .     (a(13)*rx*ry*ry*ry)+
     .     (a(14)*ry*ry*ry*ry)+
     .     (a(15)*rx*rx*rx*rx*rx)+
     .     (a(16)*rx*rx*rx*rx*ry)+
     .     (a(17)*rx*rx*rx*ry*ry)+
     .     (a(18)*rx*rx*ry*ry*ry)+
     .     (a(19)*rx*ry*ry*ry*ry)+
     .     (a(20)*ry*ry*ry*ry*ry)
      dy=(b(1)*rx)+
     .     (b(2)*ry)+
     .     (b(3)*rx*rx)+
     .     (b(4)*rx*ry)+
     .     (b(5)*ry*ry)+
     .     (b(6)*rx*rx*rx)+
     .     (b(7)*rx*rx*ry)+
     .     (b(8)*rx*ry*ry)+
     .     (b(9)*ry*ry*ry)+
     .     (b(10)*rx*rx*rx*rx)+
     .     (b(11)*rx*rx*rx*ry)+
     .     (b(12)*rx*rx*ry*ry)+
     .     (b(13)*rx*ry*ry*ry)+
     .     (b(14)*ry*ry*ry*ry)+
     .     (b(15)*rx*rx*rx*rx*rx)+
     .     (b(16)*rx*rx*rx*rx*ry)+
     .     (b(17)*rx*rx*rx*ry*ry)+
     .     (b(18)*rx*rx*ry*ry*ry)+
     .     (b(19)*rx*ry*ry*ry*ry)+
     .     (b(20)*ry*ry*ry*ry*ry)

      ! Applying the corrections (xr,yr) -> (xc,yc)
      xc=xr-dx
      yc=yr-dy
   
      return
      
      end

      
c This subroutine applies the table-of-residuals correction (Ks
c filter). It performes a bi-linear interpolation between the grid
c points.

      subroutine tab_HAWKI_Ks(xt,yt,chip,xc,yc)
      implicit none

      real*8 m_tab(12) ! Cell boundaries
      real*8 tab1cor(12,12,4) ! Table of residuals (12x12)
      real*8 dx_1(144),dy_1(144),dx_2(144),dy_2(144) ! Grid-point values
      real*8 dx_3(144),dy_3(144),dx_4(144),dy_4(144) ! Grid-point values
      integer i,j,jj ! Indexes
      integer chip ! Chip number
      real*8 xt,yt ! Uncorrected coordinates
      real*8 xc,yc ! Corrected coordinates
      ! Coefficients used to perform the bi-linear interpolation
      real*8 f1,f2,f3,f4
      real*8 c1x,c2x,c3x,c4x
      real*8 c1y,c2y,c3y,c4y
      real*8 area
      real*8 dx,dy ! Corrections to apply

      data m_tab/0.0, 256.0, 426.6667, 597.3333, 768.0,    
     .     938.6667, 1109.3333, 1280.0, 1450.6667, 1621.3333, 
     .     1792.0, 2048.0/

      data dx_1/-0.2421, -0.0743, -0.0466, -0.0245, -0.0024, 0.0014,
     .     -0.0325, 0.006, -0.0141, 0.0057, -0.0062, -0.0463,
     .     -0.0239, -0.0016, -0.0037, -0.0346, -0.0496, -0.0266,
     .     -0.0129, -0.0453, -0.0166, -0.0276, 0.0082, 0.0298,
     .     0.0015, -0.0005, -0.0355, -0.0322, -0.0659, -0.0465,
     .     -0.0582, -0.017, -0.0074, 0.014, 0.0335, 0.039,
     .     0.0165, 0.0143, -0.0266, -0.0469, -0.0495, -0.0491,
     .     -0.0237, -0.0088, 0.012, 0.0162, 0.0447, 0.0324,
     .     0.0255, -0.0038, -0.0256, -0.0251, -0.0482, -0.0214,
     .     -0.0079, -0.0116, 0.0164, 0.0217, 0.0028, 0.0398,
     .     -0.0068, -0.0109, 0.014, 0.0216, 0.0227, -0.0383,
     .     -0.0195, -0.0196, -0.0168, 0.0014, 0.0125, 0.0276,
     .     -0.0879, -0.0004, -0.0197, 0.0087, 0.0348, 0.0329,
     .     -0.0214, -0.0018, -0.0252, 0.005, 0.0002, 0.0248,
     .     -0.0336, -0.0352, -0.0001, -0.0193, 0.0116, 0.0029,
     .     0.0165, 0.0262, 0.0129, 0.0059, 0.0164, 0.0291,
     .     -0.046, -0.0074, -0.016, 0.0086, 0.0097, 0.0351,
     .     0.0282, 0.0295, 0.0245, 0.0039, -0.0159, -0.003,
     .     0.0059, -0.005, 0.0037, 0.0172, 0.0261, 0.0476,
     .     0.0425, 0.0482, 0.0105, -0.0162, -0.0092, -0.0351,
     .     0.0185, -0.0039, 0.0094, 0.0115, -0.0033, 0.0381,
     .     0.021, 0.0061, 0.0125, -0.0172, -0.0287, 0.0317,
     .     0.1031, 0.0321, -0.022, -0.0146, 0.0116, -0.0128,
     .     0.0356, -0.0187, 0.005, 0.0064, 0.054, 0.1095/
      
      data dy_1/-0.0771, 0.0081, -0.0296, 0.0344, -0.0236, 0.0392,
     .     0.0262, 0.0136, 0.0316, 0.0033, -0.0102, 0.0395,
     .     0.0368, 0.0011, 0.0079, -0.0013, 0.0419, -0.0198,
     .     0.0015, -0.0069, 0.0165, 0.0211, 0.0157, 0.0322,
     .     -0.0496, -0.0012, -0.0228, 0.0021, -0.0011, 0.0128,
     .     -0.0299, 0.0323, 0.0002, 0.0133, 0.0195, -0.0097,
     .     0.0459, -0.0373, -0.0254, 0.0083, 0.0065, 0.0302,
     .     -0.002, 0.016, 0.0255, 0.0415, -0.0117, 0.0333,
     .     0.0352, -0.0136, -0.0279, 0.0153, 0.0349, 0.024,
     .     0.0382, 0.0145, 0.0202, 0.0045, 0.018, -0.0498,
     .     -0.0074, -0.0121, -0.005, 0.0105, 0.044, 0.0308,
     .     0.0298, 0.0337, 0.0157, 0.0217, -0.007, 0.0174,
     .     0.0111, -0.0215, -0.0004, 0.0285, 0.0172, 0.0432,
     .     0.0184, 0.0221, 0.0407, 0.0209, 0.019, 0.0067,
     .     -0.0089, -0.0128, -0.0076, 0.0633, 0.055, 0.0014,
     .     0.0152, 0.0327, 0.0206, 0.0094, -0.008, 0.0005,
     .     -0.0335, 0.011, -0.0029, 0.0325, 0.0407, -0.0048,
     .     0.0156, 0.008, 0.0281, 0.0044, -0.0062, 0.0097,
     .     0.0073, -0.0042, 0.0014, 0.0225, 0.0087, 0.0082,
     .     0.0102, 0.0074, 0.0277, 0.0264, 0.0047, 0.0049,
     .     -0.0061, 0.0225, 0.0054, 0.0347, 0.0332, 0.0111,
     .     0.009, 0.0272, 0.012, 0.027, -0.0104, 0.0288,
     .     0.0356, 0.0093, 0.0196, 0.0205, 0.0205, 0.0312,
     .     0.0242, 0.0371, 0.0433, -0.011, 0.0409, -0.0419/

      data dx_2/0.0197, -0.0397, -0.0024, 0.0001, 0.0005, 0.004,
     .     -0.0066, 0.0122, 0.0176, 0.0076, -0.0015, -0.0186,
     .     0.0036, -0.0241, -0.0169, -0.0013, 0.0099, -0.0022,
     .     0.0096, -0.0077, -0.0005, 0.0078, 0.0093, -0.0081,
     .     -0.0117, 0.0044, -0.0253, -0.0084, 0.0081, -0.0123,
     .     -0.0144, -0.0035, -0.024, 0.0045, 0.0101, -0.0041,
     .     -0.0076, -0.0033, -0.0121, -0.0027, -0.0008, -0.0086,
     .     -0.0196, -0.0028, 0.0112, -0.0066, 0.0139, 0.0034,
     .     0.0141, -0.0264, 0.0238, 0.0082, -0.0013, -0.0013,
     .     -0.0131, -0.0034, -0.0067, 0.0143, -0.0033, -0.0044,
     .     -0.0042, -0.0288, 0.0037, 0.024, 0.0149, -0.0026,
     .     0.0042, 0.0108, -0.0003, 0.0014, 0.0143, -0.0056,
     .     -0.0002, -0.0071, 0.0043, 0.0076, 0.015, 0.0167,
     .     -0.0004, -0.0144, -0.0093, -0.0013, 0.0181, 0.0036,
     .     0.0177, -0.0078, 0.0051, 0.0226, 0.001, 0.0226,
     .     0, -0.0135, -0.0021, -0.0169, -0.0157, -0.0038,
     .     -0.0047, 0.003, 0.007, 0.0107, 0.0046, -0.014,
     .     0.0056, -0.0014, -0.015, -0.0052, -0.0223, -0.0076,
     .     -0.0385, 0.0083, 0.0033, 0.0167, 0.0171, 0.0053,
     .     0.001, 0.0038, 0.0064, 0.0075, 0.0164, -0.0107,
     .     0.0108, -0.0283, 0.0042, -0.0117, 0.0013, 0.0035,
     .     0.0219, 0.0004, 0.0048, 0.0136, 0.04, -0.0114,
     .     0.0558, 0.0264, 0.0168, 0.0155, 0.0269, 0.0111,
     .     0.0023, -0.0062, -0.0311, -0.0127, -0.0116, -0.0636/
      
      data dy_2/-0.0064, -0.0088, -0.0216, 0.0249, 0.0043, 0.0031,
     .     0.0106, 0.0404, 0.0125, 0.004, -0.0035, -0.0156,
     .     0.0063, 0.0189, 0.0216, 0.0013, 0.0022, -0.0066,
     .     0.0168, 0.0128, 0.018, -0.0341, 0.0001, -0.0019,
     .     -0.0084, 0.0038, 0.0127, 0.0123, 0.0111, 0.0009,
     .     -0.0116, 0.0232, -0.0009, -0.006, -0.0152, 0.0173,
     .     -0.0118, -0.0085, 0.0136, 0.0226, 0.0127, -0.0121,
     .     0.019, 0.0003, -0.0104, 0.0043, 0.0015, 0.0048,
     .     -0.0048, 0.018, -0.0001, -0.0081, 0.0218, 0.0102,
     .     0.0099, 0.0283, -0.0051, 0.0008, 0.0085, -0.0108,
     .     -0.0083, 0.0153, 0.0105, -0.0061, 0.0127, 0.0094,
     .     0.0341, 0.003, -0.0166, -0.0035, -0.0038, -0.0252,
     .     -0.0084, 0.0252, -0.0087, -0.002, 0.0145, 0.0164,
     .     0.0276, 0.0084, 0.009, -0.0103, 0.0023, -0.0157,
     .     -0.0019, 0.0106, 0.0076, 0.0023, 0.008, 0.0222,
     .     0.0079, 0.0205, 0.005, -0.0104, 0.0064, -0.0156,
     .     -0.0152, 0.0258, -0.006, -0.0035, 0.0006, 0.0272,
     .     -0.0027, 0.0058, -0.0007, -0.0009, -0.013, 0.0297,
     .     0.0212, 0.0065, 0.0079, -0.0173, -0.0026, 0.0077,
     .     0.0074, -0.0022, 0.0003, -0.0004, -0.0012, -0.0165,
     .     -0.0005, 0.0172, -0.0076, -0.0079, 0.0063, 0.0168,
     .     -0.013, -0.0093, 0.0092, -0.0147, -0.0151, 0.0238,
     .     -0.0182, -0.0001, -0.0126, 0.004, 0.0254, 0.0204,
     .     0.0127, -0.0006, -0.0178, 0.0218, 0.0253, -0.0307/

      data dx_3/0.0146, -0.0586, -0.0204, 0.0067, 0.0065, -0.0069,
     .     -0.0119, -0.0079, -0.0225, 0.0372, 0.0394, -0.0801,
     .     0.0007, -0.0444, -0.016, -0.0225, 0.0089, -0.007,
     .     0.0007, -0.0104, 0.0107, 0.0264, 0.0227, 0.0141,
     .     0.0144, -0.0371, -0.0059, 0.005, -0.0056, -0.02,
     .     -0.0055, -0.0157, -0.0211, 0.007, 0.0207, 0.019,
     .     -0.0094, -0.0209, -0.0109, 0, -0.0055, -0.018,
     .     -0.0245, -0.018, -0.0153, -0.0015, 0.0385, 0.0215,
     .     -0.0167, -0.0252, 0.0081, 0.0137, 0.0058, -0.0077,
     .     -0.0116, -0.0046, -0.0103, -0.0006, 0.0194, 0.0332,
     .     -0.0131, -0.0151, 0.0206, 0.0198, 0.0185, 0.0006,
     .     -0.009, -0.0304, -0.0056, -0.016, 0.0137, -0.0013,
     .     -0.0048, -0.0107, 0.0127, 0.0342, 0.0313, 0.0104,
     .     0.0082, -0.0148, -0.0145, -0.0192, 0.008, -0.0307,
     .     0.002, -0.0043, 0.0234, 0.0296, 0.0153, 0.0197,
     .     -0.0138, -0.0239, -0.026, -0.0269, 0.0147, -0.0197,
     .     0.0083, -0.0228, 0.0102, 0.0138, -0.0039, 0.0075,
     .     0.0033, -0.0088, 0.0024, -0.0129, 0.0128, -0.0368,
     .     0.0042, -0.021, -0.0073, 0.0222, 0.0097, 0.0041,
     .     0.022, 0.0054, -0.0021, 0.0064, 0.0143, -0.0537,
     .     0.0098, 0.0044, 0.0047, 0.0142, 0.0081, 0.022,
     .     0.0071, 0.0008, -0.006, -0.0064, 0.0157, -0.0183,
     .     0.0802, 0.0082, 0.0149, 0.0194, 0.0281, 0.0127,
     .     0.0055, -0.0214, -0.032, 0.0019, 0.0031, -0.0361/
      
      data dy_3/-0.0072, -0.0327, 0.0155, -0.0026, -0.0262, -0.0054,
     .     -0.0096, -0.007, 0.0167, -0.007, -0.0372, 0.0321,
     .     -0.0057, -0.0087, 0.022, 0.0093, -0.0085, 0.0065,
     .     0.0057, -0.0092, 0.0221, -0.012, -0.0274, 0.0012,
     .     -0.0184, -0.0103, -0.0085, 0.017, -0.0217, 0.0254,
     .     -0.015, -0.0102, -0.0028, -0.0037, -0.019, -0.0016,
     .     0.0029, -0.0137, -0.0016, 0.0138, -0.0419, -0.0033,
     .     -0.0061, -0.0031, -0.016, 0.0154, -0.0294, -0.0132,
     .     0.02, -0.0171, -0.0018, -0.0008, -0.0169, -0.0112,
     .     0.0016, -0.0002, -0.0057, 0.0043, -0.0186, 0,
     .     -0.0168, 0.0133, 0.0004, 0.0004, 0.0005, 0.0062,
     .     0.0066, 0.0086, -0.0261, -0.0101, -0.0115, -0.0219,
     .     -0.0155, -0.0081, -0.0002, 0.0073, 0.0049, -0.0015,
     .     0.0032, -0.0058, -0.0134, -0.025, 0.013, -0.0371,
     .     0.0044, 0.0112, -0.0119, 0.0092, -0.0018, -0.0024,
     .     -0.0002, 0.0075, -0.0262, -0.0066, -0.004, -0.0114,
     .     -0.0189, 0.0047, 0.0041, -0.0133, 0.0117, -0.0222,
     .     -0.0012, -0.0017, 0.0004, -0.0161, 0.0112, -0.0145,
     .     0.0024, 0.0108, -0.0073, -0.0083, 0.0019, 0.0268,
     .     0.0097, 0.005, -0.0003, -0.0247, -0.0119, 0.0027,
     .     0.011, 0.0168, -0.005, 0.0096, -0.0052, 0.0181,
     .     0.0075, 0.0059, 0.0094, 0.0076, -0.0052, 0.0029,
     .     -0.021, 0.0078, 0.0198, 0.01, 0.0142, 0.0152,
     .     0.0061, -0.0178, -0.0021, 0.0116, 0.0058, -0.0082/

      data dx_4/-0.0842, 0.0216, -0.0125, -0.0255, 0.0067, -0.0194,
     .     0.0067, 0.0323, 0.0218, 0.0239, 0.0325, 0.0048,
     .     -0.0428, 0.0085, 0.0127, -0.016, -0.0065, 0.0176,
     .     0.0126, 0.0112, 0.0135, -0.0014, 0.0098, 0.0078,
     .     -0.0119, 0.0237, 0.0157, -0.0141, -0.0172, -0.0032,
     .     0.0091, 0.0027, -0.0016, 0.021, 0.005, 0.0232,
     .     -0.0222, 0.0164, -0.0129, -0.0135, 0.0028, 0.0098,
     .     0.0017, 0.003, 0.0264, 0.0086, 0.0134, 0.0417,
     .     -0.0102, 0.0083, -0.0235, 0.0057, -0.0244, 0.0103,
     .     0.0376, 0.0071, 0.0078, 0.0022, -0.0105, 0.0182,
     .     -0.0075, 0.0014, -0.0009, -0.013, -0.0147, 0.0162,
     .     0.0111, 0.0076, 0.0139, 0.0196, -0.0054, 0.0266,
     .     -0.0137, 0.0148, -0.0227, -0.0207, 0.0239, -0.0068,
     .     0.0067, -0.0042, 0.0317, -0.0397, -0.0202, -0.0051,
     .     -0.0068, -0.0104, -0.0157, -0.0019, -0.0078, 0.0216,
     .     0.0148, 0.0327, 0.0226, 0.0154, -0.0251, 0.0205,
     .     0.0054, 0.0019, -0.0048, -0.001, -0.0146, -0.0337,
     .     -0.0157, 0.0312, 0.0514, 0.0419, -0.0094, 0.0352,
     .     -0.0019, 0.0209, 0.0075, -0.0074, -0.0084, -0.0059,
     .     0.0049, 0.0098, 0.0003, -0.0284, -0.0222, -0.0543,
     .     0.0154, -0.0029, 0.0186, -0.0067, 0.0048, 0.0034,
     .     -0.0313, -0.0098, 0.0011, -0.0164, -0.0428, 0.0239,
     .     -0.0081, -0.0157, -0.026, 0.0338, 0.0084, -0.0032,
     .     -0.0009, -0.0229, 0.0016, -0.0135, -0.008, 0.0532/

      data dy_4/0.0193, 0.0237, 0.0061, 0.0347, 0.0216, 0.0227,
     .     -0.0014, -0.0198, -0.005, 0.0289, -0.0258, -0.0105,
     .     0.0212, 0.023, -0.0042, 0.0149, 0.0268, 0.014,
     .     -0.0032, -0.0001, 0.0134, 0.0127, -0.0123, -0.0381,
     .     0.0193, 0.0111, 0.0026, 0.0033, 0.0256, 0.0172,
     .     -0.0095, -0.0073, 0.0221, -0.007, 0.0002, -0.0216,
     .     0.0361, 0.0222, 0.0093, 0.0156, 0.0086, 0.0051,
     .     -0.0299, -0.0177, 0.0104, 0.0042, -0.022, -0.0216,
     .     -0.0086, 0.0059, 0.0122, 0.0032, 0.0152, 0.0137,
     .     0.0152, -0.0294, -0.0041, 0.0114, -0.0029, -0.0219,
     .     0.0137, -0.0027, 0.0082, 0.006, 0.0274, 0.0291,
     .     -0.0013, 0.0111, 0.0077, -0.0309, -0.0452, 0.0158,
     .     0.0006, 0.001, 0.0055, -0.001, 0.0214, 0.0267,
     .     -0.007, -0.0127, 0.03, -0.037, -0.0242, -0.0287,
     .     0.004, 0.0021, 0.0034, -0.0016, -0.0022, 0.0129,
     .     0.0027, -0.0046, -0.0023, -0.0138, -0.041, 0.0217,
     .     -0.001, 0.0048, 0.0177, 0.0024, -0.0092, -0.0204,
     .     -0.0281, -0.0084, 0.0224, 0.0376, 0.0102, -0.0575,
     .     0.0037, 0.0098, 0.0055, 0.0093, 0.0154, -0.015,
     .     0.0122, -0.0204, -0.0115, -0.0125, -0.0343, 0.0165,
     .     -0.0053, 0.0069, 0.0162, 0.017, -0.0045, -0.0208,
     .     0.0182, 0.0187, -0.0223, -0.0038, -0.0012, -0.0529,
     .     -0.0385, 0.01, 0.0298, -0.0041, 0.0046, 0.0245,
     .     -0.0246, 0.0149, 0.0219, -0.0283, -0.0077, 0.0153/
 
      ! Building the table of residuals
      do i=1,12
         do j=1,12
            jj=(i-1)*12+(j)
            if (chip.eq.1) then
               tab1cor(i,j,3)=dx_1(jj)
               tab1cor(i,j,4)=dy_1(jj)
            endif
            if (chip.eq.2) then
               tab1cor(i,j,3)=dx_2(jj)
               tab1cor(i,j,4)=dy_2(jj)
            endif
            if (chip.eq.3) then
               tab1cor(i,j,3)=dx_3(jj)
               tab1cor(i,j,4)=dy_3(jj)
            endif
            if (chip.eq.4) then
               tab1cor(i,j,3)=dx_4(jj)
               tab1cor(i,j,4)=dy_4(jj)
            endif
            tab1cor(i,j,1)=m_tab(i)
            tab1cor(i,j,2)=m_tab(j)
         enddo
      enddo
      
      ! Bi-linear interpolation
      do i=1,11
         do j=1,11
            if (xt.gt.tab1cor(i,j,1)
     .           .and.xt.le.tab1cor((i+1),j,1)
     .           .and.yt.gt.tab1cor(i,j,2)
     .           .and.yt.le.tab1cor(i,(j+1),2)) then
               c1x=tab1cor(i,j,3)
               c1y=tab1cor(i,j,4)
               c2x=tab1cor((i+1),j,3)
               c2y=tab1cor((i+1),j,4)
               c3x=tab1cor((i+1),(j+1),3)
               c3y=tab1cor((i+1),(j+1),4)
               c4x=tab1cor(i,(j+1),3)
               c4y=tab1cor(i,(j+1),4)
               area=((tab1cor((i+1),j,1)-tab1cor(i,j,1))*
     .              (tab1cor(i,(j+1),2)-tab1cor(i,j,2)))
               f1=((tab1cor((i+1),j,1)-xt)*
     .              (tab1cor(i,(j+1),2)-yt))/area
               f2=((xt-tab1cor(i,j,1))*
     .              (tab1cor(i,(j+1),2)-yt))/area
               f3=((xt-tab1cor(i,j,1))*
     .              (yt-tab1cor(i,j,2)))/area
               f4=((tab1cor((i+1),j,1)-xt)*
     .              (yt-tab1cor(i,j,2)))/area
               
               dx=(f1*c1x + f2*c2x + f3*c3x + f4*c4x)*1.0d0
               dy=(f1*c1y + f2*c2y + f3*c3y + f4*c4y)*1.0d0
               goto 19873
            else
               dx=0
               dy=0
            endif
         enddo               
      enddo

19873 continue
      
      ! Applying the corrections (xt,yt) -> (xc,yc)
      xc=xt-1*dx
      yc=yt-1*dy
      
      return

      end


c This subroutine computes the average periodicity correction (Ks
c filter).

      real*8 function step_Ks(x)
      implicit none

      real*8 x ! x coordinate
      integer i,iu ! Indexes
      real fx ! x coordinate modulus 128
      real fxlist(6) ! Nodes of the periodic trend
      real dxlist(6) ! Median value of the periodicity in each node
      real ff ! Multiplying factor

      data fxlist/-1.7926, 2.8191, 62.2074, 66.8191, 126.2074, 130.8191/
      data dxlist/-0.0245, 0.0275, 0.0275, -0.0245, -0.0245, 0.0275/
 
      fx = x-int(x/128)*128

      ! Locating surrounding nodes
      iu = 1
      do i = 1, 5
         if (fx.ge.fxlist(i).and.
     .        fx.le.fxlist(i+1)) iu = i
      enddo 
      
      ! Computing the periodicity correction step_Ks
      ff  = (fx-fxlist(iu)) /(fxlist(iu+1)-fxlist(iu))
      step_Ks = dxlist(iu) + ff*(dxlist(iu+1)-dxlist(iu))
      
      return

      end


c This subroutine computes the fine-tuned periodicity correction for
c each chip (Ks filter).

      real*8 function adj_Ks(xr,cp)
      implicit none
      
      real*8 xr ! x coordinate
      real*8 aux ! Auxiliary variable
      integer i ! Index
      integer cp ! Chip number
      real*8 xf(33) ! Bins along the axis
      real*8 df1(32),df2(32),df3(32),df4(32) ! Correction to apply in each bin
      data xf/0, 64, 128, 192, 256, 320, 384, 448, 512, 576, 640, 704,
     .     768, 832, 896, 960, 1024, 1088, 1152, 1216,1280, 1344, 
     .     1408, 1472, 1536, 1600, 1664, 1728, 1792, 1856, 1920, 1984,
     .     2048/
      data df1/-0.06615467, -0.01814303, 0.06779911, 0.04143266,
     .     0.05060014, 0.01499753, 0.02087172, -0.02049854, -0.02582686,
     .     -0.04038792, -0.05431493, -0.04747084, -0.04717887,
     .     -0.02963269, -0.02013036, -0.00119077, 0.02361100,
     .     0.02047539, 0.04164106, 0.02049128, 0.04914909, 0.03316771,
     .     0.04546852, 0.00911948, 0.02819976, -0.01716354, -0.00725752,
     .     -0.04417353, -0.02770366,-0.03710445, 0.00746596, 0.04693112/
      data df2/-0.01161270, -0.01464974, 0.02563246, -0.00138221,
     .     0.01574198, 0.00753110, 0.01765988, -0.00271928, -0.00513319,
     .     -0.00575940, -0.00963142, -0.00402080, -0.00461383,
     .     0.00487151, -0.00042939, 0.00983543, 0.01020180, 0.00340531,
     .     0.00308067, -0.00708010, -0.00285638, -0.00007341, 
     .     -0.00241341, -0.00483412, 0.00365111, 0.00066185, 
     .     -0.00432208, -0.00212875, -0.01064582, -0.00064352,
     .     -0.00710631, 0.00753992/
      data df3/-0.02718477, 0.00945271, -0.00367232, 0.00967728,
     .     -0.00908916, 0.00163366, -0.01208396, -0.00490390,
     .     -0.00589943, -0.00677907, -0.01518874, -0.00213414,
     .     -0.01440865, -0.00117678, -0.01213416, -0.00501041,
     .     -0.00795669, 0.01191116, -0.00000380, 0.01978574,
     .     0.00654772, 0.02122292, 0.00538377, 0.01286177,
     .     0.00207755, -0.00797391, -0.00899829, -0.01033933,
     .     -0.00467242, -0.01452932, 0.00830200, 0.01393792/
      data df4/-0.01180597, 0.00387572, -0.00068705, 0.01193499,
     .     0.00207833, 0.01308500, 0.00369523, 0.00346678, -0.00165673,
     .     -0.00264247, -0.01014271, -0.01066697, 0.00059817,
     .     -0.00674532, 0.00110011, -0.00371381, 0.00254137, 0.00996442,
     .     0.00183672, 0.01118660, 0.00172315, 0.00389793, -0.00636161,
     .     -0.00090031, -0.00006345, -0.01522526, 0.00075762,
     .     -0.00978063, 0.01443332, -0.01348553, 0.00487840, 0.01385468/

      ! Computing the correction in each bin
      do i=1,32
         if (xr.gt.xf(i).and.xr.le.xf(i+1)) then
            if (cp.eq.1) then
               aux=df1(i)
            endif
            if (cp.eq.2) then
               aux=df2(i)
            endif
            if (cp.eq.3) then
               aux=df3(i)
            endif
            if (cp.eq.4) then
               aux=df4(i)
            endif
            goto 9876
         endif
      enddo
      
 9876 continue
      
      adj_Ks=aux
      
      return
      
      end


c This subroutine performes the polynomial correction (Ks filter).

      subroutine poly_HAWKI_Ks(xr,yr,chip,xc,yc)

      implicit none
 
      integer i ! Index
      integer chip ! Chip number
      real*8 xr, yr ! Uncorrected coordinates
      real*8 xc, yc ! Corrected coordinates

      real*8 a(20) ! Polynomial coefficients
      real*8 b(20) ! Polynomial coefficients
      real*8 x_cen,y_cen ! Reference positions (1024,1024)

      real*8 dx, dy ! Corrections to apply
      real*8 rx, ry ! Normalized positions

      x_cen=1024
      y_cen=1024

      ! Choosing the polynomial coefficients

      a(1)=0
      a(2)=0

      if (chip.eq.1) then

         a(3)=  -1.0351063937d0 
         a(4)=  -0.6737957154d0 
         a(5)=  -0.3287857927d0 
         a(6)=   1.5833019753d0 
         a(7)=   0.2188501310d0 
         a(8)=   0.6442255005d0 
         a(9)=   0.0787846213d0 
         a(10)=  0.1320602914d0 
         a(11)=  0.0703365748d0 
         a(12)=  0.0560173632d0 
         a(13)=  0.0257294191d0 
         a(14)= -0.0105773193d0 
         a(15)= -1.0760816964d0 
         a(16)= -0.1688623467d0 
         a(17)= -0.3772925745d0 
         a(18)= -0.0561425173d0 
         a(19)= -0.1895553006d0 
         a(20)= -0.0508506031d0 
         b(1)=   0.9880759825d0 
         b(2)=   0.3946405237d0 
         b(3)=  -0.1282841171d0 
         b(4)=  -0.6617844909d0 
         b(5)=  -0.7161953371d0 
         b(6)=  -0.0137884675d0 
         b(7)=   0.2463487730d0 
         b(8)=  -0.0213110704d0 
         b(9)=   0.4430096418d0 
         b(10)= -0.1227016300d0 
         b(11)= -0.0024741004d0 
         b(12)=  0.0608124000d0 
         b(13)=  0.0940826048d0 
         b(14)= -0.0502938884d0 
         b(15)=  0.0698873327d0 
         b(16)=  0.0121138338d0 
         b(17)=  0.0006805039d0 
         b(18)= -0.0453330642d0 
         b(19)=  0.0233695738d0 
         b(20)= -0.1710729680d0 

      endif


      if (chip.eq.2) then

         a(3)=    0.7611075557d0 
         a(4)=   -0.6606907790d0 
         a(5)=    0.3085018270d0 
         a(6)=    0.6436925516d0 
         a(7)=   -0.2030136389d0 
         a(8)=    0.2822865084d0 
         a(9)=   -0.2708835907d0 
         a(10)=   0.0568454129d0 
         a(11)=   0.0132321281d0 
         a(12)=   0.0715900335d0 
         a(13)=  -0.0424600469d0 
         a(14)=  -0.0080681453d0 
         a(15)=  -0.2146875751d0 
         a(16)=   0.1507224801d0 
         a(17)=   0.0034171111d0 
         a(18)=  -0.0013857061d0 
         a(19)=   0.0372974227d0 
         a(20)=   0.2084981561d0 
         b(1)=   -1.0146657286d0 
         b(2)=    0.5807871653d0 
         b(3)=   -0.2191863653d0 
         b(4)=    0.4480409965d0 
         b(5)=   -0.7984437707d0 
         b(6)=    0.0015300445d0 
         b(7)=    0.2427253569d0 
         b(8)=   -0.0744462158d0 
         b(9)=    0.3855085169d0 
         b(10)=  -0.0134368829d0 
         b(11)=   0.0658153547d0 
         b(12)=  -0.0393744713d0 
         b(13)=  -0.0560613587d0 
         b(14)=   0.0471264129d0 
         b(15)=  -0.0397427791d0 
         b(16)=   0.0463833102d0 
         b(17)=  -0.0246754927d0 
         b(18)=  -0.0312113229d0 
         b(19)=   0.0022114547d0 
         b(20)=  -0.0961593209d0 
         
      endif
      
      if (chip.eq.3) then
      
         a(3)=  -0.8821911773d0 
         a(4)=   0.4147077200d0 
         a(5)=  -0.3402665928d0 
         a(6)=   0.5779248832d0 
         a(7)=  -0.0649390907d0 
         a(8)=   0.3068818216d0 
         a(9)=  -0.3942367940d0 
         a(10)= -0.0022324840d0 
         a(11)=  0.0256679121d0 
         a(12)= -0.0041300210d0 
         a(13)=  0.0134855848d0 
         a(14)=  0.0260232455d0 
         a(15)= -0.2433473749d0 
         a(16)=  0.0541705284d0 
         a(17)= -0.1089073504d0 
         a(18)=  0.0741276984d0 
         a(19)= -0.0334700191d0 
         a(20)=  0.3160270981d0 
         b(1)=  -1.2517812906d0 
         b(2)=   0.1845994685d0 
         b(3)=   0.1828224967d0 
         b(4)=  -0.5255906191d0 
         b(5)=   0.7254864377d0 
         b(6)=  -0.0427033546d0 
         b(7)=   0.1145226433d0 
         b(8)=  -0.0690856897d0 
         b(9)=   0.3881518482d0 
         b(10)= -0.0034187560d0 
         b(11)=  0.0036136596d0 
         b(12)=  0.0370550202d0 
         b(13)= -0.0019151994d0 
         b(14)= -0.0150427086d0 
         b(15)=  0.0452332689d0 
         b(16)=  0.0597003678d0 
         b(17)= -0.0285085469d0 
         b(18)= -0.0283024008d0 
         b(19)=  0.0523325987d0 
         b(20)= -0.1086427226d0 
  
      endif
      
      if (chip.eq.4) then

         a(3)=   0.8572361611d0 
         a(4)=   0.6778685947d0 
         a(5)=   0.2447785691d0 
         a(6)=   0.6929444299d0 
         a(7)=   0.1563468447d0 
         a(8)=   0.4673796037d0 
         a(9)=   0.1949926754d0 
         a(10)=  0.0106947638d0 
         a(11)= -0.0061141470d0 
         a(12)=  0.1017892008d0 
         a(13)=  0.0276084875d0 
         a(14)=  0.0181808374d0 
         a(15)= -0.2216255395d0 
         a(16)= -0.1300688968d0 
         a(17)= -0.0637800870d0 
         a(18)=  0.0360658029d0 
         a(19)= -0.1011944405d0 
         a(20)= -0.1626486772d0 
         b(1)=   0.7646681270d0 
         b(2)=   0.4620650641d0 
         b(3)=   0.2114432732d0 
         b(4)=   0.4424093524d0 
         b(5)=   0.7144636109d0 
         b(6)=  -0.0565079528d0 
         b(7)=   0.2819385178d0 
         b(8)=  -0.0059830123d0 
         b(9)=   0.3664538936d0 
         b(10)=  0.0400364288d0 
         b(11)=  0.0209885928d0 
         b(12)=  0.0731025621d0 
         b(13)=  0.0005967351d0 
         b(14)=  0.0232641771d0 
         b(15)=  0.0372053577d0 
         b(16)=  0.0357585708d0 
         b(17)=  0.0834381999d0 
         b(18)= -0.0279619333d0 
         b(19)=  0.0119020246d0 
         b(20)= -0.0666749252d0 
 
      endif

      ! Computing the corrections
      rx=((xr)-x_cen)/x_cen
      ry=((yr)-y_cen)/y_cen
      dx=(a(1)*rx)+
     .     (a(2)*ry)+
     .     (a(3)*rx*rx)+
     .     (a(4)*rx*ry)+
     .     (a(5)*ry*ry)+
     .     (a(6)*rx*rx*rx)+
     .     (a(7)*rx*rx*ry)+
     .     (a(8)*rx*ry*ry)+
     .     (a(9)*ry*ry*ry)+
     .     (a(10)*rx*rx*rx*rx)+
     .     (a(11)*rx*rx*rx*ry)+
     .     (a(12)*rx*rx*ry*ry)+
     .     (a(13)*rx*ry*ry*ry)+
     .     (a(14)*ry*ry*ry*ry)+
     .     (a(15)*rx*rx*rx*rx*rx)+
     .     (a(16)*rx*rx*rx*rx*ry)+
     .     (a(17)*rx*rx*rx*ry*ry)+
     .     (a(18)*rx*rx*ry*ry*ry)+
     .     (a(19)*rx*ry*ry*ry*ry)+
     .     (a(20)*ry*ry*ry*ry*ry)
      dy=(b(1)*rx)+
     .     (b(2)*ry)+
     .     (b(3)*rx*rx)+
     .     (b(4)*rx*ry)+
     .     (b(5)*ry*ry)+
     .     (b(6)*rx*rx*rx)+
     .     (b(7)*rx*rx*ry)+
     .     (b(8)*rx*ry*ry)+
     .     (b(9)*ry*ry*ry)+
     .     (b(10)*rx*rx*rx*rx)+
     .     (b(11)*rx*rx*rx*ry)+
     .     (b(12)*rx*rx*ry*ry)+
     .     (b(13)*rx*ry*ry*ry)+
     .     (b(14)*ry*ry*ry*ry)+
     .     (b(15)*rx*rx*rx*rx*rx)+
     .     (b(16)*rx*rx*rx*rx*ry)+
     .     (b(17)*rx*rx*rx*ry*ry)+
     .     (b(18)*rx*rx*ry*ry*ry)+
     .     (b(19)*rx*ry*ry*ry*ry)+
     .     (b(20)*ry*ry*ry*ry*ry)
      
      ! Applying the correction (xr,yr) -> (xc,yc)
      xc=xr-dx
      yc=yr-dy
    
      return
      
      end


