extracting portion of .nc file based on date

7 次查看(过去 30 天)
I have a netCDF file containing data for a whole. I want to extract a portion of the data based on date. For example
StartDate = datenum ([2008 04 01 0 0 0]); %start reading from month 4, day 1
EndDate = datenum ([2008 04 30 0 0 0]); % end reading month 4, day 1.
filename = ('2008.nc'); %name of file.
I tried to do filename =('2008.nc', StartDate, EndDate); but didn't work...any help please?

回答(1 个)

Chad Greene
Chad Greene 2016-3-31
This will require a couple of steps. First, type
ncdisp('2008.nc')
into your command window. This will tell you what's in the nc file. It will probably also tell you how they've encoded the date. In my experience, ever nc file seems to encode the date differently--often is the number of days since January 1, 1970 or some arbitrary date.
Your nc file will probably have some time variable. Load that time variable (here I'm assume it's called time but it could be called something else) like this:
t = ncread('2008.nc','time');
Use the information you gleaned from ncdisp to convert t to Matlab's datenum format, and then you'll be able to get the indices corresponding to the time range of interest. That is, you'll have a starting index and a total number of time points which is the stride. Then you can load your variable of interest for only the time range of interest using ncread where you specify the start and stride. The count will be ones of the same dimension as your variable.

标签

Community Treasure Hunt

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

Start Hunting!

Translated by