FUNCTION getLocFn, webFn, SUBDIR=subdir

;;-----------------------------------------------------------------------------
;; PURPOSE:
;;  Converts remote file locations into local ones that can be read into
;;  IDL. Does this in two different ways. If there exists a file in the
;;  running directory called dical.config, then the first part of the
;;  address (assumes the remote address ends in .edu or .gov), is replaced
;;  with the directory specified in the config file. This is done if the
;;  program is running on a machine with copies of the files specified
;;  by the remote address. If dical.config does not specify a directory,
;;  then a process is spawned which retrieves the files and stores them in
;;  the Temp/ directory. Then the file names of these local files are
;;  returned.
;;
;;  The returned array will only contain unique filenames. Thus, backup
;;  sources for a file can be specififed as seperate input entries.
;;
;;  This only works on a machine with the wget function
;;
;; CALLING SEQUENCE:
;;  fn = getLocFn(webFn)
;;
;; REQUIRED INPUTS:
;;  webFn - A N-lengh vector of remote filenames to convert to local
;;   filenames
;;
;; OUTPUTS:
;;  RETURN - A N-length vector of local filenames where RETURN[i] is
;;   for the file specified by webFn[i]
;;
;; OPTIONAL INPUT KEYWORDS:
;;  SUBDIR - Subdirectory in the Temp folder to store any file retrieved
;;
;; EXAMPLE:
;;      IDL> fn = getLocFn(webFn)
;;
;; PROCEDURES USED (i.e. called directly!):
;;  wget, getDICalConfig
;;
;; MODIFICATION HISTORY:
;;   2004-06-04  M. Desnoyer    Created
;;
;;-----------------------------------------------------------------------------

;;ON_ERROR, 2

n = N_ELEMENTS(webFn)
out = strarr(n)

;; Open the config file
config = getDICalConfig()

;; Free the linked list contained in the configuration
freeLinkedList, config.sqlServers

;; See if a directory has been specified
IF config.calibDir NE '' THEN BEGIN
    pre = config.calibDir

    ;; Make sure the prefix has a slash on the end
    IF strpos(pre, path_sep(), /REVERSE_SEARCH) NE (strlen(pre)-1) THEN $
       pre = pre+path_sep()

    ;; Replace the address ending with .edu or .gov with the prefix from
    ;; the file
    keyLoc = strPos(webFn, '.edu')+5
    IF min(keyLoc) EQ 4 THEN keyLoc = (strPos(webFn, '.gov')+5)>keyLoc
    IF min(keyLoc) EQ 4 THEN $
       message, 'Invalid web address found. Must be from .gov or .edu'
    FOR i=0, n-1 DO out[i] = pre+strmid(webFn[i],keyLoc[i])
    RETURN, out[uniq(out, reverse(sort(out)))]
ENDIF

;; No config file so we have to retrieve the files

;; Figure out which directory to dump files in
dir = 'Temp'
IF keyword_set(SUBDIR) THEN $
    dir = dir + path_sep() + $
       strjoin(strsplit(subdir, path_sep(), /extract),path_sep())

;; Create the wget command to spawn. Will dump files into the Temp directory
cmd = 'wget -P '+dir+' -Nq --http-user="encke" --http-passwd="ekcne" '
FOR i=0, n-1 DO cmd = cmd + webFn[i] + ' '

;; Run wget
IF !version.os_family EQ 'unix' THEN $
    SPAWN, cmd, EXIT_STATUS=error, /sh $
ELSE SPAWN, cmd, res, eStr,EXIT_STATUS=error

;; Build up the local file names by extracting the filename and adding dir
;; as a prefix
fnLoc = strpos(webFn, '/', /REVERSE_SEARCH)
FOR i=0, n-1 DO BEGIN
    out[i] = dir+path_sep()+strmid(webFn[i],fnLoc[i]+1)
    ;; Make sure the file exists
    IF (NOT file_test(out[i], /read)) THEN $
       message, 'Could not retrieve: ' + webFn[i]
ENDFOR

;; Find all the unique filenames
out = out[uniq(out, reverse(sort(out)))]

RETURN, out
END