; $Id: fcs_get_files.pro,v 1.3 2005/02/08 17:20:27 azuah Exp $
;###############################################################################
; LICENSE:
;  The software in this file is written by an employee of 
;  National Institute of Standards and Technology 
;  as part of the DAVE software project.
;
;  The DAVE software package is not subject to copyright protection
;  and is in the public domain. It should be considered as an
;  experimental neutron scattering data reduction, visualization, and
;  analysis system. As such, the authors assume no responsibility
;  whatsoever for its use, and make no guarantees, expressed or
;  implied, about its quality, reliability, or any other
;  characteristic. The use of certain trade names or commercial
;  products does not imply any endorsement of a particular product,
;  nor does it imply that the named product is necessarily the best
;  product for the stated purpose. We would appreciate acknowledgment
;  if the DAVE software is used of if the code in this file is
;  included in another product.
;
;###############################################################################
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_get_files_cleanup,tlb
widget_control,tlb,get_uvalue = pState
ptr_free,pState
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_get_files_restore_defaults,event
widget_control,event.top,get_uvalue = pState

uname_id = widget_info(event.top,find_by_uname = 'USERNAME')
widget_control,uname_id,set_value = (*pState).defaults.USERNAME

pword_id = widget_info(event.top,find_by_uname = 'PWORD')
widget_control,pword_id,set_value = (*pState).defaults.PWORD

dir_id = widget_info(event.top,find_by_uname = 'DIR')
widget_control,dir_id,set_value = (*pState).defaults.DIR

fname_id = widget_info(event.top,find_by_uname = 'FILENAME')
widget_control,fname_id,set_value = (*pState).defaults.FILENAME

end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
function readmrec,filename
if n_elements(filename) eq 0 then begin
  filename = DIALOG_PICKFILE(title = 'Select a MREC file',$
                               /read,filter = '*.mrec')
  if ((file_test(filename) eq 0) or (filename eq '')) then return,-1
endif

text=strarr(100000)
count=0
dumstr=''
openr, lun, filename, /get_lun
WHILE (NOT EOF(lun)) DO BEGIN
 readf,lun,dumstr
 text[count]=dumstr
 count=count+1
endwhile

free_lun,lun

textout=text[0:count]
text=''
return,textout
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_get_files_mrec,event
widget_control,event.top,get_uvalue = pState
  ;for each OS:
os = !version.os_family
CASE os of
'unix': $
	begin
	   mrecfile=string((*pState).DIR)+"_fcs.mrec"
	   msg = 'Getting '+mrecfile
           fcs_flashMessage_create,msg,base,event
  	   wait,1
          ;ftp the file here:
	   mrecst="ftp://charlotte.ncnr.nist.gov/pub/icpdata/fcs/mrec/"+mrecfile
          ; spawn,['wget',mrecst],/noshell	;get the file
	   cd,(*!dave_defaults).datdir
           spawn,'wget -N -q '+mrecst 	;get the file
	   fcs_flashMessage_destroy,base
	   test=file_test(mrecfile,/read)	;check that the file is here
	   CASE test of
		0 :$
		   BEGIN
			void=dialog_message("The MREC file has a problem",/error)
			return
    	           END
		1 :$
		   BEGIN
			textout=readmrec(mrecfile)
			;check that the base is realized:
			map=widget_info((*pState).ctrlBase,/map)
			if (map eq 1) then begin
				widget_control,(*pState).wtext,set_value=textout
			endif
    	           END
	 	 else:
		 endcase

	end
