<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://climate-cms.wikis.unsw.edu.au/index.php?action=history&amp;feed=atom&amp;title=CodeBreak_16%2F03%2F2022</id>
	<title>CodeBreak 16/03/2022 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://climate-cms.wikis.unsw.edu.au/index.php?action=history&amp;feed=atom&amp;title=CodeBreak_16%2F03%2F2022"/>
	<link rel="alternate" type="text/html" href="http://climate-cms.wikis.unsw.edu.au/index.php?title=CodeBreak_16/03/2022&amp;action=history"/>
	<updated>2026-10-15T07:42:42Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.31.0</generator>
	<entry>
		<id>http://climate-cms.wikis.unsw.edu.au/index.php?title=CodeBreak_16/03/2022&amp;diff=3458&amp;oldid=prev</id>
		<title>C.carouge: Created page with &quot; = &lt;span style=&quot;font-size:13.999999999999998pt;  font-family:Arial;  color:#434343;  background-color:transparent;  font-weight:400;  font-style:normal;  font-variant:normal;...&quot;</title>
		<link rel="alternate" type="text/html" href="http://climate-cms.wikis.unsw.edu.au/index.php?title=CodeBreak_16/03/2022&amp;diff=3458&amp;oldid=prev"/>
		<updated>2022-03-22T23:00:48Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot; = &amp;lt;span style=&amp;quot;font-size:13.999999999999998pt;  font-family:Arial;  color:#434343;  background-color:transparent;  font-weight:400;  font-style:normal;  font-variant:normal;...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
