PRO imgcleane,ima,jma,header=header
;  this code restores the edges of DI VIS images 
; after the work of 'imgclean' (written by E. Deutsch)
; which deletes CRs (and most of pixels at the edges of a DI VIS image)
; ima is the input image, and jma is the output image
; to get ima use              ima=readfits('file.fit',header)
; to run this code use        imgcleane,ima,jma,header
; to get the final file use   fits_write,'final-file.fit',jma,header
; to get deleted pixels use   del =ima ne jma
;                             fits_write,'del.fit',del,header
; The correction of the edges was made by Sergei Ipatov 
; (ipatov@astro.umd.edu) on Dec. 6, 2005.
; We add letter 'e' to 'imgclean' to show that the code restores 'edges'.
;
; We recommend 'imgclean' for automatic consideration of a large
; number of MRI or HRI images (RADREV or RAD, not raw images),
; because it is a reliable (it always normally comes to the end for 
; all DI images, in contrast e.g. to crfind) and fast code.
; For specific images other codes can give better results.
; Imgclean deletes too many pixels which are not cosmic rays
; from the edge of the comet when the image of the comet is large
; (and sometimes the brightness of pixels deleted near the coma 
; is replaced by a wrong brightness which spoils the image, so 
; after such replacement the image begins to look like as some material
; was ejected from the comet).
; In this case it may be better to use images for which cosmic rays
; are not deleted or try to use other codes for individual images.
; We do not know such code for recognition of cosmic rays which 
; always give good results for images with a coma and/or a comet.
;
; Besides imgclean.pro, also starchck.pro is used by imgcleane.
; If it is needed, you add new key words in the header here.
; We do not change the header.

imgmode=sxpar(header,'imgmode')
nx=sxpar(header,'naxis1')-1
ny=sxpar(header,'naxis2')-1  ; size of the image [0:ny]
jma=ima  ; jma will be changed by imgclean
jmae=ima ; we use jmae to restore the edges
imgclean,jma   ; jma is changed by imgclean ; running imgclean ===
;
; We find the sizes of the edges based on imgmode.
; Notes: The number of restored raws and lines in the edges is
; greater than that in Table IV on page 71 in Space Science 
; Reviews, v. 117, Nos. 1-2, 2005 (e.g. 8 for mode=1),
; because e.g. for mode=1 9th row and 9th line are bright,
; 10th row and 10th line can have a lot of bright pixels,
; and 11th line can have some bright pixels which are not cosmic rays.
; The values of iy presented below were suggested 
; by Dennis Wellnitz. Dennis suggested to use ix=iy-1, 
; but for mode=3 I found that the 6th column also can contain a lot of 
; deleted pixels. So below the values of ix were taken to be
; equal to iy suggested by Dennis. Probably for some modes
; the values of ix can be the same as it was suggested to Dennis,
; but as there are some examples which give larger edges, I took ix=iy.
; I will be glad to get any comments about the sizes of the edges
; which must be restored in order to find true values.
; For what modes can we take ix=iy-1?
; Depending on one's problem, one can consider other values of ix and iy.
ix=0 ; 
iy=0
if imgmode eq 1 or imgmode eq 9 then begin
ix=11  ;10  ;8     ; number of restored pixels from the edge in 'x' direction
iy=11       ;8
endif
if imgmode eq 2 or imgmode eq 3 or imgmode eq 4 then begin
ix=6   ;5   ;4     ; 
iy=6        ;4
endif
if imgmode eq 5 or imgmode eq 6 then begin
ix=4   ;3   ;2     ; 
iy=4        ;2
endif
if imgmode eq 7 then begin
ix=2   ;1   ;0     ; 
iy=2        ;1
endif
if imgmode eq 8 then begin
ix=1   ;0   ; 0
iy=1        ; 0
endif
ix1=ix-1
iy1=iy-1
;
; now restore the edges
if ix ne 0 then begin
jma(0:ix1,0:ny)=jmae(0:ix1,0:ny)
jma(nx-ix1:nx,0:ny)=jmae(nx-ix1:nx,0:ny)
endif
if iy ne 0 then begin
jma(0:nx,0:iy1)=jmae(0:nx,0:iy1)
jma(0:nx,ny-iy1:ny)=jmae(0:nx,ny-iy1:ny)
endif

return
end