Matlab+code

=Here we (together) build the code for Matlab to do the homework.=

Data Import: use this ncload.m function

code format="matlab" function [data] = ncload(filename,varname)

%NCLOAD % Load netcdf file. Use ncdump 'filename' to determine varnames within % netcdf prior to calling.

ncid=netcdf.open(filename,'NC_NOWRITE'); varid=netcdf.inqvarid(ncid,varname); data=netcdf.getvar(ncid,varid); end code

Simple averages
===Averaging over months & plotting result R(lat,lon)===

uwnd_timemean=squeeze(nanmean(uwnd,3)); %(144,73)

contourf(lon,lat,uwnd_timemean'); colorbar xlabel('Longitude') ylabel('Latitude') title('Time mean of uwnd') %Can make this MUCH fancier with coastlines, map projections etc. %And also the m_map toolbox that can be downloaded at: http://www.eos.ubc.ca/~rich/map.html.

===Averaging over longitude & plotting result R(lat,t)===

uwnd_lonmean=squeeze(nanmean(uwnd,1)); %(73,12)

contourf(lat,time,uwnd_lonmean'); colorbar xlabel('Latitude') ylabel('Time') title('Longitude mean of uwnd')

%Greta Leber, MPO 581, HW #1: Basic Stats code format="matlab" %Basic Parts:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% 1. Download and read, or use OpenDAP to read into memory, the 3-4 %variables in 3 dimensions from "Getting Data" page at left. % % * Near-surface wind (u and v) % * Near-surface air temperature (called 'air') % * Precipitation rate P % * Outgoing Longwave Radiation (OLR) % % These are all monthly climatologies (~25 year long term means, 'ltm' in % file names) of near-surface variables. Array dimensions are [72 or 73, %144, 12], with 2.5 degree and monthly resolutions.

%After downloading netcdf files, load variables into workspace using %previously written function ncload.

lat=double(ncload('vwnd.mon.ltm.nc','lat')); lon=double(ncload('vwnd.mon.ltm.nc','lon')); time=double(ncload('vwnd.mon.ltm.nc','time')); vwnd=double(ncload('vwnd.mon.ltm.nc','vwnd')); uwnd=double(ncload('uwnd.mon.ltm.nc','uwnd')); precip=double(ncload('precip.mon.ltm.nc','precip')); olr=double(ncload('olr.mon.ltm.nc','olr')); air=double(ncload('air.mon.ltm.nc','air'));

%Note: If you don't specify double, data format is varied.

%After loading in, noticed that lat dim on precip var is different, so after comparison %saw that values are at different places, eg will need a separate lat for precip (can %interpolate later onto the same grid if necessary).

precip_lat=double(ncload('precip.mon.ltm.nc','lat')); precip_lon=double(ncload('precip.mon.ltm.nc','lon'));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% 2. Calculate zonally averaged P and v over longitude, and plot the %resulting latitude-time series. Put latitude on the y axis, and month on %the x axis. Make comparison plots of latitude-time sections at our %longitude (~90W). Do we live at a "typical" longitude or is North America %unusual? (plots aligned sensiby on the page would be nice)

%First, calculate a 'vwnd_lonmean'

vwnd_lonmean=squeeze(nanmean(vwnd,1)); %(73,12)

%Now calculate a 'precip_lonmean'

precip_lonmean=squeeze(nanmean(precip,1)); %(72,12)

%Now find where in lon is "our longitude"

[~,index_vwnd_90W]=min(abs(lon-270)); [~,index_precip_90W]=min(abs(precip_lon-270));

%Make a comparison plot

month=1:12;

figure

subplot(1,2,1)

contourf(month,lat,vwnd_lonmean,10); colorbar xlabel('Month') ylabel('Latitude') title('Zonally Averaged vwnd over Time') set(gca,'XTick',1:1:12) set(gca,'XTickLabel',{'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'})

subplot(1,2,2)

contourf(month,lat,squeeze(vwnd(index_vwnd_90W,:,:)),10); colorbar xlabel('Month') ylabel('Latitude') title('vwnd over Time at 90W') set(gca,'XTick',1:1:12) set(gca,'XTickLabel',{'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'})

figure

subplot(1,2,1)

contourf(month,precip_lat,precip_lonmean,10); colorbar xlabel('Month') ylabel('Latitude') title('Zonally Averaged precip over Time') set(gca,'XTick',1:1:12) set(gca,'XTickLabel',{'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'})

subplot(1,2,2)

contourf(month,precip_lat,squeeze(precip(index_precip_90W,:,:)),10); colorbar xlabel('Month') ylabel('Latitude') title('precip over Time at 90W') set(gca,'XTick',1:1:12) set(gca,'XTickLabel',{'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'})

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%3. Average air temperature over both lat and lon, to make a 12-month time %series. Which season has the warmest global mean surface temperature? %Can you understand why?

%First, do a lon average:

air_lonmean=squeeze(nanmean(air,1)); %(72,12)

%Now need to weight by lat:

coslat=cos(deg2rad(lat)); coslat(1)=0;

air_lonmean_weighted=zeros(size(air_lonmean));

for i=1:73 air_lonmean_weighted(i,:)=air_lonmean(i,:).*coslat(i); end

air_spatialmean=(nansum(air_lonmean_weighted,1)/nansum(coslat))';

%Now plot the 12-month time series

figure plot(air_spatialmean) set(gca,'XTick',1:1:12) set(gca,'XTickLabel',{'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'}) axis([1 12 12 16]) title('Temperature Time Series') xlabel('Month') ylabel('Temperature (^°C)')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%4. Make a map of the temporal (i.e. seasonal) standard deviation of %precipitation, expressed as a percentage of the annual mean precipitation. %This might be one definition of a "monsoonal" climate.

%First calculate the temporal std and mean and their ratio:

precip_temporal_std=std(precip,0,3); precip_temporal_mean=squeeze(nanmean(precip,3)); precip_temporal_stdovermean=precip_temporal_std./precip_temporal_mean;

%Now, plot using m_map toolbox to get a map projection:

%Need to redefine my x to make map projection work x=-180:(360)/143:180;

figure m_proj('Miller Cylindrical') m_contourf(x',precip_lat,precip_temporal_stdovermean',15); m_coast('LineWidth',2,'Color','black') m_grid title('"Monsoonal Climate" (stdev/mean for Precipitation)') colorbar

%Or perhaps try another projection:

figure m_proj('Robinson') m_contourf(x',precip_lat,precip_temporal_stdovermean',15); m_coast('LineWidth',2,'Color','black') m_grid title('"Monsoonal Climate" (stdev/mean for Precipitation)') colorbar

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%5. What is the space-time standard deviation of 'air' (temperature)? Best %to build this step by step: find the global annual mean of air^2, the %global annual mean of air, compute var = meansquare - mean^2, then %stdev = sqrt(var).

air_temporal_mean=squeeze(nanmean(air,3)); air_temporal_x_mean=squeeze(nanmean(air_temporal_mean,1)); air_temporal_x_y_mean=nansum(air_temporal_x_mean.*coslat')/nansum(coslat);

air_temporal_meansquare=squeeze(nanmean(air.^2,3)); air_temporal_x_meansquare=squeeze(nanmean(air_temporal_meansquare,1)); air_temporal_x_y_meansquare=nansum((air_temporal_x_meansquare).*coslat')/nansum(coslat);

air_var=air_temporal_x_y_meansquare-(air_temporal_x_y_mean^2); air_std=sqrt(air_var);

code