<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
{% import 'common/iso19139-charstring.j2' as cs %}
<!-- This metadata record was generated by pygeometa-{{ pygeometa_version }} (https://github.com/geopython/pygeometa) -->
<wmdr:WIGOSMetadataRecord gml:id="id_{{ record['metadata']['identifier'] }}" xmlns:gco="http://www.isotc211.org/2005/gco" xmlns:gmd="http://www.isotc211.org/2005/gmd" xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:wmdr="http://def.wmo.int/wmdr/2017" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://def.wmo.int/wmdr/2017 http://schemas.wmo.int/wmdr/1.0/wmdr.xsd">
    <wmdr:headerInformation>
        <wmdr:Header>
            <wmdr:fileDateTime>{{ record['metadata']['datestamp'].strftime('%Y-%m-%dT%H:%M:%SZ') }}</wmdr:fileDateTime>
            <wmdr:recordOwner>
                {% set contact = record['contact']['record_owner'] %}
                {% set contact_id = 'recordOwner' %}
                {% set role = 'pointOfContact' %}
                {% include "contact.j2" %}
            </wmdr:recordOwner>
        </wmdr:Header>
    </wmdr:headerInformation>
    {% for k, v in record['facility'].items() %}
    <wmdr:facility>
        <wmdr:ObservingFacility gml:id="_{{ v['identifier'] }}">
            <gml:identifier codeSpace="http://wigos.wmo.int">http://wigos.wmo.int/{{ v['identifier'] }}</gml:identifier>
            <gml:name>{{ v['name'] }}</gml:name>
            <wmdr:responsibleParty>
                <wmdr:ResponsibleParty>
                    <wmdr:responsibleParty>
                        {% set contact = record['contact']['facility'] %}
                        {% set contact_id = 'responsibleParty' %}
                        {% set role = 'pointOfContact' %}
                        {% include "contact.j2" %}
                    </wmdr:responsibleParty>
                </wmdr:ResponsibleParty>
            </wmdr:responsibleParty>
            {% for gl in v['spatiotemporal'] %}
            <wmdr:geospatialLocation>
                <wmdr:GeospatialLocation>
                    {% set pos = gl['location']['point'].split(',') %}
                    {% if pos == [''] %}
                    <wmdr:geoLocation nilReason="missing"/>
                    {% else %}
                    <wmdr:geoLocation>
                        <gml:Point srsDimension="{{ pos|length }}" srsName="http://www.opengis.net/def/crs/EPSG/0/{{ gl['location']['crs'] }}" gml:id="p{{ loop.index }}">
                            <gml:pos>{{ pos[1] }} {{ pos[0] }} {{ pos[2] }}</gml:pos>
                        </gml:Point>
                    </wmdr:geoLocation>
                    {% endif %}
                    {% if v['geoposition_method'] %}
                    <wmdr:geopositioningMethod xlink:href="http://codes.wmo.int/wmdr/GeopositioningMethod/{{ v['geopositioning_method']|lower }}"/>
                    {% endif %}
                    <wmdr:validPeriod>
                        <gml:TimePeriod gml:id="tp{{ loop.index }}">
                            <gml:beginPosition>{{ gl['timeperiod']['begin'] }}</gml:beginPosition>
                            {% if gl['timeperiod']['end'] %}
                            <gml:endPosition>{{ gl['timeperiod']['end'] }}</gml:endPosition>
                            {% else %}
                            <gml:endPosition indeterminatePosition="now"/>
                            {% endif %}
                        </gml:TimePeriod>
                    </wmdr:validPeriod>
                </wmdr:GeospatialLocation>
            </wmdr:geospatialLocation>
            {% endfor %}
            <wmdr:onlineResource>
                <gmd:CI_OnlineResource>
                    <gmd:linkage>
                        <gmd:URL>{{ v['url'] }}</gmd:URL>
                    </gmd:linkage>
                    <gmd:protocol>
                        <gco:CharacterString>WWW:LINK</gco:CharacterString>
                    </gmd:protocol>
                    <gmd:function>
                        <gmd:CI_OnLineFunctionCode codeList="http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#CI_OnLineFunctionCode" codeListValue="information" codeSpace="ISOTC211/19115">information</gmd:CI_OnLineFunctionCode>
                    </gmd:function>
                </gmd:CI_OnlineResource>
            </wmdr:onlineResource>
            {% if v['date_established'] %}
            <wmdr:dateEstablished>{{ v['date_established'] }}</wmdr:dateEstablished>
            {% endif %}
            <wmdr:wmoRegion xlink:href="http://codes.wmo.int/wmdr/WMORegion/{{ v['wmo_region'] }}"/>
            <wmdr:territory>
                <wmdr:Territory>
                    <wmdr:territoryName xlink:href="http://codes.wmo.int/wmdr/TerritoryName/{{ v['territory_name']|upper }}"/>
                </wmdr:Territory>
            </wmdr:territory>
            {% for pa in v['program_affiliation'] %}
            {% set outer_loop = loop %}
            <wmdr:programAffiliation>
                <wmdr:ProgramAffiliation>
                    <wmdr:programAffiliation xlink:href="http://codes.wmo.int/wmdr/ProgramAffiliation/{{ pa['program'] }}"/>
                    {% for rs in pa['reporting_status'] %}
                    <wmdr:reportingStatus>
                        <wmdr:ReportingStatus>
                            <wmdr:reportingStatus xlink:href="http://codes.wmo.int/wmdr/ReportingStatus/{{ rs['status'] }}"/>
                            <wmdr:validPeriod>
                                 <gml:TimePeriod gml:id="stp_{{ outer_loop.index }}_{{ loop.index }}">
                                    <gml:beginPosition>{{ rs['valid_period']['begin'] }}</gml:beginPosition>
                                    {% if rs['valid_period']['end'] %}
                                    <gml:endPosition>{{ rs['valid_period']['end'] }}</gml:endPosition>
                                    {% else %}
                                    <gml:endPosition indeterminatePosition="now"/>
                                    {% endif %}
                                </gml:TimePeriod>
                            </wmdr:validPeriod>
                        </wmdr:ReportingStatus>
                    </wmdr:reportingStatus>
                    {% endfor %}
                </wmdr:ProgramAffiliation>
            </wmdr:programAffiliation>
            {% endfor %}
        </wmdr:ObservingFacility>
    </wmdr:facility>
    {% endfor %}
</wmdr:WIGOSMetadataRecord>
