from netCDF4._netCDF4 import Variable


variables = {"template": {"standard_name": "",
                          "standard_name_url": "",
                          "long_name": "",
                          "units": "",
                          "ioos_category": "",
                          "accuracy": "",
                          "precision": "",
                          "coordinates": "depth_below_ground_surface bottom_of_interval top_of_interval time latitude longitude platform_name profile"},

             "excess_ice":{"standard_name": "ice_volume_in_frozen_ground_in_excess_of_pore_volume_in_unfrozen_ground_expressed_as_fraction_of_frozen_ground_volume",
                          "standard_name_url": "https://mmisw.org/ont/cf/parameter/ice_volume_in_frozen_ground_in_excess_of_pore_volume_in_unfrozen_ground_expressed_as_fraction_of_frozen_ground_volume",
                          "long_name": "Excess ice content",
                          "units": "percent",
                          "valid_min":"0",
                          "ioos_category": "Ice Distribution",
                          "accuracy": "",
                          "precision": "",
                          "coordinates": "depth_below_ground_surface bottom_of_interval top_of_interval time latitude longitude platform_name profile"},

            "sand": {"standard_name": "mass_fraction_of_sand_in_soil",
                          "standard_name_url": "https://mmisw.org/ont/cf/parameter/mass_fraction_of_sand_in_soil",
                          "long_name": "Sand content (by mass)",
                          "units": "percent",
                          "valid_min":"0",
                          "valid_max":"100",
                          "ioos_category": "Other",
                          "accuracy": "",
                          "precision": "",
                          "coordinates": "depth_below_ground_surface bottom_of_interval top_of_interval time latitude longitude platform_name profile"},

            "silt": {"standard_name": "mass_fraction_of_silt_in_soil",
                          "standard_name_url": "https://mmisw.org/ont/cf/parameter/mass_fraction_of_silt_in_soil",
                          "long_name": "Silt content (by mass)",
                          "units": "percent",
                          "valid_min":"0",
                          "valid_max":"100",
                          "ioos_category": "Other",
                          "accuracy": "",
                          "precision": "",
                          "coordinates": "depth_below_ground_surface bottom_of_interval top_of_interval time latitude longitude platform_name profile"},

            "clay": {"standard_name": "mass_fraction_of_clay_in_soil",
                          "standard_name_url": "https://mmisw.org/ont/cf/parameter/mass_fraction_of_clay_in_soil",
                          "long_name": "Clay content (by mass)",
                          "units": "percent",
                          "valid_min":"0",
                          "valid_max":"100",
                          "ioos_category": "Other",
                          "accuracy": "",
                          "precision": "",
                          "coordinates": "depth_below_ground_surface bottom_of_interval top_of_interval time latitude longitude platform_name profile"},
            
            "gravel": {"standard_name": "mass_fraction_of_gravel_in_soil",
                          "standard_name_url": "https://mmisw.org/ont/cf/parameter/mass_fraction_of_gravel_in_soil",
                          "long_name": "Gravel content (by mass)",
                          "units": "percent",
                          "valid_min":"0",
                          "valid_max":"100",
                          "ioos_category": "Other",
                          "accuracy": "",
                          "precision": "",
                          "coordinates": "depth_below_ground_surface bottom_of_interval top_of_interval time latitude longitude platform_name profile"},

            "liquid_limit": {#"standard_name": "soil_liquid_limit",
                            #"standard_name_url": "https://mmisw.org/ont/cf/parameter/soil_liquid_limit",
                            "long_name": "Atterberg liquid limit",
                            "units": "percent",
                          "valid_min":"0",
                          "valid_max":"100",
                            "ioos_category": "Other",
                            "accuracy": "",
                            "precision": "",
                            "coordinates": "depth_below_ground_surface bottom_of_interval top_of_interval time latitude longitude platform_name profile"},

            "plastic_limit": {#"standard_name": "soil_plastic_limit",
                             #"standard_name_url": "https://mmisw.org/ont/cf/parameter/soil_plastic_limit",
                             "long_name": "Atterberg plastic limit",
                             "units": "percent",
                          "valid_min":"0",
                          "valid_max":"100",
                             "ioos_category": "Other",
                             "accuracy": "",
                             "precision": "",
                             "coordinates": "depth_below_ground_surface bottom_of_interval top_of_interval time latitude longitude platform_name profile"},

            "ground_ice_class": {"long_name": "Ground ice classification",
                                "ioos_category": "Other",
                                "accuracy": "",
                                "precision": "",
                                "_Encoding": "UTF-8",
                                "coordinates": "depth_below_ground_surface bottom_of_interval top_of_interval time latitude longitude platform_name profile"},

            "soil_ph": {"standard_name": "soil_water_ph",
                             "standard_name_url": "https://mmisw.org/ont/cf/parameter/soil_water_ph",
                             "long_name": "Soil water pH",
                                # "units": "1",
                                "ioos_category": "Other",
                                "accuracy": "",
                                "precision": "",
                                "_Encoding": "UTF-8",
                                "coordinates": "depth_below_ground_surface bottom_of_interval top_of_interval time latitude longitude platform_name profile"},
            
            "materials": {#"standard_name": "",
                          #"standard_name_url": "",
                             "long_name": "Material classification",
                                #"units": "1",
                                "ioos_category": "Other",
                                "accuracy": "",
                                "precision": "",
                                "_Encoding": "UTF-8",
                                "coordinates": "depth_below_ground_surface bottom_of_interval top_of_interval time latitude longitude platform_name profile"},

            "materials_description": {#"standard_name": "",
                          #"standard_name_url": "",
                             "long_name": "Material description",
                                #"units": "1",
                                "ioos_category": "Other",
                                "accuracy": "",
                                "precision": "",
                                "_Encoding": "UTF-8",
                                "coordinates": "depth_below_ground_surface bottom_of_interval top_of_interval time latitude longitude platform_name profile"},

            "volumetric_moisture": {"standard_name": "volume_fraction_of_condensed_water_in_soil",
                                    "standard_name_url": "https://mmisw.org/ont/cf/parameter/volume_fraction_of_condensed_water_in_soil",
                                  "long_name": "Volumetric moisture content",
                                  "units": "percent",
                                  "valid_min" : "0",
                                "ioos_category": "Other",
                                "accuracy": "",
                                "precision": "",
                                "coordinates": "depth_below_ground_surface bottom_of_interval top_of_interval time latitude longitude platform_name profile"},

            "gravimetric_moisture": {#"standard_name": "",
                                     #"standard_name_url": "",
                                     "long_name": "Gravimetric moisture content",
                                     "units": "percent",
                                     "valid_min":"0",
                                     "ioos_category": "Other",
                                     "accuracy": "",
                                     "precision": "",
                                     "coordinates": "depth_below_ground_surface bottom_of_interval top_of_interval time latitude longitude platform_name profile"},

            "visible_ice": {"standard_name": "volume_fraction_of_frozen_water_in_soil",
                            "standard_name_url": "https://mmisw.org/ont/cf/parameter/volume_fraction_of_frozen_water_in_soil",
                            "long_name": "Visible ice content",
                            "units": "percent",
                            "valid_min":"0",
                            "ioos_category": "Other",
                            "accuracy": "",
                            "precision": "",
                            "coordinates": "depth_below_ground_surface bottom_of_interval top_of_interval time latitude longitude platform_name profile"},
    }

def add_variable(rootgrp, name, dtype, dimensions, fill_value=None) -> Variable:
    if name not in variables.keys():
        raise KeyError("Variable name not in variable templates list")
    else:
        vt = variables[name]

    var = rootgrp.createVariable(name, dtype, dimensions, fill_value=fill_value)
    
    for key, val in vt.items():
        var.setncattr(key, val)

    return var