[2] | 1 | ;+ |
---|
[232] | 2 | ; |
---|
[163] | 3 | ; @file_comments |
---|
[314] | 4 | ; compute the gradient of a variable located on Arakawa C-grid. |
---|
[2] | 5 | ; |
---|
[163] | 6 | ; @categories |
---|
[167] | 7 | ; Calculation |
---|
[2] | 8 | ; |
---|
[163] | 9 | ; @param FIELD |
---|
[238] | 10 | ; The field for which we want to compute the gradient. A 2D (xy), |
---|
[242] | 11 | ; 3D (xyz or yt) or 4D (xyzt) array or a structure readable by |
---|
[314] | 12 | ; <pro>litchamp</pro> and containing a 2D (xy), 3D (xyz or yt) or 4D (xyzt) array. |
---|
[242] | 13 | ; Note that the dimension of the array must suit the domain dimension. |
---|
[2] | 14 | ; |
---|
[167] | 15 | ; @param DIREC {type=scalar string} |
---|
| 16 | ; the gradient direction: 'x', 'y', 'z' |
---|
[2] | 17 | ; |
---|
[314] | 18 | ; @keyword MILLION {default=0}{type=scalar: 0 or 1} |
---|
| 19 | ; Activate to multiply returned array by 1.e6 |
---|
| 20 | ; |
---|
| 21 | ; @keyword _EXTRA |
---|
| 22 | ; Used to declare that this routine accepts inherited keywords |
---|
| 23 | ; |
---|
[238] | 24 | ; @returns |
---|
[314] | 25 | ; the gradient of the input data with the same size 2D, 3D or 4D |
---|
| 26 | ; array, located on the appropriate grid (see restrictions) |
---|
[2] | 27 | ; |
---|
[163] | 28 | ; @uses |
---|
[238] | 29 | ; cm_4cal |
---|
| 30 | ; cm_4data |
---|
[314] | 31 | ; cm_4mesh |
---|
[2] | 32 | ; |
---|
[163] | 33 | ; @restrictions |
---|
[226] | 34 | ; - Works only for Arakawa C-grid. |
---|
[167] | 35 | ; - When computing the gradient, the result is not on the same grid point |
---|
| 36 | ; than the input data. In consequence, we update, vargrid and the grid position |
---|
| 37 | ; parameters (firstx[tuvf], lastx[tuvf], nx[tuvf], firsty[tuvf], lasty[tuvf], |
---|
| 38 | ; ny[tuvf], firstz[tw], lastz[tw], nz[tw]). |
---|
[226] | 39 | ; - points that cannot be computed (domain boundaries, coastline) are set to NaN |
---|
[167] | 40 | ; - the common variable jpt is used to differ xyz (jpt=1) and xyt (jpt\=1) cases. |
---|
[142] | 41 | ; |
---|
[163] | 42 | ; @examples |
---|
[168] | 43 | ; IDL> \@tst_initorca2 |
---|
[167] | 44 | ; IDL> plt, grad({arr:gphit,g:'T'}, 'x') |
---|
| 45 | ; IDL> plt, grad({arr:gphit,g:'T'}, 'y') |
---|
[163] | 46 | ; |
---|
| 47 | ; @history |
---|
| 48 | ; Sebastien Masson (smasson\@lodyc.jussieu.fr) |
---|
| 49 | ; |
---|
| 50 | ; @version |
---|
| 51 | ; $Id$ |
---|
| 52 | ; |
---|
[2] | 53 | ;- |
---|
[314] | 54 | FUNCTION grad, field, direc, MILLION = million, _EXTRA = ex |
---|
[114] | 55 | ; |
---|
| 56 | compile_opt idl2, strictarrsubs |
---|
| 57 | ; |
---|
[167] | 58 | @cm_4cal ; for jpt |
---|
| 59 | @cm_4data ; for varname, vargrid, vardate, varunit, valmask |
---|
[226] | 60 | @cm_4mesh |
---|
[314] | 61 | ; |
---|
| 62 | time1 = systime(1) ; for key_performance |
---|
[2] | 63 | ;------------------------------------------------------------ |
---|
[25] | 64 | ; |
---|
[167] | 65 | IF finite(glamu[0])*finite(gphiu[0])*finite(glamv[0])*finite(gphiv[0]) EQ 0 THEN $ |
---|
[25] | 66 | return, report(['This version of grad is based on Arakawa C-grid.' $ |
---|
| 67 | , 'U and V grids must therefore be defined']) |
---|
| 68 | ; |
---|
[167] | 69 | res = litchamp(field) |
---|
| 70 | szres = size(res) |
---|
| 71 | grille, mask, glam, gphi, gdep, nx, ny, nz $ |
---|
[226] | 72 | , firstx, firsty, firstz, lastx, lasty, lastz |
---|
[167] | 73 | ; |
---|
| 74 | if n_elements(valmask) EQ 0 then valmask = 1.e20 |
---|
| 75 | varname = 'grad of '+varname |
---|
[314] | 76 | IF keyword_set(million) THEN varunit = '1.e6*'+varunit+'/m' ELSE varunit = varunit+'/m' |
---|
[167] | 77 | case strupcase(vargrid) of |
---|
| 78 | 'T':BEGIN |
---|
| 79 | case direc of |
---|
[226] | 80 | 'x':BEGIN |
---|
[167] | 81 | divi = e1u[firstx:lastx, firsty:lasty] |
---|
| 82 | newmask = (umask())[firstx:lastx, firsty:lasty, firstz:lastz] |
---|
| 83 | vargrid = 'U' |
---|
[226] | 84 | firstxu = firstxt & lastxu = lastxt & nxu = nxt |
---|
[167] | 85 | firstyu = firstyt & lastyu = lastyt & nyu = nyt |
---|
| 86 | END |
---|
| 87 | 'y':BEGIN |
---|
| 88 | divi = e2v[firstx:lastx, firsty:lasty] |
---|
| 89 | newmask = (vmask())[firstx:lastx, firsty:lasty, firstz:lastz] |
---|
| 90 | vargrid = 'V' |
---|
[226] | 91 | firstxv = firstxt & lastxv = lastxt & nxv = nxt |
---|
[167] | 92 | firstyv = firstyt & lastyv = lastyt & nyv = nyt |
---|
| 93 | END |
---|
| 94 | 'z':BEGIN |
---|
| 95 | divi = e3w[firstz:lastz] |
---|
| 96 | newmask = mask |
---|
| 97 | vargrid = 'W' |
---|
[226] | 98 | firstzw = firstzt & lastzw = lastzt & nzw = nzt |
---|
[167] | 99 | END |
---|
| 100 | ELSE:return, report('Bad definition of direction argument') |
---|
| 101 | ENDCASE |
---|
| 102 | END |
---|
| 103 | 'W':BEGIN |
---|
| 104 | case direc of |
---|
[226] | 105 | 'x':BEGIN |
---|
[167] | 106 | divi = e1u[firstx:lastx, firsty:lasty] |
---|
| 107 | newmask = (umask())[firstx:lastx, firsty:lasty, firstz:lastz] |
---|
| 108 | vargrid = 'U' |
---|
[226] | 109 | firstxu = firstxt & lastxu = lastxt & nxu = nxt |
---|
[167] | 110 | firstyu = firstyt & lastyu = lastyt & nyu = nyt |
---|
| 111 | END |
---|
| 112 | 'y':BEGIN |
---|
| 113 | divi = e2v[firstx:lastx, firsty:lasty] |
---|
| 114 | newmask = (vmask())[firstx:lastx, firsty:lasty, firstz:lastz] |
---|
| 115 | vargrid = 'V' |
---|
[226] | 116 | firstxv = firstxt & lastxv = lastxt & nxv = nxt |
---|
[167] | 117 | firstyv = firstyt & lastyv = lastyt & nyv = nyt |
---|
| 118 | END |
---|
| 119 | 'z':BEGIN |
---|
| 120 | divi = e3t[firstz:lastz] |
---|
| 121 | newmask = mask |
---|
| 122 | vargrid = 'T' |
---|
[226] | 123 | firstzt = firstzw & lastzt = lastzw & nzt = nzw |
---|
[167] | 124 | END |
---|
| 125 | ELSE:return, report('Bad definition of direction argument') |
---|
| 126 | endcase |
---|
| 127 | END |
---|
| 128 | 'U':BEGIN |
---|
| 129 | case direc of |
---|
| 130 | 'x':BEGIN |
---|
| 131 | divi = (shift(e1t, -1, 0))[firstx:lastx, firsty:lasty] |
---|
| 132 | newmask = tmask[firstx:lastx, firsty:lasty, firstz:lastz] |
---|
| 133 | vargrid = 'T' |
---|
[226] | 134 | firstxt = firstxu & lastxt = lastxu & nxt = nxu |
---|
[167] | 135 | firstyt = firstyu & lastyt = lastyu & nyt = nyu |
---|
| 136 | END |
---|
| 137 | 'y':BEGIN |
---|
| 138 | divi = e2f[firstx:lastx, firsty:lasty] |
---|
| 139 | newmask = (fmask())[firstx:lastx, firsty:lasty, firstz:lastz] |
---|
| 140 | vargrid = 'F' |
---|
[226] | 141 | firstxf = firstxu & lastxf = lastxu & nxf = nxu |
---|
[167] | 142 | firstyf = firstyu & lastyf = lastyu & nyf = nyu |
---|
| 143 | END |
---|
| 144 | 'z':BEGIN |
---|
| 145 | divi = e3w[firstz:lastz] |
---|
| 146 | newmask = mask |
---|
| 147 | vargrid = 'W' |
---|
[226] | 148 | firstzw = firstzt & lastzw = lastzt & nzw = nzt |
---|
[167] | 149 | END |
---|
| 150 | ELSE:return, report('Bad definition of direction argument') |
---|
| 151 | endcase |
---|
| 152 | END |
---|
| 153 | 'V':BEGIN |
---|
| 154 | case direc of |
---|
| 155 | 'x':BEGIN |
---|
| 156 | divi = e1f[firstx:lastx, firsty:lasty] |
---|
| 157 | newmask = (fmask())[firstx:lastx, firsty:lasty, firstz:lastz] |
---|
| 158 | vargrid = 'F' |
---|
| 159 | firstxf = firstxv & lastxf = lastxv & nxf = nxv |
---|
| 160 | firstyf = firstyv & lastyf = lastyv & nyf = nyv |
---|
| 161 | END |
---|
| 162 | 'y':BEGIN |
---|
| 163 | divi = (shift(e2t, 0, -1))[firstx:lastx, firsty:lasty] |
---|
| 164 | newmask = tmask[firstx:lastx, firsty:lasty, firstz:lastz] |
---|
| 165 | vargrid = 'T' |
---|
| 166 | firstxt = firstxv & lastxt = lastxv & nxt = nxv |
---|
| 167 | firstyt = firstyv & lastyt = lastyv & nyt = nyv |
---|
| 168 | END |
---|
| 169 | 'z':BEGIN |
---|
| 170 | divi = e3w[firstz:lastz] |
---|
| 171 | newmask = mask |
---|
| 172 | vargrid = 'W' |
---|
[226] | 173 | firstzw = firstzt & lastzw = lastzt & nzw = nzt |
---|
[167] | 174 | END |
---|
| 175 | ELSE:return, report('Bad definition of direction argument') |
---|
| 176 | endcase |
---|
| 177 | END |
---|
| 178 | 'F':BEGIN |
---|
[2] | 179 | ; case direc of |
---|
[25] | 180 | ; 'x':divi = (shift(e1v, -1, 0))[firstx:lastx, firsty:lasty] |
---|
| 181 | ; 'y':divi = (shift(e2u, 0, -1))[firstx:lastx, firsty:lasty] |
---|
| 182 | ; 'z':divi = e3w[firstz:lastz] |
---|
[2] | 183 | ; ELSE:return, report('Bad definition of direction argument') |
---|
| 184 | ; endcase |
---|
[167] | 185 | return, report('F grid: case not coded, please contact SAXO team...') |
---|
| 186 | END |
---|
| 187 | ELSE:return, report('Bad definition of vargrid') |
---|
| 188 | ENDCASE |
---|
[314] | 189 | IF keyword_set(million) THEN divi = temporary(divi)*1.e-6 |
---|
[167] | 190 | res = fitintobox(temporary(res)) |
---|
| 191 | IF n_elements(res) EQ 1 AND res[0] EQ -1 THEN return, res |
---|
| 192 | case 1 of |
---|
[2] | 193 | ;---------------------------------------------------------------------------- |
---|
| 194 | ;xy |
---|
| 195 | ;---------------------------------------------------------------------------- |
---|
[167] | 196 | szres[0] EQ 2:BEGIN |
---|
| 197 | land = where((temporary(mask))[*, *, firstz] EQ 0) |
---|
| 198 | if land[0] NE -1 then res[temporary(land)] = !values.f_nan |
---|
| 199 | case direc of |
---|
[226] | 200 | 'x':BEGIN |
---|
[167] | 201 | res = (shift(res, -1, 0)-res)/temporary(divi) |
---|
| 202 | if key_periodic EQ 0 OR nx NE jpi THEN res[nx-1, *] = !values.f_nan |
---|
| 203 | if vargrid EQ 'T' OR vargrid EQ 'V' then res = shift(temporary(res), 1, 0) |
---|
| 204 | END |
---|
[226] | 205 | 'y':BEGIN |
---|
[167] | 206 | res = (shift(res, 0, -1)-res)/temporary(divi) |
---|
| 207 | res[*, ny-1] = !values.f_nan |
---|
[226] | 208 | if vargrid EQ 'T' OR vargrid EQ 'U' then res = shift(temporary(res), 0, 1) |
---|
[167] | 209 | END |
---|
| 210 | ELSE:return, report('Bad definition of direction argument for the type of array') |
---|
| 211 | ENDCASE |
---|
| 212 | land = where((temporary(newmask))[*, *, firstz] EQ 0) |
---|
| 213 | if land[0] NE -1 then res[temporary(land)] = valmask |
---|
| 214 | END |
---|
[2] | 215 | ;---------------------------------------------------------------------------- |
---|
| 216 | ;xyt |
---|
| 217 | ;---------------------------------------------------------------------------- |
---|
[167] | 218 | szres[0] EQ 3 AND jpt NE 1:BEGIN |
---|
| 219 | land = where((temporary(mask))[*, *, firstz] EQ 0, cnt) |
---|
| 220 | if land[0] NE -1 then BEGIN |
---|
| 221 | land = (temporary(land))#replicate(1L, jpt) + replicate(1L, cnt)#(nx*ny*lindgen(jpt)) |
---|
| 222 | res[temporary(land)] = !values.f_nan |
---|
| 223 | ENDIF |
---|
| 224 | divi = (temporary(divi))[*]#replicate(1., jpt) |
---|
| 225 | case direc of |
---|
[226] | 226 | 'x':BEGIN |
---|
[167] | 227 | res = (shift(res, -1, 0, 0)-res)/temporary(divi) |
---|
| 228 | if key_periodic EQ 0 OR nx NE jpi THEN res[nx-1, *, *] = !values.f_nan |
---|
| 229 | if vargrid EQ 'T' OR vargrid EQ 'V' then res = shift(temporary(res), 1, 0, 0) |
---|
| 230 | END |
---|
[226] | 231 | 'y':BEGIN |
---|
[167] | 232 | res = (shift(res, 0, -1, 0)-res)/temporary(divi) |
---|
| 233 | res[*, ny-1, *] = !values.f_nan |
---|
[226] | 234 | if vargrid EQ 'T' OR vargrid EQ 'U' then res = shift(temporary(res), 0, 1, 0) |
---|
[167] | 235 | END |
---|
| 236 | ELSE:return, report('Bad definition of direction argument for the type of array') |
---|
| 237 | ENDCASE |
---|
| 238 | land = where((temporary(newmask))[*, *, firstz] EQ 0, cnt) |
---|
| 239 | if land[0] NE -1 then BEGIN |
---|
| 240 | land = (temporary(land))#replicate(1L, jpt) + replicate(1L, cnt)#(nx*ny*lindgen(jpt)) |
---|
| 241 | res[temporary(land)] = valmask |
---|
| 242 | ENDIF |
---|
| 243 | END |
---|
[2] | 244 | ;---------------------------------------------------------------------------- |
---|
| 245 | ;xyz |
---|
| 246 | ;---------------------------------------------------------------------------- |
---|
[167] | 247 | szres[0] EQ 3 AND jpt EQ 1:BEGIN |
---|
| 248 | land = where(mask EQ 0) |
---|
| 249 | if land[0] NE -1 then res[temporary(land)] = !values.f_nan |
---|
| 250 | case direc OF |
---|
[226] | 251 | 'x':BEGIN |
---|
[167] | 252 | divi = (temporary(divi))[*]#replicate(1., nz) |
---|
| 253 | res = (shift(res, -1, 0, 0)-res)/temporary(divi) |
---|
| 254 | if key_periodic EQ 0 OR nx NE jpi THEN res[nx-1, *, *] = !values.f_nan |
---|
| 255 | if vargrid EQ 'T' OR vargrid EQ 'V' then res = shift(temporary(res), 1, 0, 0) |
---|
| 256 | END |
---|
[226] | 257 | 'y':BEGIN |
---|
[167] | 258 | divi = (temporary(divi))[*]#replicate(1., nz) |
---|
| 259 | res = (shift(res, 0, -1, 0)-res)/temporary(divi) |
---|
| 260 | res[*, ny-1, *] = !values.f_nan |
---|
[226] | 261 | if vargrid EQ 'T' OR vargrid EQ 'U' then res = shift(temporary(res), 0, 1, 0) |
---|
[167] | 262 | END |
---|
| 263 | 'z':BEGIN |
---|
| 264 | divi = replicate(1., nx*ny)#(temporary(divi))[*] |
---|
| 265 | if nx EQ 1 OR ny EQ 1 then res = reform(res, nx, ny, nz, /overwrite) |
---|
[226] | 266 | if vargrid EQ 'W' THEN BEGIN |
---|
[167] | 267 | res = (shift(res, 0, 0, 1)-res)/temporary(divi) |
---|
| 268 | res[*, *, 0] = !values.f_nan |
---|
| 269 | ENDIF ELSE BEGIN |
---|
| 270 | res = (res-shift(res, 0, 0, -1))/temporary(divi) |
---|
| 271 | res[*, *, nz-1] = !values.f_nan |
---|
| 272 | ENDELSE |
---|
| 273 | END |
---|
| 274 | ENDCASE |
---|
| 275 | land = where(temporary(newmask) EQ 0) |
---|
| 276 | if land[0] NE -1 then res[temporary(land)] = valmask |
---|
| 277 | END |
---|
[2] | 278 | ;---------------------------------------------------------------------------- |
---|
| 279 | ;xyzt |
---|
| 280 | ;---------------------------------------------------------------------------- |
---|
[167] | 281 | szres[0] EQ 4:BEGIN |
---|
| 282 | land = where(temporary(mask) EQ 0, cnt) |
---|
| 283 | if land[0] NE -1 then BEGIN |
---|
| 284 | land = (temporary(land))#replicate(1L, jpt) + replicate(1L, cnt)#(nx*ny*nz*lindgen(jpt)) |
---|
| 285 | res[temporary(land)] = !values.f_nan |
---|
| 286 | ENDIF |
---|
| 287 | case direc OF |
---|
[226] | 288 | 'x':BEGIN |
---|
[167] | 289 | divi = (temporary(divi))[*]#replicate(1., nz*jpt) |
---|
| 290 | res = (shift(res, -1, 0, 0, 0)-res)/temporary(divi) |
---|
| 291 | if key_periodic EQ 0 OR nx NE jpi THEN res[nx-1, *, *, *] = !values.f_nan |
---|
| 292 | if vargrid EQ 'T' OR vargrid EQ 'V' then res = shift(temporary(res), 1, 0, 0, 0) |
---|
| 293 | END |
---|
[226] | 294 | 'y':BEGIN |
---|
[167] | 295 | divi = (temporary(divi))[*]#replicate(1., nz*jpt) |
---|
| 296 | res = (shift(res, 0, -1, 0, 0)-res)/temporary(divi) |
---|
| 297 | res[*, ny-1, *, *] = !values.f_nan |
---|
[226] | 298 | if vargrid EQ 'T' OR vargrid EQ 'U' then res = shift(temporary(res), 0, 1, 0, 0) |
---|
[167] | 299 | END |
---|
| 300 | 'z':BEGIN |
---|
| 301 | divi = replicate(1., nx*ny)#(temporary(divi))[*] |
---|
| 302 | divi = (temporary(divi))[*]#replicate(1L, jpt) |
---|
| 303 | if nx EQ 1 OR ny EQ 1 then res = reform(res, nx, ny, nz, jpt, /overwrite) |
---|
[226] | 304 | if vargrid EQ 'W' THEN BEGIN |
---|
[167] | 305 | res = (shift(res, 0, 0, 1, 0)-res)/temporary(divi) |
---|
| 306 | res[*, *, 0, *] = !values.f_nan |
---|
| 307 | ENDIF ELSE BEGIN |
---|
| 308 | res = (res-shift(res, 0, 0, -1, 0))/temporary(divi) |
---|
| 309 | res[*, *, nz-1, *] = !values.f_nan |
---|
| 310 | ENDELSE |
---|
| 311 | END |
---|
| 312 | ENDCASE |
---|
| 313 | land = where(newmask EQ 0, cnt) |
---|
| 314 | if land[0] NE -1 then BEGIN |
---|
| 315 | land = (temporary(land))#replicate(1L, jpt) + replicate(1L, cnt)#(nx*ny*nz*lindgen(jpt)) |
---|
| 316 | res[temporary(land)] = valmask |
---|
| 317 | ENDIF |
---|
| 318 | END |
---|
| 319 | ELSE:return, report('input array must have 2, 3 or 4 dimensions') |
---|
| 320 | ENDCASE |
---|
[314] | 321 | |
---|
| 322 | if keyword_set(key_performance) THEN print, 'time curl', systime(1)-time1 |
---|
| 323 | |
---|
[167] | 324 | return, res |
---|
| 325 | END |
---|