<div dir="ltr"><div dir="ltr"><div>Debasish, thanks for the extra description. I see that Dennis just explained <b>bin_sum</b>. Here is a different method, possibly what you are looking for. This is a variation of my original answer for 1-D coordinates rather than 2-D coordinates.</div><div><br></div><div>This uses NCL single-value coordinate subscripting to position each input coordinate pair into the nearest grid box. Notice the exact position of the curly brackets. This is not the only way to locate a point to a grid box, but it is the simplest. Untested:</div><div><br></div><div> dims = (/ nlat, nlon /)</div><div> xsum = new (dims, float)</div><div> xsum(:,:) = 0</div><div><br></div><div> do n = 0, npoints-1</div><div> xsum({lat(n)}, {lon(n)}) = xsum({lat(n)}, {lon(n)}) + dvar(n)</div><div> end do</div><div><br></div><div>This will not work properly if there are missing values or locations outside of the output grid. These can be fixed with a little extra code, but I want to give you a starting point. There may also be ways to optimize this brute force method, if better run time is needed. Good luck.</div><div><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Oct 19, 2020 at 4:04 PM Paimazumder, Debasish <<a href="mailto:Debasish.Paimazumder@aig.com">Debasish.Paimazumder@aig.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
<div lang="EN-US">
<div class="gmail-m_-1571831064249387120WordSection1">
<p class="MsoNormal">Hi Dave <u></u><u></u></p>
<p class="MsoNormal">In this case, input dlon, dlat and dvar are from csv file and output grid lat/lon from netcdf file. See below their dimension. I would like to create a new variable at lat/lon grid by summing all dvar based on number of dlon, dlat within
each of lat/lon grid.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">For example – if we have 50 dlat/dlon in one latXlon grid, then new variable in lat/lon grid = sum of dvar from all 50 dlat/dlon.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">With regards<u></u><u></u></p>
<p class="MsoNormal">-Dave<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><b>Input <u></u><u></u></b></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Variable: dlat<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Type: float<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Total Size: 1392092 bytes<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)"> 348023 values<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Number of Dimensions: 1<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Dimensions and sizes: [348023]<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Coordinates:<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Number Of Attributes: 1<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)"> _FillValue : 9.96921e+36<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Variable: dlon<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Type: float<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Total Size: 1392092 bytes<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)"> 348023 values<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Number of Dimensions: 1<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Dimensions and sizes: [348023]<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Coordinates:<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Number Of Attributes: 1<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)"> _FillValue : 9.96921e+36<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Variable: dvar<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Type: float<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Total Size: 1392092 bytes<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)"> 348023 values<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Number of Dimensions: 1<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Dimensions and sizes: [348023]<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Coordinates:<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Number Of Attributes: 1<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)"> _FillValue : 9.96921e+36</span><u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><b>Output grid –<u></u><u></u></b></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Variable: lat<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Type: float<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Total Size: 744 bytes<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)"> 186 values<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Number of Dimensions: 1<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Dimensions and sizes: [lat | 186]<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Coordinates:<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)"> lat: [-46.375..-0.125]<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Number Of Attributes: 4<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)"> units : degree_north<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)"> standard_name : latitude<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)"> long_name : latitude<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)"> bounds : lat_bnds<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Variable: lon<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Type: float<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Total Size: 964 bytes<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)"> 241 values<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Number of Dimensions: 1<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Dimensions and sizes: [lon | 241]<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Coordinates:<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)"> lon: [100.125..160.125]<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)">Number Of Attributes: 4<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)"> units : degree_east<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)"> standard_name : longitude<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)"> long_name : longitude<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(32,56,100)"> bounds : lon_bnds<u></u><u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><b>From:</b> Dave Allured - NOAA Affiliate <<a href="mailto:dave.allured@noaa.gov" target="_blank">dave.allured@noaa.gov</a>>
<br>
<b>Sent:</b> Sunday, October 18, 2020 4:36 PM<br><u></u></p>
<p class="MsoNormal"><u></u><br></p>
<div>
<div>
<div>
<p class="MsoNormal">Debasish,<u></u><u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Sorry, you originally said "I have ... 2d lat, lon grid" and I misunderstood what that meant. See other answers from Barry and Dennis. If you need more help, please show printVarSummary for your input variables x, lat, and lon, so we
can be clear about what type of input data you have.<u></u><u></u></p>
</div>
</div>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal">On Sun, Oct 18, 2020 at 11:32 AM Paimazumder, Debasish <<a href="mailto:Debasish.Paimazumder@aig.com" target="_blank">Debasish.Paimazumder@aig.com</a>> wrote:<u></u><u></u></p>
</div>
<blockquote style="border-style:none none none solid;border-left-width:1pt;border-left-color:rgb(204,204,204);padding:0in 0in 0in 6pt;margin:5pt 0in 5pt 4.8pt">
<div>
<p class="MsoNormal">Dave- <span style="color:black">getind_latlon2d needs two-dimensional lat/lon while my output grid 1-d lat/lon. </span>
<u></u><u></u></p>
<div>
<p class="MsoNormal" style="margin-bottom:12pt"><span style="color:black">Interpolation from location to 1-d grid could be an option but we won’t get the accumulation (sum) at the output grid.</span><u></u><u></u></p>
<div>
<p class="MsoNormal">Best regards, <u></u><u></u></p>
<div>
<p class="MsoNormal">Dave<u></u><u></u></p>
</div>
</div>
<div>
<p class="MsoNormal"><br>
<br>
<u></u><u></u></p>
<blockquote style="margin-top:5pt;margin-bottom:5pt">
<p class="MsoNormal">On Oct 18, 2020, at 12:54 PM, Barry Lynn <<a href="mailto:barry.h.lynn@gmail.com" target="_blank">barry.h.lynn@gmail.com</a>> wrote:<u></u><u></u></p>
</blockquote>
</div>
<blockquote style="margin-top:5pt;margin-bottom:5pt">
<div>
<div>
<p class="MsoNormal">Right, that can be done. Or, you can interpolate the data from the fine to coarse grid.
<u></u><u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">I am not sure if the latter will conserve the averages like doing the averaging in a loop.<u></u><u></u></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal">On Sun, Oct 18, 2020 at 6:58 PM Dave Allured - NOAA Affiliate via ncl-talk <<a href="mailto:ncl-talk@mailman.ucar.edu" target="_blank">ncl-talk@mailman.ucar.edu</a>> wrote:<u></u><u></u></p>
</div>
<blockquote style="border-style:none none none solid;border-left-width:1pt;border-left-color:rgb(204,204,204);padding:0in 0in 0in 6pt;margin:5pt 0in 5pt 4.8pt">
<div>
<p class="MsoNormal">Use the function <b>getind_latlon2d</b> to get the (i,j) grid point indices for the point data coordinates. Then do a single loop over the point data to accumulate sums and counts on the output grid.
<u></u><u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal">On Sun, Oct 18, 2020 at 9:09 AM Paimazumder, Debasish via ncl-talk <<a href="mailto:ncl-talk@mailman.ucar.edu" target="_blank">ncl-talk@mailman.ucar.edu</a>> wrote:<u></u><u></u></p>
</div>
<blockquote style="border-style:none none none solid;border-left-width:1pt;border-left-color:rgb(204,204,204);padding:0in 0in 0in 6pt;margin:5pt 0in 5pt 4.8pt">
<div>
<p class="MsoNormal">Hi Barry, <u></u><u></u></p>
<div>
<p class="MsoNormal">Thanks for your response. Actually I don’t have time dimension. I have location level data (at high resolution).<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">For example, x(m) with lat(m) and lon (m) where m = 300k location <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">My output grid is 32x32 km and I would like to do following <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">1. Estimate the number of locations are in each 32x32 km grid<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">2. Accumulate (sum) the x at each output grid.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-bottom:12pt">For example- if I have 100 locations at one of the 32x32km grid, I will sum all x from 100 locations and assign the sum to the output grid.<u></u><u></u></p>
<div>
<p class="MsoNormal">Best regards, <u></u><u></u></p>
<div>
<p class="MsoNormal">Dave<u></u><u></u></p>
</div>
</div>
<div>
<p class="MsoNormal"><br>
<br>
<u></u><u></u></p>
<blockquote style="margin-top:5pt;margin-bottom:5pt">
<p class="MsoNormal">On Oct 18, 2020, at 3:50 AM, Barry Lynn <<a href="mailto:barry.h.lynn@gmail.com" target="_blank">barry.h.lynn@gmail.com</a>> wrote:<u></u><u></u></p>
</blockquote>
</div>
<blockquote style="margin-top:5pt;margin-bottom:5pt">
<div>
<div>
<p class="MsoNormal">Hi: <u></u><u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">If you have two grids of the same dimensions, but different times, you can simply add them together.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">If you have grids of a different size, you can interpolate the data to this grid, one grid at a time.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">See for example:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal"><a href="https://urldefense.proofpoint.com/v2/url?u=https-3A__www.ncl.ucar.edu_Applications_regrid.shtml&d=DwMFaQ&c=kn4_INW_mBCDHV_xJEVJkg&r=fpudUXThXLQwAKQPDCnmVaSzm2dwE9OxcWi7MuJ0jbo&m=IaFm8AuXStzIPZXMbIRmp6fjt5zy74eAUUQzoQb5pBc&s=tUIMDu-yehcnE2-mHTaX2NhZiAg5eBHJsqf3e1Bmbl8&e=" target="_blank">https://www.ncl.ucar.edu/Applications/regrid.shtml</a><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">This may also help.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal"><a href="https://urldefense.proofpoint.com/v2/url?u=https-3A__www.ncl.ucar.edu_Document_Functions_Built-2Din_dim-5Fsum-5Fn.shtml&d=DwMFaQ&c=kn4_INW_mBCDHV_xJEVJkg&r=fpudUXThXLQwAKQPDCnmVaSzm2dwE9OxcWi7MuJ0jbo&m=IaFm8AuXStzIPZXMbIRmp6fjt5zy74eAUUQzoQb5pBc&s=4tdKFBxFBOUpMYFj95WyO8MUdUcYRniTWsJIvCCbhAE&e=" target="_blank">https://www.ncl.ucar.edu/Document/Functions/Built-in/dim_sum_n.shtml</a><u></u><u></u></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal">On Sun, Oct 18, 2020 at 7:59 AM Paimazumder, Debasish via ncl-talk <<a href="mailto:ncl-talk@mailman.ucar.edu" target="_blank">ncl-talk@mailman.ucar.edu</a>> wrote:<u></u><u></u></p>
</div>
<blockquote style="border-style:none none none solid;border-left-width:1pt;border-left-color:rgb(204,204,204);padding:0in 0in 0in 6pt;margin:5pt 0in 5pt 4.8pt">
<div>
<div>
<p class="MsoNormal">Hi NCL users<u></u><u></u></p>
<p class="MsoNormal">I have location level data and 2d lat, lon grid and I’m trying to accumulate the data on 2d lat, lon grid.<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">For example
<u></u><u></u></p>
<p class="MsoNormal">Input - x (n) with lat(n), lon(n)<u></u><u></u></p>
<p class="MsoNormal">Output grid - lat(i)xlon(j)<u></u><u></u></p>
<p class="MsoNormal">I’m trying to estimate number of location within each grid and accumulate x at each grid.
<u></u><u></u></p>
<p class="MsoNormal">Is there any function in NCL that will solve this problem?<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">With regards<u></u><u></u></p>
<p class="MsoNormal">-Debasish
<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal"><b><span style="font-size:12pt;color:rgb(0,176,240)">Debasish PaiMazumder, PhD |</span></b><span style="font-size:12pt;color:rgb(102,102,102)">Tel
</span><a href="tel:%2B1%20215%20255%206371" target="_blank"><span style="font-size:12pt">+1 215 255 6052</span></a><span style="font-size:12pt;color:rgb(102,102,102)"> | Cell
</span><a href="tel:%2B1%20718%20913%206946" target="_blank"><span style="font-size:12pt">+1 267 449 5226</span></a><span style="font-size:12pt;color:rgb(102,102,102)"> |</span><a href="mailto:Debasish.Paimazumder@aig.com" target="_blank"><span style="font-size:12pt">Debasish.Paimazumder@aig.com</span></a><u></u><u></u></p>
</div>
</div>
</blockquote>
</div>
</div>
</blockquote>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</div>
</blockquote>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</div>
</blockquote></div></div></div>