Project

General

Profile

Equalize time steps for several nc files

Added by Linda van Garderen over 6 years ago

Dear all,

I have a wind velocity nc file with f.i. 1200 times teps and I have an SLP nc file with 5100 time steps. Is it possible to select the time steps present in the wind velocity file, in the SLP file? In that way the SLP file and velocity file have exactly the same time steps.

Thank you in advance!

Linda


Replies (13)

RE: Equalize time steps for several nc files - Added by Karin Meier-Fleischer over 6 years ago

Hi Linda,

do the timesteps have the same frequency, e.g. daily values? Then you can easily select the same time range as in your velocity file. But if they have different time intervals, for instance, the first file has daily data and the other file has monthly data, then you have to select the same years and do a monavg and settaxis on the daily values.

Please, be more specific so that we are able to help you better.

-Karin

RE: Equalize time steps for several nc files - Added by Linda van Garderen over 6 years ago

Dear Karin.

I use 3hourly timesteps in both nc files. The wind velocity was the 90 percentile selected with CDO of an original file of exactly the same time steps as the SLP.
Now I would like to find out what is the SLP patterns fit on these 90p wind velocity time steps. I will then overlay the data later in maps.

Great if you could help me out!
Thank you!

Linda

RE: Equalize time steps for several nc files - Added by Ralf Mueller over 6 years ago

Hi Linda!
Could you upload some example data? Is the set of relevant timesteps contiguous? I that case, you only need the number of the first one and its length.

cheers
ralf

RE: Equalize time steps for several nc files - Added by Linda van Garderen over 6 years ago

Dear Ralf,

Unfortunately I cannot upload this data. It is not contiguous. The 90th percentile time steps are randomly divided over the SLP time steps. I need this function to see which time steps are available in the velocity file and select exactly the same ones in the SLP file without trying to use order of steps etc.

I hope you can still help, even though i cannot upload the files.

Thank you for trying to help me!

Linda

RE: Equalize time steps for several nc files - Added by Ralf Mueller over 6 years ago

Ok, then lets try the select operator: it can handle complete timestamps with the date keyword. So at first, you need to turn the list of all timestamps from the velocity file into a comma separated list. this can be done by the following (supposing that the velocity file is called velo.nc

cdo -s showtimestamp velo.nc | perl -pe 's/^\s*//' | tr -s ' ' | tr ' ' ',' 

if you work in a unix-like system, this should work out of the box. To select these steps from your SLP.nc, you can use this command
cdo select,date=$(cdo -s showtimestamp velo.nc | perl -pe 's/^\s*//' | tr -s ' ' | tr ' ' ',') SLP.nc relevantSLP.nc

hth
ralf

RE: Equalize time steps for several nc files - Added by Linda van Garderen over 6 years ago

Dear Ralf,

Thank you for this input.
For my normal nc files this works very well.

Unfortunately for my multiple model file this doesn't work as each time step is originally represented 6 times (6 model runs). So the shape of my velocity file is 1240 timesteps, and relevantSLP is now 6144 timesteps.
I had assumed then when merging files (the 6 separate model runs) with mergetime in CDO, CDO provides a unique time stamp to all the steps (no matter the equal date and time on several steps), but it looks like it didn't do that and every time stamp seems to represent 6 actual steps. Is this true? Because that would be causing the above method not to do what we wanted it to do.

Is there otherwise a way to first create unique timestamps in the full 6 model nc file without loosing date information, before selecting the 90th percentile and then the relevantSLP. Or is there another CDO way out?

Thank you!

Linda

RE: Equalize time steps for several nc files - Added by Ralf Mueller over 6 years ago

Linda van Garderen wrote:

Dear Ralf,

Thank you for this input.
For my normal nc files this works very well.

Unfortunately for my multiple model file this doesn't work as each time step is originally represented 6 times (6 model runs). So the shape of my velocity file is 1240 timesteps, and relevantSLP is now 6144 timesteps.
I had assumed then when merging files (the 6 separate model runs) with mergetime in CDO, CDO provides a unique time stamp to all the steps (no matter the equal date and time on several steps), but it looks like it didn't do that and every time stamp seems to represent 6 actual steps. Is this true? Because that would be causing the above method not to do what we wanted it to do.

yes, CDO does not invent new timestamps. in order to distinguish, you have to keep the models runs separate (Or average them, of it's not that important)

Is there otherwise a way to first create unique timestamps in the full 6 model nc file without loosing date information, before selecting the 90th percentile and then the relevantSLP. Or is there another CDO way out?

Multiple timesteps are no problem for the select operator. It will find all of them. to separate your models, you could shift the time axes of your runs with shifttime or use settaxis instead.

hth
ralf

RE: Equalize time steps for several nc files - Added by Linda van Garderen over 6 years ago

Thank you very much! I will find a way for this last part.

RE: Equalize time steps for several nc files - Added by Ehsan Sharifi about 4 years ago

Hi,
I've just faced with this problem, but with different time format.

I appreciate it if you could please help me in this regard.

The date format of the first file is like this 2007-08-16T05:00:01, 2008-07-15T05:00:01, 2008-03-16T05:00:01 .....

while the second file date format is like : 2008-06-01T00:00:00, 2008-07-01T00:00:00, 2008-08-01T00:00:00 .....

and I would like to select the similar dates based on ONLY years and months of the first file.

I tried showdate but it compares the year-month-day. how can I change the format or ...?

Any help greatly appreciated.

RE: Equalize time steps for several nc files - Added by John Johnson over 3 years ago

Ehsan Sharifi wrote:

Hi,
I've just faced with this problem, but with different time format.

I appreciate it if you could please help me in this regard.

The date format of the first file is like this 2007-08-16T05:00:01, 2008-07-15T05:00:01, 2008-03-16T05:00:01 .....

while the second file date format is like : 2008-06-01T00:00:00, 2008-07-01T00:00:00, 2008-08-01T00:00:00 .....

and I would like to select the similar dates based on ONLY years and months of the first file.

I tried showdate but it compares the year-month-day. how can I change the format or ...?

Any help greatly appreciated.

Has this question been answered ? I am facing the same problem

RE: Equalize time steps for several nc files - Added by Ehsan Sharifi over 3 years ago

Not really from here. I solved the problem by writing some python code. At the moment I don't have that code in hand, but I was something like this:

import xarray as xr 
with xr.open_dataset("yourfile_1.nc") as ds1:
   ds1.coords["time"] = ds1['time'].values.astype('datetime64[M]')

Then open the second dataset and select the same months based on your first or second file:

with xr.open_dataset("yourfile_2.nc") as ds2:
   ds2 =  ds2.where(ds2['time'].isin(ds1['time']),  drop=True)

I hope it helps.

RE: Equalize time steps for several nc files - Added by Ralf Mueller over 3 years ago

John Johnson wrote:

Has this question been answered ? I am facing the same problem

Hi!

I have to admit that I never really understood the issue itself. maybe you can describe it a bit more.

From what I got it seems like you have a bunch of files with different intervals (days and months) and you want to select some of them based on the year, is that correct?

cheers
ralf

RE: Equalize time steps for several nc files - Added by John Johnson over 3 years ago

Thanks Ralf! the xarray did the Job

    (1-13/13)