Project

General

Profile

Selecting same dates as another file

Added by Rachel Taylor about 1 year ago

Hi everyone,

I have one netcdf file with the dates/timesteps that I need to analyse (not every day), and I want to select the same dates from another file which has values for every day. Is there a way to quickly select the same dates that are in my first file, from the second?

Thanks in advance!
-Rachel


Replies (11)

RE: Selecting same dates as another file - Added by Ralf Mueller about 1 year ago

hi Rachel!

Please have a look into the select operator It accepts multiple input files and use the same selection on all of them.

example call:

cdo -select,date=2003-12-03T14:30:00 <input files> <output file> 

cheers
ralf

RE: Selecting same dates as another file - Added by Rachel Taylor about 1 year ago

Hi Ralf,

Thanks very much for your reply and help. I think I may not have explained my problem very clearly though. I am not trying to make the same selection on multiple input files, but to take the time axis from one file, and use that to select identical times from the second file. Is that possible, do you know?

Thanks again!
-Rachel

RE: Selecting same dates as another file - Added by Estanislao Gavilan about 1 year ago

Hi Rachel,

I think I have an idea that what you want. It should be something like this, but I am having an error saying "No such file or directory". I am not sure how to fix that part, maybe Ralph can help with that.

VARIABLE=$(cdo showtimestamp firstfile.nc)
cdo -select,date=$VARIABLE secondfile.nc output.nc

Kind regards,

Estanislao

RE: Selecting same dates as another file - Added by Rachel Taylor about 1 year ago

Hi Estanislao,

That sounds genius! I played around with it a bit, and unfortunately I think there is something wrong with the formatting with passing the timestamp directly to -select,date because when it says "No such file or directory", it assumes that the date is the input file as there is a space before it where there should be (I think) a comma. That's alright, I think I've figured out a different approach to the problem which should work. Thanks both anyway for your help - hugely appreciated!

Many thanks,
-Rachel

RE: Selecting same dates as another file - Added by Estanislao Gavilan about 1 year ago

Hi Rachel,

as you well guessed there should be a comma after each date. If you find a way around please post it. Other people will benefit from your script.

Kind regards,

Estanislao

RE: Selecting same dates as another file - Added by Ralf Mueller about 1 year ago

replace space with comma:

Lets start with a list of timesteps:

% cdo -O -f nc -s -showtimestamp -settaxis,2001-01-01,12:00:00,12hours -for,1,10
  2001-01-01T12:00:00  2001-01-02T00:00:00  2001-01-02T12:00:00  2001-01-03T00:00:00  2001-01-03T12:00:00  2001-01-04T00:00:00  2001-01-04T12:00:00  2001-01-05T00:00:00  2001-01-05T12:00:00  2001-01-06T00:00:00

this can be piped through sed or perl to do text replacement. I will use sed to remove the leading spaces and perl to replace the two spaces between timestamps with a single comma:

% cdo -s -showtimestamp -settaxis,2001-01-01,12:00:00,12hours -for,1,10 | sed 's/^[ \t]*//' | perl -pe 's/  /,/g' 
2001-01-01T12:00:00,2001-01-02T00:00:00,2001-01-02T12:00:00,2001-01-03T00:00:00,2001-01-03T12:00:00,2001-01-04T00:00:00,2001-01-04T12:00:00,2001-01-05T00:00:00,2001-01-05T12:00:00,2001-01-06T00:00:00

So with a variable like

TIMESTEPS=$(cdo -s -showtimestamp -settaxis,2001-01-01,12:00:00,12hours -for,1,10 | sed 's/^[ \t]*//' | perl -pe 's/  /,/g')
you can use it for the select operators like shown by Estanislao above

in-fact you can call this in a single line without the variable if you want:

% cdo -s -showyear -select,date=$(cdo -s -showtimestamp -settaxis,2001-01-01,12:00:00,12hours -for,1,10 | sed 's/^[ \t]*//' | perl -pe 's/  /,/g' ) -settaxis,2001-01-01,12:00:00,12hours -for,1,10 
 2001

cheers
ralf

RE: Selecting same dates as another file - Added by Rachel Taylor about 1 year ago

Wow, Ralf, that's incredible! Thanks for your help - amazing!

RE: Selecting same dates as another file - Added by Estanislao Gavilan about 1 year ago

I am always learning something new thanks to Ralph. Rachel, bear in mind that 'for' is selecting the first 10 timesteps. You will have to change that

RE: Selecting same dates as another file - Added by Ralf Mueller about 1 year ago

thx, Rachel. My goto docu for shell is the Advanced Bash Scripting Guide Although it's kind of old, it's still valid. Bash is kind of old, too.

If there were no leading spaces and only a single space between the timestamps, the call would be a lot simpler:

 cdo -s -showtimestamp inputfile.nc | tr ' ' ','

I am sure there is also a solution with sed, only. But I never got used to sed very much, that's why I use perl most of the time.

RE: Selecting same dates as another file - Added by Ralf Mueller about 1 year ago

Estanislao Gavilan wrote in RE: Selecting same dates as another file:

I am always learning something new thanks to Ralph. Rachel, bear in mind that 'for' is selecting the first 10 timesteps. You will have to change that

ah, yes! I totally forgot to mention: this constuct -settaxis,2001-01-01,12:00:00,12hours -for,1,10 emulates a time series - you must replace it with your input files.

thx Estanislao

RE: Selecting same dates as another file - Added by Rachel Taylor about 1 year ago

Thanks both! Yes, I worked out that I would need to replace the time axis with my own files. Also that the dates matched, but not the times, so if anyone else has a similar issue -showdate worked when I couldn't use -showtimestamp

Thanks again Ralf and Estanislao! Very informative!

    (1-11/11)