<div dir="ltr">
<div style="font-family:Calibri,Helvetica,sans-serif,serif,EmojiFont;font-size:12pt;color:rgb(0,0,0)">
<div>
<p style="margin-top:0px;margin-bottom:0px">Hello all,</p>
<p style="margin-top:0px;margin-bottom:0px"><br>
</p>
<p style="margin-top:0px;margin-bottom:0px">I am having a problem while
 converting a raster object as NetCDF (.nc) file, with keeping the CRS 
information in the output file.</p>
<p style="margin-top:0px;margin-bottom:0px">Here is a reproducible code:</p>
<blockquote style="border-color:rgb(200,200,200);border-left:3px solid rgb(200,200,200);padding-left:1ex;margin-left:0.8ex;color:rgb(102,102,102)">
<pre><code><span>require</span><span>(</span><span>raster</span><span>)</span><span>
require</span><span>(</span><span>ncdf4</span><span>)</span><span>
CurrTemp </span><span><-</span><span> tempfile</span><span>()</span><span>
download.file</span><span>(</span><span>url </span><span>=</span><span> </span><span>"<a href="https://seaice.uni-bremen.de/data/amsre/asi_daygrid_swath/s6250/2003/feb/Antarctic/asi-s6250-20030214-v5.hdf">https://seaice.uni-bremen.de/data/amsre/asi_daygrid_swath/s6250/2003/feb/Antarctic/asi-s6250-20030214-v5.hdf</a>"</span><span>,</span><span> destfile </span><span>=</span><span> CurrTemp</span><span>,</span><span> mode </span><span>=</span><span> </span><span>"wb"</span><span>,</span><span> quiet </span><span>=</span><span> T</span><span>)</span><span>
r </span><span><-</span><span> raster</span><span>(</span><span>CurrTemp</span><span>)</span><span>
r </span><span><-</span><span> flip</span><span>(</span><span>r</span><span>,</span><span>2</span><span>)</span><span>
extent</span><span>(</span><span>r</span><span>)</span><span> </span><span><-</span><span> c</span><span>(</span><span>-3950000</span><span>,</span><span> </span><span>3950000</span><span>,</span><span> </span><span>-3950000</span><span>,</span><span> </span><span>4350000</span><span>)</span><span>
crs</span><span>(</span><span>r</span><span>)</span><span> </span><span><-</span><span> </span><span></span>"+proj=stere +lat_0=-90 +lat_ts=-70 +lon_0=0 +k=1 +x_0=0 +y_0=0 +a=6378273 +b=6356889.449 +units=m +no_defs"<span>
r
</span><span># class       : RasterLayer </span><span>
</span><span># dimensions  : 1328, 1264, 1678592  (nrow, ncol, ncell)</span><span>
</span><span># resolution  : 6250, 6250  (x, y)</span><span>
</span><span># extent      : -3950000, 3950000, -3950000, 4350000  (xmin, xmax, ymin, ymax)</span><span>
</span><span># coord. ref. : +proj=stere +lat_0=-90 +lat_ts=-70 +lon_0=0 +k=1 +x_0=0 +y_0=0 +a=6378273 +b=6356889.449 +units=m +no_defs </span><span>
</span><span># data source : in memory</span><span>
</span><span># names       : layer </span><span>
</span><span># values      : 0, 100  (min, max)</span></code></pre>
</blockquote>
<p style="margin-top:0px;margin-bottom:0px">So far the raster object reads well, with CRS information included.</p>
<p style="margin-top:0px;margin-bottom:0px">However, when I try to save
 it as .nc file, R prints "coord. ref. : NA", and the produced file does
 not contain the CRS information.</p>
<blockquote style="border-color:rgb(200,200,200);border-left:3px solid rgb(200,200,200);padding-left:1ex;margin-left:0.8ex;color:rgb(102,102,102)">
<pre><code><span>writeRaster</span><span>(</span><span>r</span><span>,</span><span> filename </span><span>=</span><span> </span><span>"O:/Ahmed/<a href="http://r001.nc">r001.nc</a>"</span><span>,</span><span> varname</span><span>=</span><span>"IceConc"</span><span>,</span><span>
            overwrite</span><span>=</span><span>TRUE</span><span>,</span><span> format</span><span>=</span><span>"CDF"</span><span>,</span><span>
            xname</span><span>=</span><span>"Longitude"</span><span>,</span><span> yname</span><span>=</span><span>"Latitude"</span><span>)</span><span>
</span><span># class       : RasterLayer </span><span>
</span><span># dimensions  : 1328, 1264, 1678592  (nrow, ncol, ncell)</span><span>
</span><span># resolution  : 6250, 6250  (x, y)</span><span>
</span><span># extent      : -3950000, 3950000, -3950000, 4350000  (xmin, xmax, ymin, ymax)</span><span>
</span><span># coord. ref. : NA </span><span>
</span><span># data source : O:/Ahmed/<a href="http://r001.nc">r001.nc</a> </span><span>
</span><span># names       : IceConc </span><span>
</span><span># zvar        : IceConc</span><span>

raster</span><span>(</span><span>"O:/Ahmed/<a href="http://r001.nc">r001.nc</a>"</span><span>)</span><span>
</span><span># class       : RasterLayer </span><span>
</span><span># dimensions  : 1328, 1264, 1678592  (nrow, ncol, ncell)</span><span>
</span><span># resolution  : 6250, 6250  (x, y)</span><span>
</span><span># extent      : -3950000, 3950000, -3950000, 4350000  (xmin, xmax, ymin, ymax)</span><span>
</span><span># coord. ref. : NA </span><span>
</span><span># data source : O:/Ahmed/<a href="http://r001.nc">r001.nc</a> </span><span>
</span><span># names       : IceConc </span><span>
</span><span># zvar        : IceConc</span></code></pre>
</blockquote>
<p style="margin-top:0px;margin-bottom:0px">Any solution?</p>
<p style="margin-top:0px;margin-bottom:0px"><br>
</p>
<div style="margin-top:0px;margin-bottom:0px">N.B. I also sent the same question at
<a href="https://stackoverflow.com/questions/54593552/saving-r-raster-to-netcdf-nc-file-with-keeping-crs-information" target="_blank" rel="noopener noreferrer" title="https://stackoverflow.com/questions/54593552/saving-r-raster-to-netcdf-nc-file-with-keeping-crs-information">
stackoverflow</a>, without a solution so far.<br>
</div>
<p style="margin-top:0px;margin-bottom:0px"><br>
</p>
<p style="margin-top:0px;margin-bottom:0px">Thanks</p>
</div>
<br>
</div>
<div id="gmail-x_signature">
<div id="gmail-x_divtagdefaultwrapper" dir="ltr" style="font-size:12pt;color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,serif,EmojiFont">
<div><span style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,serif,EmojiFont;font-size:16px">~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</span><br>
</div>
<div>
<div style="margin:0px;font-size:15px;color:rgb(33,33,33);text-align:start;background-color:rgb(255,255,255)">
<font size="2" face="Calibri,sans-serif"><span style="margin:0px;font-size:11pt"><b>Dr. Ahmed El-Gabbas,</b></span></font></div>
<div style="margin:0px;font-size:15px;color:rgb(33,33,33);text-align:start;background-color:rgb(255,255,255)">
<font size="2" face="Calibri,sans-serif"><span style="margin:0px;font-size:11pt">Ocean Acoustics Lab,</span></font></div>
<div style="margin:0px;font-size:15px;color:rgb(33,33,33);text-align:start;background-color:rgb(255,255,255)">
<font size="2" face="Calibri,sans-serif"><span style="margin:0px;font-size:11pt">Alfred-Wegener-Institut</span></font></div>
<div style="margin:0px;font-size:15px;color:rgb(33,33,33);text-align:start;background-color:rgb(255,255,255)">
<font size="2" face="Calibri,sans-serif"><span style="margin:0px;font-size:11pt"><span style="margin:0px" lang="de">Helmholtz-Zentrum für Polar und Meeresforschung</span></span></font></div>
<div style="margin:0px;font-size:15px;color:rgb(33,33,33);text-align:start;background-color:rgb(255,255,255)">
<div><img src="cid:ii_js1t5v9q0" alt="image.png" width="375" height="67"><br></div></div>
<div style="margin:0px;font-size:15px;color:rgb(33,33,33);text-align:start;background-color:rgb(255,255,255)">
<font size="2" face="Calibri,sans-serif"><span style="margin:0px;font-size:11pt">
<div style="margin:0px;font-size:15px;font-family:Calibri,Helvetica,sans-serif,serif,EmojiFont;background-color:rgb(255,255,255)">
<font size="2" face="Calibri,sans-serif"><span style="margin:0px;font-size:11pt">My Website:<span style="margin:0px">  </span><font style="margin:0px" color="blue"><a href="https://elgabbas.github.io/" target="_blank" rel="noopener noreferrer" title="Ctrl+Click to follow link
https://elgabbas.github.io/" style="margin:0px">https://elgabbas.github.io</a></font></span></font></div>
</span></font></div>
</div>
</div>
</div>

</div>