Project

General

Profile

Set variabele in lon,lat range to a set of random values between min and max

Added by Leonore Jungandreas over 4 years ago

Hi,

I have a variable (within a nc-file) where I want to change the values within a specific lon,lat range.
with:

ncap2 -v -O -s var(tile, lat_min:lat_max ,lon_min:lon_max)=0.3

I am able to change the values of the desired range to one fixed value.

My question is, is there a possibility not to use one fixed value (here 0.3) but a range (for example between 0.1 to 0.4) and than give each lon-lat-gridpoint a random value? It is basically to create some heterogenity.

Cheers,
Leo


Replies (5)

RE: Set variabele in lon,lat range to a set of random values between min and max - Added by Karin Meier-Fleischer over 4 years ago

Hi Leo,

you can use the expr operator with the rand function.

For example

cdo -expr,'var = 0.4 * rand(var)' infile outfile

-Karin

RE: Set variabele in lon,lat range to a set of random values between min and max - Added by Leonore Jungandreas over 4 years ago

Hi Karin,

thanks for the quick reply.
Is there also the option to include a selection on which lon,lat range I want to do that?

If I do something like:

cdo expr,'var(12,66:116,109:215)=0.4*rand(var)' infile outfile

It gives me an error.

Cheers, Leo

RE: Set variabele in lon,lat range to a set of random values between min and max - Added by Karin Meier-Fleischer over 4 years ago

You can't use array indexing to select a lonlat region! You have to use the sellonlatbox operator (see FAQ).

For example

Select the subregion and compute random data:

cdo -expr,'var=0.4*rand(var)' -sellonlatbox,0,10,0,10 infile.nc rand_10x10.nc

Expand the grid back to the infile grid:

cdo -remapbil,infile.nc rand_10x10.nc rand_infile_grid.nc

Use values from rand_infile_grid.nc if > 0 otherwise infile values

cdo ifthenelse -setmisstoc,0 rand_infile_grid.nc rand_infile_grid.nc infile.nc outfile_rand.nc

RE: Set variabele in lon,lat range to a set of random values between min and max - Added by Leonore Jungandreas over 4 years ago

Hey Karin,

thanks a lot, that worked perfectly.

Cheers, Leo

RE: Set variabele in lon,lat range to a set of random values between min and max - Added by Leonore Jungandreas over 4 years ago

Hey Karin,

I just stumbled over another question...
My var is three dimensional (but not time) and I want to preform the above operation only for one index of that 3rd dimension.

Is there a possibility to select such indexes of dimensions?

Leo

    (1-5/5)