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

Last change on this file since 413 was 399, checked in by smasson, 15 years ago

wrong bugfix in previous changeset:398

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