Issue saving epw file from measured data

I am creating a new EPW from measured data using ladybug. Here is a code sample

    epw_data = EPW.from_missing_values()
    from ladybug.location import Location
    epw_data.location = Location('Paris',None,'FR',48.85,2.35,1,35.0)
    epw_data.years.values = df_epw_file.index.year.values
    for col in set(df_epw_file.columns):
        if np.any(np.isnan(df_epw_file[col].values)):
            continue
        col_formatted = col.lower().replace(" ","_")
        if hasattr(epw_data, col_formatted):
            getattr(epw_data, col_formatted).values = df_epw_file[col].values

    epw_data.write(str(write_location))

This is not stand alone but that gives an idea. The problem i face is in the saved file, the last line of the epw is weird. See below the last two lines

2021,12,31,23,0,?9?9?9?9E0?9?9?9?9?9?9?9?9?9?9?9?9?9?9?9*9*9?9?9?9,10.87,10.11,96.3,102618.0,9999,9999,331.23,9999,0.0,0.0,999999,999999,999999,9999,229.84,2.92,8.87,99,9999,99999,9,999999999,999,999,0.0,99,999,0.0,99
2021,1,31,24,0,?9?9?9?9E0?9?9?9?9?9?9?9?9?9?9?9?9?9?9?9*9*9?9?9?9,-1.02,-1.5,97.83,102618.0,9999,9999,235.24,9999,0.0,0.0,999999,999999,999999,9999,315.2,1.54,2.35,99,9999,99999,9,999999999,999,999,0.0,99,999,0.0,99

it is finishing with a line that has month = 1 instead of 12 and the data is not what I have input in the fields. I made sure I am assigning tables of length 8760. I am doing something wrong ?

Hi @tomaszG,

Can you provide the full measured dataset ? It’s hard to help you sorted this out with out the input data.

Also you might want to check this post as it might be related to your problem:

thnaks for the reply !

I suspect it is related to the shift that is discussed in the thread, which I don’t quite understand. here is a standalone code to reproduce my issue

import pandas as pd
import numpy as np
from pathlib import Path
from datetime import datetime
import pytz

from ladybug.epw import EPW

def write_epw_file(df_epw_file, write_location):
    #Test with ladybug
    epw_data = EPW.from_missing_values()
    from ladybug.location import Location
    epw_data.location = Location('Paris',None,'FR',48.85,2.35,1,35.0)
    epw_data.years.values = df_epw_file.index.year.values
    #this is required to be considered properly as AMY and not TMY
    epw_data.header[-1] = epw_data.header[-1].replace("Sunday", df_epw_file.index[0].strftime('%A'))
    # epw_data.approximate_design_day(day_type='SummerDesignDay')
    # epw_data.approximate_design_day(day_type='WinterDesignDay')
    # match_cols = [()]
    for col in set(df_epw_file.columns):
        if np.any(np.isnan(df_epw_file[col].values)):
            continue
        col_formatted = col.lower().replace(" ","_")
        if hasattr(epw_data, col_formatted):
            getattr(epw_data, col_formatted).values = df_epw_file[col].values

    

    epw_data.write(str(write_location))

def test_write_epw_file():
    fake_data = np.tile(np.arange(24),365)
    df_epw_file = pd.DataFrame(fake_data, columns=['dry_bulb_temperature'], index = pd.date_range('2021-01-01', periods = 8760, freq = '60T'))
    write_location = Path(__file__).parent / 'test.epw'
    write_epw_file(df_epw_file, write_location)

if __name__=="__main__":
    # parse_weather_files()
    # tweak_weather_files()
    test_write_epw_file()
    # path = Path(__file__).parent / 'test' / 'weather' / 'US_CO_Boulder_AMY_2012.epw'
    # look_at_epw(path)
    plt.show()

If you observe the file created, it starts with temperature =1, which suggests the first element was skipped, and then ends with 0, but on top of this the date for the last line is strange.

2021,1,31,24,0 where it should 2021,12,31,24,0