'Windows': $
	begin
		datdir=(*!dave_defaults).datdir
		script=datdir+'ftpget.cmd'
		;create  a batch file:
	    if file_test(script) then file_delete,script
	        OPENW, datout, script, /GET_LUN
  			printf,datout,'@Echo Off'
  			printf,datout,"If Not %1'==' (Call :Main %*&Goto :EOF)"
  			printf,datout,'(Goto :EOF)'
  			printf,datout,':Main'
  			printf,datout,'SetLocal'
  			printf,datout,'Set FtpUser=anonymous'
  			printf,datout,'Set FtpPass=noone@nowhere.no'
     		printf,datout,'Set FtpScript=%TEMP%.\$TPGET0.TMP'
    		printf,datout,'For /F "Tokens=1* Delims=/" %%A In ("%1") Do ('
    		printf,datout,'Set Hostname=%%A&Set Filename=%%B)'
    		printf,datout,"If Not %2'==' (If Not Exist %2.\NUL MkDir %2&ChDir /D %2)"
    		printf,datout,'Echo user %FtpUser% %FtpPass%>%FtpScript%'
     		printf,datout,'Echo prompt>>%FtpScript%'
     		printf,datout,'Echo mget %Filename%>>%FtpScript%'
     		printf,datout,'Echo bye>>%FtpScript%'
     		printf,datout,'%SystemRoot%\system32\FTP.EXE -n -s:%FtpScript% %Hostname%'
     		printf,datout,'Del %FtpScript%'
     		printf,datout,'EndLocal'
     		printf,datout,'(Goto :EOF)'
	 		free_lun,datout
			;run the batch to get the files
			cd,datdir
			mrecfile=string((*pState).DIR)+"_fcs.mrec"
		  	msg = 'Getting '+mrecfile
	               fcs_flashMessage_create,msg,base,event
			mrecst="ftpget charlotte/pub/icpdata/fcs/mrec/"+mrecfile
			spawn,mrecst,/hide
			;delete the batch file
			file_delete,script
 			 fcs_flashMessage_destroy,base
	test=file_test(mrecfile,/read)	;check that the file is here
	   CASE test of
		0 :$
		   BEGIN
			void=dialog_message("The MREC file has a problem",/error)
			return
    	           END
		1 :$
		   BEGIN
			textout=readmrec(datdir+mrecfile)
			;check that the base is realized:
			map=widget_info((*pState).ctrlBase,/map)
			if (map eq 1) then begin
				widget_control,(*pState).wtext,set_value=textout
			endif
    	           END
	 	 else:
		 endcase

	end
else:$
	begin
		msg = 'NOT IMPLEMENTSED IN THIS VERSION OF DAVE'
		fcs_flashMessage_create,msg,base,event
        	wait,3
		fcs_flashMessage_destroy,base
	end
endcase

end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_get_files_ftp,event
widget_control,event.top,get_uvalue = pState
  ;for each OS:
os = !version.os_family
dir=string((*pState).dir)
file=string((*pState).filename)

CASE os of
'unix': $
	begin
	 msg = 'Getting '+file
	 fcs_flashMessage_create,msg,base,event
         wait,1
	 st="ftp://charlotte.ncnr.nist.gov/pub/icpdata/fcs/"+dir+"/"+file
	 cd,(*!dave_defaults).datdir
         spawn,'wget -N -q '+st 	;get the file
 	 fcs_flashMessage_destroy,base
	end
