Home > utilities > do_volume.m

do_volume

PURPOSE ^

Calculate a volume for a given time step above a threshold value.

SYNOPSIS ^

function [Plots,totalVol]=do_volume(plotOPTS,FVCOM,startIdx,thresholdValue)

DESCRIPTION ^

 Calculate a volume for a given time step above a threshold value.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [Plots,totalVol]=do_volume(plotOPTS,FVCOM,startIdx,thresholdValue)
0002 % Calculate a volume for a given time step above a threshold value.
0003 m_mappath;
0004 
0005 [nx,nz,ttot]=size(FVCOM.(plotOPTS.var_plot));
0006 
0007 % Get the time step (in seconds)
0008 dt=round((plotOPTS.Time_record(2)-plotOPTS.Time_record(1))*24*60*60);
0009 
0010 % Sigma layer fraction (nz*nz)
0011 sigThickness=roundn(abs(diff(FVCOM.siglev,1,2)),-5); % roundn to even values out.
0012 % Total depth (nz*ttot)
0013 totalDepth=repmat(FVCOM.h,1,size(FVCOM.zeta,2))+FVCOM.zeta;
0014 % Volume for every element for each time step
0015 cellVolume=nan(nx,nz,ttot);
0016 for ii=1:nz
0017     for jj=1:ttot
0018         cellVolume(:,ii,jj)=totalDepth(:,jj).*sigThickness(:,ii).*FVCOM.art1*dt;
0019     end
0020 end
0021 
0022 if plotOPTS.nz_plot==0
0023     % Depth averaging the results
0024     cellVolume=squeeze(sum(cellVolume,2));
0025     dataArray=squeeze(mean(FVCOM.(plotOPTS.var_plot)(:,:,:),2));
0026 else
0027     dataArray=FVCOM.(plotOPTS.var_plot)(:,:,:);
0028 end
0029 
0030 %%
0031 % Calculate volume of elements matching threshold condition
0032 totalVolTemp=0;
0033 colourSpec=hsv(length(plotOPTS.nz_plot));
0034 plotSymbols={'.','o','x','+','^','*','p','h'};
0035 
0036 for tt=1:size(startIdx,2)
0037     
0038     foundXY=cell(1,length(plotOPTS.nz_plot));
0039     
0040     figure(plotOPTS.figure); clf
0041     m_proj('UTM','lon',[plotOPTS.range_lon],'lat',[plotOPTS.range_lat],'zon',plotOPTS.zone,'ell','grs80')
0042     m_grid('box','fancy')
0043     m_usercoast(plotOPTS.coastline_file,'Color','k','LineWidth',3);
0044     [X,Y]=m_ll2xy(plotOPTS.mesh.lon,plotOPTS.mesh.lat,'clip','on');
0045 
0046     fprintf('Time step %i of %i\n',startIdx(tt),length(plotOPTS.Time_record))
0047     % Find all values of pH greater than the thresholdValue for the layers
0048     % specified in .nz_plot only.
0049     
0050     for ss=1:size(plotOPTS.nz_plot,2)
0051         if plotOPTS.nz_plot==0
0052             % Depth averaged, so easy.
0053             idx=thresholdTest(dataArray(:,startIdx(tt)),plotOPTS.volume_thresh,thresholdValue);
0054             % Do we have any locations where the condition has been met?
0055             if ~isempty(idx)
0056                 totalVolTemp=totalVolTemp+sum(sum(cellVolume(idx,startIdx(tt))));
0057             end
0058         else
0059             % Loop through the layers
0060             for qq=1:size(plotOPTS.nz_plot,2)
0061                 idx=thresholdTest(dataArray(:,plotOPTS.nz_plot(qq),startIdx(tt)),plotOPTS.volume_thresh,thresholdValue);
0062                 % Again, look for areas where we have met the threshold
0063                 % condition. Here, we need to save the xy coordinates for
0064                 % each find with the layer number.
0065                 if ~isempty(idx)
0066                     totalVolTemp=totalVolTemp+sum(sum(cellVolume(idx,plotOPTS.nz_plot(qq),startIdx(tt))));
0067                     % This may skip the odd layer...
0068                     foundXY{qq}.xy=[X(idx),Y(idx),repmat(plotOPTS.nz_plot(qq),length(idx),1)];
0069                 end
0070             end
0071         end
0072     end
0073     if tt==1;
0074         totalVol(tt)=totalVolTemp;
0075     else
0076         totalVol(tt)=totalVol(tt-1)+totalVolTemp;
0077     end
0078     % Do a figure identifying the locations above the threshold for the
0079     % surface sigma layer.
0080     if plotOPTS.nz_plot==0
0081         Plots(plotOPTS.figure).handles=patch('Vertices',[X,Y],'Faces',...
0082             plotOPTS.mesh.tri,'Cdata',squeeze(cellVolume(:,tt)),...
0083             'edgecolor','interp','facecolor','interp');
0084     else
0085             Plots(plotOPTS.figure).handles=patch('Vertices',[X,Y],'Faces',...
0086         plotOPTS.mesh.tri,'Cdata',squeeze(cellVolume(:,1,tt)),...
0087         'edgecolor','interp','facecolor','interp');
0088     end
0089     hold on
0090     % TODO: For multiple layers, do unique indices for each layer so they
0091     % can be plotted separately here (a la do_vector_plot.m).
0092     if ~isempty(idx)
0093         for kk=1:size(plotOPTS.nz_plot,2);
0094             plot(foundXY{kk}.xy(:,1),foundXY{kk}.xy(:,2),plotSymbols{mod(kk,length(plotSymbols)+1)},'Color',colourSpec(kk,:))
0095         end
0096     end
0097     if plotOPTS.do_mesh
0098         % plot vertices
0099         [X,Y]=m_ll2xy(plotOPTS.mesh.lon,plotOPTS.mesh.lat,'clip','on');
0100         patch('Vertices',[X,Y],'Faces',plotOPTS.mesh.tri,...
0101             'EdgeColor',[0.6 0.6 0.6],'FaceColor','none'); hold on
0102     end
0103     % Some useful text
0104     [textX,textY]=m_ll2xy(-4.45,50.13);
0105     text(textX,textY,sprintf('Total volume:\t\t%.2fkm^{3}\nVolume this time step:\t%.2fkm^{3}\n',totalVol(tt)/1e9,totalVolTemp/1e9))
0106         
0107     pause(plotOPTS.pause)
0108     caxis(plotOPTS.clims)
0109     colorbar
0110     set(get(colorbar,'YLabel'),'String','Volume (m^{3})')
0111     
0112     if tt~=size(startIdx,2)
0113         delete(Plots(plotOPTS.figure).handles)
0114     end
0115 end
0116 
0117 function idx=thresholdTest(dataArray,thresholdType,thresholdValue)
0118 % Abstract some of the complexity into a function.
0119 % Usage: thresholdTest(inputData,thresholdValue)
0120     switch thresholdType
0121         case -1
0122             idx=find(dataArray<thresholdValue);
0123         case 1
0124             idx=find(dataArray>thresholdValue);
0125         case 0
0126             idx=find(dataArray==thresholdValue);
0127         otherwise
0128             error('Unrecognised value for ''plotOPTS.volume_thresh''.')
0129     end
0130 
0131     return

Generated on Thu 19-Mar-2015 12:20:56 by m2html © 2005