<div dir="ltr"><div class="gmail_default">Sebastian,</div><div class="gmail_default"><br></div><div class="gmail_default">First, to get the dimension sizes of an array, this is slightly cleaner code:</div><div class="gmail_default"><font face="monospace, monospace"><br></font></div><div class="gmail_default"><font face="monospace, monospace"> dims = dimsizes(sst)</font></div><div class="gmail_default"><div class="gmail_default"><font face="monospace, monospace"> times = dims(0) </font></div><div class="gmail_default"><font face="monospace, monospace"> row = dims(1) ; perhaps call it "nlat" instead of "row"? </font></div><div class="gmail_default"><font face="monospace, monospace"> col = dims(2) ; "nlon" instead of "ncol" ?</font></div></div><div class="gmail_default"><br></div><div class="gmail_default">Second, when dealing with whole arrays, you don't need to use "(:,:,:)" type of syntax.</div><div class="gmail_default"><br></div><div class="gmail_default"><font face="monospace, monospace">sst = a->anom(:,:,:)</font></div><div class="gmail_default"><font face="arial, helvetica, sans-serif"><br></font></div><div class="gmail_default"><font face="arial, helvetica, sans-serif">or</font></div><div class="gmail_default"><font face="monospace, monospace"><br></font></div><div class="gmail_default"><font face="monospace, monospace">sst_avg(:,:)=sst_avg(:,:)/times <br></font></div><div class="gmail_default"><br></div><div class="gmail_default">If you want the whole array, then:</div><div class="gmail_default"><br></div><div class="gmail_default"><font face="monospace, monospace">sst = a->anom</font></div><div class="gmail_default"><font face="arial, helvetica, sans-serif"><br></font></div><div class="gmail_default"><font face="arial, helvetica, sans-serif">and</font></div><div class="gmail_default"><font face="monospace, monospace"><br></font></div><div class="gmail_default"><font face="monospace, monospace">sst_avg = sst_avg/times <br></font></div><div class="gmail_default"><br></div><div class="gmail_default">work fine. </div><div class="gmail_default"><br></div><div class="gmail_default"><div class="gmail_default"><br></div></div><div class="gmail_default">Third, you want to avoid looping in NCL to do calculations, because it's an unnecessarily expensive operation, and NCL allows for direct array computations. </div><div class="gmail_default"><br></div><div class="gmail_default">Fourth, in this case, there's a function for doing averages across a specified dimension. Please see "dim_avg" and "dim_avg_n":</div><div class="gmail_default"><br></div><div class="gmail_default"><a href="http://www.ncl.ucar.edu/Document/Functions/Built-in/dim_avg.shtml" target="_blank">http://www.ncl.ucar.edu/Document/Functions/Built-in/dim_avg.shtml</a><br></div><div class="gmail_default"><a href="http://www.ncl.ucar.edu/Document/Functions/Built-in/dim_avg_n.shtml" target="_blank">http://www.ncl.ucar.edu/Document/Functions/Built-in/dim_avg_n.shtml</a><br></div><div class="gmail_default"><br></div><div class="gmail_default">"dim_avg_n_Wrap" will further preserve metadata:</div><div class="gmail_default"><br></div><div class="gmail_default"><a href="http://www.ncl.ucar.edu/Document/Functions/Contributed/dim_avg_n_Wrap.shtml" target="_blank">http://www.ncl.ucar.edu/Document/Functions/Contributed/dim_avg_n_Wrap.shtml</a><br></div><div class="gmail_default"><br></div><div class="gmail_default" style="font-size:small">I don't know what format your daily temperature values are in, but let's say you have one value per day. This means that the first 7 values of the time dimension will be associated with the first week, the second 7 values with the second week, etc.</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">In this case, then, you can reshape the array to be nweeks x 7 x nlat x nlon, and then take the average across the second dimension (dimension index 1, because dimension indexing starts at 0 and goes from left to right).</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">This will only work if you actually have a multiple of 7 time values. So, for example, let's say you have 52 weeks of daily values, hence time is 52 * 7 = 364. Here's how you calculate a weekly average:</div><div class="gmail_default" style="font-size:small"><div class="gmail_default"><font face="monospace, monospace"><br> dims = dimsizes(sst)</font></div><div class="gmail_default"><div class="gmail_default"><font face="monospace, monospace"> ntime = dims(0) </font></div><div class="gmail_default"><font face="monospace, monospace"> nlat = dims(1) </font></div><div class="gmail_default"><font face="monospace, monospace"> nlon = dims(2)</font></div><div class="gmail_default"><font face="monospace, monospace"><br></font></div></div></div><div class="gmail_default" style="font-size:small"><font face="monospace, monospace"> days_per_week = 7 </font></div><div class="gmail_default" style="font-size:small"><font face="monospace, monospace"><br></font></div><div class="gmail_default" style="font-size:small"><font face="monospace, monospace"> nweeks = ntime / days_per_week</font></div><div class="gmail_default" style="font-size:small"><font face="monospace, monospace"><br></font></div><div class="gmail_default" style="font-size:small"><font face="monospace, monospace">; Reshape 3D array to be a 4D array</font></div><div class="gmail_default" style="font-size:small"><font face="monospace, monospace"> sst4d = reshape(sst,(/nweeks,days_per_week,nlat,nlon/))</font></div><div class="gmail_default" style="font-size:small"><font face="monospace, monospace"><br></font></div><div class="gmail_default" style="font-size:small"><span style="font-family:monospace,monospace">; average across the "1" dimension</span><font face="monospace, monospace"><br></font></div><div class="gmail_default" style="font-size:small"><font face="monospace, monospace"> sst_weekly_avg = dim_avg_n(sst4d,1) </font></div><div class="gmail_extra"><br><div class="gmail_quote"><div class="gmail_default" style="font-size:small;display:inline">--Mary</div></div><div class="gmail_quote"><div class="gmail_default" style="font-size:small;display:inline"><br></div></div><div class="gmail_quote"><div class="gmail_default" style="font-size:small;display:inline"></div>On Wed, Aug 5, 2015 at 12:02 PM, Sebastian Otarola-Bustos <span dir="ltr"><<a href="mailto:Sebastian.F.Otarola-Bustos.1@nd.edu" target="_blank">Sebastian.F.Otarola-Bustos.1@nd.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi everybody, I'm doing very simple array operations, but it seems that NCL doesn't agree with me, and I'm getting very wrong numbers.<br><br>Here is the code I'm refering too: <br><br>I'm trying to make a weekly average of daily sst temperatures, so sst is( times, lat,lon), then I define a 2D array(lat,lon) , sst_avg, and initializes with 0 , up to now everythings goes ok. But in this case, after the orange do sst_average is still full with zeros. Finally, I'm just trying to divide each element of sst_avg by number of times(days), so I'll get the average. But this not work. And it's seems to be very basic error. Any help would be really appreciated.<br><br><br><div><font face="garamond, serif"> <font size="4">sst = a->anom(:,:,:) </font></font></div><div><font face="garamond, serif" size="4"> printVarSummary(sst)</font></div><div><font face="garamond, serif" size="4"> times=dimsizes(sst(:,0,0)) </font></div><div><font face="garamond, serif" size="4"> row = dimsizes(sst(0,:,0)) </font></div><div><font face="garamond, serif" size="4"> col = dimsizes(sst(0,0,:)) </font></div><div><font face="garamond, serif" size="4"> sst_avg = new((/row,col/),"float",0)</font></div><div><font face="garamond, serif" size="4"> <span style="background-color:rgb(255,153,0)"> do it =0,times-1 </span></font></div><div><font face="garamond, serif" size="4" style="background-color:rgb(255,153,0)"> sst_avg(:,:)=sst_avg(:,:)+sst(it,:,:)</font></div><div><font face="garamond, serif" size="4" style="background-color:rgb(255,153,0)"> end do</font></div><div><font face="garamond, serif" size="4"> print(sst_avg) </font></div><div><font face="garamond, serif" size="4"> sst_avg(:,:)=sst_avg(:,:)/times ; Calculo anomalia semanal promedio</font></div><div><br><br>All the best, <br>Sebastián. <br></div></div>
<br>_______________________________________________<br>
ncl-talk mailing list<br>
<a href="mailto:ncl-talk@ucar.edu" target="_blank">ncl-talk@ucar.edu</a><br>
List instructions, subscriber options, unsubscribe:<br>
<a href="http://mailman.ucar.edu/mailman/listinfo/ncl-talk" rel="noreferrer" target="_blank">http://mailman.ucar.edu/mailman/listinfo/ncl-talk</a><br>
<br></blockquote></div><br></div></div>