title: R rasterFromXYZ: x cell sizes are not regular
body: <p>Given a <code>rasterLayer</code> <code>a</code>,</p>
  <pre><code>library(raster)
  library(rasterVis)
  a=raster('p190001.grd')
  head(a)
  </code></pre>
  <p>Based on the description below (see below) which is also found in this link,
  <a href="ftp://ccrp.tor.ec.gc.ca/pub/EC_data/CANGRD/" rel="nofollow noreferrer">ftp://ccrp.tor.ec.gc.ca/pub/EC_data/CANGRD/
  </a></p>
  <p>I came up with the following <code>CRS</code></p>
  <pre><code>mycrs &lt;- CRS(&quot;+proj=stere +lat_0=90 +lat_ts=60 +lon_0=-110 +x_0=1884770 +y_0=5220000 +datum=WGS84 +to_meter=50000&quot;)
  
     proj4string(a) &lt;- mycrs
      projection(a) &lt;- mycrs
  a
      class       : RasterLayer 
  dimensions  : 95, 125, 11875  (nrow, ncol, ncell)
  resolution  : 1, 1  (x, y)
  extent      : -0.5, 124.5, -0.5, 94.5  (xmin, xmax, ymin, ymax)
  coord. ref. : +proj=stere +lat_0=90 +lat_ts=60 +lon_0=-110 +x_0=1884770 +y_0=5220000 +datum=WGS84 +to_meter=50000 +ellps=WGS84 +towgs84=0,0,0 
  data source : in memory
  names       : p190001 
  values      : 4.59, 335.45  (min, max)
  
  a[a==170141000918782798866653488190622531584.00]=NA # change missing value identifier
  levelplot(a)
  </code></pre>
  <p>For the sake of completeness, I have created a <code>.Rdata</code> file <code>~209 KB</code> which can be found <a href="https://www.dropbox.com/s/axt06fydrfn68yv/dat.RData?dl=0" rel="nofollow noreferrer">here</a>.</p>
  <p>The intention is to collapse the <code>a</code> to a dataframe and perform some analysis on it then do a <code>rasterFROMXY</code>. However, I get the error:</p>
  <blockquote>
  <p>Error in rasterFromXYZ(dt) : x cell sizes are not regular</p>
  </blockquote>
  <p>My code:</p>
  <pre><code>library(data.table)
  v &lt;- getValues(a) # get vector
  dt &lt;- as.data.table(v)
  
  # Add LATITUDE and LONGITUDE columns to dt
  
  dt[,LATITUDE:=grid_pnt_lls$y]
  dt[,LONGITUDE:=grid_pnt_lls$x]
  dtnames &lt;- c(names(dt)[2:3],names(dt)[1]) 
  
  setcolorder(dt,dtnames)
  
  vcols &lt;- c('LONGITUDE','LATITUDE','v')
  
  setcolorder(dt,vcols)
  
  library(data.table)
  setnames(dt, &quot;LONGITUDE&quot;, &quot;x&quot;)
  setnames(dt, &quot;LATITUDE&quot;, &quot;y&quot;)
  setnames(dt, &quot;v&quot;, &quot;z&quot;)
  
  
  ras=rasterFromXYZ(dt)#     Error in rasterFromXYZ(dt) : x cell sizes are not regular
  </code></pre>
  <p>As a workaround, I tried to <code>projectRaster</code> to regular latlon (<code>projj &lt;- CRS(&quot;+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0&quot;</code>)
  ) and even <code>rasterrize</code>but could not get the original dimensions of <code>a</code>. I would like <code>ras</code>to have same dimensions as <code>a</code> but on <code>latlon</code> grid.</p>
  <pre><code>#===========================================================================  
  </code></pre>
  <p>The CANGRD grid is in polar stereographic projection with a 50 km spatial resolution. The grid is a 125 (columns) by 95 (rows) matrix, where the SW corner (0,0) is at 40.0451°N latitude and 129.8530°W longitude. The projection is true at 60.0°N and centered on 110.0°W. The file ‘CANGRD_points_LL.txt’ lists the latitudes and longitudes for each grid point.</p>
  <pre><code>The general format of the ‘YYYYDD.grd’ file is:
  Id – ‘DSAA’ identifies the file as an ASCII grid file
  nx ny - nx is the integer number of grid lines along the X axis (columns)
          ny is the integer number of grid lines along the Y axis (rows)
  xlo xhi - xlo is the minimum X value of the grid
            xhi is the maximum X value of the grid
  ylo yhi - ylo is the minimum Y value of the grid
            yhi is the maximum Y value of the grid
  zlo zhi - are the minimum and maximum Z values of the grid.      
  </code></pre>