function csa_untar, tarred_fn

; Create the required directories and then untar the gzipped tarball
; without leaving the working directory

  CD, CURRENT=c & PRINT, 'pwd: ', c

  ; From http://www.idlcoyote.com/code_tips/machinespecific.html
  ; this should solve the OS directory issue:
  CASE StrUpCase(!Version.OS_Family) OF
    'WINDOWS': slash = '\'
    'UNIX':    slash = '/'    
  ENDCASE
    
  ; Get path for tarred file (e.g., /Users/jsmith), since this
  ; won't be included in untar_list:
  tgz_path = file_dirname(tarred_fn) 
  print, 'Path to tgz file: ', tgz_path
  print, ''

  ; Use file_untar with /LIST to get directories needed:
  file_untar, tarred_fn, /list, files=untar_list
  print, 'The following directories and files will be created: '
  print, untar_list
  print, ''
  
  ; Make inital 'CSA_Download_yyyymmdd_hhmm' directory (common to all):
  ; Find first slash (needed again later)
  first_slash = strpos(untar_list[0], slash)
  ; First dir to be made is tgz_path + untar_list up to 1st slash
  csa_download_path = tgz_path+slash+ strmid(untar_list[0], 0, first_slash)

  file_mkdir, csa_download_path

  ; Go through untar_list and make dataset directories as needed
  for i = 0, n_elements(untar_list)-1 do begin

    ; Find the 2nd slash by restricting search to after 1st slash
    second_slash = strpos(strmid(untar_list[i], first_slash+1), slash)
    ; Dataset directories are between 1st and 2nd slash in tgz untar_list    
    dataset_dir = strmid(untar_list[i], first_slash+1, second_slash)
    
    full_dataset_dir = csa_download_path+slash+dataset_dir

    ; If the first one, initialise the string array, including full path
    if i eq 0 then begin
      dirs = [full_dataset_dir]
    endif else begin
      ; If it's not the first and not the same as any already in the list, 
      ; append it to the list of directories to be made
      if where(dirs eq full_dataset_dir) eq -1 then begin
        dirs = [dirs, full_dataset_dir]
      endif
    endelse
     
  endfor
  
  print, dirs
  file_mkdir, dirs

  ; Now we can untar since the directory structure is in place
  file_untar, tarred_fn

  ; We're still in original working directory
  CD, CURRENT=c & PRINT, 'pwd: ', c

end