source: trunk/SRC/Grid/computegrid.pro @ 493

Last change on this file since 493 was 493, checked in by pinsard, 10 years ago

fix some typos in comments

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 51.1 KB
Line 
1;+
2;
3; @file_comments
4; compute the grid parameters (cm_4mesh) common
5;
6; domains sizes:
7; ---------------
8; jpi, jpj, jpk, jpiglo, jpjglo, jpkglo, jpidta, jpjdta, jpkdta
9;
10; domains positions regarding to the original grid:
11; --------------------------------------------------
12; ixminmesh, ixmaxmesh, iyminmesh, iymaxmesh, izminmesh, izmaxmesh
13; ixmindta, ixmaxdta, iymindta, iymaxdta, izmindta, izmaxdta
14;
15; horizontal parameters:
16; ----------------------
17; glamt, glamf, gphit, gphit, e1t, e2t
18;
19; additional horizontal parameters if FULLCGRID keyword is defined:
20; -----------------------------------------------------------------
21; glamu, glamv, gphiu, gphiv, e1u, e1v, e1f, e2u, e2v, e2f
22;
23; verticals parameters:
24; ---------------------
25; gdept, gdepw, e3t, e3w
26;
27; masks:
28; ------
29; tmask
30;
31; additional masks if FULLCGRID keyword is defined:
32; -------------------------------------------------
33; umaskred, vmaskred, fmaskredx, fmaskredy
34;
35; triangles_list:
36; ---------------
37; triangulation
38;
39; key_* variables:
40; ----------------
41; key_onearth, key_periodic, key_shift, key_stride, key_partialstep,
42; key_yreverse, key_zreverse, key_gridtype
43;
44; xxx related variables:
45; ----------------------
46; ccmeshparameters, ccreadparameters
47;
48; @categories
49; Grid
50;
51; @param startx {in}{optional}{type=scalar}
52;       x starting point, optional if [XY]AXIS keyword is used
53;
54; @param starty {in}{optional}{type=scalar}
55;       y starting point, optional if [XY]AXIS keyword is used
56;
57; @param stepxin {in}{optional}{type=scalar or vector}
58;       x direction step, optional if [XY]AXIS keyword is used, must be > 0
59;       if stepxin is a vector nx is not used
60;
61; @param stepyin {in}{optional}{type=scalar or vector}
62;       y direction step, optional if [XY]AXIS keyword is used,
63;       could be > 0 (south to north) or < 0 (north to south)
64;       if stepyin is a vector ny is not used
65;
66; @param nxin {in}{optional}{type=scalar}
67;       number of points in x direction,
68;       optional if [XY]AXIS keyword is used or stepxin is a vector
69;
70; @param nyin {in}{optional}{type=scalar}
71;       number of points in y direction,
72;       optional if [XY]AXIS keyword is used or stepyin is a vector
73;
74; @keyword FULLCGRID {default=0}{type=scalar: 0 or 1}
75;       Activate to specify that you want to compute all the C grid parameters:
76;       definition of glam[uv], gphi[uv], e1[uvf], e2[uvf], [uv]maskred and
77;       fmaskred[xy] will be add to the default computations
78;
79; @keyword GLAMBOUNDARY {default=those defined in the file}{type=2 elements vector}
80;       Longitude boundaries that should be used to visualize the data.
81;         lon2 > lon1
82;         lon2 - lon1 le 360
83;       By default, the common (cm_4mesh) variable key_shift will be automatically
84;       defined according to GLAMBOUNDARY.
85;
86; @keyword MASK {default=array of 1}{type=2D or 3D array}
87;       Specify the land(0)/sea(1) mask
88;
89; @keyword ONEARTH {default=1}{type=scalar: 0 or 1}
90;       Force the manual definition of data localization on the earth or not
91;          0) if the data are not on the earth
92;          1) if the data are on earth (in that case we can for example use
93;             the labels 'longitude', 'latitude' in plots).
94;       The resulting value will be stored in the common (cm_4mesh) variable key_onearth
95;       ONEARTH=0 forces PERIODIC=0, SHIFT=0 and is cancelling GLAMBOUNDARY
96;
97; @keyword PERIODIC {default=computed by using the first line of glamt}{type=scalar: 0 or 1}
98;       Force the manual definition of the grid zonal periodicity.
99;       The resulting value will be stored in the common (cm_4mesh) variable key_periodic
100;       PERIODIC=0 forces SHIFT=0
101;
102; @keyword PLAIN {default=0}{type=scalar: 0 or 1}
103;       Force YREVERSE=0, ZREVERSE=0, PERIODIC=0, SHIFT=0, STRIDE=[1, 1, 1] and
104;       suppress the automatic redefinition of the domain in case of x periodicity overlap,
105;       y periodicity overlap (ORCA type only) and mask border to 0.
106;
107; @keyword SHIFT {default=computed according to glamboundary}{type=scalar}
108;       Force the manual definition of the zonal shift that must be apply to the data.
109;       The resulting value will be stored in the common (cm_4mesh) variable key_shift
110;       Note that if key_periodic=0 then in any case key_shift=0.
111;
112; @keyword STRCALLING {type=string}
113;       a string containing the calling command used to
114;       call computegrid (this is used by <pro>xxx</pro>)
115;
116; @keyword STRIDE {default=[1, 1, 1]}{type=3 elements vector}
117;       Specify the stride in x, y and z direction. The resulting
118;       value will be stored in the common (cm_4mesh) variable key_stride
119;
120; @keyword XAXIS {type=1D or 2D array}
121;       Specify longitudes in this case startx, stepx and nx are not used but
122;       could be necessary if the y axis is not defined with yaxis. It must be
123;       possible to sort the first line of xaxis in the increasing order by
124;       shifting its elements.
125;
126; @keyword YAXIS {type=1D or 2D array}
127;       Specify latitudes in this case starty, stepy and ny are not used but
128;       starty and stepy could be necessary if the x axis is not defined with
129;       xaxis. It must be sorted in the increasing or decreasing order (along each column if 2d array).
130;
131; @keyword XYINDEX activate to specify that the horizontal grid should
132;       be simply defined by using the index of the points
133;          (xaxis = findgen(nx) and yaxis = findgen(ny))
134;       using this keyword forces key_onearth=0
135;
136; @keyword XMINMESH {default=0L}{type=scalar}
137;       Define common (cm_4mesh) variables ixminmesh used to define the localization
138;       of the first point of the grid along the x direction in a zoom of the original grid
139;
140; @keyword YMINMESH {default=0L}{type=scalar}
141;       Define common (cm_4mesh) variables iyminmesh used to define the localization
142;       of the first point of the grid along the y direction in a zoom of the original grid
143;
144; @keyword ZMINMESH {default=0L}{type=scalar}
145;       Define common (cm_4mesh) variables izminmesh used to define the localization
146;       of the first point of the grid along the z direction in a zoom of the original grid
147;
148; @keyword XMAXMESH {default=jpiglo-1}{type=scalar}
149;       Define common (cm_4mesh) variables ixmaxmesh used to define the localization
150;       of the last point of the grid along the x direction in a zoom of the original grid
151;       Note that if XMAXMESH < 0 then ixmaxmesh is defined as ixmaxmesh = jpiglo -1 + xmaxmesh
152;
153; @keyword YMAXMESH {default=jpjglo-1}{type=scalar}
154;       Define common (cm_4mesh) variables iymaxmesh used to define the localization
155;       of the last point of the grid along the y direction in a zoom of the original grid
156;       Note that if YMAXMESH < 0 then iymaxmesh is defined as iymaxmesh = jpjglo -1 + ymaxmesh
157;
158; @keyword ZMAXMESH {default=jpkglo-1}{type=scalar}
159;       Define common (cm_4mesh) variables izmaxmesh used to define the localization
160;       of the last point of the grid along the z direction in a zoom of the original grid
161;       Note that if ZMAXMESH < 0 then izmaxmesh is defined as izmaxmesh = jpkglo -1 + maxmesh
162;
163; @keyword FBASE2TBASE
164;       Activate when the model is a C grid based on a F point
165;       (with a F point at the bottom-left corner and a T point at the
166;       upper-right corner). In this case, we ignore
167;         - the first line of F and V points
168;         - the last  line of T and U points
169;         - if the grid is not x-periodic, the first column of F and U points
170;         - if the grid is not x-periodic, the last  column of T and V points.
171;       => we are back to a C grid based on T point as for OPA model.
172;       Note that in that case, key_gridtype = 'c_f' and not 'c' (-> used in read_ncdf)
173;       Note that activate FBASE2TBASE forces FULLCGRID=1
174;
175; @keyword FFBASE2TBASE
176;       Activate when the model is a C grid based on a F point
177;       (with a F point at the bottom-left corner and again a F point at the
178;       upper-right corner). In this case, we ignore
179;         - the first line of F and V points
180;         - if the grid is not x-periodic, the first column of F and U points
181;       => we are back to a C grid based on T point as for OPA model.
182;       Note that in that case, key_gridtype = 'c_ff' and not 'c' (-> used in read_ncdf)
183;       Note that activate FFBASE2TBASE forces FULLCGRID=1
184;
185; @keyword UBASE2TBASE
186;       Activate when the model is a C grid based on a U point
187;       (with a U point at the bottom-left corner and a T point at the
188;       upper-right corner). In this case, we ignore
189;         - if the grid is not x-periodic, the first column of F and U points
190;         - if the grid is not x-periodic, the last  column of T and V points.
191;       => we are back to a C grid based on T point as for OPA model.
192;       Note that in that case, key_gridtype = 'c_u' and not 'c' (-> used in read_ncdf)
193;       Note that activate UBASE2TBASE forces FULLCGRID=1
194;
195; @keyword VBASE2TBASE
196;       Activate when the model is a C grid based on a V point
197;       (with a V point at the bottom-left corner and a T point at the
198;       upper-right corner). In this case, we ignore
199;         - the first line of F and V points
200;         - the last  line of T and U points
201;       => we are back to a C grid based on T point as for OPA model.
202;       Note that in that case, key_gridtype = 'c_v' and not 'c' (-> used in read_ncdf)
203;       Note that activate VBASE2TBASE forces FULLCGRID=1
204;
205; @keyword ROMSH {type=2D array}
206;       This array is the final bathymetry at RHO-points. It is stored in the common
207;       variable (cm_4mesh) romszinfos.h
208;       Used when the model is a ROMS C-grid with one more point
209;       in longitude for T and V grid and one more point in latitude
210;       for T and U grid. In this case, we ignore
211;         - the last line of T and U points
212;         - the last column of T and V points.
213;      => we are back to a C grid based on T point as for OPA model.
214;       Note that activate ROMSH forces FULLCGRID=1
215;
216; @keyword STRCALLING {type=scalar string}
217;       Used by xxx...
218;
219; @keyword YREVERSE {default=computed according to gphit[0, 1:jpj-1] LT gphit[0, 0:jpj-2]}{type=scalar}
220;       Force the manual definition of the y reverse that must be apply to the data.
221;       The resulting value will be stored in the common (cm_4mesh) variable key_yreverse
222;
223; @keyword ZAXIS {type=1D}
224;       Specify the vertical axis. Must be sorted in the increasing or decreasing order
225;
226; @keyword ZREVERSE {default=computed according to gdept[0] GT gdept[1]}{type=scalar}
227;       Force the manual definition of the z reverse that must be apply to the data.
228;       The resulting value will be stored in the common (cm_4mesh) variable key_zreverse
229;
230; @keyword _EXTRA
231; not used in the present case ...
232;
233; @uses
234; <pro>cm_4mesh</pro>
235; <pro>cm_4data</pro>
236; <pro>cm_4cal</pro>
237;
238
239; @restrictions
240; if the grid has x/y periodicity overlap and/or if
241; the mask has 0 everywhere at the border (like a closed sea) and
242; if (we did not activate /plain and xminmesh, xmaxmesh, yminmesh,
243; ymaxmesh keywords are defined to their default values), we redefine
244; xminmesh, xmaxmesh, yminmesh, ymaxmesh in order to remove the
245; overlapping part and/or to open the domain (avoid it be forced
246; to use cell_fill = 1).
247;
248; FUV points definition is not exact if the grid is irregular
249;
250; @history
251; Sebastien Masson (smasson\@lodyc.jussieu.fr)
252;                      2000-04-20
253; Sept 2004, several bug fix to suit C grid type...
254; Aug 2005, rewrite almost everything...
255;
256; @version
257; $Id$
258;
259;-
260PRO computegrid, startx, starty, stepxin, stepyin, nxin, nyin $
261                 , XAXIS=xaxis, YAXIS=yaxis, ZAXIS=zaxis $
262                 , MASK=mask, GLAMBOUNDARY=glamboundary $
263                 , XMINMESH=xminmesh, XMAXMESH=xmaxmesh $
264                 , YMINMESH=yminmesh, YMAXMESH=ymaxmesh $
265                 , ZMINMESH=zminmesh, ZMAXMESH=zmaxmesh $
266                 , ONEARTH=onearth, PERIODIC=periodic $
267                 , PLAIN=plain, SHIFT=shift, STRIDE=stride $
268                 , YREVERSE=yreverse, ZREVERSE=zreverse  $
269                 , FULLCGRID=fullcgrid, XYINDEX=xyindex $
270                 , UBASE2TBASE=ubase2tbase, VBASE2TBASE=vbase2tbase $
271                 , FBASE2TBASE=fbase2tbase, FFBASE2TBASE=ffbase2tbase $
272                 , STRCALLING=strcalling, ROMSH=romsh, _EXTRA=ex
273;
274  compile_opt idl2, strictarrsubs
275;
276@cm_4mesh
277@cm_4data
278@cm_4cal
279  IF NOT keyword_set(key_forgetold) THEN BEGIN
280@updatenew
281@updatekwd
282  ENDIF
283;---------------------------------------------------------
284;------------------------------------------------------------
285  time1 = systime(1)            ; for key_performance
286;------------------------------------------------------------
287;
288;====================================================
289; Check input parameters
290;====================================================
291;
292; xaxis related parameters
293;
294  if n_elements(xaxis) NE 0 then BEGIN
295    CASE (size(xaxis))[0] OF
296      0:nx = 1L
297      1:nx = (size(xaxis))[1]
298      2:nx = (size(xaxis))[1]
299    ENDCASE
300  ENDIF ELSE BEGIN
301    IF n_elements(startx) EQ 0 THEN BEGIN
302      dummy = report('If xaxis is not given, startx must be defined')
303      return
304    ENDIF
305    CASE n_elements(stepxin) OF
306      0:BEGIN
307        dummy = report('If xaxis is not given, stepxin must be defined')
308        return
309      END
310      1:BEGIN
311        IF n_elements(nxin) EQ 0 THEN BEGIN
312          dummy = report('If xaxis is not given and stepxin has only one element, nx must be defined')
313          return
314        ENDIF ELSE nx = nxin
315      END
316      ELSE:nx = n_elements(stepxin)
317    ENDCASE
318  ENDELSE
319;
320; yaxis related parameters
321;
322  if n_elements(yaxis) NE 0 then BEGIN
323    CASE (size(yaxis))[0] OF
324      0:ny = 1L
325      1:ny = (size(yaxis))[1]
326      2:ny = (size(yaxis))[2]
327    ENDCASE
328  ENDIF ELSE BEGIN
329    IF n_elements(starty) EQ 0 THEN BEGIN
330      dummy = report('If yaxis is not given, starty must be defined')
331      return
332    ENDIF
333    CASE n_elements(stepyin) OF
334      0:BEGIN
335        dummy = report('If yaxis is not given, stepyin must be defined')
336        return
337      END
338      1:BEGIN
339        IF n_elements(nyin) EQ 0 THEN BEGIN
340          dummy = report('If yaxis is not given and stepyin has only one element, ny must be defined')
341          return
342        ENDIF ELSE ny = nyin
343      END
344      ELSE:ny = n_elements(stepyin)
345    ENDCASE
346  ENDELSE
347;
348; zaxis related parameters
349;
350  if n_elements(zaxis) NE 0 then BEGIN
351    CASE (size(zaxis))[0] OF
352      0:nz = 1L
353      1:nz = (size(zaxis))[1]
354      ELSE:BEGIN
355        ras = report( 'not coded')
356        stop
357      END
358    ENDCASE
359  ENDIF ELSE nz = 1L
360;
361;====================================================
362; Others automatic definitions...
363;====================================================
364;
365  IF keyword_set(romsh) THEN fullcgrid = 1
366  CASE 1 OF
367    keyword_set(ffbase2tbase):key_gridtype = 'c_ff'
368    keyword_set(fbase2tbase):key_gridtype = 'c_f'
369    keyword_set(ubase2tbase):key_gridtype = 'c_u'
370    keyword_set(vbase2tbase):key_gridtype = 'c_v'
371    else:key_gridtype = 'c'
372  ENDCASE
373  IF strlen(key_gridtype) NE 1 THEN fullcgrid = 1
374;
375  IF n_elements(xminmesh) NE 0 OR n_elements(xmaxmesh) NE 0 THEN BEGIN
376    IF n_elements(xminmesh) EQ 0 THEN xminmesh = ixminmesh
377    IF n_elements(xmaxmesh) EQ 0 THEN xmaxmesh = ixmaxmesh
378    IF nx EQ jpi AND xminmesh EQ ixminmesh AND xmaxmesh EQ ixmaxmesh THEN xalreadycut = 1
379  ENDIF
380  IF keyword_set(xalreadycut) THEN BEGIN
381    xmin = 0
382    xmax = jpi - 1
383    nxx = jpi
384  ENDIF ELSE BEGIN
385    jpiglo = long(nx)
386    IF n_elements(xminmesh) NE 0 THEN ixminmesh = long(xminmesh[0]) ELSE ixminmesh  = 0l
387    IF n_elements(xmaxmesh) NE 0 THEN ixmaxmesh = long(xmaxmesh[0]) ELSE ixmaxmesh  = jpiglo-1
388    IF ixmaxmesh LT 0 THEN ixmaxmesh = jpiglo -1 + ixmaxmesh
389    ixmaxmesh = 0 > ixmaxmesh < (jpiglo-1)
390    ixminmesh = 0 > ixminmesh < ixmaxmesh
391    jpi = ixmaxmesh-ixminmesh+1
392    xmin = ixminmesh
393    xmax = ixmaxmesh
394    nxx = jpiglo
395  ENDELSE
396
397  IF n_elements(yminmesh) NE 0 OR n_elements(ymaxmesh) NE 0 THEN BEGIN
398    IF n_elements(yminmesh) EQ 0 THEN yminmesh = iyminmesh
399    IF n_elements(ymaxmesh) EQ 0 THEN ymaxmesh = iymaxmesh
400    IF ny EQ jpj AND yminmesh EQ iyminmesh AND ymaxmesh EQ iymaxmesh THEN yalreadycut = 1
401  ENDIF
402  IF keyword_set(yalreadycut) THEN BEGIN
403    ymin = 0
404    ymax = jpj - 1
405    nyy = jpj
406  ENDIF ELSE BEGIN
407    jpjglo = long(ny)
408    IF n_elements(yminmesh) NE 0 THEN iyminmesh = long(yminmesh[0]) ELSE iyminmesh  = 0l
409    IF n_elements(ymaxmesh) NE 0 THEN iymaxmesh = long(ymaxmesh[0]) ELSE iymaxmesh  = jpjglo-1
410    IF key_gridtype EQ 'c_v' OR key_gridtype EQ 'c_f' THEN iymaxmesh = iymaxmesh-1
411    IF iymaxmesh LT 0 THEN iymaxmesh = jpjglo -1 + iymaxmesh
412    iymaxmesh = 0 > iymaxmesh < (jpjglo-1)
413    iyminmesh = 0 > iyminmesh < iymaxmesh
414    jpj = iymaxmesh-iyminmesh+1
415    ymin = iyminmesh
416    ymax = iymaxmesh
417    nyy = jpjglo
418  ENDELSE
419
420  IF n_elements(zminmesh) NE 0 AND n_elements(zmaxmesh) NE 0 THEN BEGIN
421    IF nz EQ jpk AND zminmesh EQ izminmesh AND zmaxmesh EQ izmaxmesh THEN zalreadycut = 1
422  ENDIF
423  IF keyword_set(zalreadycut) THEN BEGIN
424    zmin = 0
425    zmax = jpk - 1
426    nzz = jpk
427  ENDIF ELSE BEGIN
428   jpkglo = long(nz)
429    IF n_elements(zminmesh) NE 0 THEN izminmesh = long(zminmesh[0]) ELSE izminmesh  = 0l
430    IF n_elements(zmaxmesh) NE 0 THEN izmaxmesh = long(zmaxmesh[0]) ELSE izmaxmesh  = jpkglo-1
431    IF izmaxmesh LT 0 THEN izmaxmesh = jpkglo -1 + izmaxmesh
432    izmaxmesh = 0 > izmaxmesh < (jpkglo-1)
433    izminmesh = 0 > izminmesh < izmaxmesh
434    jpk = izmaxmesh-izminmesh+1
435    zmin = izminmesh
436    zmax = izmaxmesh
437    nzz = jpkglo
438  ENDELSE
439;
440; impact of plain keyword:
441;
442  IF keyword_set(plain) THEN BEGIN
443    yreverse = 0
444    zreverse = 0
445    periodic = 0
446    shift = 0
447    stride = [1, 1, 1]
448  ENDIF
449;
450; avoid basics errors...
451;
452  jpidta = jpiglo
453  jpjdta = jpjglo
454  jpkdta = jpkglo
455  ixmindta = 0
456  ixmaxdta = jpidta-1
457  iymindta = 0
458  iymaxdta = jpjdta-1
459  izmindta = 0
460  izmaxdta = jpkdta-1
461;
462  key_partialstep = 0
463  if n_elements(stride) eq 3 then key_stride = stride $
464  ELSE key_stride = [1, 1, 1]
465;
466; check xyindex and its consequences
467;
468  if keyword_set(xyindex) then onearth = 0
469;
470; check onearth and its consequences
471;
472  IF n_elements(onearth) EQ 0 THEN key_onearth = 1b $
473  ELSE key_onearth = keyword_set(onearth)
474  IF NOT key_onearth THEN BEGIN
475    periodic = 0
476    shift = 0
477  ENDIF
478
479  r = 6371000.
480;
481;====================================================
482; X direction : glamt
483;====================================================
484;
485; def of glamt
486;
487  if n_elements(xaxis) NE 0 then BEGIN
488    if keyword_set(xyindex) THEN glamt = findgen(jpiglo) ELSE glamt = xaxis
489  ENDIF ELSE BEGIN
490    if keyword_set(xyindex) THEN stepx = 1. ELSE stepx = stepxin
491    CASE 1 OF
492      n_elements(stepx):glamt = startx + findgen(jpiglo)*stepx
493      size(stepx, /n_dimensions):glamt = startx + total(stepx, /cumulative)
494      ELSE:BEGIN
495        dummy = report('Wrong definition of stepx...')
496        return
497      END
498    ENDCASE
499  ENDELSE
500;
501; apply glamboundary
502;
503  IF keyword_set(glamboundary) AND key_onearth THEN BEGIN
504    IF glamboundary[0] GE glamboundary[1] THEN stop
505    IF glamboundary[1]-glamboundary[0] GT 360 THEN stop
506    glamt = glamt MOD 360
507    smaller = where(glamt LT glamboundary[0])
508    if smaller[0] NE -1 then glamt[smaller] = glamt[smaller]+360.
509    bigger = where(glamt GE glamboundary[1])
510    if bigger[0] NE -1 then glamt[bigger] = glamt[bigger]-360.
511  ENDIF
512;
513; force glamt to have 2 dimensions
514;
515  IF n_elements(glamt) EQ nxx*nyy THEN glamt = reform(glamt, nxx, nyy, /over) $
516  ELSE glamt = reform(glamt, nxx, /over)
517  CASE size(glamt, /n_dimensions) OF
518    1:BEGIN
519      IF n_elements(glamt) EQ 1 THEN glamt = replicate(glamt[0], jpi, jpj) $
520      ELSE glamt = glamt[xmin:xmax]#replicate(1, jpj)
521    END
522    2:glamt = glamt[xmin:xmax, ymin:ymax]
523  ENDCASE
524; keep 2d array even with degenerated dimension
525  IF jpj EQ 1 THEN glamt = reform(glamt, jpi, jpj, /over)
526;
527;====================================================
528; Y direction : gphit
529;====================================================
530;
531; def of gphit
532;
533  if n_elements(yaxis) NE 0 THEN BEGIN
534    if keyword_set(xyindex) THEN gphit = findgen(jpjglo) ELSE gphit = yaxis
535  ENDIF ELSE BEGIN
536    if keyword_set(xyindex) THEN stepy = 1. ELSE stepy = stepyin
537    CASE 1 OF
538      n_elements(stepy):gphit = starty + findgen(jpjglo)*stepy
539      size(stepy, /n_dimensions):gphit = starty + total(stepy, /cumulative)
540      ELSE:BEGIN
541        dummy = report('Wrong definition of stepy...')
542        return
543      END
544    ENDCASE
545  ENDELSE
546;
547; force gphit to have 2 dimensions
548;
549  IF n_elements(gphit) EQ nxx*nyy THEN gphit = reform(gphit, nxx, nyy, /over) $
550  ELSE gphit = reform(gphit, nyy, /over)
551  CASE size(gphit, /n_dimensions) OF
552    1:BEGIN
553      IF n_elements(gphit) EQ 1 THEN gphit = replicate(gphit[0], jpi, jpj) $
554      ELSE gphit = replicate(1, jpi)#gphit[ymin:ymax]
555    END
556    2:gphit = gphit[xmin:xmax, ymin:ymax]
557  ENDCASE
558; keep 2d array even with degenerated dimension
559  IF jpj EQ 1 THEN gphit = reform(gphit, jpi, jpj, /over)
560;
561;====================================================
562; check y periodicity... Only according to ORCA grid
563;====================================================
564; check the periodicity if iyminmesh and iymaxmesh have the default definitions...
565  IF NOT keyword_set(plain) AND key_onearth EQ 1 $
566    AND iyminmesh EQ 0l AND iymaxmesh eq jpjglo-1 AND jpj GE 3 AND jpi GE 2 THEN BEGIN
567
568    CASE 1 OF
569      ixminmesh EQ 0l AND ixmaxmesh eq jpiglo-1 $
570        AND array_equal(gphit[1:*, jpj-1], reverse(gphit[1:*, jpj-3])) EQ 1:BEGIN
571; T pivot
572        ymaxmesh = -1
573        recall = 1
574      END
575      ixminmesh EQ 1l AND ixmaxmesh eq jpiglo-2 $
576         AND array_equal(gphit[*, jpj-1], reverse(shift(gphit[*, jpj-3], -1))) EQ 1:BEGIN
577; T pivot
578        ymaxmesh = -1
579        recall = 1
580      END
581      ixminmesh EQ 0l AND ixmaxmesh eq jpiglo-1 $
582       AND array_equal(gphit[*, jpj-1], reverse(gphit[*, jpj-2])) EQ 1:BEGIN
583; F pivot
584        ymaxmesh = -1
585        recall = 1
586      END
587      ixminmesh EQ 1l AND ixmaxmesh eq jpiglo-2 $
588         AND array_equal(gphit[*, jpj-1], reverse(gphit[*, jpj-2])) EQ 1:BEGIN
589; F pivot
590        ymaxmesh = -1
591        recall = 1
592      END
593      ELSE:
594    ENDCASE
595  ENDIF
596;
597;====================================================
598; check x periodicity...
599;====================================================
600IF n_elements(periodic) NE 0 THEN forcenoperio = 1 - keyword_set(periodic)
601; check the periodicity if ixminmesh and ixmaxmesh have the default definitions...
602  IF NOT keyword_set(plain) AND NOT keyword_set(forcenoperio) AND key_onearth EQ 1 $
603     AND ixminmesh EQ 0l AND ixmaxmesh eq jpiglo-1 AND jpi GE 3 THEN BEGIN
604    CASE 0 OF
605      total((glamt[0, *] - glamt[jpi-2, *]) MOD 360) $
606      + total((glamt[1, *] - glamt[jpi-1, *]) MOD 360):BEGIN
607        xminmesh = 1
608        xmaxmesh = -1
609        recall = 1
610      END
611      total((glamt[0, *] - glamt[jpi-2, *]) MOD 360):BEGIN
612        xminmesh = 1
613        recall = 1
614      END
615      total((glamt[1, *] - glamt[jpi-1, *]) MOD 360):BEGIN
616        xmaxmesh = -1
617        recall = 1
618      END
619      total((glamt[0, *] - glamt[jpi-1, *]) MOD 360):BEGIN
620        xmaxmesh = -1
621        recall = 1
622      END
623      ELSE:
624    ENDCASE
625  ENDIF
626;====================================================
627; recall computegrid if needed...
628;====================================================
629  IF keyword_set(recall) THEN BEGIN
630    computegrid, XAXIS = glamt, YAXIS = gphit, ZAXIS = zaxis $
631                 , MASK = mask, GLAMBOUNDARY = glamboundary $
632                 , XMINMESH = xminmesh, XMAXMESH = xmaxmesh $
633                 , YMINMESH = yminmesh, YMAXMESH = ymaxmesh $
634                 , ZMINMESH = zminmesh, ZMAXMESH = zmaxmesh $
635                 , PERIODIC = periodic, SHIFT = shift, STRIDE = stride $
636                 , FULLCGRID = fullcgrid, XYINDEX = xyindex $
637                 , STRCALLING = strcalling $
638                 , ROMSH = romsh, _extra = ex
639    return
640  ENDIF
641;
642;====================================================
643; def key_yreverse
644;====================================================
645;
646  IF n_elements(yreverse) EQ 0 THEN BEGIN
647    IF jpj GT 1 THEN BEGIN
648      IF total(gphit[0, 1:jpj-1] LT gphit[0, 0:jpj-2]) GT jpj/2 THEN key_yreverse = 1 ELSE key_yreverse = 0
649    ENDIF ELSE key_yreverse = 0
650  ENDIF ELSE key_yreverse = yreverse
651  IF keyword_set(key_yreverse) THEN BEGIN
652    gphit = reverse(gphit, 2)
653    glamt = reverse(glamt, 2)
654  ENDIF
655;
656;====================================================
657; def of key_shift
658;====================================================
659;
660; definition of key_shift by shifting the array to have the min
661; values of glamt[*, 0] in glamt[0, 0]
662;
663  IF n_elements(shift) EQ 0 THEN BEGIN
664    IF jpi GT 1 then BEGIN
665      xtest = glamt[*, 0]
666      key_shift = (where(xtest EQ min(xtest)))[0]
667      IF key_shift NE 0 THEN key_shift = jpi - key_shift
668    ENDIF ELSE key_shift = 0
669  ENDIF ELSE key_shift = shift
670;
671;====================================================
672; def of key_periodic
673;====================================================
674;
675  IF n_elements(periodic) EQ 0 THEN BEGIN
676    IF jpi GT 1 THEN BEGIN
677      xtest = shift(glamt[*, 0], key_shift)
678; check that xtest is now sorted in the increasing order
679      IF array_equal(sort(xtest), lindgen(jpi)) EQ 0 THEN BEGIN
680        ras = report(['WARNING: we cannot sort the xaxis with a simple shift...', $
681        'we force key_periodic = 0 and key_shift = 0', $
682        'only horizontal plot may be ok...'])
683        key_periodic = 0
684        xnotsorted = 1
685      ENDIF ELSE BEGIN
686        key_periodic = (xtest[jpi-1]+2*(xtest[jpi-1]-xtest[jpi-2])) $
687                       GE (xtest[0]+360)
688      ENDELSE
689    ENDIF ELSE key_periodic = 0
690  ENDIF ELSE key_periodic = keyword_set(periodic)
691;
692; update key_shift
693;
694  key_shift = key_shift * (key_periodic EQ 1)
695;
696  IF (key_gridtype EQ 'c_u' OR key_gridtype EQ 'c_f') AND NOT keyword_set(key_periodic) THEN BEGIN
697    ixmaxmesh = ixmaxmesh-1
698    jpi = jpi-1
699  ENDIF
700;
701;====================================================
702; apply key_shift
703;====================================================
704;
705  if keyword_set(key_shift) then BEGIN
706    glamt = shift(glamt, key_shift, 0)
707    gphit = shift(gphit, key_shift, 0)
708    IF jpj EQ 1 THEN BEGIN
709      glamt = reform(glamt, jpi, jpj, /over)
710      gphit = reform(gphit, jpi, jpj, /over)
711    ENDIF
712  ENDIF
713;
714;====================================================
715; Are we using a "regular" grid (that can be described
716; with x vector and y vector)?
717;====================================================
718;
719; to get faster, we first test the most basic cases before
720; testing the full array.
721;
722  CASE 1 OF
723    keyword_set(xyindex):key_irregular = 0b
724    jpi EQ 1 OR jpj EQ 1:key_irregular = 0b
725    n_elements(xaxis) EQ 0 AND n_elements(yaxis) EQ 0:key_irregular = 0b
726    size(reform(xaxis), /n_dimensions) EQ 1 AND size(reform(xaxis), /n_dimensions) EQ 1:key_irregular = 0b
727    n_elements(xaxis) EQ 0 AND size(reform(yaxis), /n_dimensions) EQ 1:key_irregular = 0b
728    n_elements(yaxis) EQ 0 AND size(reform(xaxis), /n_dimensions) EQ 1:key_irregular = 0b
729    array_equal(glamt[*, 0], glamt[*, jpj-1]) EQ 0:key_irregular = 1b
730    array_equal(gphit[0, *], gphit[jpi-1, *]) EQ 0:key_irregular = 1b
731    array_equal(glamt, glamt[*, 0]#replicate(1, jpj)) EQ 0:key_irregular = 1b
732    array_equal(gphit, replicate(1, jpi)#(gphit[0, *])[*]) EQ 0:key_irregular = 1b
733    ELSE:key_irregular = 0b
734  ENDCASE
735;
736;====================================================
737; def of glamf: defined as the middle of T(i,j) T(i+1,j+1)
738;====================================================
739;
740  IF jpi GT 1 THEN BEGIN
741; we must compute stepxf: x distance between T(i,j) T(i+1,j+1)
742    CASE 1 OF
743      n_elements(stepx):stepxf = stepx
744      size(stepx, /n_dimensions):stepxf = stepx#replicate(1, jpj)
745      ELSE:BEGIN
746        if (keyword_set(key_onearth) AND keyword_set(xnotsorted)) $
747          OR (keyword_set(key_periodic) AND key_irregular) then BEGIN
748          stepxf = (glamt + 720) MOD 360
749          IF jpj EQ 1 THEN stepxf = reform(stepxf, jpi, jpj, /over)
750          stepxf = shift(stepxf, -1, -1) - stepxf
751          stepxf = [ [[stepxf]], [[stepxf + 360]], [[stepxf - 360]] ]
752          stepxf = min(abs(stepxf), dimension = 3)
753          IF NOT keyword_set(key_periodic) THEN $
754            stepxf[jpi-1, *] = stepxf[jpi-2, *]
755        ENDIF ELSE BEGIN
756          stepxf = shift(glamt, -1, -1) - glamt
757          IF keyword_set(key_periodic) THEN $
758            stepxf[jpi-1, *] = 360 + stepxf[jpi-1, *] $
759          ELSE stepxf[jpi-1, *] = stepxf[jpi-2, *]
760        ENDELSE
761        IF jpj GT 1 THEN BEGIN
762          stepxf[*, jpj-1] = stepxf[*, jpj-2]
763          stepxf[jpi-1, jpj-1] = stepxf[jpi-2, jpj-2]
764        ENDIF
765      END
766    ENDCASE
767    glamf = glamt + 0.5 * stepxf
768  ENDIF ELSE glamf = glamt + 0.5
769;
770  IF keyword_set(key_periodic) AND (max(glamf)-min(glamt)) GE 360 THEN BEGIN
771    IF NOT keyword_set(glamboundary) THEN BEGIN
772      bigger = where(glamf GE min(glamt)+360)
773      glamf[bigger] = glamf[bigger]-360.
774    ENDIF ELSE glamf = glamboundary[0] > temporary(glamf) < glamboundary[1]
775  ENDIF
776;
777  IF jpj EQ 1 THEN glamf = reform(glamf, jpi, jpj, /over)
778;
779;====================================================
780; def of gphif: defined as the middle of T(i,j) T(i+1,j+1)
781;====================================================
782;
783  IF jpj GT 1 THEN BEGIN
784; we must compute stepyf: y distance between T(i,j) T(i+1,j+1)
785    CASE 1 OF
786      n_elements(stepy):stepyf = stepy
787      size(stepy, /n_dimensions):stepyf = replicate(1, jpi)#stepy
788      ELSE:BEGIN
789        stepyf = shift(gphit, -1, -1) - gphit
790        stepyf[*, jpj-1] = stepyf[*, jpj-2]
791        IF jpi GT 1 THEN BEGIN
792          if NOT keyword_set(key_periodic) THEN $
793            stepyf[jpi-1, *] = stepyf[jpi-2, *]
794          stepyf[jpi-1, jpj-1] = stepyf[jpi-2, jpj-2]
795        ENDIF
796      END
797    ENDCASE
798    gphif = gphit + 0.5 * stepyf
799  ENDIF ELSE gphif = gphit + 0.5
800  IF key_onearth THEN gphif = -90. > gphif < 90.
801;
802  IF jpj EQ 1 THEN gphif = reform(gphif, jpi, jpj, /over)
803;
804;====================================================
805; e1t: x distance between U(i-1,j) and U(i,j)
806;====================================================
807;
808; *-|-*---|---*---|
809;
810  IF jpi GT 1 THEN BEGIN
811    IF n_elements(stepx) NE 1 THEN BEGIN
812      IF keyword_set(irregular) THEN BEGIN
813; we must compute stepxu: x distance between T(i,j) T(i+1,j)
814        IF keyword_set(key_periodic) THEN BEGIN
815          stepxu = (glamt + 720) MOD 360
816          stepxu = shift(stepxu, -1, 0) - stepxu
817          stepxu = [ [[stepxu]], [[stepxu + 360]], [[stepxu - 360]] ]
818          stepxu = min(abs(stepxu), dimension = 3)
819        ENDIF ELSE BEGIN
820          stepxu = shift(glamt, -1, 0) - glamt
821          stepxu[jpi-1, *] = stepxf[jpi-2, *]
822        ENDELSE
823      ENDIF ELSE stepxu = stepxf
824      IF jpj EQ 1 THEN stepxu = reform(stepxu, jpi, jpj, /over)
825      e1t = 0.5*(stepxu+shift(stepxu, 1, 0))
826      IF NOT keyword_set(key_periodic) THEN $
827        e1t[0, *] = e1t[1, *]
828    ENDIF ELSE e1t = replicate(stepx, jpi, jpj)
829  ENDIF ELSE e1t = replicate(1b, jpi, jpj)
830;
831  IF jpj EQ 1 THEN e1t = reform(e1t, jpi, jpj, /over)
832;
833;====================================================
834; e2t: y distance between V(i,j-1) and V(i,j)
835;====================================================
836;
837  IF jpj GT 1 THEN BEGIN
838; we must compute stepyv: y distance between T(i,j) T(i,j+1)
839    IF n_elements(stepy) NE 1 THEN BEGIN
840      IF keyword_set(key_irregular) THEN BEGIN
841        stepyv = shift(gphit, 0, -1) - gphit
842        stepyv[*, jpj-1] = stepyv[*, jpj-2]
843      ENDIF ELSE stepyv = stepyf
844      e2t = 0.5*(stepyv+shift(stepyv, 0, 1))
845      e2t[*, 0] = e2t[*, 1]
846    ENDIF ELSE e2t = replicate(stepy, jpi, jpj)
847  ENDIF ELSE e2t = replicate(1b, jpi, jpj)
848;
849  IF key_onearth THEN e2t = r * !pi/180. * temporary(e2t)
850;
851  IF jpj EQ 1 THEN e2t = reform(e2t, jpi, jpj, /over)
852;
853;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
854  IF keyword_set(fullcgrid) THEN BEGIN
855;
856;====================================================
857; def of glamu: defined as the middle of T(i,j) T(i+1,j)
858;====================================================
859;
860    IF keyword_set(irregular) THEN BEGIN
861      glamu = glamt + 0.5 * stepxu
862      IF keyword_set(glamboundary) AND key_onearth THEN $
863        glamu = glamboundary[0] > temporary(glamu) < glamboundary[1]
864    ENDIF ELSE glamu = glamf
865;
866    IF jpj EQ 1 THEN glamu = reform(glamu, jpi, jpj, /over)
867;
868;====================================================
869; def of gphiu: defined as the middle of T(i,j) T(i+1,j)
870;====================================================
871;
872    IF jpi GT 1 THEN BEGIN
873 ; we must compute stepyu: y distance between T(i+1,j) T(i,j)
874      IF keyword_set(key_irregular) THEN BEGIN
875       stepyu = shift(gphit, -1, 0) - gphit
876        IF NOT keyword_set(key_periodic) THEN $
877          stepyu[jpi-1, *] = stepyu[jpi-2, *]
878        gphiu = gphit + 0.5 * stepyu
879      ENDIF ELSE gphiu = gphit
880    ENDIF ELSE gphiu = gphit
881  IF key_onearth THEN gphiu = -90. > gphiu < 90.
882;
883  IF jpj EQ 1 THEN gphiu = reform(gphiu, jpi, jpj, /over)
884;
885;====================================================
886; def of glamv: defined as the middle of T(i,j) T(i,j+1)
887;====================================================
888;
889    IF jpj GT 1 THEN BEGIN
890 ; we must compute stepxv: x distance between T(i,j) T(i,j+1)
891      IF keyword_set(irregular) THEN BEGIN
892        IF keyword_set(key_periodic) THEN BEGIN
893          stepxv = (glamt + 720) MOD 360
894          stepxv = shift(stepxv, 0, -1) - stepxv
895          stepxv = [ [[stepxv]], [[stepxv + 360]], [[stepxv - 360]] ]
896          stepxv = min(abs(stepxv), dimension = 3)
897        ENDIF ELSE stepxv = shift(glamt, 0, -1) - glamt
898        stepxv[*, jpj-1] = stepxv[*, jpj-2]
899        glamv = glamt + 0.5 * stepxv
900        IF keyword_set(glamboundary) AND key_onearth THEN $
901          glamv = glamboundary[0] > temporary(glamv) < glamboundary[1]
902      ENDIF ELSE glamv = glamt
903    ENDIF ELSE glamv = glamt
904;
905;====================================================
906; def of gphiv: defined as the middle of T(i,j) T(i,j+1)
907;====================================================
908;
909    IF keyword_set(key_irregular) THEN $
910      gphiv = gphit + 0.5 * stepyv $
911    ELSE gphiv = gphif
912    IF key_onearth THEN gphiv = -90. > gphiv < 90.
913;
914    IF jpj EQ 1 THEN gphiv = reform(gphiv, jpi, jpj, /over)
915;
916;====================================================
917; e1u: x distance between T(i,j) and T(i+1,j)
918;====================================================
919;
920    IF jpi GT 1 AND n_elements(stepx) NE 1 THEN $
921      e1u = stepxu ELSE e1u = e1t
922;
923;====================================================
924; e2u: y distance between F(i,j-1) and F(i,j)
925;====================================================
926;
927    IF keyword_set(key_irregular) THEN BEGIN
928      e2u = gphif - shift(gphif, 0, 1)
929      e2u[*, 0] = e2u[*, 1]
930      IF key_onearth THEN e2u = r * !pi/180. * temporary(e2u)
931    ENDIF ELSE e2u = e2t
932;
933    IF jpj EQ 1 THEN e2u = reform(e2u, jpi, jpj, /over)
934;
935;====================================================
936; e1v: x distance between F(i-1,j) and F(i,j)
937;====================================================
938;
939    IF keyword_set(irregular) THEN BEGIN
940      IF keyword_set(key_periodic) THEN BEGIN
941        e1v = (glamf + 720) MOD 360
942        e1v = e1v - shift(e1v, 1, 0)
943        e1v = [ [[e1v]], [[e1v + 360]], [[e1v - 360]] ]
944        e1v = min(abs(e1v), dimension = 3)
945      ENDIF ELSE BEGIN
946        e1v = glamf - shift(glamf, 1, 0)
947        e1v[0, *] = stepxf[1, *]
948      ENDELSE
949    ENDIF ELSE e1v = e1t
950;
951    IF jpj EQ 1 THEN e1v = reform(e1v, jpi, jpj, /over)
952;
953;====================================================
954; e2v: y distance between T(i,j) and T(i+1,j)
955;====================================================
956;
957    IF jpj GT 1 and n_elements(stepy) NE 1 THEN BEGIN
958      e2v = stepyv
959      IF key_onearth THEN e2v = r * !pi/180. * temporary(e2v)
960    ENDIF ELSE e2v = e2t
961;
962;====================================================
963; e1f: x distance between V(i,j) and V(i+1,j)
964;====================================================
965;
966    IF keyword_set(irregular) THEN BEGIN
967      IF keyword_set(key_periodic) THEN BEGIN
968        e1f = (glamv + 720) MOD 360
969        e1f = shift(e1f, -1, 0) - e1f
970        e1f = [ [[e1f]], [[e1f + 360]], [[e1f - 360]] ]
971        e1f = min(abs(e1f), dimension = 3)
972      ENDIF ELSE BEGIN
973        e1f = shift(glamv, -1, 0) - glamt
974        e1f[jpi-1, *] = stepxf[jpi-2, *]
975      ENDELSE
976    ENDIF ELSE e1f = e1u
977;
978    IF jpj EQ 1 THEN e1f = reform(e1f, jpi, jpj, /over)
979;
980;====================================================
981; e2f: y distance between U(i,j) and U(i,j+1)
982;====================================================
983;
984    IF keyword_set(key_irregular) THEN BEGIN
985      e2f = shift(gphiu, 0, -1) - gphiu
986      e2f[*, jpj-1] = e2f[*, jpj-2]
987      IF key_onearth THEN e2f = r * !pi/180. * temporary(e2f)
988    ENDIF ELSE e2f = e2v
989;
990    IF jpj EQ 1 THEN e2f = reform(e2f, jpi, jpj, /over)
991;
992  ENDIF
993;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
994;
995;
996;====================================================
997; e1[tuvf] from degree to meters
998;====================================================
999;
1000  IF keyword_set(key_onearth)  THEN BEGIN
1001    e1t = r * !pi/180. * temporary(e1t) * cos(!pi/180.*gphit)
1002    IF keyword_set(fullcgrid) THEN BEGIN
1003      e1u = r * !pi/180. * temporary(e1u) * cos(!pi/180.*gphiu)
1004      e1v = r * !pi/180. * temporary(e1v) * cos(!pi/180.*gphiv)
1005      e1f = r * !pi/180. * temporary(e1f) * cos(!pi/180.*gphif)
1006    ENDIF
1007  ENDIF
1008;
1009  IF jpj EQ 1 THEN BEGIN
1010    e1t = reform(e1t, jpi, jpj, /over)
1011    IF keyword_set(fullcgrid) THEN BEGIN
1012      e1u = reform(e1u, jpi, jpj, /over)
1013      e1v = reform(e1v, jpi, jpj, /over)
1014      e1f = reform(e1f, jpi, jpj, /over)
1015    ENDIF
1016  ENDIF
1017;
1018;====================================================
1019; if not fullcgrid: make sure we don't use glam[uv], gphi[uv], e[12][uvf]
1020;====================================================
1021;
1022  IF NOT keyword_set(fullcgrid) THEN BEGIN
1023    glamu = !values.f_nan & glamv = !values.f_nan
1024    gphiu = !values.f_nan & gphiv = !values.f_nan
1025    e1u = !values.f_nan & e1v = !values.f_nan & e1f = !values.f_nan
1026    e2u = !values.f_nan & e2v = !values.f_nan & e2f = !values.f_nan
1027    firstxu = !values.f_nan & lastxu = !values.f_nan & nxu = !values.f_nan
1028    firstyu = !values.f_nan & lastyu = !values.f_nan & nyu = !values.f_nan
1029    firstxv = !values.f_nan & lastxv = !values.f_nan & nxv = !values.f_nan
1030    firstyv = !values.f_nan & lastyv = !values.f_nan & nyv = !values.f_nan
1031  ENDIF
1032;
1033;====================================================
1034; Z direction
1035;====================================================
1036;
1037; z axis
1038;
1039    CASE n_elements(zaxis) OF
1040      0:BEGIN
1041        gdept = 0.
1042        key_zreverse = 0
1043      END
1044      1:BEGIN
1045        gdept = zaxis
1046        key_zreverse = 0
1047      END
1048      ELSE:BEGIN
1049        gdept = zaxis[zmin:zmax]
1050        IF n_elements(zreverse) EQ 0 THEN BEGIN
1051          IF jpk GT 1 THEN BEGIN
1052            if gdept[0] GT gdept[1] then key_zreverse = 1 ELSE key_zreverse = 0
1053          ENDIF ELSE key_zreverse = 0
1054        ENDIF ELSE key_zreverse = zreverse
1055        IF keyword_set(key_zreverse) THEN gdept = reverse(gdept)
1056      END
1057    ENDCASE
1058;
1059    if n_elements(gdept) GT 1 then BEGIN
1060      stepz = shift(gdept, -1)-gdept
1061      stepz[jpk-1] = stepz[jpk-2]
1062      gdepw = 0. > (gdept-stepz/2.)
1063    ENDIF ELSE BEGIN
1064      stepz = 1.
1065      gdepw = gdept
1066    ENDELSE
1067    IF keyword_set(romsh) THEN gdepw = gdept
1068;
1069;====================================================
1070; e3[tw]:
1071;====================================================
1072;
1073    e3t = stepz
1074    IF n_elements(stepz) GT 1 THEN BEGIN
1075      e3w = 0.5*(stepz+shift(stepz, 1))
1076      e3w[0] = 0.5*e3t[0]
1077    ENDIF ELSE e3w = e3t
1078;
1079;====================================================
1080; Mask
1081;====================================================
1082;
1083; default mask eq 1
1084  if NOT keyword_set(mask) then tmask = -1 ELSE tmask = mask
1085;
1086  if tmask[0] NE -1 then BEGIN
1087    tmask = byte(temporary(tmask))
1088    IF n_elements(mask) EQ nxx*nyy AND nzz GT 1 THEN BEGIN
1089      tmask = tmask[*]#replicate(1b, nzz)
1090      tmask = reform(tmask, nxx, nyy, nzz, /overwrite)
1091    ENDIF
1092    IF nxx EQ 1 OR nyy EQ 1 OR nzz EQ 1 THEN tmask = reform(tmask, nxx, nyy, nzz, /overwrite)
1093    tmask = tmask[xmin:xmax, ymin:ymax, zmin:zmax]
1094    IF jpi EQ 1 OR jpj EQ 1 OR jpk EQ 1 THEN tmask = reform(tmask, jpi, jpj, jpk, /over)
1095    if key_shift NE 0 then tmask = shift(tmask, key_shift, 0, 0)
1096; because tmask = reverse(tmask, 2) is not working if the 3rd
1097; dimension of tmask = 1, we call reform.
1098    IF jpk EQ 1 THEN tmask = reform(tmask, /over)
1099    IF key_yreverse EQ 1 THEN tmask = reverse(tmask, 2)
1100    IF jpk EQ 1 THEN tmask = reform(tmask, jpi, jpj, jpk, /over)
1101    IF key_zreverse EQ 1 THEN tmask = reverse(tmask, 3)
1102    IF jpk EQ 1 THEN tmask = reform(tmask, jpi, jpj, jpk, /over)
1103    IF keyword_set(fullcgrid) THEN BEGIN
1104      IF keyword_set(key_periodic) THEN BEGIN
1105        msk = tmask*shift(tmask, -1, 0, 0)
1106        umaskred = msk[jpi-1, *, *]
1107      ENDIF ELSE umaskred = tmask[jpi-1, *, *]
1108      vmaskred = tmask[*, jpj-1, *]
1109    ENDIF
1110    fmaskredy = tmask[jpi-1, *, *]
1111    fmaskredx = tmask[*, jpj-1, *]
1112  ENDIF ELSE BEGIN
1113    tmask = replicate(1b, jpi, jpj, jpk)
1114    IF keyword_set(fullcgrid) THEN BEGIN
1115      umaskred  = replicate(1b, jpj, jpk)
1116      vmaskred  = replicate(1b, jpi, jpk)
1117    ENDIF
1118    fmaskredy = replicate(1b, jpj, jpk)
1119    fmaskredx = replicate(1b, jpi, jpk)
1120  ENDELSE
1121;
1122  IF jpi GT 2 AND jpj GT 2 AND NOT keyword_set(plain) $
1123;;      AND ixminmesh EQ 0l AND ixmaxmesh eq jpiglo-1 $
1124;;      AND iyminmesh EQ 0l AND iymaxmesh eq jpjglo-1 $
1125     AND total(tmask[*, 0, *]) EQ 0 AND total(tmask[*, jpj-1, *]) EQ 0 $
1126     AND total(tmask[0, *, *]) EQ 0 AND total(tmask[jpi-1, *, *]) EQ 0 THEN BEGIN
1127    IF NOT keyword_set(key_periodic) THEN BEGIN
1128      xminmesh = ixminmesh + 1
1129      xmaxmesh = ixmaxmesh - 1
1130    ENDIF
1131    yminmesh = iyminmesh + 1
1132    ymaxmesh = iymaxmesh - 1
1133; come back to the original grid before calling computegrid with the
1134; new parameters...
1135    IF keyword_set(yreverse) THEN BEGIN
1136      gphit = reverse(gphit, 2)
1137      glamt = reverse(glamt, 2)
1138      tmask = reverse(tmask, 2)   
1139      IF jpk EQ 1 THEN tmask = reform(tmask, jpi, jpj, jpk, /over)
1140    ENDIF
1141    IF keyword_set(zreverse) THEN BEGIN
1142      zaxis = reverse(zaxis)
1143      tmask = reverse(tmask, 3)
1144      IF jpk EQ 1 THEN tmask = reform(tmask, jpi, jpj, jpk, /over)
1145    ENDIF
1146    IF key_shift NE 0 THEN BEGIN
1147      glamt = shift(glamt, -key_shift, 0)
1148      gphit = shift(gphit, -key_shift, 0)
1149      tmask = shift(tmask, -key_shift, 0, 0)
1150      IF jpk EQ 1 THEN tmask = reform(tmask, jpi, jpj, jpk, /over)
1151    ENDIF
1152    IF ixminmesh NE 0 THEN BEGIN
1153      glamt = [fltarr(ixminmesh, jpj), glamt]
1154      gphit = [fltarr(ixminmesh, jpj), gphit]
1155      tmask = [fltarr(ixminmesh, jpj), tmask]
1156      jpi = jpi+ixminmesh
1157      IF jpk EQ 1 THEN tmask = reform(tmask, jpi, jpj, jpk, /over)
1158    ENDIF
1159    IF ixmaxmesh NE jpiglo-1 THEN BEGIN
1160      glamt = [glamt, fltarr(jpiglo-1-ixmaxmesh, jpj)]
1161      gphit = [gphit, fltarr(jpiglo-1-ixmaxmesh, jpj)]
1162      tmask = [tmask, fltarr(jpiglo-1-ixmaxmesh, jpj)]
1163      jpi = jpi+jpiglo-1-ixmaxmesh
1164      IF jpk EQ 1 THEN tmask = reform(tmask, jpi, jpj, jpk, /over)
1165    ENDIF
1166    IF iyminmesh NE 0 THEN BEGIN
1167      glamt = [[fltarr(jpi, iyminmesh)], [glamt]]
1168      gphit = [[fltarr(jpi, iyminmesh)], [gphit]]
1169      tmask = [[fltarr(jpi, iyminmesh)], [tmask]]
1170      jpj = jpj+iyminmesh
1171      IF jpk EQ 1 THEN tmask = reform(tmask, jpi, jpj, jpk, /over)
1172    ENDIF
1173    IF iymaxmesh NE jpjglo-1 THEN BEGIN
1174      glamt = [[glamt], [fltarr(jpi, jpjglo-1-iymaxmesh)]]
1175      gphit = [[gphit], [fltarr(jpi, jpjglo-1-iymaxmesh)]]
1176      tmask = [[tmask], [fltarr(jpi, jpjglo-1-iymaxmesh)]]
1177      jpj = jpj+jpjglo-1-iymaxmesh
1178      IF jpk EQ 1 THEN tmask = reform(tmask, jpi, jpj, jpk, /over)
1179    ENDIF
1180    computegrid, XAXIS = glamt, YAXIS = gphit, ZAXIS = zaxis $
1181                 , MASK = tmask, GLAMBOUNDARY = glamboundary $
1182                 , XMINMESH = xminmesh, XMAXMESH = xmaxmesh $
1183                 , YMINMESH = yminmesh, YMAXMESH = ymaxmesh $
1184                 , ZMINMESH = zminmesh, ZMAXMESH = zmaxmesh $
1185                 , ONEARTH = onearth, PERIODIC = key_periodic $
1186                 , PLAIN = plain, SHIFT = key_shift, STRIDE = key_stride $
1187                 , FULLCGRID = fullcgrid, XYINDEX = xyindex $
1188                 , UBASE2TBASE = ubase2tbase, VBASE2TBASE = vbase2tbase $
1189                 , FBASE2TBASE = fbase2tbase, FFBASE2TBASE = ffbase2tbase, STRCALLING = strcalling $
1190                 , ROMSH = romsh, _extra = ex
1191    return
1192  ENDIF
1193;
1194  IF NOT keyword_set(fullcgrid) THEN BEGIN
1195    umaskred = !values.f_nan
1196    vmaskred = !values.f_nan
1197  ENDIF
1198;
1199;====================================================
1200; stride...
1201;====================================================
1202;
1203  IF total(key_stride) GT 3 THEN BEGIN
1204    IF key_shift NE 0 THEN BEGIN
1205; for explanation, see header of read_ncdf_varget.pro
1206      jpiright = key_shift
1207      jpileft = jpi - key_shift - ( (key_stride[0]-1)-((key_shift-1) MOD key_stride[0]) )
1208      jpi = ((jpiright-1)/key_stride[0]+1) + ((jpileft-1)/key_stride[0]+1)
1209    ENDIF ELSE jpi = (jpi-1)/key_stride[0]+1
1210    jpj = (jpj-1)/key_stride[1]+1
1211    jpk = (jpk-1)/key_stride[2]+1
1212;
1213    glamt = (temporary(glamt))[0:*:stride[0], 0:*:stride[1]]
1214    gphit = (temporary(gphit))[0:*:stride[0], 0:*:stride[1]]
1215    e1t = (temporary(e1t))[0:*:stride[0], 0:*:stride[1]]
1216    e2t = (temporary(e2t))[0:*:stride[0], 0:*:stride[1]]
1217    tmask = (temporary(tmask))[0:*:stride[0], 0:*:stride[1], 0:*:stride[2]]
1218    gdept = gdept[0:*:stride[2]]
1219    gdepw = gdepw[0:*:stride[2]]
1220    e3t = e3t[0:*:stride[2]]
1221    e3w = e3w[0:*:stride[2]]
1222; we must recompute glamf and gphif...
1223    IF jpi GT 1 THEN BEGIN
1224      if (keyword_set(key_onearth) AND keyword_set(xnotsorted)) $
1225        OR (keyword_set(key_periodic) AND key_irregular) then BEGIN
1226        stepxf = (glamt + 720) MOD 360
1227        stepxf = shift(stepxf, -1, -1) - stepxf
1228        stepxf = [ [[stepxf]], [[stepxf + 360]], [[stepxf - 360]] ]
1229        stepxf = min(abs(stepxf), dimension = 3)
1230        IF NOT keyword_set(key_periodic) THEN $
1231          stepxf[jpi-1, *] = stepxf[jpi-2, *]
1232      ENDIF ELSE BEGIN
1233        stepxf = shift(glamt, -1, -1) - glamt
1234        IF keyword_set(key_periodic) THEN $
1235          stepxf[jpi-1, *] = 360 + stepxf[jpi-1, *] $
1236          ELSE stepxf[jpi-1, *] = stepxf[jpi-2, *]
1237      ENDELSE
1238      IF jpj GT 1 THEN BEGIN
1239        stepxf[*, jpj-1] = stepxf[*, jpj-2]
1240        stepxf[jpi-1, jpj-1] = stepxf[jpi-2, jpj-2]
1241      ENDIF
1242      glamf = glamt + 0.5 * stepxf
1243    ENDIF ELSE glamf = glamt + 0.5
1244    IF keyword_set(key_periodic) AND (max(glamf)-min(glamt)) GE 360 THEN BEGIN
1245      IF NOT keyword_set(glamboundary) THEN BEGIN
1246        bigger = where(glamf GE min(glamt)+360)
1247        glamf[bigger] = glamf[bigger]-360.
1248      ENDIF ELSE glamf = glamboundary[0] > temporary(glamf) < glamboundary[1]
1249    ENDIF
1250    IF jpj GT 1 THEN BEGIN
1251; we must compute stepyf: y distance between T(i,j) T(i+1,j+1)
1252      stepyf = shift(gphit, -1, -1) - gphit
1253      stepyf[*, jpj-1] = stepyf[*, jpj-2]
1254      IF jpi GT 1 THEN BEGIN
1255        if NOT keyword_set(key_periodic) THEN $
1256          stepyf[jpi-1, *] = stepyf[jpi-2, *]
1257        stepyf[jpi-1, jpj-1] = stepyf[jpi-2, jpj-2]
1258      ENDIF
1259      gphif = gphit + 0.5 * stepyf
1260    ENDIF ELSE gphif = gphit + 0.5
1261;
1262    IF jpj EQ 1 THEN BEGIN
1263      glamt = reform(glamt, jpi, jpj, /over)
1264      gphit = reform(gphit, jpi, jpj, /over)
1265      glamf = reform(glamf, jpi, jpj, /over)
1266      gphif = reform(gphif, jpi, jpj, /over)
1267      e1t = reform(e1t, jpi, jpj, /over)
1268      e2t = reform(e2t, jpi, jpj, /over)
1269    ENDIF
1270;
1271    IF keyword_set(fullcgrid) THEN BEGIN
1272      glamu = (temporary(glamu))[0:*:stride[0], 0:*:stride[1]]
1273      gphiu = (temporary(gphiu))[0:*:stride[0], 0:*:stride[1]]
1274      e1u = (temporary(e1u))[0:*:stride[0], 0:*:stride[1]]
1275      e2u = (temporary(e2u))[0:*:stride[0], 0:*:stride[1]]
1276      glamv = (temporary(glamv))[0:*:stride[0], 0:*:stride[1]]
1277      gphiv = (temporary(gphiv))[0:*:stride[0], 0:*:stride[1]]
1278      e1v = (temporary(e1v))[0:*:stride[0], 0:*:stride[1]]
1279      e2v = (temporary(e2v))[0:*:stride[0], 0:*:stride[1]]
1280      e1f = (temporary(e1f))[0:*:stride[0], 0:*:stride[1]]
1281      e2f = (temporary(e2f))[0:*:stride[0], 0:*:stride[1]]
1282      umaskred = (temporary(umaskred))[0, 0:*:stride[1], 0:*:stride[2]]
1283      vmaskred = (temporary(vmaskred))[0:*:stride[0], 0, 0:*:stride[2]]
1284      fmaskredy = (temporary(fmaskredy))[0, 0:*:stride[1], 0:*:stride[2]]
1285      fmaskredx = (temporary(fmaskredx))[0:*:stride[0], 0, 0:*:stride[2]]
1286      IF jpj EQ 1 THEN BEGIN
1287        glamu = reform(glamu, jpi, jpj, /over)
1288        gphiu = reform(gphiu, jpi, jpj, /over)
1289        e1u = reform(e1u, jpi, jpj, /over)
1290        e2u = reform(e2u, jpi, jpj, /over)
1291        glamv = reform(glamv, jpi, jpj, /over)
1292        gphiv = reform(gphiv, jpi, jpj, /over)
1293        e1v = reform(e1v, jpi, jpj, /over)
1294        e2v = reform(e2v, jpi, jpj, /over)
1295        e1f = reform(e1f, jpi, jpj, /over)
1296        e2f = reform(e2f, jpi, jpj, /over)
1297      ENDIF
1298    ENDIF
1299  ENDIF
1300;
1301;====================================================
1302; apply all the grid parameters
1303;====================================================
1304;
1305  @updateold
1306  domdef
1307;
1308;====================================================
1309; Triangulation
1310;====================================================
1311;
1312  IF total(tmask) EQ jpi*jpj*jpk $
1313    AND NOT keyword_set(key_irregular) THEN triangles_list = -1 $
1314  ELSE BEGIN
1315; are we using ORCA2 ?
1316    IF jpiglo EQ 182 AND jpi EQ 180 AND jpjglo EQ 149 AND jpj EQ 148 THEN $
1317       triangles_list = triangule() ELSE triangles_list = triangule(/keep_cont)
1318  ENDELSE
1319;
1320;====================================================
1321; time axis (default definition)
1322;====================================================
1323;
1324  IF n_elements(time) EQ 0 OR n_elements(jpt) EQ 0 THEN BEGIN
1325    jpt = 1
1326    time = 0
1327  ENDIF
1328;
1329  IF NOT keyword_set(key_forgetold) THEN BEGIN
1330@updateold
1331  ENDIF
1332;====================================================
1333; grid parameters used by xxx
1334;====================================================
1335;
1336  IF NOT keyword_set(strcalling) THEN BEGIN
1337    IF n_elements(ccmeshparameters) EQ 0 THEN strcalling = 'computegrid' $
1338    ELSE strcalling = ccmeshparameters.filename
1339  ENDIF
1340  IF n_elements(glamt) GE 2 THEN BEGIN
1341    glaminfo = moment(glamt)
1342    IF finite(glaminfo[2]) EQ 0 THEN glaminfo = glaminfo[0:1]
1343    gphiinfo = moment(gphit)
1344    IF finite(gphiinfo[2]) EQ 0 THEN gphiinfo = gphiinfo[0:1]
1345  ENDIF ELSE BEGIN
1346    glaminfo = glamt
1347    gphiinfo = gphit
1348  ENDELSE
1349  IF keyword_set(romsh) THEN $
1350     romszinfos = {h:romsh[xmin:xmax, ymin:ymax], zeta:replicate(0., jpi, jpj), theta_s:-1, theta_b:-1, hc:-1} $
1351  ELSE romszinfos = {h:-1, zeta:-1, theta_s:-1, theta_b:-1, hc:-1}
1352
1353  ccmeshparameters = {filename:strcalling  $
1354          , glaminfo:float(string(glaminfo, format = '(E11.4)')) $
1355          , gphiinfo:float(string(gphiinfo, format = '(E11.4)')) $
1356          , jpiglo:jpiglo, jpjglo:jpjglo, jpkglo:jpkglo $
1357          , jpi:jpi, jpj:jpj, jpk:jpk $
1358          , ixminmesh:ixminmesh, ixmaxmesh:ixmaxmesh $
1359          , iyminmesh:iyminmesh, iymaxmesh:iymaxmesh $
1360          , izminmesh:izminmesh, izmaxmesh:izmaxmesh $
1361          , key_shift:key_shift, key_periodic:key_periodic $
1362          , key_stride:key_stride, key_gridtype:key_gridtype $
1363          , key_yreverse:key_yreverse, key_zreverse:key_zreverse $
1364          , key_partialstep:key_partialstep, key_onearth:key_onearth}
1365
1366  ccreadparameters = {funclec_name:'read_ncdf' $
1367          , jpidta:jpidta, jpjdta:jpjdta, jpkdta:jpkdta $
1368          , ixmindta:ixmindta, ixmaxdta:ixmaxdta $
1369          , iymindta:iymindta, iymaxdta:iymaxdta $
1370          , izmindta:izmindta, izmaxdta:izmaxdta}
1371;------------------------------------------------------------
1372  IF keyword_set(key_performance) EQ 1 THEN $
1373    print, 'time computegrid', systime(1)-time1
1374;------------------------------------------------------------
1375  return
1376end
Note: See TracBrowser for help on using the repository browser.