title: extract a subdomain of data from a netcdf file using lat and longs as references
body: <p>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).</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>%first I extracted the lat, lon and data variables from the .nc file:</p>
  
  <pre><code>lat=ncread(file.nc,'lat');
  lon=ncread(file.nc,'lon');
  data=ncread(file.nc,'tasmax');
  </code></pre>
  
  <p>%tasmax is 105*102*366 (a year of daily data)</p>
  
  <p>%the following for-loop is only present so I can ultimately automate this process for other regions</p>
  
  <pre><code>for r=1;
  </code></pre>
  
  <p>%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));</p>
  
  <p>%region coordinates</p>
  
  <pre><code> 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);
  </code></pre>
  
  <p>%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</p>
  
  <pre><code>if lat(i,j)&lt;=ylat_north(r) &amp;&amp; lat(i,j)&gt;=ylat_south(r) &amp;&amp; lon(i,j)&gt;=xlon_west(r) &amp;&amp;       lon(i,j)&lt;=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),:);
  </code></pre>
  
  <p>%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.</p>
  
  <pre><code>        idxij=horzcat(idxi,idxj);
  </code></pre>
  
  <p>%testlat and testlon contain the actual lat and lons for the
  subregion</p>
  
  <pre><code>        testlat(i,j)=lat(i,j);
          testlon(i,j)=lon(i,j);
  
      else
          testlat(i,j)=NaN;
          testlon(i,j)=NaN;
      end
  end
  </code></pre>
  
  <p>end</p>
  
  <p>%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.</p>