function Link=irsa_image_link(Prop,varargin)
% Construct links to ZTF images from properties structure
% Package: VO.ZTF
% Description: Construct links to ZTF images from properties structure.
% Input  : - Properties structure generated by VO.ZTF.table2prop.
%          * Arbitrary number of pairs of arguments: ...,keyword,value,...
%            where keyword are one of the followings:
%            'BaseURL' - Default is:
%                        'https://irsa.ipac.caltech.edu/ibe/data/ztf/products/'.
% Output : - Cell array of links to images.
% License: GNU general public license version 3
%     By : Eran O. Ofek                    Jan 2018
%    URL : http://weizmann.ac.il/home/eofek/matlab/
% Example: Link=VO.ZTF.irsa_image_link(Prop)
% Reliable: 2
%--------------------------------------------------------------------------

if (nargin==0)
    Prop.ImType     = 'sci';
    Prop.Year       = 2017;
    Prop.Month      = 10;
    Prop.Day        = 23;
    Prop.FracDay    = 296551;
    Prop.FieldID    = 600;
    Prop.FilterCode = 'zr';
    Prop.CCDID      = 8;
    Prop.ImTypeCode = 'o';
    Prop.QuadID     = 1;
    Prop.Product    = 'image';
end

DefV.BaseURL              = 'https://irsa.ipac.caltech.edu/ibe/data/ztf/products/';
InPar = InArg.populate_keyval(DefV,varargin,mfilename);


% Format description in:
% https://irsa.ipac.caltech.edu/docs/program_interface/ztf_metadata.html

