;+
;NAME:
;	kpmag2mdix
;PURPOSE:
;	To FTP copy Kitt Peak magnetogram FITS images, convert them
;	to the size/shape of MDI, and to save them as FITS files.
;SAMPLE CALLING SEQUENCE:
;	.run kpmag2mdi
;HISTORY:
;	Written 27-Apr-95 by M.Morrison
;	27-Apr-95 (MDM) - Changed the directory to /mdisw/dbase/ops/synoptic
;			- Changed the output file name
;			- Added some informational print statements
;	 1-Aug-95 (MDM) - Changed output directory to /mdisw/data/ops/synoptic
;			- Fixed up the FTP_COPY call
;	 2-Aug-95 (MDM) - Fixed up the FTP_COPY call some more
;	 4-Aug-95 (MDM) - Modified to save the high resolution equivalent image
;			- Changed the time format of the FITS DATE-OBS field
;-
;
node = 'sohoftp.nascom.nasa.gov'
dir = 'pub/data/synoptic/kpno'
outdir = '/mdisw/data/ops/synoptic'
ftp_file = concat_dir(outdir, 'ftp_copy.ftp')
qwrite = 1
qdisp = 1
list = ''

print, 'FTPing to ' + node + ' to get a list of available images'
delvarx, junk
ftp_copy, junk, node, dir, /list, /anon, dirlist=list, ftp_result=ftp_result, ftp_file=ftp_file
print, 'FTP Listing job messages (should be nothing)'
prstr, ftp_result
if (keyword_set(qdebug)) then spawn, 'more /0p/yohkoh/site/script/ftp_copy.ftp
;
delvarx, xx	;MDM 1-Aug-95
nn=file_info2(xx, finfo, ls=list)
files = finfo.filename
ss = wc_where(files, '*magmp*')
if (ss(0) eq -1) then begin
    print, 'Sorry, no magnetograms available'
    stop
end
;
magfiles = files(ss)
magfiles = reverse(magfiles)
ss = xmenu_sel(magfiles)
;
if (ss(0) eq -1) then stop, 'No images selected.... stopping'
infil = magfiles(ss)
;
print, 'FTPing to ' + node + ' to get the files: ', infil
ftp_copy, infil, node, dir, /anon, /get, ftp_result=ftp_result, outdir=outdir, ftp_file=ftp_file
print, 'FTP copy of images message (should be nothing)'
prstr, ftp_result

for i=0,n_elements(infil)-1 do begin
    outfil = concat_dir(outdir, infil(i))
    print, 'Reading ' + outfil
    img = rfits( outfil, head=head, date=date, time=time )
    ;dattim = sxpar(head, 'DATE') + ' ' + sxpar(head, 'UTSTART')
    dattim = sxpar(head, 'DATE') + ' ' + sxpar(head, 'UTSTOP')
    dattim = str_replace(dattim, '-', '/')
    print, fmt_tim(dattim)
    ;outfil2 = concat_dir(outdir, 'mag'+ex2fid(anytim2ex(dattim)))
    ;filnam2 = 'mdi_mag_fd_'+ex2fid(anytim2ex(dattim))
    filnam2 = 'SO_SU_MDIF_19'+string(ex2fid(anytim2ex(dattim)),format='(a6)')
    filnam2 = str_replace(filnam2, '.', '_') + '_V01.DAT'
    outfil2 = concat_dir(outdir, filnam2)
    ;filnam3 = 'mdi_mag_hr_'+ex2fid(anytim2ex(dattim))
    filnam3 = 'SO_SU_MDIH_19'+string(ex2fid(anytim2ex(dattim)),format='(a6)')
    filnam3 = str_replace(filnam3, '.', '_') + '_V01.DAT'
    outfil3 = concat_dir(outdir, filnam3)
    ;
		;r=intarr(512)
		;for i=0,511 do r(i) = n_elements(where(img(*,i) ne 0))
		;print, max(r)
    pix_kp = 4.48			;953 arcsec *2 / 425. pixels
    pix_mdi= 2.00
    ;
    nx = n_elements(img(*,0))
    ny = n_elements(img(0,*))
    newx = fix(nx * pix_kp/pix_mdi)
    newy = fix(ny * pix_kp/pix_mdi)
    temp = congrid(img, newx, newy, /interp)

    out = bytarr(1024,1024)
    x0 = (newx-1024)/2 > 0
    x1 = (x0+1023) < (newx-1)
    y0 = (newy-1024)/2 > 0
    y1 = (y0+1023) < (newy-1)
    out(0,0) = temp(x0:x1, y0:y1)
    if (newx lt 1024) then out = shift(out, (1024-newx)/2, 0)
    if (newy lt 1024) then out = shift(out, 0, (1024-newy)/2)

    if (keyword_set(qdisp)) then tvscl, rebin(out, 512, 512)
    ;
    head2 = [mk_fits_head(out), 'END                                                                             ']
    sxaddpar, head2, 'DATE', gt_day(dattim, /str, /fits)	;MDM added /FITS 4-Aug-95
    sxaddpar, head2, 'TIME', gt_time(dattim, /str)
    sxaddpar, head2, 'IMG_TYPE', 'Full Disk (FD) Magnetogram'
    sxaddpar, head2, 'COMMENT', 'MDI type image fabricated from Kitt Peak image'
    sxaddpar, head2, 'SRC_IMG', infil(i)
    print, 'Image is saved as 1024x1024 but is being displayed as 512x512'
    if keyword_set(qwrite) then begin
	print, 'Writing: ', outfil2
	ssw_file_delete, outfil2	;MDM 1-Aug-95
	wrt_fits, outfil2, head2, out
    end
    ;
    nx = 1024 / 3.27	;number of FD pixels which encompass the high res FOV
    ny = nx
    x0 = 512 - 0.5*nx
    y0 = 512 - 0.75*ny
    temp = out(x0:x0+nx-1, y0:y0+ny-1)
    out = congrid(temp, 1024, 1024, /interp)
    if (keyword_set(qdisp)) then tvscl, rebin(out, 512, 512)

    head3 = [mk_fits_head(out), 'END                                                                             ']
    sxaddpar, head3, 'DATE', gt_day(dattim, /str, /fits)	;MDM added /FITS 4-Aug-95
    sxaddpar, head3, 'TIME', gt_time(dattim, /str)
    sxaddpar, head3, 'IMG_TYPE', 'High Resolution (HR) Magnetogram'
    sxaddpar, head3, 'COMMENT', 'MDI type image fabricated from Kitt Peak image'
    sxaddpar, head3, 'SRC_IMG', infil(i)
    print, 'Image is saved as 1024x1024 but is being displayed as 512x512'
    if keyword_set(qwrite) then begin
	print, 'Writing: ', outfil3
	ssw_file_delete, outfil3	;MDM 1-Aug-95
	wrt_fits, outfil3, head3, out
    end
end
;
end
