function [ZTF]=prep_link_ztf_images(varargin)
% Prepare links and map data type for all ZTF image files in www archive.
% Package: VO
% Description: Prepare links and map data type for all ZTF image files in
%              web directory archive.
% Input  : * Arbitrary number of pairs of arguments: ...,keyword,value,...
%            where keyword are one of the followings:
%            'Class'  - The class of images to retrieve:
%                       Default is {'sci','raw','cal'}.
%            'YMD'    - If given, then retrieve only data in specific date
%                       [Year Month Day]. Default is empty.
%            'User'   - Archive username.
%                       or a cell containing a full path to a file with
%                       user/pass.
%            'Pass'   - Archive password.
% Output : - A structure containing a structure array for each image class
%            with all the meta data and links.
% License: GNU general public license version 3
%     By : Eran O. Ofek                    Nov 2017
%    URL : http://weizmann.ac.il/home/eofek/matlab/
% Example: ZTF=VO.ZTF.prep_link_ztf_images('YMD',[2017 10 15]);
% Reliable: 2
%--------------------------------------------------------------------------

DefV.Class                = {'sci','raw','cal'};  % 'ref'
DefV.YMD                  = [];
%DefV.DirStore             = '~/matlab/data/+cats/+ZTF/';
%DefV.RetrieveNew          = true;
%DefV.CatFile              = 'ZTF%sImages';
DefV.User                 = {'~/matlab/passwords/ztf_archive_pass'};
DefV.Pass                 = [];
InPar = InArg.populate_keyval(DefV,varargin,mfilename);


if (iscell(InPar.User))
    [InPar.User,InPar.Pass]=Util.code.read_user_pass_file(InPar.User{1});
end

if (~iscell(InPar.Class))
    InPar.Class = {InPar.Class};
end


%Options = weboptions('Username',InPar.User,'Password',InPar.Pass)

BaseURL = 'https://ztfweb.ipac.caltech.edu/ztf/archive/';


%https://ztfweb.ipac.caltech.edu/ztf/archive/cal/2017/1014/hifreqflat/zr/ccd01/q1/
% ztf_20171014_zr_c01_q1_hifreqflat.fits (6)
            
% https://ztfweb.ipac.caltech.edu/ztf/archive/sci/2017/1015/354109/
% ztf_20171015354109_000656_zr_c02_o_q4_sciimg.fits (8)

% https://ztfweb.ipac.caltech.edu/ztf/archive/raw/2017/1014/142731/
% ztf_20171014142731_000000_zr_c01_f.fits.fz (6)


if (~isempty(InPar.YMD))
    YMD = sprintf('%04d/%02d%02d/',InPar.YMD);
else
    YMD = '';
end


% PWD = pwd;
% cd(InPar.DirStore);

Nclass = numel(InPar.Class);
for Iclass=1:1:Nclass
    DataClass = InPar.Class{Iclass};

    URL = sprintf('%s%s/%s',BaseURL,DataClass,YMD);
    
    DataClass = InPar.Class{Iclass};
    [List,IsDir,FileName] = www.r_files_url(URL,'User',InPar.User,'Pass',InPar.Pass);
    Data = VO.ZTF.ztf_imagename_prop(List);
    %[Data(1:end).Link] = deal(cell2struct(List,'Link',2));

    % dataset name
    ZTF.(DataClass) = Data;
    
end
 
