Skip to content

Instantly share code, notes, and snippets.

@briochemc
Last active November 17, 2018 01:20
Show Gist options
  • Save briochemc/b7d2a9d46ba361c0141bff9e33dc7bdf to your computer and use it in GitHub Desktop.
Save briochemc/b7d2a9d46ba361c0141bff9e33dc7bdf to your computer and use it in GitHub Desktop.
% The following takes a 3D matrix and averages across depths
% You want to see what the z depth are?
% I would suggest following Seth John's advice: use explcit names and capitals to show big 3d things
DEPTH = z ;
depth = squeeze(DEPTH(1,1,:)) ;
itoplayers = find(depth < 55) ; % this creates a range of the indices for depth less than (i.e., above) 55m
% Here this range is 0m–50m. Check the values before to make sure:
depths_top = depth(itoplayers) ; % These are the depth levels [0, 10, 20, 30, 40, 50]
max_depths_top = max(depths_top) ; % This is the max depth of that range (50m)
min_depths_top = min(depths_top) ; % This is the min depth of that range (0m)
% Average of the d15N for z from max_depths_top to min_depths_top:
d15NO3_topavg = trapz(depths_top, d15NO3(:,:,itoplayers), 3) / (max_depths_top - min_depths_top);
d15NO3_bot = d15NO3(:,:,11) ; %Creates a 2D matrix of nitrate d15N at z level 11 (250 m)
% Now, to subtract these from each other
d15NO3_topavg_minus_bot = d15NO3_topavg - d15NO3_bot ; %Subtracts the above from each other
% Creating a 3 vector file of the above data?for use with MS Excel
lon_map = lon(:,:,1) ; % Creates a 2D matrix of the longitude
lon_xy = lon_map(:) ; % Transforms the 2D matrix into a vector
lat_map = lat(:,:,1) ; % Creates a 2D matrix of the latitude
lat_xy = lat_map(:) ; % Transforms the 2D matrix into a vector
data_xy = d15NO3_topavg_minus_bot(:) ; % Transforms the 2D matrix into a vector
d15NO3_topavg_minus_bot_xydata = [lon_xy, lat_xy, data_xy] ; % Creates the 3 vectors needed for the MS Excel format
iNaN = find(isnan(d15NO3_topavg_minus_bot_xydata)) ; % Creates an index of where the NaNs are in the above vector (data_xy)
d15NO3_topavg_minus_bot_xydata(iNaN) = -999 ; % Replaces the NaNs that were found above with -999
csvwrite('mycooldata.csv', d15NO3_topavg_minus_bot_xydata) % Creates a "cool" file that is comma separated values
% Delete the nans
iNan_data_xy = find(isnan(data_xy)) ; % find the indices of NaNs in data_xy
d15NO3_topavg_minus_bot_xydata_noNans = d15NO3_topavg_minus_bot_xydata ; % Copy the whole thing
d15NO3_topavg_minus_bot_xydata_noNans(iNan_data_xy,:) = [] ; % remove the rows that have a data value equal to NaN
csvwrite('mycooldata_noNans.csv', d15NO3_topavg_minus_bot_xydata_noNans) % Creates a "cool" file that is comma separated values with no nans
izbot = find(z(1,1,:)>95 & z(1,1,:)<255) ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment