# Re: [ferret_users] shifting a variable on a modular axis

```Hi Mark,
Yes, there is an alternate way to achieve this. But before
seeing it, let me explain why this problem occurs :

Rule : if you define a variable in ferret, whenever you access this variable
in an "action" (Ch3 Sec2.) command, ferret will go back and use this
"definition" to get its values.

So what ?? ......Let us see an example :

define axis/x=-10:10:1/MODULO xax_mod
let var     = x[gx=xax_mod]
let var_shf = var[x=@SHF:10]
list var, var_shf

We expected -10 as var_shf[i=12] but we got 11 !! From the definition of
var and xax_mod, it follows that var_shf went straight ahead to i=22 of
the basic expression for var, instead of looping back to the i=1 value.

Is there any way to tell Ferret "please don't evaluate the variable
definition anymore" ? ......NO (if i am correct), because Ferret will
always go back and look for the variable definition (even though it is
deeply nested), irrespective of the steps you taken in between.

1. Since we deal with an array, we cannot force immediate evaluation
and hence defining an intermediate variable is not effective.
2. Using the "var" in some action commands before applying @SHF also does
not help. Even commands like "load", "set grid" are not effective.
3. Specifying modulo length also didn't work.

Ok. What happens when you use the variable from a file ? Ferret will not
have access to the "variable definition" --> it won't go back to evaluate any
more expressions. Hence you will get expected results with @SHF regridding.

So the the solution i could figure out is the usage of 2 shifting operations
(one forward and one backward) on the variable and then merging these shifted
expressions to get the desired result. Let us see this method for the above
example (here we don't need a modulo axis) :

define axis/x=-10:10:1 xax
let var = x[gx=xax]
let shfpt = 10                          ! +ve SHF --> either "left" or "up"
let xsz   = `var,r=iend`                ! number of points along X
let var_shf_p = var[x=@SHF:`shfpt`]     ! shift backward (left/up)
let var_shf_n = var[x=@SHF:-`xsz-shfpt`]! shift forward  (right/down)
let var_shf_1 = XCAT(var_shf_p[i=1:`xsz-shfpt`],var_shf_n[i=`xsz-shfpt+1`:`xsz`])
let var_shf   = var_shf_1[gx=var@ASN]   ! XCAT will change X-axis,
!    put back original X-axis
list var, var_shf

Why don't use IF/THEN/ELSE instead of XCAT ? Because XCAT is better to
deal with the possible missing values in the "real world" variables. Don't
be frightened by seeing these many lines....you just need to tune "shfpt"
to get proper shifting (if you apply this example for some actual data).
Also see the a modified version of your example below.

NOTE : In my experience, writing temporary file/s may be the one and only
one possible solution sometimes ....

Hope this mail was usefull....

Regards,

Jaison

!------------example starts here--------------------------
\ cancel mode verify

define axis/x=-18:18:0.1 x_mod_axis
define axis/y=-5:5:0.1   y_axis
define grid/x=x_mod_axis/y=y_axis my_grid
set grid my_grid

let my_var = exp((-1) * y^2) * exp((-1) * (x * 0.3)^2)

pause

let shfpt = 180
let xsz   = `my_var,r=iend`

let var_shf_p = my_var[x=@SHF:`shfpt`]
let var_shf_n = my_var[x=@SHF:-`xsz-shfpt`]
let var_shf_1 = XCAT(var_shf_p[i=1:`xsz-shfpt`],var_shf_n[i=`xsz-shfpt+1`:`xsz`])
let var_shf   = var_shf_1[gx=my_var@ASN]

!------------example ends   here--------------------------

On Wed, 7 Feb 2007, Mark Williams wrote:

> Hello all
>
> I have a question relating to the shifting a variable on a modular axis and requiring it to wrap around in a modular fashion.
>
> Suppose I define a variable and set a grid with a modular axis and apply the @shf transform as follows:
>
> define axis/modulo/x=-18:18:0.1 x_mod_axis
> define axis/y=-5:5:0.1 y_axis
> define grid/x=x_mod_axis/y=y_axis my_grid
> set grid my_grid
> let my_var = exp((-1) * y^2) * exp((-1) * (x * 0.3)^2)
>
> I see a bulls eye as to be expected.
> then:
>
>
> results in the bullseye being shifted to the left boundary, but no "wrapping" around onto the right hand side.
>
> If I then go down the path a making a temporary data set and then plotting I get the result I want as follows:
>
> let my_var_sv =  my_var[x=-18:18]
> save/clob/file=mvs.nc my_var_sv
> can var my_var_sv
> use mvs.nc
>
> bulls eye in the middle
>
>
> Bullseye wrapped around the boundaries as I want.
>
> When applying the @shf transformation to any data_set which has a modular axis then the data are shifted as I would expect, but I can't get it to do the same using user defined variables.  I feel that I have overlooked something.  Is there a way without using a temporary data_sets (which is not the ferret way!!)
>
> I am now using ferret 6.0 on SUSE Linux 10.1
>
> Thanks
>
> Mark
>
>
>
>

```