'Windows': $
	begin
		datdir=(*!dave_defaults).datdir
		script=datdir+'ftpget.cmd'
		;create  a batch file:
	    if file_test(script) then file_delete,script
	        OPENW, datout, script, /GET_LUN
  			printf,datout,'@Echo Off'
  			printf,datout,"If Not %1'==' (Call :Main %*&Goto :EOF)"
  			printf,datout,'(Goto :EOF)'
  			printf,datout,':Main'
  			printf,datout,'SetLocal'
  			printf,datout,'Set FtpUser=anonymous'
  			printf,datout,'Set FtpPass=noone@nowhere.no'
     		printf,datout,'Set FtpScript=%TEMP%.\$TPGET0.TMP'
    		printf,datout,'For /F "Tokens=1* Delims=/" %%A In ("%1") Do ('
    		printf,datout,'Set Hostname=%%A&Set Filename=%%B)'
    		printf,datout,"If Not %2'==' (If Not Exist %2.\NUL MkDir %2&ChDir /D %2)"
    		printf,datout,'Echo user %FtpUser% %FtpPass%>%FtpScript%'
     		printf,datout,'Echo prompt>>%FtpScript%'
     		printf,datout,'Echo mget %Filename%>>%FtpScript%'
     		printf,datout,'Echo bye>>%FtpScript%'
     		printf,datout,'%SystemRoot%\system32\FTP.EXE -n -s:%FtpScript% %Hostname%'
     		printf,datout,'Del %FtpScript%'
     		printf,datout,'EndLocal'
     		printf,datout,'(Goto :EOF)'
	 		free_lun,datout
		;run the batch to get the files
			cd,datdir
			st="ftpget charlotte/pub/icpdata/fcs/"+dir+"/"+file
			 msg = 'Getting '+file
			 fcs_flashMessage_create,msg,base,event
			spawn,st,/hide
		;delete the batch file
			file_delete,script
 	 fcs_flashMessage_destroy,base
	end
else: $
	begin
		msg = 'NOT IMPLEMENTSED IN THIS VERSION OF DAVE'
		fcs_flashMessage_create,msg,base,event
        	wait,3
		fcs_flashMessage_destroy,base
	end
endcase

end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_get_files_event,event
widget_control,event.top,get_uvalue = pState
uname = widget_info(event.id,/uname)
case uname of
'PWORD':	$
	begin
           IF event.type EQ 0 THEN BEGIN

		Widget_Control, event.id, Get_Value=text
		text = text[0]
		length = StrLen(text)

       		; Store the password and return asterisks.
		selection = Widget_Info(event.id, /Text_Select)

      		; Insert the character at the proper location.
		Widget_Control, event.id, /Use_Text_Select, Set_Value='*'

      		; Update the current insertion point in the text widget.
		Widget_Control, event.id, Set_Text_Select=event.offset + 1

     		; Store the password.

		IF (*pstate).password EQ "" THEN (*pstate).password=String(event.ch) ELSE $
      		(*pstate).password = (*pstate).password + String(event.ch)
          endif

   		; Deal with deletion events.

	IF event.type EQ 2 THEN BEGIN

      		; Get the current text password.

   		text = (*pstate).password
   		length = StrLen(text)

      		; Put it back with the deletion subtracted.

   		(*pstate).password = StrMid(text, 0, length-event.length)
   		passwordLen = StrLen((*pstate).password)
		if (passwordLen ne 0) then begin
			Widget_Control, event.id, Set_Value=Replicate('*', passwordLen)
		endif  else begin
			Widget_Control, event.id, Set_Value=''
		endelse

      		; Reset the text insertion point in the text widget.
   		Widget_Control, event.id, Set_Text_Select=event.offset
	   ENDIF
	   Widget_Control, event.top, Set_UValue=pstate
	end

'USERNAME':	$
	begin
		Widget_Control, event.id, Get_Value=text
		(*pstate).username=text[0]
		Widget_Control, event.top, Set_UValue=pstate
	end
'DIR':	$
	begin
		Widget_Control, event.id, Get_Value=text
		(*pstate).dir=text[0]
		Widget_Control, event.top, Set_UValue=pstate
	end
'FILENAME':	$
	begin
		Widget_Control, event.id, Get_Value=text
		(*pstate).filename=text[0]
		Widget_Control, event.top, Set_UValue=pstate
	end

'GO':	$
	begin
		fcs_get_files_ftp,event
	end
'MREC':	$
	begin
		if !version.os_family ne 'unix' AND !version.os_family ne 'Windows' then begin
		   msg = 'NOT IMPLEMENTSED IN THIS VERSION OF DAVE'
		   fcs_flashMessage_create,msg,base,event
        	   wait,3
		   fcs_flashMessage_destroy,base
		endif else begin
  		   widget_control,(*pState).ctrlBase,/map
		   fcs_get_files_mrec,event
		endelse
	end
