extracting portion of .nc file based on date
8 views (last 30 days)
Show older comments
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?
0 Comments
Answers (1)
Chad Greene
on 31 Mar 2016
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.
See Also
Categories
Find more on NetCDF in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!