<?xml version="1.0" encoding="UTF-8"?>

#parse( "vcore.vmac" )
#parse( "utils.vmac" )
#parse( "general.vmac" )
#parse( "image.vmac" )
#parse( "surface.vmac" )
#parse( "mission_factory.vmac" )

## Set some needed variables

#set ($PID_FULL        = "$extra.OBJ.toString().toLowerCase()" )
#set ($PID             = "#getPidFullnameNoVersionExt($PID_FULL)" )
#set ($VERSION_ID      = "#getPIDVersion($PID_FULL)" )
#set ($INSTRUMENT_NAME = "#getInstrumentName()" )
#set ($INSTRUMENT_ID   = "#getInstrumentId()" )

#set ($COLLECTION      = "data")

#generateProlog($MSN_PROLOG_VERSION_DICT)

  <Identification_Area>
    <logical_identifier>#generateLID( $COLLECTION $PID )</logical_identifier>
    <version_id>$VERSION_ID.0</version_id>
    <title>Terrain Mesh in OBJ format</title>
    <information_model_version>$MSN_MODEL_VERSION</information_model_version>
    <product_class>Product_Observational</product_class>
  </Identification_Area>
  <Observation_Area>
    <Time_Coordinates>
        <start_date_time>#getZuluTime( $IDENT.START_TIME )</start_date_time>
        <stop_date_time>#getZuluTime( $IDENT.STOP_TIME )</stop_date_time>
    </Time_Coordinates>
    <Investigation_Area>
      <name>$MSN_DEFAULT_INVESTIGATION_NAME</name>
      <type>$MSN_DEFAULT_INVESTIGATION_TYPE</type>
      <Internal_Reference>
        <lid_reference>urn:nasa:pds:context:investigation:mission.${MSN_DEFAULT_PLATFORM}</lid_reference>
        <reference_type>data_to_investigation</reference_type>
      </Internal_Reference>
    </Investigation_Area>
    <Observing_System>
      <name>$MSN_DEFAULT_INVESTIGATION_NAME</name>
      <Observing_System_Component>
        <name>$MSN_DEFAULT_SPACECRAFT</name>
        <type>Spacecraft</type>
        <Internal_Reference>
          #set ($invest = $DEFAULT_INVESTIGATION.toLowerCase())
          <lid_reference>urn:nasa:pds:context:instrument_host:spacecraft.$MSN_DEFAULT_INVESTIGATION_NAME.toLowerCase()</lid_reference>
          <reference_type>is_instrument_host</reference_type>
        </Internal_Reference>
      </Observing_System_Component>
      <Observing_System_Component>
        <name>$INSTRUMENT_NAME</name>
        <type>Instrument</type>
        <Internal_Reference>
          <lid_reference>urn:nasa:pds:context:instrument:${INSTRUMENT_ID}.${MSN_DEFAULT_PLATFORM}</lid_reference>
          <reference_type>is_instrument</reference_type>
        </Internal_Reference>
      </Observing_System_Component>
    </Observing_System>
    <Target_Identification>
      #if ( $IDENT.TARGET_NAME )
        #debug("IDENTIFICATION.TARGET_NAME")
        #set ($target_name = $IDENT.TARGET_NAME.toString().toLowerCase() )       
        #debug("IDENTIFICATION.TARGET_TYPE")
        #set ($target_type = $IDENT.TARGET_TYPE.toString().toLowerCase() )       
      #else
        #set ($target_name = $MSN_DEFAULT_TARGET)
        #debug("IDENTIFICATION.TARGET_TYPE")
        #set ($target_type = $MSN_DEFAULT_TARGET_TYPE)
      #end
      <name>$text.capitalize($target_name)</name>
      <type>$text.capitalize($target_type)</type>
      <Internal_Reference>
        <lid_reference>urn:nasa:pds:context:target:${target_type}.${target_name}</lid_reference>
        <reference_type>data_to_target</reference_type>
      </Internal_Reference>
    </Target_Identification>
    <Mission_Area>
    </Mission_Area>
    <Discipline_Area>
      <geom:Geometry>
        <geom:Geometry_Lander>
          <geom:Motion_Counter>
            <geom:name>RMC</geom:name>
            #set ($count = $IDENT.ROVER_MOTION_COUNTER.size() - 1)
            #foreach ( $i in [ 0..$count ] )
            <geom:Motion_Counter_Index>
              <geom:index_id>$IDENT.ROVER_MOTION_COUNTER_NAME.get($foreach.index)</geom:index_id>
              <geom:index_value_number>$IDENT.ROVER_MOTION_COUNTER.get($foreach.index)</geom:index_value_number>
            </geom:Motion_Counter_Index>
            #end
          </geom:Motion_Counter>
        </geom:Geometry_Lander>
      </geom:Geometry>
      <img_surface:Surface_Imaging>
        <img_surface:Derived_Product_Parameters>
          #set ( $coordSystemName = "" )
          #set ( $coordSystemIndex = "" )
          #set ( $coordSystemName  = $extra.REFERENCE_COORD_SYSTEM_NAME )
          #set ( $coordSystemIndex = $extra.REFERENCE_COORD_SYSTEM_INDEX )
          #getCoordinateSpaceReference( $coordSystemName $coordSystemIndex "geom" "DERIVED_IMAGE_PARMS" )

        </img_surface:Derived_Product_Parameters>
      </img_surface:Surface_Imaging>
    </Discipline_Area>
