MATLAB Answers

Damith
0

extract portion of netCDF file based on latitide and longitude

Asked by Damith
on 25 Mar 2014
Latest activity Commented on by Ashish Uthama on 27 Mar 2014
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

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?
Hi David,
I need to extract the 'r' value at the lat,long grid point location (4 corners of the rectangle) from the netCDF file.
Thanks,

Sign in to comment.

1 Answer

Answer by Ashish Uthama on 26 Mar 2014
Edited by Ashish Uthama on 26 Mar 2014
 Accepted Answer

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).

  2 Comments

Thanks Asish.
How can I find the 'r' variable value using latstart and latend at the 4 points with longitude information given. If you can show me that would be appreciated. I am a beginner to MATLAB so I dont know how to create a function.
Thanks.
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.

Sign in to comment.