<jupyter_start><jupyter_text>Create metadataThis notebook creates a metadata.json file based on the EnergyPlus eplusout.csv file.  Setup<jupyter_code>import requests
import csv
import json<jupyter_output><empty_output><jupyter_text>Get csv file from figshare<jupyter_code>csv_download_url='https://figshare.com/ndownloader/files/35934071?private_link=464885898d0041bfa8fd'
response=requests.get(csv_download_url)
csv_text=response.text
csv_text.split('\n')[0]<jupyter_output><empty_output><jupyter_text>Get header row<jupyter_code>csv_reader=csv.reader(csv_text.splitlines())
header_row=next(csv_reader)
header_row=[x.strip() for x in header_row]
header_row<jupyter_output><empty_output><jupyter_text>Function to create column descriptions<jupyter_code>base_url=r"https://bigladdersoftware.com/epx/docs/9-6/input-output-reference/"
reference_dict={
    "Site Outdoor Air Drybulb Temperature": \
        "group-location-climate-weather-file-access.html#site-outdoor-air-drybulb-temperature-c",
    "Zone Mean Radiant Temperature": \
        "group-thermal-zone-description-geometry.html#zone-mean-radiant-temperature-c-1",
    "Site Total Sky Cover": \
        "group-location-climate-weather-file-access.html#site-total-sky-cover",
    "Site Opaque Sky Cover": \
        "group-location-climate-weather-file-access.html#site-opaque-sky-cover",
    "Zone Mean Air Temperature": \
        "group-thermal-zone-description-geometry.html#zone-mean-air-temperature-c-1",
    "Zone Air Heat Balance Surface Convection Rate": \
        "group-thermal-zone-description-geometry.html#zone-air-heat-balance-surface-convection-rate-w",
    "Zone Air Heat Balance Air Energy Storage Rate": \
        "group-thermal-zone-description-geometry.html#zone-air-heat-balance-air-energy-storage-rate-w",
    "Site Daylight Saving Time Status": \
        "group-location-climate-weather-file-access.html#site-daylight-saving-time-status",
    "Site Day Type Index": \
        "group-location-climate-weather-file-access.html#site-day-type-index",
    "Zone Total Internal Latent Gain Energy": \
        "group-thermal-zone-description-geometry.html#zone-total-internal-latent-gain-energy-j",
    "Other Equipment Total Heating Energy": \
        "group-internal-gains-people-lights-other.html#outputs-5-004",
    "Surface Inside Face Temperature": \
        "group-thermal-zone-description-geometry.html#surface-inside-face-temperature-c",
    "Surface Outside Face Temperature": \
        "group-thermal-zone-description-geometry.html#surface-outside-face-temperature-c",
    "Surface Inside Face Convection Heat Transfer Coefficient": \
        "group-thermal-zone-description-geometry.html#surface-inside-face-convection-heat-transfer-coefficient-wm2-k",
    "Surface Outside Face Convection Heat Transfer Coefficient": \
        "group-thermal-zone-description-geometry.html#surface-outside-face-convection-heat-transfer-coefficient-wm2-k"
}

qudt_dict={
    #"": "http://qudt.org/vocab/unit/FRACTION",
    "C": "http://qudt.org/vocab/unit/DEG_C",
    "W": "http://qudt.org/vocab/unit/W-PER-M",
    "J": "http://qudt.org/vocab/unit/J",
    "W/m2-K": "http://qudt.org/vocab/unit/W-PER-M2-K"
}


time_interval_dict={
    "Hourly": "H1",
    "Daily": "D1",
    "Monthly": "M1"    
}


def create_column_description(header):
    ""
    d={
        "@type": "Column",
        "titles": header,
    }
    
    variable=header.split('[')[0].strip()
    
    try:
        units=header.split('[')[1].split(']')[0].strip()
    except IndexError:
        units=None
        
    try:
        time_interval=header.split('(')[1].split(')')[0].strip()
    except IndexError:
        time_interval=None
    
    # dc:description
    d["dc:description"]= header
    
    # schema:variableMeasured
    d['schema:variableMeasured']=variable
    
    # schema:unitText
    if units:
        d['schema:unitText']=units
        
    # schema:duration
    if time_interval:
        d['schema:duration']=time_interval_dict[time_interval]
    
    # dc:reference
    reference_url=None
    for k,v in reference_dict.items():
        if variable.endswith(k):
            reference_url=v
    if reference_url:
        d['dc:references']={"@id":f"{base_url}{reference_url}"}
    else:
        if not variable is None:
            print(variable)
        
    # http://purl.org/linked-data/sdmx/2009/attribute#unitMeasure
    qudt_url=qudt_dict.get(units,None)
    if qudt_url:
        d['http://purl.org/linked-data/sdmx/2009/attribute#unitMeasure']={"@id":f"{qudt_url}"}
    else:
        print(units)
    
    # datatype
    if variable=='Date/Time':
        d['datatype']='string'
    else:
        d['datatype']='number'
        
    # other comments
    if variable=='Date/Time':
        d['rdfs:comment']='The Date/Time column contains a non-standard date and time format which does match any of the CSVW data format options.'
        
    return d

# test below
header='Environment:Site Outdoor Air Drybulb Temperature [C](Hourly)'
create_column_description(header)<jupyter_output><empty_output><jupyter_text>Create & save metadata dict<jupyter_code>d={
    "@context": "http://www.w3.org/ns/csvw",
    "@type": "Table",
    "url": "eplusout.csv",
    "dc:title": "EnergyPlus simulation test",
    "dc:description": "An EnergyPlus simulation of an office room in Sir Frank Gibb building, Loughborough University",
    "dc:creator": "ABCE Open Research Team",   
    "tableSchema": {
        "@type": "Schema",
        "columns": [create_column_description(header) for header in header_row]
        }
}
with open('eplusout.csv-metadata.json','w') as f:
    json.dump(d, f, indent=4)
d<jupyter_output>Date/Time
None
{'fraction_non_alphanumeric': 0.14255506607929516, 'fraction_numerical': 0.00933920704845815, 'mean_word_length': 3.5408, 'pattern_counts': {'":': 36, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '495484', 'license': 'MIT', 'n_tokens_mistral': 1950, 'n_tokens_neox': 1879, 'n_words': 299, 'path': 'csv-on-the-web-working-with-energyplus-results/create_metadata.ipynb', 'repo_name': 'stevenkfirth/stevenfirth', 'size': '5675'}