Nlink = numel(Prop);
Link  = cell(Nlink,1);
for Ilink=1:1:Nlink
    
    
    FileFracDay = sprintf('%04d%02d%02d%06d',Prop(Ilink).Year, Prop(Ilink).Month, Prop(Ilink).Day, Prop(Ilink).FracDay);

    switch lower(Prop(Ilink).ImType)
        case 'sci'
            % Example:
            % https://irsa.ipac.caltech.edu/ibe/data/ztf/products/sci/'+year/'+month+day+'/'+fracday+'/ztf_'+filefracday+'_'+paddedfield+'_'+filtercode+'_c'+paddedccdid+'_'+imgtypecode+'_q'+qid+'_sciimg_ra'+in_ra+'_dec'+in_dec+'.fits'
            % https://irsa.ipac.caltech.edu/ibe/data/ztf/products/sci/2017/1023/296551/ztf_20171023296551_000600_zr_c08_o_q1_sciimg.fits

            % https://irsa.ipac.caltech.edu/docs/program_interface/ztf_metadata.html

            switch lower(Prop(Ilink).Product)
                case 'log'
                    Suffix = 'log.txt';
                case 'mask'
                    Suffix = 'mskimg.fits';
                case 'image'
                    Suffix = 'sciimg.fits';
                case 'scilog'
                    Suffix = 'sciimlog.txt';
                case 'sex'
                    Suffix = 'sexcat.fits';
                case 'dao'
                    Suffix = 'psfcat.fits';
                case 'daopsf'
                    Suffix = 'sciimgdao.psf';
                case 'diff'
                    Suffix = 'scimrefdiffimg.fits.fz';
                case 'diffpsf'
                    Suffix = 'diffimgpsf.fits';
                
                otherwise
                    error('Unknown Product option');
            end
            Link{Ilink} = sprintf('%s%s/%04d/%02d%02d/%06d/ztf_%s_%06d_%s_c%02d_%s_q%d_%s',...
                                    InPar.BaseURL, Prop(Ilink).ImType, ...
                                    Prop(Ilink).Year, Prop(Ilink).Month, Prop(Ilink).Day, Prop(Ilink).FracDay, ...
                                    FileFracDay, ...
                                    Prop(Ilink).FieldID, Prop(Ilink).FilterCode, Prop(Ilink).CCDID, ...
                                    Prop(Ilink).ImTypeCode, ...
                                    Prop(Ilink).QuadID,...
                                    Suffix);




        case 'raw'
            % Example:
            % https://irsa.ipac.caltech.edu/ibe/data/ztf/products/raw/'+year/'+month+day+fracdate+'/ztf_'+filefracday+'_'+paddedfield+filtercode+'_c'+paddedccdid+'_'+imgtypecode+'.fits.fz'
            % https://irsa.ipac.caltech.edu/ibe/data/ztf/products/raw/2017/1015/354109/ztf_20171015354109_000656_zr_c06_o.fits.fz
            
            switch lower(Prop(Ilink).ImTypeCode)
                case 'o'
                    % object
                    FieldID = Prop(Ilink).FieldID;
                    Filter  = Prop(Ilink).FilterCode;
                case 'f'
                    % flat
                    FieldID = 0;
                    Filter  = Prop(Ilink).FilterCode;
                case 'b'
                    % bias
                    FieldID = 0;
                    Filter  = '00';
                otherwise
                    error('Unknown ImTypeCode');
            end
                    
            Link{Ilink} = sprintf('%s%s/04d/%02d%02d/%06d/ztf_%s_%06d_%s_c%02f_%s.fits.fz',...
                                    InPar.BaseURL, Prop(Ilink).ImType, ...
                                    Prop(Ilink).Year, Prop(Ilink).Month, Prop(Ilink).Day, Prop(Ilink).FracDay, ...
                                    FileFracDay, ...
                                    FieldID, Filter, Prop(Ilink).CCDID, ...
                                    Prop(Ilink).ImTypeCode);
                                    

           

        case 'cal'
            
            switch lower(Prop(Ilink).ImTypeCode)
                case 'b'
                    % bias:
                    % https://irsa.ipac.caltech.edu/ibe/data/ztf/products/cal/'+year/'+month+day+'/bias/'+filtercode+'ccd'+paddedccdid+'/q'+qid+'/ztf_'+filestartdate+'_'+filtercode+'_c'+paddedccdid+'_q'+qid+'_bias.fits'
                    % https://irsa.ipac.caltech.edu/ibe/data/ztf/products/cal/2017/1014/bias/00/ccd06/q2/ztf_20171014_00_c06_q2_bias.fits

                    Link{Ilink} = sprintf('%s%s/04d/%02d%02d/bias/00/ccd%02d/q%d/ztf_%04d%02d%02d_00_c%02d_q%d_bias.fits',...
                                    InPar.BaseURL, Prop(Ilink).ImType, ...
                                    Prop(Ilink).Year, Prop(Ilink).Month, Prop(Ilink).Day, ...
                                    Prop(Ilink).CCDID, Prop(Ilink).QuadID, ...
                                    Prop(Ilink).Year, Prop(Ilink).Month, Prop(Ilink).Day, ...
                                    Prop(Ilink).CCDID, Prop(Ilink).QuadID);
                                   
                case 'f'
                    % flat
                    % https://irsa.ipac.caltech.edu/ibe/data/ztf/products/cal/'+year/'+month+day+'/hifreqflat/'+filtercode+'/ccd'+paddedccdid+'/q'+qid+'/ztf_'+filestartdate+'_'+filtercode+'_c'+paddedccdid+'_q'+qid+'_hifreqflat.fits'
                    % https://irsa.ipac.caltech.edu/ibe/data/ztf/products/cal/2017/1014/hifreqflat/zr/ccd06/q2/ztf_20171014_zr_c06_q2_hifreqflat.fits

                    Link{Ilink} = sprintf('%s%s/04d/%02d%02d/hifreqflat/%s/ccd%02d/q%d/ztf_%04d%02d%02d_%s_c%02d_q%d_hifreqflat.fits',...
                                    InPar.BaseURL, Prop(Ilink).ImType, ...
                                    Prop(Ilink).Year, Prop(Ilink).Month, Prop(Ilink).Day, ...
                                    Prop(Ilink).FilterCode, ...
                                    Prop(Ilink).CCDID, Prop(Ilink).QuadID, ...
                                    Prop(Ilink).Year, Prop(Ilink).Month, Prop(Ilink).Day, ...
                                    Prop(Ilink).FilterCode, ...
                                    Prop(Ilink).CCDID, Prop(Ilink).QuadID);
                otherwise
                    error('Unknown ImTypeCode')
            end

        case 'ref'
            error('ref type not available yet')
            
        otherwise
            error('Unknown ImType option');
    end
end