= &amp;lt;span style=&amp;quot;font-size:13.999999999999998pt;  font-family:Arial;  color:#434343;  background-color:transparent;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap&amp;quot;&amp;gt;Copying global attributes from NetCDF&amp;lt;/span&amp;gt; =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;font-size:11pt;  font-family:Arial;  color:#000000;  background-color:transparent;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap&amp;quot;&amp;gt;There was a question where data was converted using a python notebook and xarray, but the global attributes of the input file should be copied over to the output file.&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;font-size:11pt;  font-family:Arial;  color:#000000;  background-color:transparent;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap&amp;quot;&amp;gt;The new file was saved using the line&amp;lt;/span&amp;gt;&amp;amp;nbsp;&lt;br /&gt;
&amp;lt;pre&amp;gt;pr.to_dataset(name='pr').to_netcdf(&amp;lt;filename&amp;gt;)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;font-size:11pt;  font-family:Arial;  color:#000000;  background-color:transparent;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap&amp;quot;&amp;gt;We recommended to do it in two steps, and add the copying of the attributes in between:&amp;lt;/span&amp;gt; &amp;amp;nbsp;&lt;br /&gt;
&amp;lt;pre&amp;gt;ds = pr.to_dataset(name='pr') &lt;br /&gt;
ds.attrs = f.attrs &lt;br /&gt;
ds.to_netcddf(&amp;lt;filename&amp;gt;)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= &amp;lt;span style=&amp;quot;font-size:13.999999999999998pt;  font-family:Arial;  color:#434343;  background-color:transparent;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap&amp;quot;&amp;gt;Calculating differential in xarray&amp;lt;/span&amp;gt; =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;font-size:11pt;  font-family:Arial;  color:#000000;  background-color:transparent;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap&amp;quot;&amp;gt;The question revolved around calculating the differential of the density (⍴) with respect to the depth (&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;font-size:11pt;  font-family:'Times New Roman';  color:#000000;  background-color:transparent;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap&amp;quot;&amp;gt;z&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;font-size:11pt;  font-family:Arial;  color:#000000;  background-color:transparent;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap&amp;quot;&amp;gt;). Attempting this calculation using np.gradient led to broadcasting errors. It is much easier to use an xarray aware function like &amp;lt;/span&amp;gt;[https://xarray.pydata.org/en/stable/generated/xarray.DataArray.differentiate.html &amp;lt;span style=&amp;quot;font-size:11pt;  font-family:Arial;  color:#1155cc;  background-color:transparent;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:underline;  -webkit-text-decoration-skip:none;  text-decoration-skip-ink:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap&amp;quot;&amp;gt;xarray.differentiate&amp;lt;/span&amp;gt;]&amp;lt;span style=&amp;quot;font-size:11pt;  font-family:Arial;  color:#000000;  background-color:transparent;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap&amp;quot;&amp;gt;, which knows how to broadcast because of the named coordinates and uses the spacing in the &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;font-size:11pt;  font-family:'Times New Roman';  color:#000000;  background-color:transparent;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap&amp;quot;&amp;gt;z&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;font-size:11pt;  font-family:Arial;  color:#000000;  background-color:transparent;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap&amp;quot;&amp;gt;coordinate to properly calculate the differential δ⍴/δ&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;font-size:11pt;  font-family:'Times New Roman';  color:#000000;  background-color:transparent;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap&amp;quot;&amp;gt;z&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;font-size:11pt;  font-family:Arial;  color:#000000;  background-color:transparent;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap&amp;quot;&amp;gt;.&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= &amp;lt;span style=&amp;quot;font-size:13.999999999999998pt;  font-family:Arial;  color:#434343;  background-color:transparent;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap&amp;quot;&amp;gt;Regression analysis on spatial data&amp;lt;/span&amp;gt; =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;font-size:11pt;  font-family:Arial;  color:#000000;  background-color:transparent;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap&amp;quot;&amp;gt;The question was about the suitability of using &amp;lt;/span&amp;gt;[https://xarray.pydata.org/en/stable/generated/xarray.DataArray.polyfit.html?highlight=polyfit &amp;lt;span style=&amp;quot;font-size:11pt;  font-family:Arial;  color:#1155cc;  background-color:transparent;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:underline;  -webkit-text-decoration-skip:none;  text-decoration-skip-ink:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap&amp;quot;&amp;gt;xarray.Dataset.polyfit&amp;lt;/span&amp;gt;]&amp;lt;span style=&amp;quot;font-size:11pt;  font-family:Arial;  color:#000000;  background-color:transparent;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap&amp;quot;&amp;gt;to do regression analysis on spatial data. This might not be the correct tool, as it has limited options for the fitting function. If more than a simple fit is required, it might be better to use something like &amp;lt;/span&amp;gt;[https://sklearn-xarray.readthedocs.io/en/latest/auto_examples/plot_linear_regression.html &amp;lt;span style=&amp;quot;font-size:11pt;  font-family:Arial;  color:#1155cc;  background-color:transparent;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:underline;  -webkit-text-decoration-skip:none;  text-decoration-skip-ink:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap&amp;quot;&amp;gt;sklearn-xarray&amp;lt;/span&amp;gt;]&amp;lt;span style=&amp;quot;font-size:11pt;  font-family:Arial;  color:#000000;  background-color:transparent;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap&amp;quot;&amp;gt;or wrap &amp;lt;/span&amp;gt;[https://stackoverflow.com/questions/62105673/applying-stats-linregress-to-every-grid-cell-in-a-netcdf-file &amp;lt;span style=&amp;quot;font-size:11pt;  font-family:Arial;  color:#1155cc;  background-color:transparent;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:underline;  -webkit-text-decoration-skip:none;  text-decoration-skip-ink:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap&amp;quot;&amp;gt;scipy.stats.lingress&amp;lt;/span&amp;gt;]&amp;lt;span style=&amp;quot;font-size:11pt;  font-family:Arial;  color:#000000;  background-color:transparent;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap&amp;quot;&amp;gt;in a function and apply this to all points.&amp;lt;/span&amp;gt;&lt;/div&gt;</summary>
		<author><name>C.carouge</name></author>
		
	</entry>
</feed>