function Link=prop2link(Data,varargin)
% Construct a URL link for a ZTF file given its structure of properties.
% Package: VO.ZTF
% Description: Construct a URL link for a ZTF file given its structure of
%              properties.
% Input  : - A structure of ZTF files properties (e.g., generated using
%            VO.ZTF.prep_link_ztf_images.m.
% Output : - Cell array of URL links to file names.
% License: GNU general public license version 3
%     By : Eran O. Ofek                    Nov 2017
%    URL : http://weizmann.ac.il/home/eofek/matlab/
% Example: Link=VO.ZTF.prop2link(Data)
% Reliable: 2
%--------------------------------------------------------------------------


%DefV.Class                = {'sci','raw','cal'};  % 'ref'
%InPar = InArg.populate_keyval(DefV,varargin,mfilename);


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)


Nd   = numel(Data);
Link = cell(size(Data));
for Id=1:1:Nd
    if strcmp(Data(Id).Filt(1),'0')
        Filter = '00';
    else
        Filter = sprintf('%s',Data(Id).Filt);
    end
    
    switch lower(Data(Id).DirType)
        case 'sci'
            ExtraPath = sprintf('%s/%04d/%02d%02d/%06d/',Data(Id).DirType,Data(Id).Year,Data(Id).Month,Data(Id).Day,Data(Id).Frac.*1e6);
            
            FileName = sprintf('ztf_%04d%02d%02d%06d_%06d_%s_c%02d_%s_q%d_%s',Data(Id).Year,Data(Id).Month,Data(Id).Day,Data(Id).Frac.*1e6,...
                                                                          Data(Id).Field,Filter,Data(Id).CCD,Data(Id).Type,Data(Id).Amp,Data(Id).ImExt);
        case 'cal'
            ExtraPath = sprintf('%s/%04d/%02d%02d/',Data(Id).DirType,Data(Id).Year,Data(Id).Month,Data(Id).Day);
            if (~isempty(strfind(Data(Id).ImExt,'bias')))
                ExtraPath = sprintf('%sbias/00/',ExtraPath);
            elseif (~isempty(strfind(Data(Id).ImExt,'hifreqflat')))
                ExtraPath = sprintf('%shifreqflat/%d/',ExtraPath,Filter);
            else
                error('Unknown ImExt option: %s',Data(Id).ImExt);
            end
            ExtraPath = sprintf('%sccd%02d/q%d/',ExtraPath,Data(Id).CCD,Data(Id).Amp);
                        
            FileName = sprintf('ztf_%04d%02d%02d_%s_c%02d_q%d_%s',Data(Id).Year,Data(Id).Month,Data(Id).Day,...
                                                              Filter,Data(Id).CCD,Data(Id).Amp,Data(Id).ImExt);
        case 'raw'
            ExtraPath = sprintf('%s/%04d/%02d%02d/%06d/',Data(Id).DirType,Data(Id).Year,Data(Id).Month,Data(Id).Day,Data(Id).Frac.*1e6);

            FileName = sprintf('ztf_%04d%02d%02d%06d_%06d_%s_c%02d_%s',Data(Id).Year,Data(Id).Month,Data(Id).Day,Data(Id).Frac.*1e6,...
                                                                          Data(Id).Field,Filter,Data(Id).CCD,...
                                                                          Data(Id).ImExt);
        otherwise
            error('Unknwon DirType option');
    end
    
    Link{Id} = sprintf('%s%s%s',BaseURL,ExtraPath,FileName);
end