</Observation_Area>
## new chunk
<Reference_List>
## loop thru the PNGS
  #if ($extra.PNGS)
  #set ($pngs_size = $extra.PNGS.size() )##
  #foreach ( $i in [1..$pngs_size] )
  <Source_Product_Internal>
  ## <lidvid_reference>urn:nasa:pds:insight_cameras:data:$SKIN_NAME::$SKIN_VERSION.0</lidvid_reference>
  ## <p>$extra.PNGS.get($foreach.index).replace('.png', '').toLowerCase()</p>
  <lidvid_reference>#generateLIDVID( $collection $extra.PNGS.get($foreach.index).replace('.png', '').toLowerCase() )</lidvid_reference>
  <reference_type>data_to_calibrated_source_product</reference_type>
    <comment>Pointer to the mesh skin (texture).</comment>
  </Source_Product_Internal>
  #end
  #end
  
 <Source_Product_Internal>
   <lidvid_reference>#generateLIDVID( $collection $extra.XYZ.replace('.VIC', '').toLowerCase() )</lidvid_reference>
  <reference_type>data_to_calibrated_source_product</reference_type>
   <comment>Pointer to the XYZ product from which the geometry is derived.</comment>
  </Source_Product_Internal>
  </Reference_List>

  <File_Area_Observational>
    <File>
      ## <file_name>$obj_file_name</file_name>
      <file_name>$extra.OBJ</file_name>
    </File>
#include( $extra.lbl)
  </File_Area_Observational>
  <File_Area_Observational_Supplemental>
    <File>
      ## <file_name>$mtl_file_name</file_name>
      <file_name>$extra.MTL</file_name>
    </File>
    <Stream_Text>
      <offset unit="byte">0</offset>
      <parsing_standard_id>7-Bit ASCII Text</parsing_standard_id>
      <record_delimiter>Carriage-Return Line-Feed</record_delimiter>
    </Stream_Text>
  </File_Area_Observational_Supplemental>
#if ($extra.PNGS)
  #set ($pngs_size = $extra.PNGS.size() )##
  #foreach ( $i in [1..$pngs_size] )
  <File_Area_Observational_Supplemental>
    <File>
      <file_name>$extra.PNGS.get($foreach.index)</file_name>
    </File>
    <Encoded_Image>
      <offset unit="byte">0</offset>
      <encoding_standard_id>PNG</encoding_standard_id>
    </Encoded_Image>
 </File_Area_Observational_Supplemental>
    #end
#else
    <File>
     <file_name>$png_file_name</file_name>
     </File>
    <Encoded_Image>
      <offset unit="byte">0</offset>
      <encoding_standard_id>PNG</encoding_standard_id>
    </Encoded_Image>
  </File_Area_Observational_Supplemental>
#end
</Product_Observational>

