              [:gco:Date (second (re-matches #"(\d\d\d\d-\d\d-\d\d)T.*" (str (:PublicationDate pub-ref))))]]
             [:gmd:dateType
              [:gmd:CI_DateTypeCode
-              {:codeList (str (:iso code-lists) "#CI_DateTypeCode")
+              {:codeList (str (:iso u/code-lists) "#CI_DateTypeCode")
                :codeListValue "publication"} "publication"]]]])
         [:gmd:edition (char-string (:Edition pub-ref))]
         (when (:DOI pub-ref)

    [:gmd:date
     [:gco:DateTime value]]
    [:gmd:dateType
     [:gmd:CI_DateTypeCode {:codeList (str (:ngdc code-lists) "#CI_DateTypeCode")
                            :codeListValue date-name} date-name]]]])

(def attribute-data-type-code-list
  "http://earthdata.nasa.gov/metadata/resources/Codelists.xml#EOS_AdditionalAttributeDataTypeCode")

(defn- generate-title
  "Returns an ISO title string from the ShortName and LongName fields of the given record."
  [record]
  (let [{short-name :ShortName long-name :LongName} record]
    (if (seq long-name) (str short-name iso/keyword-separator long-name) short-name)))

(defn- science-keyword->iso-keyword-string
  "Returns an ISO science keyword string from the given science keyword."
  [science-keyword]
  (let [{category :Category
         topic :Topic
         term :Term
         variable-level-1 :VariableLevel1
         variable-level-2 :VariableLevel2
         variable-level-3 :VariableLevel3
         detailed-variable :DetailedVariable} science-keyword]
    (str/join iso/keyword-separator (map #(or % iso/nil-science-keyword-field)
                                         [category topic term variable-level-1 variable-level-2
                                          variable-level-3 detailed-variable]))))

(defn- generate-descriptive-keywords
  "Returns the content generator instructions for the given descriptive keywords."
  ([keywords]
   (generate-descriptive-keywords keywords nil))
  ([keywords keyword-type]
   [:gmd:MD_Keywords
    (for [keyword keywords]
      [:gmd:keyword [:gco:CharacterString keyword]])
    (when keyword-type
      [:gmd:type
       [:gmd:MD_KeywordTypeCode
        {:codeList (str (:ngdc code-lists) "#MD_KeywordTypeCode")
         :codeListValue keyword-type} keyword-type]])
    [:gmd:thesaurusName {:gco:nilReason "unknown"}]]))

(defn- generate-projects-keywords
  "Returns the content generator instructions for descriptive keywords of the given projects."
  [projects]
  (let [project-keywords (map generate-title projects)]
    (generate-descriptive-keywords project-keywords "project")))

(defn- generate-characteristics
  "Returns the generated characteristics content generator instructions, with the type
  argument used for the EOS_AdditionalAttributeTypeCode codeListValue attribute value and content."
  [type characteristics]
  [:eos:otherProperty
   [:gco:Record
    [:eos:AdditionalAttributes
     (for [characteristic characteristics]
       [:eos:AdditionalAttribute
        [:eos:reference
         [:eos:EOS_AdditionalAttributeDescription
          [:eos:type
           [:eos:EOS_AdditionalAttributeTypeCode
            {:codeList (str (:earthdata code-lists) "#EOS_AdditionalAttributeTypeCode")
             :codeListValue type}
            type]]
          [:eos:name
           (char-string (:Name characteristic))]
          [:eos:description
           (char-string (:Description characteristic))]
          [:eos:dataType
           [:eos:EOS_AdditionalAttributeDataTypeCode
            {:codeList (str (:earthdata code-lists) "#EOS_AdditionalAttributeDataTypeCode")
             :codeListValue (:DataType characteristic)}
            (:DataType characteristic)]]
          [:eos:parameterUnitsOfMeasure
           (char-string (:Unit characteristic))]]]
        [:eos:value
         (char-string (:Value characteristic))]])]]])

(defn- generate-sensors
  "Returns content generator instructions for the given sensors."
  [sensors]
  (for [sensor sensors]
    [:eos:sensor
     [:eos:EOS_Sensor
      [:eos:citation
       [:gmd:CI_Citation
        [:gmd:title
         [:gco:CharacterString (generate-title sensor)]]
        [:gmd:date {:gco:nilReason "unknown"}]]]
      [:eos:identifier
       [:gmd:MD_Identifier
        [:gmd:code
         (char-string (:ShortName sensor))]
        [:gmd:description
         (char-string (:LongName sensor))]]]
      [:eos:type
       (char-string (:Technique sensor))]
      [:eos:description {:gco:nilReason "missing"}]
      echo-attributes-info
      (generate-characteristics "sensorInformation" (:Characteristics sensor))]]))

(defn- generate-instruments
  "Returns content generator instructions for the given instruments."
  [instruments]
  (for [instrument instruments]
    [:gmi:instrument
     [:eos:EOS_Instrument
      [:gmi:citation
       [:gmd:CI_Citation
        [:gmd:title
         [:gco:CharacterString (generate-title instrument)]]
        [:gmd:date {:gco:nilReason "unknown"}]]]
      [:gmi:identifier
       [:gmd:MD_Identifier
        [:gmd:code
         (char-string (:ShortName instrument))]
        [:gmd:description
         (char-string (:LongName instrument))]]]
      [:gmi:type
       (char-string (:Technique instrument))]
      [:gmi:description {:gco:nilReason "missing"}]
      [:eos:otherPropertyType
       [:gco:RecordType {:xlink:href "http://earthdata.nasa.gov/metadata/schema/eos/1.0/eos.xsd#xpointer(//element[@name='AdditionalAttributes'])"}
        "Echo Additional Attributes"]]
      (generate-characteristics "instrumentInformation" (:Characteristics instrument))
      (generate-sensors (:Sensors instrument))]]))

(defn- generate-platforms
  "Returns the content generator instructions for the given platforms."
  [platforms]
  (for [platform platforms]
    [:gmi:platform
     [:eos:EOS_Platform
      [:gmi:identifier
       [:gmd:MD_Identifier
        [:gmd:code
         (char-string (:ShortName platform))]
        [:gmd:description
         (char-string (:LongName platform))]]]
      [:gmi:description (char-string (:Type platform))]
      (generate-instruments (:Instruments platform))

      ;; Characteristics
      (when-let [characteristics (:Characteristics platform)]
        [:eos:otherPropertyType
         [:gco:RecordType {:xlink:href "http://earthdata.nasa.gov/metadata/schema/eos/1.0/eos.xsd#xpointer(//element[@name='AdditionalAttributes'])"}
          "Echo Additional Attributes"]]
        (generate-characteristics "platformInformation" characteristics))]]))

(defn- generate-projects
  [projects]

            [cmr.umm-spec.umm-to-xml-mappings.iso19115-2.spatial :as spatial]
            [cmr.umm-spec.xml.gen :refer :all]
            [cmr.umm-spec.util :as su]
            [cmr.umm-spec.umm-to-xml-mappings.iso19115-2.platform :as platform]
            [cmr.umm-spec.iso19115-2-util :as u]))

(def iso19115-2-xml-namespaces
  {:xmlns:xs "http://www.w3.org/2001/XMLSchema"
