function [ output_meta ] = meta2sicd_s1manifest( domnode )
%META2SICD_S1MANIFEST Converts Sentinel-1 manifest.safe description into a SICD-style metadata structure
%
% In general, the Sentinel-1 manifest.safe file doesn't have much real
% metadata directly in it.  However, it is the document that points to all
% of the data and metadata files in the package.
%
% Written by: Wade Schwartzkopf, NGA/IDT
%
% //////////////////////////////////////////
% /// CLASSIFICATION: UNCLASSIFIED       ///
% //////////////////////////////////////////

%% Setup
% Overall wrapping node is in a namespace that makes things messier
% (than they already are) in a XPath call, so we go down one level.
domnode = domnode.getFirstChild;
xp=javax.xml.xpath.XPathFactory.newInstance.newXPath();

%% CollectionInfo
platform_xp_str = ['metadataSection/' ...
    'metadataObject[@ID="platform"]/' ...
    'metadataWrap/' ...
    'xmlData/' ...
    '*[local-name()=''platform'']/'];
output_meta.CollectionInfo.CollectorName = [
    char(xp.evaluate(...
    [platform_xp_str '*[local-name()=''familyName'']'],domnode)) ...
    char(xp.evaluate(...
    [platform_xp_str '*[local-name()=''number'']'],domnode))];
% The following core name is unique within all Sentinel-1 collections.
% Perhaps we would rather use the NGA CoreName standard.
% We also may need to further add to this CoreName, if each collect has
% multiple swaths and bursts which must each go into their own SICD, each
% requiring their own unique CoreNames.
standalone_xp_str = ['metadataSection/' ...
    'metadataObject[@ID="generalProductInformation"]/' ...
    'metadataWrap/' ...
    'xmlData/' ...
    '*[local-name()=''standAloneProductInformation'']/'];
% Since each TOPSAR product has multiple CDPs (bursts), we really don't
% have enough information here to make a unique identifier for each CDP.
% We will defer generating the CoreName until meta2sicd_s1product.m
% output_meta.CollectionInfo.CoreName = char(xp.evaluate(...
%     [standalone_xp_str '*[local-name()=''missionDataTakeID'']'],domnode));
output_meta.CollectionInfo.CollectType='MONOSTATIC';
output_meta.CollectionInfo.RadarMode.ModeID = ...
    char(xp.evaluate(...
    [platform_xp_str ...
    '*[local-name()=''instrument'']/' ...
    '*[local-name()=''extension'']/' ...
    '*[local-name()=''instrumentMode'']/' ...
    '*[local-name()=''mode'']'],domnode));
if strcmp(output_meta.CollectionInfo.RadarMode.ModeID, 'SM')
    output_meta.CollectionInfo.RadarMode.ModeType='STRIPMAP';
else
    % Actually TOPSAR.  Not what we normally think of for DYNAMIC STRIPMAP,
    % but it is definitely not SPOTLIGHT, and doesn't seem to be regular
    % STRIPMAP either.
    output_meta.CollectionInfo.RadarMode.ModeType='DYNAMIC STRIPMAP';
end
output_meta.CollectionInfo.Classification='UNCLASSIFIED';

%% ImageCreation
% These fields describe the final SLC post processing step, not the SLC
% processing itself, although in datasets we have seen, this information
% has been the same.
processing_xp_str = ['metadataSection/' ...
    'metadataObject[@ID="processing"]/' ...
    'metadataWrap/' ...
    'xmlData/' ...
    '*[local-name()=''processing'']/'];
facility_xp_str = [processing_xp_str '*[local-name()=''facility'']/'];
software_xp_str = [facility_xp_str '*[local-name()=''software'']/'];
output_meta.ImageCreation.Application = [...
    char(xp.evaluate([software_xp_str '@name'],domnode)) ' '...
    char(xp.evaluate([software_xp_str '@version'],domnode))];
output_meta.ImageCreation.DateTime = datenum( ...
    char(xp.evaluate([processing_xp_str '@stop'],domnode)), ...
    'yyyy-mm-ddTHH:MM:SS.FFF');
output_meta.ImageCreation.Site = [ ...
    char(xp.evaluate([facility_xp_str '@name'],domnode)) ', ' ...
    char(xp.evaluate([facility_xp_str '@site'],domnode)) ', ' ...
    char(xp.evaluate([facility_xp_str '@country'],domnode))];
output_meta.ImageCreation.Profile='Prototype';

%% Timeline
% The start/stop times given in the manifest file appear to be the zero
% Doppler times for the first and the last lines, not the total collect
% time, which seems to be given by firstLineSensingTime/lastLineSensingTime
% in the annotation XML file. These start/stop times in the manifest file
% also seem to be replicated in the annotation XML file.
% acquistion_xp_str = ['metadataSection/' ...
%     'metadataObject[@ID="acquisitionPeriod"]/' ...
%     'metadataWrap/' ...
%     'xmlData/' ...
%     '*[local-name()=''acquisitionPeriod'']/'];
% char(xp.evaluate(...
%     [acquistion_xp_str '*[local-name()=''startTime'']'],domnode));
% char(xp.evaluate(...
%     [acquistion_xp_str '*[local-name()=''stopTime'']'],domnode));

%% RadarCollection
num_pols = str2double(xp.evaluate(...
    ['count(' standalone_xp_str '*[local-name()=''transmitterReceiverPolarisation''])'],domnode));
for i = 1:num_pols
    pol = char(xp.evaluate(...
        [standalone_xp_str '*[local-name()=''transmitterReceiverPolarisation''][' num2str(i) ']'],domnode));
    output_meta.RadarCollection.RcvChannels.ChanParameters(i).TxRcvPolarization=[pol(1) ':' pol(2)];
end

end


% //////////////////////////////////////////
% /// CLASSIFICATION: UNCLASSIFIED       ///
% //////////////////////////////////////////