1 |
% |
2 |
% [RHO] = compute_density(SNAPSHOT) |
3 |
% |
4 |
% For a time snapshot, this program computes the |
5 |
% 3D density from potential temperature and salinity fields. |
6 |
% THETA and SALTanom are supposed to be defined on the same |
7 |
% domain and grid. |
8 |
% SALTanom is by default a salinity anomaly vs 35PSU. |
9 |
% If not, (is absolute value) set the global variable is_SALTanom to 0 |
10 |
% |
11 |
% |
12 |
% Files names are: |
13 |
% INPUT: |
14 |
% ./netcdf-files/<SNAPSHOT>/<netcdf_THETA>.<netcdf_domain>.<netcdf_suff> |
15 |
% ./netcdf-files/<SNAPSHOT>/<netcdf_SALTanom>.<netcdf_domain>.<netcdf_suff> |
16 |
% OUPUT: |
17 |
% ./netcdf-files/<SNAPSHOT>/RHO.<netcdf_domain>.<netcdf_suff> |
18 |
% |
19 |
% 06/21/2006 |
20 |
% gmaze@mit.edu |
21 |
% |
22 |
|
23 |
|
24 |
function varargout = compute_density(snapshot) |
25 |
|
26 |
|
27 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
28 |
%% Setup |
29 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
30 |
global sla netcdf_THETA netcdf_SALTanom netcdf_domain netcdf_suff |
31 |
global is_SALTanom |
32 |
pv_checkpath |
33 |
|
34 |
|
35 |
%% THETA and SALTanom files name: |
36 |
filTHETA = strcat(netcdf_THETA ,'.',netcdf_domain); |
37 |
filSALTa = strcat(netcdf_SALTanom,'.',netcdf_domain); |
38 |
|
39 |
%% Path and extension to find them: |
40 |
pathname = strcat('netcdf-files',sla,snapshot); |
41 |
%pathname = '.'; |
42 |
ext = strcat('.',netcdf_suff); |
43 |
|
44 |
%% Load netcdf files: |
45 |
ferfile = strcat(pathname,sla,filTHETA,ext); |
46 |
ncTHETA = netcdf(ferfile,'nowrite'); |
47 |
THETAvariables = var(ncTHETA); |
48 |
|
49 |
ferfile = strcat(pathname,sla,filSALTa,ext); |
50 |
ncSALTa = netcdf(ferfile,'nowrite'); |
51 |
SALTavariables = var(ncSALTa); |
52 |
|
53 |
%% Gridding: |
54 |
% Don't care about the grid here ! |
55 |
% SALTanom and THETA are normaly defined on the same grid |
56 |
% So we compute rho on it. |
57 |
|
58 |
%% Flags: |
59 |
global toshow % Turn to 1 to follow the computing process |
60 |
|
61 |
|
62 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
63 |
%% Now we compute the density |
64 |
%% The routine used is densjmd95.m |
65 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
66 |
|
67 |
% Axis (usual netcdf files): |
68 |
if toshow,disp('Dim');end |
69 |
[lon lat dpt] = coordfromnc(ncTHETA); |
70 |
nx = length(lon); |
71 |
ny = length(lat); |
72 |
nz = length(dpt); |
73 |
|
74 |
% Pre-allocate: |
75 |
if toshow,disp('Pre-allocate');end |
76 |
RHO = zeros(nz,ny,nx); |
77 |
|
78 |
global is_SALTanom |
79 |
if exist('is_SALTanom') |
80 |
if is_SALTanom == 1 |
81 |
bS = 35; |
82 |
else |
83 |
bS = 0; |
84 |
end |
85 |
end |
86 |
|
87 |
% Then compute density RHO: |
88 |
for iz = 1 : nz |
89 |
if toshow,disp(strcat('Compute density at level:',num2str(iz),'/',num2str(nz)));end |
90 |
|
91 |
S = SALTavariables{4}(iz,:,:) + bS; % Move the anom to an absolute field |
92 |
T = THETAvariables{4}(iz,:,:); |
93 |
P = (0.09998*9.81*dpt(iz))*ones(ny,nx); |
94 |
RHO(iz,:,:) = densjmd95(S,T,P); |
95 |
|
96 |
end %for iz |
97 |
|
98 |
|
99 |
|
100 |
|
101 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
102 |
%% Record output: |
103 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
104 |
|
105 |
% General informations: |
106 |
netfil = strcat('RHO','.',netcdf_domain,'.',netcdf_suff); |
107 |
units = 'kg/m^3'; |
108 |
ncid = 'RHO'; |
109 |
longname = 'Density'; |
110 |
uniquename = 'density'; |
111 |
|
112 |
% Open output file: |
113 |
nc = netcdf(strcat(pathname,sla,netfil),'clobber'); |
114 |
|
115 |
% Define axis: |
116 |
nc('X') = nx; |
117 |
nc('Y') = ny; |
118 |
nc('Z') = nz; |
119 |
|
120 |
nc{'X'} = 'X'; |
121 |
nc{'Y'} = 'Y'; |
122 |
nc{'Z'} = 'Z'; |
123 |
|
124 |
nc{'X'} = ncfloat('X'); |
125 |
nc{'X'}.uniquename = ncchar('X'); |
126 |
nc{'X'}.long_name = ncchar('longitude'); |
127 |
nc{'X'}.gridtype = nclong(0); |
128 |
nc{'X'}.units = ncchar('degrees_east'); |
129 |
nc{'X'}(:) = lon; |
130 |
|
131 |
nc{'Y'} = ncfloat('Y'); |
132 |
nc{'Y'}.uniquename = ncchar('Y'); |
133 |
nc{'Y'}.long_name = ncchar('latitude'); |
134 |
nc{'Y'}.gridtype = nclong(0); |
135 |
nc{'Y'}.units = ncchar('degrees_north'); |
136 |
nc{'Y'}(:) = lat; |
137 |
|
138 |
nc{'Z'} = ncfloat('Z'); |
139 |
nc{'Z'}.uniquename = ncchar('Z'); |
140 |
nc{'Z'}.long_name = ncchar('depth'); |
141 |
nc{'Z'}.gridtype = nclong(0); |
142 |
nc{'Z'}.units = ncchar('m'); |
143 |
nc{'Z'}(:) = dpt; |
144 |
|
145 |
% And main field: |
146 |
nc{ncid} = ncfloat('Z', 'Y', 'X'); |
147 |
nc{ncid}.units = ncchar(units); |
148 |
nc{ncid}.missing_value = ncfloat(NaN); |
149 |
nc{ncid}.FillValue_ = ncfloat(NaN); |
150 |
nc{ncid}.longname = ncchar(longname); |
151 |
nc{ncid}.uniquename = ncchar(uniquename); |
152 |
nc{ncid}(:,:,:) = RHO; |
153 |
|
154 |
|
155 |
|
156 |
% Close files: |
157 |
close(ncTHETA); |
158 |
close(ncSALTa); |
159 |
close(nc); |
160 |
|
161 |
|
162 |
% Output: |
163 |
output = struct('RHO',RHO,'dpt',dpt,'lat',lat,'lon',lon); |
164 |
switch nargout |
165 |
case 1 |
166 |
varargout(1) = {output}; |
167 |
end |