MATLAB Answers

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

12 views (last 30 days)
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.

  0 Comments

Sign in to comment.

Accepted Answer

Mohammad Abouali
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 Comments

Phil Brown
Phil Brown on 4 Mar 2016
Dear Mohammad Abouali, thank you very much for the immediate and detailed answer. I will try what you suggested right away! I am obliged to you for your help.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by