Project

General

Profile

change variable name for 365 nc files!

Added by Yousef Albuhaisi over 4 years ago

Hi,

My questions is how to change a variable name for 365 files (daily soil moisture datasets) at once.

Thank you for helping.

Yousef


Replies (12)

RE: change variable name for 365 nc files! - Added by Ralf Mueller over 4 years ago

ncrename -v <oldVarName>,<newVarName> <yourfile>
is the command to do this without creating a new file. Now all you have to do is write a loop over your 365 files.

hth
ralf

RE: change variable name for 365 nc files! - Added by Yousef Albuhaisi over 4 years ago

oops!, Maybe I didn't explain what I want :)

the loop is my issue, could you please help to do a loop using cdo?

Regards.

Yousef

RE: change variable name for 365 nc files! - Added by Ralf Mueller over 4 years ago

Since I don't know the interpreter you are using, you might check this link https://hyperpolyglot.org/unix-shells#execution-control and select the right one on your own.

hth
ralf

RE: change variable name for 365 nc files! - Added by Yousef Albuhaisi over 4 years ago

I use linux.

if I say that my data files are from 01-01-2015 to 31-12-2015 and file name format is sm_d_01-01-2015.nc for instance, how I can loop through these files?

Thanks in Advance.

RE: change variable name for 365 nc files! - Added by Ralf Mueller over 4 years ago

Yousef Albuhaisi wrote:

I use linux.

good choice, but it doesn't tell you (and me) which shell you are using. I guess it's bash, but please check the output of

echo $SHELL
This will make it clear

if I say that my data files are from 01-01-2015 to 31-12-2015 and file name format is sm_d_01-01-2015.nc for instance, how I can loop through these files?

I think you have to get familiar with wildcards first. wildcards are a syntactic way of your command line interpreter (running in your terminal/konsole/whatever
) to select multiple files. The simplest example might the * symbol:

*.nc
simply selects all nc files in your current directory. Just google
 unix wildcards
in case you need some more info. Just one thing: you can easily check wildcards with the ls command.

hth
ralf

RE: change variable name for 365 nc files! - Added by Yousef Albuhaisi over 4 years ago

Hi,

When I do echo $SHELL, i got this /bin/bash

now if I do the following:

cdo -chname,SM_D_LPRM,vsm *.nc *.nc

is it going to work?

Regards

RE: change variable name for 365 nc files! - Added by Ralf Mueller over 4 years ago

Yousef Albuhaisi wrote:

Hi,

When I do echo $SHELL, i got this /bin/bash

your command line interpreter (shell) is bash then

now if I do the following:

cdo -chname,SM_D_LPRM,vsm *.nc *.nc

is it going to work?

nope, inputs and output have to given explicitly - you have to write a loop. Try this for testing what is captured by your wildcard:

 for file in *.nc; do ls $f; done

CDO will always create a new file whereas NCOs (like ncrename) can write to the input directly and do not need to create a new file. So depending on what you want, you have to choose the tools.

then just add your command to the loop with a new output name like this:

 for file in *.nc; do ls $f; cdo -chanem,SM_D_LPRM,vsm $f changedName_$f ; done
or with
 for file in *.nc; do ls $f; ncrename -v SM_D_LPRM,vsm $f ; done

warning: ncrename will change your input - so if you do something wrong your original files might be corrupted afterwards!

I'd recommend CDO because of that. It leaves input untouched and you can rerun stuff for testing as often as you like

Regards

RE: change variable name for 365 nc files! - Added by Yousef Albuhaisi over 4 years ago

when I do

for file in *.nc; do ls $f; cdo -chanem,SM_D_LPRM,vsm $f new_$f ; done

it says:

cdo chname (Abort): Too few streams specified! Operator needs 1 input and 1 output streams.

RE: change variable name for 365 nc files! - Added by Ralf Mueller over 4 years ago

as sorry - I messed up the variable name, use this instead

for file in *.nc; do ls $file; cdo -chanem,SM_D_LPRM,vsm $file new_$file ; done

RE: change variable name for 365 nc files! - Added by Krishna Shiva over 4 years ago

Thanks For Sharing All This Information. I Have Solved My Problem Of Changing Variable Name of 365 nc files. Thanks Again https://biggbossvoting.net/

    (1-12/12)