ensmean: More advanced filename substitution
Added by Oliver Watt-Meyer over 12 years ago
Hi,
I see in the CDO documentation that it is possible to call
cdo ensmean ifile[1-6] ofile
as a shorthand for
cdo ensmean ifile1 ifile2 ifile3 ifile4 ifile5 ifile6 ofile
.
First off, is there a way to do this for a series of files with an increment different from 1 (say, ifile1980 ifile1985 ifile1990)? Essentially, I'm looking for an equivalent to the "-n" loop option of NCO.
Second, is there a simple way to make a filename substitution for a series of files that do not always contain the same number of digits. I.e. is there a shorthand way to call the following:
cdo ensmean ifile1 ifile2 ifile3 ifile4 ifile5 ifile6 ifile7 ifile8 ifile9 ifile10 ofile
Thanks!
Oliver
Replies (3)
RE: ensmean: More advanced filename substitution - Added by Jaison-Thomas Ambadan over 12 years ago
Hi,
is there a way to do this for a series of files with an increment different from 1 (say, ifile1980 ifile1985 ifile1990)?
There is no "increment" - CDO only look for all the files which has the suffix (or file name/extension which contains ...) between 1-6. For example, say you do not have ifile4, "cdo -ensmean ifile[1-6] ofile" calculates the ensemble mean of the rest of the input files. so if you would like to calculate the "ensmean" of ifile1980 ifile1985 ifile1990 - you could try:
cdo -ensmean ifile19[8-9][0-5] ofile
the "[8-9]" look for the appropriate file extension which contains digits 8 to 9; same is the case for [0-5]
Second, is there a simple way to make a filename substitution for a series of files that do not always contain the same number of digits. I.e. is there a shorthand way to call the following:
cdo ensmean ifile1 ifile2 ifile3 ifile4 ifile5 ifile6 ifile7 ifile8 ifile9 ifile10 ofile
The answer is similar BUT it would be better if you could rename your files (may be with linux "rename") such that the file names have equal number of "digits"; ie, renname ifile1 to ifile01, and ifile9 to ifile09, then you can try: cdo -ensmean ifile[0-9][0-9] ofile
or say if you have 48 files (ifile001 .. ifile048): cdo -ensmean ifile0[0-4][0-9] ofile
Hope this helps!
Cheers,
J.
RE: ensmean: More advanced filename substitution - Added by Uwe Schulzweida over 12 years ago
File Name Substitution is a powerful Shell feature and not part of CDO.
Short command for:
cdo ensmean ifile1 ifile2 ifile3 ifile4 ifile5 ifile6 ifile7 ifile8 ifile9 ifile10 ofilecould be:
cdo ensmean ifile* ofileBut this will use all files starting with ifile and the order could be changed. You can check it with the UNIX command ls:
ls ifile* ifile1 ifile10 ifile2 ifile3 ifile4 ifile5 ifile6 ifile7 ifile8 ifile9The order of the input files doesn't matter for ensmean. But it is very important for the CDO operators copy, cat and mergetime. Therefore it is a good praxis to name/rename your files in a way that there are alphanumerically sorted. Here is a table of the basic Pattern-Matching Characters:
? | match any single character |
* | match zero or more characters, including null |
[abc] | match any character or characters between the brackets |
[x?/span>z] | match any character or characters in the range x to z |
[a?/span>ce?/span>g] | match any character or characters in the range a to c, e to g |
[!abc] | match any character or characters not between the brackets |
[!x ?/span>z] | match any character or characters not in the range x to z |
RE: ensmean: More advanced filename substitution - Added by Oliver Watt-Meyer over 12 years ago
The answer is similar BUT it would be better if you could rename your files (may be with linux "rename") such that the file names have equal number of "digits"; ie, renname ifile1 to ifile01, and ifile9 to ifile09, then you can try: cdo -ensmean ifile[0-9][0-9] ofile
I'm working with a fairly large amount of CMIP5-format data, which uses 'r1', 'r2' ... 'r10' in the filename (and directory structure) to indicate different realizations, so I am not keen to start changing filenames. However, I think by using BASH variables I should be able to resolve my issue.
Thanks for the help!