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