/[MITgcm]/MITgcm_contrib/gmaze_pv/B_compute_relative_vorticity.m
ViewVC logotype

Diff of /MITgcm_contrib/gmaze_pv/B_compute_relative_vorticity.m

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.3 by gmaze, Tue Jan 30 22:10:10 2007 UTC revision 1.4 by gmaze, Thu Feb 1 17:02:02 2007 UTC
# Line 6  Line 6 
6  % horizontal speed fields U,V (x,y,z) as:  % horizontal speed fields U,V (x,y,z) as:
7  % OMEGA = ( -dVdz ; dUdz ; dVdx - dUdy )  % OMEGA = ( -dVdz ; dUdz ; dVdx - dUdy )
8  %       = (   Ox  ;  Oy  ;     ZETA    )  %       = (   Ox  ;  Oy  ;     ZETA    )
9  % 3 output files are created.  % 3 outputs files are created.
10    %
11    % (U,V) must have same dimensions and by default are defined on
12    % a C-grid.
13    % If (U,V) are defined on an A-grid (coming from a cube-sphere
14    % to lat/lon grid interpolation for example), ie at the same points
15    % as THETA, SALTanom, ... the global variable 'griddef' must
16    % be set to 'A-grid'. Then (U,V) are moved to a C-grid for the computation.
17    %
18    % ZETA is computed at the upper-right corner of the C-grid.
19    % OMEGAX and OMEGAY are computed at V and U locations but shifted downward
20    % by 1/2 grid. In case of a A-grid for (U,V), OMEGAX and OMEGAY are moved
21    % to a C-grid according to the ZETA computation.
22    %
23  %  %
24  % Files names are:  % Files names are:
25  % INPUT:  % INPUT:
# Line 17  Line 30 
30  % ./netcdf-files/<SNAPSHOT>/OMEGAY.<netcdf_domain>.<netcdf_suff>  % ./netcdf-files/<SNAPSHOT>/OMEGAY.<netcdf_domain>.<netcdf_suff>
31  % ./netcdf-files/<SNAPSHOT>/ZETA.<netcdf_domain>.<netcdf_suff>  % ./netcdf-files/<SNAPSHOT>/ZETA.<netcdf_domain>.<netcdf_suff>
32  %  %
33  % 06/07/2006  % 2006/06/07
34  % gmaze@mit.edu  % gmaze@mit.edu
35  %  %
36    % Last update:
37    % 2007/02/01 (gmaze) : Fix bug in ZETA grid and add compatibility with A-grid
38    %
39        
40  function [] = B_compute_relative_vorticity(snapshot)  % On the C-grid, U and V are supposed to have the same dimensions and are
41    % defined like this:
42    %
43    %  y
44    %  ^      -------------------------
45    %  |      |     |     |     |     |
46    %  | ny   U  *  U  *  U  *  U  *  |
47    %  |      |     |     |     |     |
48    %  |   ny -- V --- V --- V --- V --
49    %  |      |     |     |     |     |
50    %  |      U  *  U  *  U  *  U  *  |
51    %  |      |     |     |     |     |
52    %  |      -- V --- V --- V --- V --
53    %  |      |     |     |     |     |
54    %  |      U  *  U  *  U  *  U  *  |
55    %  |      |     |     |     |     |
56    %  |      -- V --- V --- V --- V --
57    %  |      |     |     |     |     |
58    %  |  1   U  *  U  *  U  *  U  *  |
59    %  |      |     |     |     |     |
60    %  |    1 -- V --- V --- V --- V --
61    %  |      
62    %  |      1                 nx
63    %  |         1                 nx
64    %--|-------------------------------------> x
65    %  |
66    %
67    % On the A-grid, U and V are defined on *, so we simply shift U westward by 1/2 grid
68    % and V southward by 1/2 grid. New (U,V) have the same dimensions as original fields
69    % but with first col for U, and first row for V set to NaN. Values are computed by
70    % averaging two contiguous values.
71    %
72    
73    function varargout = B_compute_relative_vorticity(snapshot)
74    
75    
76  %%%%%%%%%%%%%%%%%%%%%%%%%%%%  %%%%%%%%%%%%%%%%%%%%%%%%%%%%
77  % Setup  % Setup
78  %%%%%%%%%%%%%%%%%%%%%%%%%%%%  %%%%%%%%%%%%%%%%%%%%%%%%%%%%
79  global sla netcdf_UVEL netcdf_VVEL netcdf_domain netcdf_suff  global sla netcdf_UVEL netcdf_VVEL netcdf_domain netcdf_suff griddef
80  pv_checkpath  pv_checkpath
81    
82    
# Line 41  pathname = strcat('netcdf-files',sla,sna Line 90  pathname = strcat('netcdf-files',sla,sna
90  ext      = strcat('.',netcdf_suff);  ext      = strcat('.',netcdf_suff);
91    
92    
93  %% Load files:  %% Load files and axis:
94  ferfile          = strcat(pathname,sla,filU,ext);  ferfile          = strcat(pathname,sla,filU,ext);
95  ncU              = netcdf(ferfile,'nowrite');  ncU              = netcdf(ferfile,'nowrite');
96  [Ulon Ulat Udpt] = coordfromnc(ncU);  [Ulon Ulat Udpt] = coordfromnc(ncU);
# Line 52  ncV              = netcdf(ferfile,'nowri Line 101  ncV              = netcdf(ferfile,'nowri
101    
102  clear ext ferfile  clear ext ferfile
103    
104    %% Load grid definition:
105    global griddef
106    if length(griddef) == 0
107      griddef = 'C-grid'; % By default
108    end
109    switch lower(griddef)
110     case {'c-grid','cgrid','c'}
111        % Nothing to do here
112     case {'a-grid','agrid','a'}
113        disp('Found (U,V) defined on A-grid')
114        % Move Ulon westward by 1/2 grid point:
115         Ulon = [Ulon(1)-abs(diff(Ulon(1:2))/2) ; (Ulon(1:end-1)+Ulon(2:end))/2];
116        % Move V southward by 1/2 grid point:
117         Vlat = [Vlat(1)-abs(diff(Vlat(1:2))/2); (Vlat(1:end-1)+Vlat(2:end))/2];
118        % Now, (U,V) axis are defined as if they came from a C-grid
119        % (U,V) fields are moved to a C-grid during computation...
120     otherwise
121        error('The grid must be: C-grid or A-grid');
122        return
123    end %switch griddef
124      
125      
126  %% Optionnal flags  %% Optionnal flags
127  computeZETA = 1; % Compute ZETA or not ?  computeZETA = 1; % Compute ZETA or not ?
128  global toshow % Turn to 1 to follow the computing process  global toshow % Turn to 1 to follow the computing process
# Line 73  if toshow,disp('Dim'),end Line 144  if toshow,disp('Dim'),end
144    ny = length(Ulat)-1;    ny = length(Ulat)-1;
145    nx = length(Vlon)-1;    nx = length(Vlon)-1;
146    nz = length(Udpt); % Note that Udpt=Vdpt    nz = length(Udpt); % Note that Udpt=Vdpt
   ZETA_lon = Ulon(1:nx);  
   ZETA_lat = Vlat(1:ny);  
