Next: , Previous: RAM variables, Up: ncap2 netCDF Arithmetic Processor


4.1.13 Where statement

A where() combines the definition and application of a mask all in one go and can lead to succinct code. The full syntax of a where() statement is as follows:

     // Single assign (nb the else block is optional)
     where (mask)
        var1=expr1;
     elsewhere
        var1=expr2;
     
     
     // Multiple assigns
     where( mask) {
         var1=expr1;
         var2=expr2;
         ...
         } elsewhere {
         var1=expr3
         var2=expr4
         var3=expr5;
         ...
         }
     

example:

Consider the variables:
float lon_2D_rct(lat,lon);
float var_msk(lat,lon);
Suppose we want to multiply by two the elements for which var_msk is equal to 1;

     where(var_msk==1)
       lon_2D_rct=2*lon_2D_rct;

Another example
Suppose we have the variable
int RDM(time);
And we want to set the values less than 8 or greater than 80 to 0.

     where(RDM <8 || RDM >80)
       RDM=0;

A more complex example.
Consider the situation where we have irregularly gridded data, described using rank 2 variables:

double lat(south_north,east_west)
double lon(south_north,east_west)
double temperature(south_north,east west)
To find the average temperature in a region [lat_min,lat_max] and [lon_min,lon_max]:

     
     temperature_msk[$south_north,$east_west]=0.0;
     
     where(lat >= lat_min && lat <= lat_max) && (lon >= lon_min && > lon <= lon_max)
       temperature_msk=temperature;
     elsewhere
       temperature_msk=temperature@_FillValue;
     
     temp_avg=temperature_msk.avg();
     temp_max=temperature.max();