'HIDE':	$
	begin
		 widget_control,(*pState).ctrlBase,map = 0
	end
'DEFAULTS':	$
	begin
		fcs_get_files_restore_defaults,event
	end
'DISMISS':	$
	begin
		widget_control,event.top,/destroy
	end
else:
endcase

end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_get_files,	group_leader = group_leader,	$
			savetodir=savetodir,$
			err=err

; Widget definition module
registerName = 'fcs_get_files'
err = 0
if xregistered(registerName) then begin
  err = 1
  return
endif
if n_elements(group_leader) eq 0 then begin
  group_leader = 0L
  notifyIDS = [0L,0L]
endif
if n_elements(savetodir) eq 0 then begin
  savetodir='/home/craig/idl/fcs/'
endif

; Specify some default values
defaults =	{	getPREFS,			$
				USERNAME:'anonymous',			$
  				PWORD:'',			$
  				DIR:'200303',			$
  				FILENAME:'mar122003_01.tof'}

; If the user has not supplied any preferences then define the
; outgoing preferences to be the defaults


filename=defaults.filename
dir=defaults.dir

; Set up the widget layout
tsize = 9
tlb = widget_base(group_leader = group_leader,/col, $
	title = 'Time-of-flight data retrival', $
	/base_align_left,/tlb_frame_attr)

outputTOP = tlb
void = widget_label(tlb,value = "Charlotte dir structure: YYYYMM")
void = widget_label(tlb,value = "File naming scheme: mar122003_01.tof")

rowBase1 = widget_base(tlb,/row)
;void = cw_field(rowbase1,value = strtrim(string(defaults.USERNAME),2), $
;	uname = 'USERNAME',title = 'Login',/row,xsize = tsize,/all_events)

;void = widget_text(rowbase1,value = strtrim(string(defaults.PWORD),2), $
;	uname = 'PWORD',xsize = tsize,/all_events)
;void = widget_label(tlb,value = "(anon: use email address as passwd)")

rowBase2 = widget_base(tlb,/row,/align_center)
void = cw_field(rowbase2,value = dir, $
	uname = 'DIR',title = 'Directory',/row,xsize = 15,/all_events)
rowBase3 = widget_base(tlb,/row,/align_center)
void = cw_field(rowbase3,value = filename, $
	uname = 'FILENAME',title = 'File',/row,xsize = 20,/all_events)


rowBase4 = widget_base(tlb,/row,/align_center)
void = widget_button(rowBase4,value = 'Go',uname = 'GO')
void = widget_button(rowBase4,value = 'Mrec',uname = 'MREC')
void = widget_button(rowBase4,value = 'Defaults',uname = 'DEFAULTS')
void = widget_button(rowBase4,value = 'Dismiss',uname = 'DISMISS')

textout="No file yet"
tlbgeom=widget_info(tlb,/geometry)
tlbx=tlbgeom.xsize
ctrlBase = widget_base(group_leader = tlb,/col,title = 'MREC text',map = 0,xsize = 500,$
		xoffset=tlbx)
wtext=widget_text(ctrlbase,value=textout,/scroll,uname = 'WTEXT',xsize = 55,ysize = 20)
void = widget_button(ctrlBase,value = 'HIDE',uname = 'HIDE')

widget_control,tlb,/realize
widget_control,ctrlBase,/realize
state =		{	        defaults:defaults,		$
				group_leader:group_leader, $
				username:'fcs'  ,$
				dir:dir  ,$
				filename:filename  ,$
				password:'',$
				savetodir:savetodir,$
				ctrlBase:ctrlBase,$
				wtext:wtext}
pState = ptr_new(state,/no_copy)
widget_control,tlb,set_uvalue = pState
widget_control,(*pState).ctrlBase,set_uvalue = pState

xmanager,registerName,tlb,/no_block,cleanup = 'fcs_get_files_cleanup'
xmanager,registerName,(*pState).ctrlBase
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