147        
148  %%%%%%%%%%%%%%  %%%%%%%%%%%%%%
149  %% Pre-allocation:  %% Pre-allocation:
# Line 83  ZETA = zeros(nz,ny-1,nx-1).*NaN; Line 152  ZETA = zeros(nz,ny-1,nx-1).*NaN;
152  dx   = zeros(ny-1,nx-1);  dx   = zeros(ny-1,nx-1);
153  dy   = zeros(ny-1,nx-1);  dy   = zeros(ny-1,nx-1);
154    
155    ZETA_lon = Ulon(2:nx+1);
156    ZETA_lat = Vlat(2:ny+1);
157    
158  %%%%%%%%%%%%%%  %%%%%%%%%%%%%%
159  %% Compute relative vorticity for each z-level:  %% Compute relative vorticity for each z-level:
160  if computeZETA  if computeZETA
161  for iz=1:nz  for iz = 1 : nz
162    if toshow    if toshow
163      disp(strcat('Computing \zeta at depth : ',num2str(Udpt(iz)),...      disp(strcat('Computing \zeta at depth : ',num2str(Udpt(iz)),...
164                  'm (',num2str(iz),'/',num2str(nz),')'   ));                  'm (',num2str(iz),'/',num2str(nz),')'   ));
# Line 95  for iz=1:nz Line 167  for iz=1:nz
167    % Get velocities:    % Get velocities:
168    U = ncU{4}(iz,:,:);    U = ncU{4}(iz,:,:);
169    V = ncV{4}(iz,:,:);    V = ncV{4}(iz,:,:);
170      switch lower(griddef)
171       case {'a-grid','agrid','a'}
172        % Move U westward by 1/2 grid point:
173        % (1st col is set to nan, but axis defined)
174        U = [ones(ny+1,1).*NaN  (U(:,1:end-1) + U(:,2:end))/2];
175        % Move V southward by 1/2 grid point:
176        % (1st row is set to nan but axis defined)
177        V = [ones(1,nx+1).*NaN;  (V(1:end-1,:) + V(2:end,:))/2];
178        % Now, U and V are defined as if they came from a C-grid
179      end  
180        
181    % And now compute the vertical component of relative vorticity:    % And now compute the vertical component of relative vorticity:
182    % (TO DO: m_lldist accepts tables as input, so this part may be    % (TO DO: m_lldist accepts tables as input, so this part may be
# Line 113  for iz=1:nz Line 195  for iz=1:nz
195        ZETA(iz,iy,ix) = dVdx - dUdy;              ZETA(iz,iy,ix) = dVdx - dUdy;      
196      end %for ix      end %for ix
197    end %for iy    end %for iy
   
198  end %for iz  end %for iz
199    
200  %%%%%%%%%%%%%%  %%%%%%%%%%%%%%
# Line 197  Ox = zeros(O_nz,O_ny(1),O_nx(1)).*NaN; Line 278  Ox = zeros(O_nz,O_ny(1),O_nx(1)).*NaN;
278  Oy = zeros(O_nz,O_ny(2),O_nx(2)).*NaN;  Oy = zeros(O_nz,O_ny(2),O_nx(2)).*NaN;
279    
280  %%%%%%%%%%%%%%  %%%%%%%%%%%%%%
281  %% Horizontal components:  %% Computation:
282    
283  %% Vertical grid differences:  %% Vertical grid differences:
284  dZ   = diff(Udpt);  dZ   = diff(Udpt);
# Line 206  Odpt = Udpt(1:O_nz) + dZ/2; Line 287  Odpt = Udpt(1:O_nz) + dZ/2;
287  %% Zonal component of OMEGA:  %% Zonal component of OMEGA:
288  if toshow,disp('Zonal direction ...'); end  if toshow,disp('Zonal direction ...'); end
289  [a dZ_3D c] = meshgrid(Vlat,dZ,Vlon); clear a c  [a dZ_3D c] = meshgrid(Vlat,dZ,Vlon); clear a c
290            V = ncV{4}(:,:,:);  V = ncV{4}(:,:,:);
291           Ox = - ( V(2:O_nz+1,:,:) - V(1:O_nz,:,:) ) ./ dZ_3D;  switch lower(griddef)
292       case {'a-grid','agrid','a'}
293        % Move V southward by 1/2 grid point:
294        % (1st row is set to nan but axis defined)
295        V = cat(2,ones(O_nz+1,1,O_nx(1)).*NaN,(V(:,1:end-1,:) + V(:,2:end,:))/2);
296        % Now, V is defined as if it came from a C-grid
297    end
298    Ox = - ( V(2:O_nz+1,:,:) - V(1:O_nz,:,:) ) ./ dZ_3D;
299  clear V dZ_3D % For memory use  clear V dZ_3D % For memory use
300    
301  %% Meridional component of OMEGA:  %% Meridional component of OMEGA:
302  if toshow,disp('Meridional direction ...'); end  if toshow,disp('Meridional direction ...'); end
303  [a dZ_3D c] = meshgrid(Ulat,dZ,Ulon); clear a c  [a dZ_3D c] = meshgrid(Ulat,dZ,Ulon); clear a c
304            U = ncU{4}(:,:,:);  U = ncU{4}(:,:,:);
305           Oy = ( U(2:O_nz+1,:,:) - U(1:O_nz,:,:) ) ./ dZ_3D;  switch lower(griddef)
306       case {'a-grid','agrid','a'}
307        % Move U westward by 1/2 grid point:
308        % (1st col is set to nan, but axis defined)
309        U = cat(3,ones(O_nz+1,O_ny(2),1).*NaN,(U(:,:,1:end-1) + U(:,:,2:end))/2);
310        % Now, V is defined as if it came from a C-grid
311    end  
312    Oy = ( U(2:O_nz+1,:,:) - U(1:O_nz,:,:) ) ./ dZ_3D;
313  clear U dZ_3D % For memory use  clear U dZ_3D % For memory use
314    
315  clear dZ  clear dZ
316    
317    
318  %%%%%%%%%%%%%%  %%%%%%%%%%%%%%
319  %% Record Zonal component:  %% Record Zonal component:
320  if toshow,disp('Records ...'); end  if toshow,disp('Records ...'); end
# Line 326  nc{ncid}.uniquename    = ncchar(uniquena Line 422  nc{ncid}.uniquename    = ncchar(uniquena
422  nc{ncid}(:,:,:)        = Oy;  nc{ncid}(:,:,:)        = Oy;
423    
424  nc=close(nc);  nc=close(nc);
425    close(ncU);
426    close(ncV);
427    
428  % Outputs:  % Outputs:
429  OMEGA = struct(...  OMEGA = struct(...
# Line 336  OMEGA = struct(... Line 433  OMEGA = struct(...
433      );      );
434  switch nargout  switch nargout
435   case 1   case 1
436    varargout(1) = OMEGA;    varargout(1) = {OMEGA};
437  end  end

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.4

  ViewVC Help
Powered by ViewVC 1.1.22