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.