import csv


def write_smn(datetime_vec, value_avg_vec, value_std_vec, fname):
    """
    writes SwissMetNet data in format datetime,avg_value, std_value

    Parameters
    ----------
    datetime_vec : datetime array
        array containing the measurement time
    value_avg_vec : float array
        array containing the average value
    value_std_vec : float array
        array containing the standard deviation
    fname : str
        file name where to store the data

    Returns
    -------
    fname : str
        the name of the file where data has written

    """
    with open(fname, 'w', newline='') as csvfile:
        fieldnames = ['datetime', 'avg', 'std']
        writer = csv.DictWriter(csvfile, fieldnames)
        writer.writeheader()
        for i, value_avg in enumerate(value_avg_vec):
            writer.writerow({
                'datetime': datetime_vec[i].strftime('%Y%m%d%H%M%S'),
                'avg': value_avg,
                'std': value_std_vec[i]})

        csvfile.close()

    return fname