extract portion of netCDF file based on latitide and longitude
    7 views (last 30 days)
  
       Show older comments
    
Hi All,
I need to extract grid data from a nectCDF file. For example, if I provide the spatial extents in terms of latitude and longitudes, how can I extract data for the extent. Lat, long coordinates for the grid are: 9.84N,105.30E,10.02N,105.69E (first two are the coordinates of the lower left and second two are for the upper right). Please see the original (example.nc) netCDF file is attached inside the zipped folder.
I need to extract point data for the above spatila extent using the original file (example.nc)
Thanks in advance for the help.
2 Comments
  David Young
      
 on 25 Mar 2014
				Have you looked at the netCDF functions in MATLAB - ncinfo, ncread and so on? If you have, can you be more specific about what the difficulty is? For example, what goes wrong when you call them to read your data?
Accepted Answer
  Ashish Uthama
    
 on 26 Mar 2014
        
      Edited: Ashish Uthama
    
 on 26 Mar 2014
  
      Try using ncdisp first to look at the structure of your file. Usually, netcdf files have data defined on 'dimension' variables - lat and lon. So you would first read the lat and lon variables from the file ( ncread ).
 >> lon = ncread('/tmp/example.nc','longitude')'
 lon =
Columns 1 through 11
104.1250  104.3750  104.6250  104.8750  105.1250  105.3750  105.6250  105.8750  106.1250  106.3750  106.6250
Columns 12 through 13
106.8750  107.1250
 >> lat = ncread('/tmp/example.nc','latitude')'
 lat =
Columns 1 through 11
    8.1250    8.3750    8.6250    8.8750    9.1250    9.3750    9.6250    9.8750   10.1250   10.3750   10.6250
Column 12
   10.8750
You would then have to find the indices into lat and lon variables within which your grid lies. Use FIND to locate those indices:
>> latstart = find(lat>9.84,1, 'first')
latstart =
       8
>> latend = find(lat<10.02,1, 'last')
latend =
       8
Once you get these start/end indices to each of your variable dimension, you can use the partial read capability of NCREAD to read from your 'r' variable.
If you have to do this kind of operation frequently, consider wrapping this logic in a function.
(Note - have a look at the linked functions and examples. Give it a try and post back if you have trouble adapting this idea).
3 Comments
  Ashish Uthama
    
 on 27 Mar 2014
				Have a look at ncread, and try out the example. Look at the second example in particular, which uses the start, count and stride inputs. You would only need the start and count inputs for your problem. Give it a try and post back if you get stuck.
More Answers (0)
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!


