
function eis_adjust_mask, mask, newwvl, info=info, time=time

;+
; NAME
;
;     EIS_ADJUST_MASK()
;
; EXPLANATION
;
;     Because of the grating tilt and CCD offset in the EIS
;     instrument, the same pixel position in an EIS image formed from
;     one wavelength will not directly correspond to the same pixel
;     position in an image from a different wavelength. For this
;     reason a pixel mask generated with EIS_PIXEL_MASK needs to be
;     adjusted when being used for another wavelength.
;
;     This routine performs the adjustment using information from the
;     routine EIS_CCD_OFFSET.
;
; INPUTS
;
;     MASK    Mask structure returned by EIS_PIXEL_MASK.
;
;     NEWVL   Wavelength for which new mask is to be created.
;
; OUTPUTS
;
;     The routine creates a new mask structure with tags:
;     .wvl   Contains the input wavelength NEWWVL
;     .image A mask derived from MASK.IMAGE which has been adjusted
;            for the various instrumental spatial offsets: CCD offsets
;            (X and Y) and grating tilt.
;     .slit  The same value as for MASK.SLIT
;
; OPTIONAL OUTPUTS
;
;     INFO   If the adjusted mask has less pixels than the original
;            mask, then info is returned as 1. Otherwise it is 0.
;
; OPTIONAL INPUTS
;
;     TIME   The start time of the observation. This determines if the
;            X-offset should be applied (or not).
;
; CALLS
;
;     EIS_CCD_OFFSET
;
; HISTORY
;
;     Ver.1, 27-Oct-2008, Peter Young
;     Ver.2, 15-Dec-2009, Peter Young
;        added INFO= optional output
;     Ver.3, 10-May-2010, Peter Young
;        added TIME= optional input
;     Ver.4, 2-Apr-2012, Peter Young
;        X-offset is set to zero if data are from sit-and-stare study.
;     Ver.5, 10-Oct-2013, Peter Young
;        Added an information message if TIME= has not been set.
;-

newmask=mask
newmask.wvl=newwvl

im1=mask.image
im2=newmask.image*0b

siz=size(im1)
xsiz=siz[1]

y1=eis_ccd_offset(mask.wvl)
y2=eis_ccd_offset(newwvl)

ydiff=round(y2-y1)

x_off=2
IF n_elements(time) NE 0 THEN BEGIN
  obs_tai=anytim2tai(time)
  chck_tai=anytim2tai('24-aug-2008 12:00')
  IF obs_tai GT chck_tai THEN BEGIN
    x_off=0
  ENDIF 
ENDIF ELSE BEGIN
  print,'% EIS_ADJUST_MASK: The observation time (TIME=) has not been set.'
  print,'                   Assuming data was taken *before* grating move on 24-Aug-2008.'
  print,'                   A 2" offset between SW and LW will be applied in X-direction.'
ENDELSE 

;
; If sit-and-stare data, then set x_off=0
;
IF xsiz EQ 1 THEN x_off=0

xdiff=0
IF (newwvl GT 230 AND mask.wvl LE 230.) THEN xdiff=-x_off
IF (newwvl LE 230. AND mask.wvl GT 230.) THEN xdiff=x_off


siz=size(im1)
ny=siz[2]
nx=siz[1]

CASE 1 OF 
  ydiff GT 0: BEGIN
    im2[*,ydiff:ny-1]=im1[*,0:ny-ydiff-1]
  END
 ;
  ydiff LT 0: BEGIN
    im2[*,0:ny-abs(ydiff)-1]=im1[*,abs(ydiff):ny-1]
  END
 ;
  ydiff EQ 0: im2=im1
ENDCASE

IF xdiff NE 0 THEN BEGIN
  xshift=abs(xdiff)/mask.slit
  IF xdiff LT 0 THEN BEGIN
    im2[0:nx-1-xshift,*]=im2[xshift:nx-1,*]
    im2[nx-xshift:*,*]=0b
  ENDIF ELSE BEGIN
    im2[xshift:nx-1,*]=im2[0:nx-1-xshift,*]
    im2[0:xshift-1,*]=0b
  ENDELSE
ENDIF

newmask.image=im2

chck=where(mask.image EQ 1,n1)
chck=where(newmask.image EQ 1,n2)

info=0
IF n1 NE n2 THEN info=1



return,newmask

END
