1 |
% |
2 |
% [] = A_COMPUTE_POTENTIAL_DENSITY(SNAPSHOT) |
3 |
% |
4 |
% For a time snapshot, this program computes the |
5 |
% 3D potential density from potential temperature and salinity. |
6 |
% THETA and SALTanom are supposed to be defined on the same |
7 |
% domain and grid. |
8 |
% |
9 |
% 06/07/2006 |
10 |
% gmaze@mit.edu |
11 |
% |
12 |
|
13 |
|
14 |
function A_compute_potential_density(snapshot) |
15 |
|
16 |
|
17 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
18 |
%% Setup |
19 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
20 |
global sla netcdf_THETA netcdf_SALTanom netcdf_domain netcdf_suff |
21 |
pv_checkpath |
22 |
|
23 |
|
24 |
%% THETA and SALTanom files name: |
25 |
filTHETA = strcat(netcdf_THETA ,'.',netcdf_domain); |
26 |
filSALTa = strcat(netcdf_SALTanom,'.',netcdf_domain); |
27 |
|
28 |
%% Path and extension to find them: |
29 |
pathname = strcat('netcdf-files',sla,snapshot); |
30 |
ext = strcat('.',netcdf_suff); |
31 |
|
32 |
%% Load netcdf files: |
33 |
ferfile = strcat(pathname,sla,filTHETA,ext); |
34 |
ncTHETA = netcdf(ferfile,'nowrite'); |
35 |
THETAvariables = var(ncTHETA); |
36 |
|
37 |
ferfile = strcat(pathname,sla,filSALTa,ext); |
38 |
ncSALTa = netcdf(ferfile,'nowrite'); |
39 |
SALTavariables = var(ncSALTa); |
40 |
|
41 |
%% Gridding: |
42 |
% Don't care about the grid here ! |
43 |
% SALTanom and THETA are normaly defined on the same grid |
44 |
% So we compute sigma_theta on it. |
45 |
|
46 |
%% Flags: |
47 |
global toshow % Turn to 1 to follow the computing process |
48 |
|
49 |
|
50 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
51 |
%% Now we compute potential density |
52 |
%% The routine used is densjmd95.m |
53 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
54 |
|
55 |
% Axis (usual netcdf files): |
56 |
if toshow,disp('Dim');end |
57 |
[lon lat dpt] = coordfromnc(ncTHETA); |
58 |
nx = length(lon); |
59 |
ny = length(lat); |
60 |
nz = length(dpt); |
61 |
|
62 |
% Pre-allocate: |
63 |
if toshow,disp('Pre-allocate');end |
64 |
SIGMATHETA = zeros(nz,ny,nx); |
65 |
|
66 |
% Then compute potential density SIGMATHETA: |
67 |
for iz = 1 : nz |
68 |
if toshow,disp(strcat('Compute potential density at level:',num2str(iz),'/',num2str(nz)));end |
69 |
|
70 |
S = SALTavariables{4}(iz,:,:) + 35; % Move the anom to an absolute field |
71 |
T = THETAvariables{4}(iz,:,:); |
72 |
SIGMATHETA(iz,:,:) = densjmd95(S,T,zeros(ny,nx)) - 1000; |
73 |
|
74 |
% Eventualy make a plot of the field: |
75 |
if 0 |
76 |
clf;pcolor(squeeze(SIGMATHETA(iz,:,:))); |
77 |
shading interp;caxis([10 40]);colorbar |
78 |
drawnow |
79 |
M(iz)=getframe; % To make a video |
80 |
end %if1 |
81 |
end %for iz |
82 |
|
83 |
|
84 |
|
85 |
|
86 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
87 |
%% Record output: |
88 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
89 |
|
90 |
% General informations: |
91 |
netfil = strcat('SIGMATHETA','.',netcdf_domain,'.',netcdf_suff); |
92 |
units = 'kg/m^3-1000'; |
93 |
ncid = 'ST'; |
94 |
longname = 'Potential Density'; |
95 |
uniquename = 'potential_density'; |
96 |
|
97 |
% Open output file: |
98 |
nc = netcdf(strcat(pathname,sla,netfil),'clobber'); |
99 |
|
100 |
% Define axis: |
101 |
nc('X') = nx; |
102 |
nc('Y') = ny; |
103 |
nc('Z') = nz; |
104 |
|
105 |
nc{'X'} = 'X'; |
106 |
nc{'Y'} = 'Y'; |
107 |
nc{'Z'} = 'Z'; |
108 |
|
109 |
nc{'X'} = ncfloat('X'); |
110 |
nc{'X'}.uniquename = ncchar('X'); |
111 |
nc{'X'}.long_name = ncchar('longitude'); |
112 |
nc{'X'}.gridtype = nclong(0); |
113 |
nc{'X'}.units = ncchar('degrees_east'); |
114 |
nc{'X'}(:) = lon; |
115 |
|
116 |
nc{'Y'} = ncfloat('Y'); |
117 |
nc{'Y'}.uniquename = ncchar('Y'); |
118 |
nc{'Y'}.long_name = ncchar('latitude'); |
119 |
nc{'Y'}.gridtype = nclong(0); |
120 |
nc{'Y'}.units = ncchar('degrees_north'); |
121 |
nc{'Y'}(:) = lat; |
122 |
|
123 |
nc{'Z'} = ncfloat('Z'); |
124 |
nc{'Z'}.uniquename = ncchar('Z'); |
125 |
nc{'Z'}.long_name = ncchar('depth'); |
126 |
nc{'Z'}.gridtype = nclong(0); |
127 |
nc{'Z'}.units = ncchar('m'); |
128 |
nc{'Z'}(:) = dpt; |
129 |
|
130 |
% And main field: |
131 |
nc{ncid} = ncfloat('Z', 'Y', 'X'); |
132 |
nc{ncid}.units = ncchar(units); |
133 |
nc{ncid}.missing_value = ncfloat(NaN); |
134 |
nc{ncid}.FillValue_ = ncfloat(NaN); |
135 |
nc{ncid}.longname = ncchar(longname); |
136 |
nc{ncid}.uniquename = ncchar(uniquename); |
137 |
nc{ncid}(:,:,:) = SIGMATHETA; |
138 |
|
139 |
nc=close(nc); |
140 |
|