3131from os import path
3232
3333import xarray
34+ import numpy as np
3435import yaml
3536from boundary import Segment
3637
@@ -53,18 +54,24 @@ def write_day(date, glorys_dir, segments, variables, output_prefix):
5354 return
5455
5556 glorys = (
56- xarray .open_dataset (file_path )
57+ xarray .open_dataset (file_path , decode_times = False )
5758 .rename ({'latitude' : 'lat' , 'longitude' : 'lon' , 'depth' : 'z' })
5859 )
5960
61+ # Capture time attributes and encoding
62+ time_attrs = glorys ['time' ].attrs if 'time' in glorys .coords else None
63+ time_encoding = glorys ['time' ].encoding if 'time' in glorys .coords else None
64+
6065 for segment in segments :
6166 for variable in variables :
6267 if variable == 'uv' :
6368 print (f"Processing { segment .border } { variable } " )
64- segment .regrid_velocity (glorys ['uo' ], glorys ['vo' ], suffix = f"{ date :%Y%m%d} " , flood = False )
69+ segment .regrid_velocity (glorys ['uo' ], glorys ['vo' ], suffix = f"{ date :%Y%m%d} " , flood = False ,
70+ time_attrs = time_attrs , time_encoding = time_encoding )
6571 elif variable in ['thetao' , 'so' , 'zos' ]:
6672 print (f"Processing { segment .border } { variable } " )
67- segment .regrid_tracer (glorys [variable ], suffix = f"{ date :%Y%m%d} " , flood = False )
73+ segment .regrid_tracer (glorys [variable ], suffix = f"{ date :%Y%m%d} " , flood = False ,
74+ time_attrs = time_attrs , time_encoding = time_encoding )
6875
6976def concatenate_files (nsegments , output_dir , variables , ncrcat_names , first_date , last_date , adjust_timestamps = False ):
7077 """Concatenate annual files using ncrcat."""
@@ -93,16 +100,41 @@ def concatenate_files(nsegments, output_dir, variables, ncrcat_names, first_date
93100 adjust_file_timestamps (output_file )
94101
95102def adjust_file_timestamps (file_path ):
96- """Adjust timestamps for the first and last records in a file."""
97- with xarray .open_dataset (file_path ) as ds :
98- if 'time' in ds :
99- time = ds ['time' ].copy ()
100- adjusted_time = time .astype ('datetime64[ns]' )
101-
102- adjusted_time [0 ] = adjusted_time [0 ].dt .floor ('D' )
103- adjusted_time [- 1 ] = adjusted_time [- 1 ].dt .ceil ('D' )
103+ """
104+ Adjust timestamps for the first and last records in a file while preserving attributes and raw numerical format.
105+ """
106+ with xarray .open_dataset (file_path , decode_times = False ) as ds :
107+ # Explicitly load the dataset into memory if it's lazy-loaded
108+ ds .load ()
104109
105- ds = ds .assign_coords (time = adjusted_time )
110+ if 'time' in ds :
111+ # Extract the time variable, attributes, and encoding
112+ time = ds ['time' ]
113+ time_attrs = time .attrs # Save the original attributes
114+ time_encoding = time .encoding # Save the original encoding
115+ time_values = time .values .copy ()
116+
117+ # Ensure the 'time' variable has more than one entry
118+ if len (time_values ) > 1 :
119+ # Adjust the first and last timestamps in raw numerical format
120+ time_values [0 ] = np .floor (time_values [0 ]) # Floor to the start of the day
121+ time_values [- 1 ] = np .ceil (time_values [- 1 ]) # Ceil to the end of the day
122+
123+ # Create a new DataArray for time while preserving attributes
124+ new_time = xarray .DataArray (
125+ time_values ,
126+ dims = time .dims ,
127+ attrs = time_attrs ,
128+ name = 'time'
129+ )
130+
131+ # Assign the new time variable back to the dataset
132+ ds = ds .assign_coords (time = new_time )
133+
134+ # Reapply the original encoding to ensure consistency
135+ ds ['time' ].encoding = time_encoding
136+
137+ # Save the updated dataset
106138 ds .to_netcdf (file_path )
107139 print (f"Timestamps adjusted for { file_path } " )
108140
0 commit comments