Hi,
I download data for my team, and until now the variable in the files was time I use bash scripts.
I try to modify valid_time to an integer using ncap2 :
ncap2 -O -h -s “valid_time=int(valid_time)” {RES_REANALYSIS}/{file_fin_monthly} {RES_REANALYSIS2}/{file_fin_monthly_new}
and also to modify the name and the dimension valid_time into time :
ncrename -d valid_time,time -v valid_time,time
${RES_REANALYSIS}/$file_monthly
But it does not work.
If someone knows how to do this ?
Thanks
Hello
I tried with NCO but that results in loss of data w.r.t time. One of my colleague (Muhammad Yasrab) gave me this py code:
import xarray as xr
Load the NetCDF file
file_path = ‘/.nc’ # Change this to your file path
output_path = ‘/.nc’ # Path for the renamed output
Open the NetCDF file
ds = xr.open_dataset(file_path)
Check if ‘valid_time’ exists and rename it
if ‘valid_time’ in ds:
ds = ds.rename({‘valid_time’: ‘time’})
else:
print(“‘valid_time’ variable not found.”)
Save the modified dataset to a new NetCDF file
ds.to_netcdf(output_path)
Close the dataset to free resources
ds.close()
print(f"File saved as {output_path}")
This does convert the valid_time to time without loss of data. I am still validating the results which I got with this data. Apparently, they look fine. You can give it a try.
Hi,
Thanks for the answer. I could change the valid_time into time.
I still have a problem to concatenate files with cdo mergetime as valid_time is still in the dimensions and time has not the correct attribute UNLIMITED : I have
dimensions:
valid_time = UNLIMITED ; // (0 currently)
time = 12 ;
longitude = 1440 ;
latitude = 721 ;
How can I remove valid_time from the dimensions et get time = UNLIMITED ; // (12 currently) ?
Thanks
I use NCO, but I am sure you can find the appropriate alternative. You can try following:
module load NCO/5.1.3
make time variable dimensionless as you might get error while attempting to concatenate (ncrcat: ERROR no variables fit criteria for processing) --mk_rec_dmn time filename.nc outputfilename.nc (for all required files)
then concatenate using ncrcat -h
In my case, end result was:
dimensions:
latitude = 721 ;
longitude = 1440 ;
time = UNLIMITED ; // (8760 currently)
I have used files after this process in my model and they are running fine.
Hope it helps.
Hi,
Thanks for the answer.
Which command do you use with the following attributes (ncrcat ?)
–mk_rec_dmn time filename.nc outputfilename.nc.
Best regards,
Hello
You can use:
ncks --mk_rec_dmn time in.nc out.nc
makes the dimension “time” the record dimension
(reference: NetCDF operators (NCOs) for file manipulation and simple
calculations Para (9) Fixing the time variable, Part I: Adding a record dimension to a file)
Hi,
Thanks a lot for your help and for the references.
I could update my files.
1 Like