# How can I transform 3-dimensional netCDF data to a 2-dimensional table efficiently

11 views (last 30 days)
Phil Brown on 4 Mar 2016
Hi guys, I am having a problem in manipulating the data on a netCDF file (CMIP5 Project).
The file contains Sea Surface Temperatures, with longitude 180x1, latitude 120x1, time 360x1 (monthly). So the 'tas' (temperature) size is 180x120x360. Now what I would like to do is take this 3-dimensional netcdf data and make it 2-dimensional so as it will be easier to manipulate and I can calculate averages, min-max temperatures etc. for specific latitudes, longitudes and time series every time. So for example I would like to create a 2-dimensional table for each year with the 12-months separated and longitudes-latitudes in series. I don't know if I explained this properly or if my idea can actually work.
I am very new to Matlab so sorry if this can be handled with a very simple solution that I haven't found yet.
Thank you in advance for any help.

Mohammad Abouali on 4 Mar 2016
Edited: Mohammad Abouali on 4 Mar 2016
I think you are better off keeping it 3D as it is. Here is how you can calculate some of the stuff you asked very easilly.
Assuming TAS is 180x120x360 [lon x lat x Time (monthly)]:
To calculate min,max,mean for each latitude for each month issue:
meanLat = squeeze(mean(TAS,2));
maxLat = squeeze(max(TAS,[],2));
minLat = squeeze(min(TAS,[],2));
To calculate min,max,mean for each longitude for each month issue:
meanLon = squeeze(mean(TAS,1));
maxLon = squeeze(max(TAS,[],1));
minLon = squeeze(min(TAS,[],1));
In this case actually you can also use
meanLon = squeeze(mean(TAS));
maxLon = squeeze(max(TAS));
minLon = squeeze(min(TAS));
To calculate min,max,mean at each location for the entire time period do
meanTAS = mean(TAS,3);
maxTAS = max(TAS,[],3);
minTAS = min(TAS,[],3);
To separate Month and year issue:
TAS4D = reshape(TAS, 180, 120, 12, 30);
Now TAS4D is a 4D array of size 180x120x12x30 [lon x lat x month x year] just note that year index starts from 1. So if you have data from 2001to 2030 (30 years of data) index 1 (tas(:,:,:,1)) refers to 2001, index 2 (tas(:,:,:,2)) refers to 2002 and so on.
now you can calculate yearly mean,min,max at each location:
meanLat = squeeze(mean(TAS4D,3));
maxLat = squeeze(max(TAS4D,[],3));
minLat = squeeze(min(TAS4D,[],3));
calculate monthly mean,min,max across the entire time period:
meanLat = mean(TAS4D,4);
maxLat = max(TAS4D,[],4);
minLat = min(TAS4D,[],4);
Well, the list can go on. But the point is that keep that spatial data in 3D and you would have much more flexibility.
##### 2 CommentsShowHide 1 older comment
Mohammad Abouali on 4 Mar 2016
You are welcome. Let us know if you needed more help.