function Data=ztf_imagename_prop(ImageName)
% Given a ZTF image name identify the image type, time, field, filter, etc.
% Package: VO.ZTF
% Description: Given a ZTF image name identify the image properties and
%              type, including image directory type {sci,raw,cal}, time,
%              field, filter (0 for bias images), CCD, amplifier,
%              image type, and image type/extension.
% Input  : - Cell array of images name, or string containing a single
%            image name with optional wild cards.
% Output : - A structure array containing the following fields (per image):
%            .DirType
%            .Year  - Year (UTC).
%            .Month - Month (UTC).
%            .Day   - Day (UTC).
%            .Frac  - fraction of day (UTC).
%            .JD    - Julian Day (UTC).
%            .Field - ZTF field.
%            .Filt  - Filter ('b'|'0' - no filter)
%            .CCD   - CCD number (1..16)
%            .Type  - Image type:
%                     'o' - object
%                     'b' - bias
%                     'f' - flat
%            .Amp   - Amplifier (quadrant) number (1..4)
%            .ImExt - Image type name and extension.
% License: GNU general public license version 3
%     By : Eran O. Ofek                    Nov 2017
%    URL : http://weizmann.ac.il/home/eofek/matlab/
% Example:
% Data=VO.ZTF.ztf_imagename_prop({'ztf_20171014_00_c01_q1_bias.fits','ztf_20171015354109_000656_zr_c02_o_q4_sciimg.fits','ztf_20171014142731_000000_zr_c01_f.fits.fz'});
% Reliable: 2
%--------------------------------------------------------------------------



%DefV. = 
%InPar = InArg.populate_keyval(DefV,varargin,mfilename);

if (~iscell(ImageName))
    [~,ImageName] = Util.files.create_list(ImageName,NaN);
    %ImageName = {ImageName};
end


Nim  = numel(ImageName);
Data = Util.struct.struct_def({'DirType','Year','Month','Day','Frac','JD','Field','FilterID','Filter','Filt','CCD','Type','Amp','ImExt'},Nim,1);
for Iim=1:1:Nim
    
    % split image name by "_"
    RE = regexp(ImageName{Iim},'_','split');
    
    switch numel(RE)
        case 8
            DirType = 'sci';
        otherwise
            switch lower(RE{5}(1))
                case 'q'
                    DirType = 'cal';
                case 'c'
                    DirType = 'raw';
                otherwise
                    error('Unknown DirType option');
            end
    end
    Data(Iim).DirType = DirType;
   
    % index of first character in that contains info
    PosI.Field =1;
    %PosI.Filt  =2;
    PosI.CCD   =2;
    PosI.Type  =1;
    PosI.Amp   =2;
    PosI.ImExt =1;
    % convert field to numeric?
    PosN.Field = true;
    %PosN.Filt  = false;
    PosN.CCD   = true;
    PosN.Type  = false;
    PosN.Amp   = true;
    PosN.ImExt = false;
    
    switch lower(DirType)
        case 'cal'
            % Calibration image:
            % E.g.,
            % https://ztfweb.ipac.caltech.edu/ztf/archive/cal/2017/1014/bias/00/ccd01/q1/
            % ztf_20171014_00_c01_q1_bias.fits (6)
            %       https://ztfweb.ipac.caltech.edu/ztf/archive/cal/2017/1014/hifreqflat/zr/ccd01/q1/
            %       ztf_20171014_zr_c01_q1_hifreqflat.fits (6)
            Pos.Field = []; 
            %Pos.Filt  = 3;  
            Pos.CCD   = 4;  
            Pos.Type  = []; 
            Pos.Amp   = 5;  
            Pos.ImExt = 6;  
            
            Data(Iim).Filt = RE{3};
            
            
        case 'raw'
            % raw image
            % E.g.,
            % https://ztfweb.ipac.caltech.edu/ztf/archive/raw/2017/1014/142731/
            % ztf_20171014142731_000000_zr_c01_f.fits.fz (6)
            Pos.Field = 3;
            %Pos.Filt  = 4;
            Pos.CCD   = 5;
            Pos.Type  = [];
            Pos.Amp   = [];
            Pos.ImExt = 6;
            
            Data(Iim).Filt = RE{4};

        case 'sci'
            % science image
            % E.g.,
            % https://ztfweb.ipac.caltech.edu/ztf/archive/sci/2017/1015/354109/
            % ztf_20171015354109_000656_zr_c02_o_q4_sciimg.fits (8)
            Pos.Field = 3;
            Pos.Filt  = 4;
            Pos.CCD   = 5;
            Pos.Type  = 6;
            Pos.Amp   = 7;
            Pos.ImExt = 8;
            
            Data(Iim).Filt = RE{4};

        otherwise
            error('Unknown DirType option');
    end
    
    
    % read the Y/M/D/F from image name
    [Data(Iim).Year, Data(Iim).Month, Data(Iim).Day, Data(Iim).Frac] = ztf_read_date(RE);
    if (isnan(Data(Iim).Frac))
        Data(Iim).JD = celestial.time.julday([Data(Iim).Day, Data(Iim).Month, Data(Iim).Year]);
    else
        Data(Iim).JD = celestial.time.julday([Data(Iim).Day, Data(Iim).Month, Data(Iim).Year, Data(Iim).Frac]);
    end
   
    FN = fieldnames(Pos);
    Nf = numel(FN);
    for If=1:1:Nf
        if (isempty(Pos.(FN{If})))
            Data(Iim).(FN{If}) = NaN;
        else
            I1                 = PosI.(FN{If});  % index of first char in data
            Data(Iim).(FN{If}) = RE{Pos.(FN{If})}(I1:end);
            if (PosN.(FN{If}))
                % convert to number
                Data(Iim).(FN{If}) = str2double(Data(Iim).(FN{If}));
            end
            
        end 
    
    end
    
    switch lower(Data(Iim).Filt)
        case 'zg'
            Data(Iim).Filter   = 'g';
            Data(Iim).FilterID = 1;
        case 'zr'
            Data(Iim).Filter   = 'r';
            Data(Iim).FilterID = 2;
        case 'zi'
            Data(Iim).Filter   = 'i';
            Data(Iim).FilterID = 3;
        otherwise
            Data(Iim).Filter   = NaN;
            Data(Iim).FilterID = NaN;
    end
    
end

end

function [Year, Month, Day, Frac]=ztf_read_date(RE)
    Year   = str2double(RE{2}(1:4));
    Month  = str2double(RE{2}(5:6));
    Day    = str2double(RE{2}(7:8));
    if (numel(RE{2})==8)
        % no fraction of day
        Frac = NaN;
    else
        Frac   = str2double(RE{2}(9:end))./1e6;
    end
end

