1
votes

I am trying to extract a specific subdomain of data from a 'tasmax' variable, from a netcdf file based on latitude and longitudes.My extraction criteria are: latitudes between 44 and 50 degrees latitude; and between 5 and -5 degrees longitude (France).

My 'lat' variable is 105 by 102 (dimensions: rlon,rlat, where rlon and rlat are vectors of rotated lat-longs) covering the entirety of Europe.

My 'lon' variable is also 105 by 102 (dimensions: rlon,rlat again). Both the lat and lon variables are regular lat/longs,i.e. not rotated.

Since I only have regular lat long data as my criteria for extracting the data, I believe I have to use the grids of lat and lon (as opposed to the vectors of rlat and rlon) to extract the data.

%first I extracted the lat, lon and data variables from the .nc file:

lat=ncread(file.nc,'lat');
lon=ncread(file.nc,'lon');
data=ncread(file.nc,'tasmax');

%tasmax is 105*102*366 (a year of daily data)

%the following for-loop is only present so I can ultimately automate this process for other regions

for r=1;

%create a 3d variable the size of the lat/lon grids and fill it with NaNs..so ultimately, the only data in it will be the region I'm interested in domainrun=NaN(105,102,size(data,3));

%region coordinates

 ylat_north = [50]';
 ylat_south = [44]';
 xlon_east = [5]';
 xlon_west = [-5]';

icounter=0;
for j=1:size(lat,2);
for i = 1:size(lat,1);

%if the element by element lat and lons lie within the lat and lons specified for the region, then the indices for each point are saved in idxi and idxj, while the actual data itself is written to the 'domainrun' matrix (previously entirely filled with NaNs). This results in a matrix for the region, containing only the data for that specific subdomain along with NaNs everywhere else

if lat(i,j)<=ylat_north(r) && lat(i,j)>=ylat_south(r) && lon(i,j)>=xlon_west(r) &&       lon(i,j)<=xlon_east(r);

        icounter=icounter+1;

        idxi(icounter,1)=i;
        idxj(icounter,1)=j;
        domainrun(idxi(icounter,1),idxj(icounter,1),:)=data(idxi(icounter,1),idxj(icounter,1),:);

%idxij provides the i and j indices for extracting the data from the domain:these indices are the same for both the lat and lon variables here,as they have been created based on the premise that they describe the xy area within the region being extracted.

        idxij=horzcat(idxi,idxj);

%testlat and testlon contain the actual lat and lons for the subregion

        testlat(i,j)=lat(i,j);
        testlon(i,j)=lon(i,j);

    else
        testlat(i,j)=NaN;
        testlon(i,j)=NaN;
    end
end

end

%While this code works, it is extremely messy I think. What I want is a way to extract the tasmax data based on regular lat lon criteria, using nc commands...or anything simpler. This question is different to others on stackoverflow, in that all the others were dealing with vectors of lat and lon, which would be easier to use to extract data from tasmax using nc commands (instead of grids of lat lons). Any help would be appreciated.

1
Even if your LAT LON are not vectors, are they regular? i.e is the spacing between each point in 'tasmax' consistent? If so, it essentially contains the same information as a vector (but repeated across rows for one and columns for the other). You can use the start/count syntax of NCREAD to only read in the part you need. I'll see if I can post an example later in the day.Ashish Uthama
@Ashish Uthama. Thanks for the response. No, the spacing between the grid squares is not consistent.matlab_newby
Are they at least in strictly increasing order? i.e would the region you want to extract form a 'rectangle'? If not, I guess what you have would be the best. If they are, the core logic would be similar to your code, what you could do is read lat and lon first and find the top left and bottom right indices and then use the START, COUNT syntax option of NCREAD. I am not getting a clear idea of how your data is stored to create a dummy file/example code.Ashish Uthama
@Ashish Uthama. the region I want doesn't form a rectangle, due to the fact that the grid squares change size towards the pole. I'm going to try and upload the lat lon data I have tomorrow to show you. Thanks for your input.matlab_newby
@Ashish Uthama : I've included links to the lat lons here. filedropper.com/lon ************ filedropper.com/lat ********** filedropper.com/data_13matlab_newby

1 Answers

2
votes

If you don't mind me offering a non-matlab solution, you can select a region from the command line with cdo:

cdo sellonlatbox,-5,5,44,50 input_data.nc france.nc 

which you can then read into matlab.

cdo is easy to install in ubuntu

sudo apt-get install cdo 

and ubuntu is also available under windows 10 too these days...