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 (the 'elsewhere' 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; ... }
where(foo=foo.get_missing()) foo=1;
will not work as expected.
Example:
Consider the variables float lon_2D_rct(lat,lon);
and
float var_msk(lat,lon);
.
Suppose we wish to multiply by two the elements for which var_msk
equals 1:
where(var_msk==1) lon_2D_rct=2*lon_2D_rct;
Suppose that we have the variable int RDM(time)
and that we want
to set its values less than 8 or greater than 80 to 0:
where(RDM < 8 || RDM > 80) RDM=0;
Consider irregularly gridded data, described using rank 2 coordinates:
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 bounded by
[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();