<div dir="ltr"><div class="gmail_default" style="font-size:small">Ipsita,</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">I think the problem is with the &quot;mask&quot; function.</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">I always have a hard time remembering how this function works, so I remember this:</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">  The mask function protects the first argument at locations where the second argument is equal to the third argument.</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">The script has this line:</div><div class="gmail_default" style="font-size:small">







<p class="gmail-p1"><span class="gmail-s1">  precc_3d=</span><span class="gmail-s2">mask</span><span class="gmail-s1">(precc,precc_mask_3d,-9999)</span></p><p class="gmail-p1">Which effectively is going to keep the values of &quot;precc&quot; at locations where precc_mask_3d is equal to -9999. </p><p class="gmail-p1">I think what it should be is:</p><p class="gmail-p1">







</p><p class="gmail-p1"><span class="gmail-s1">  precc_3d=</span><span class="gmail-s2">mask</span><span class="gmail-s1">(precc,precc_mask_3d,1)</span></p><p class="gmail-p1">I&#39;m assuming that &quot;precc_mask_3d&quot; is set to the value 1 at locations where the data is inside India.</p><p class="gmail-p1">--Mary</p><p class="gmail-p1"><br></p><p class="gmail-p1"><br></p></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Mar 22, 2017 at 2:24 PM, Ipshita Majhi <span dir="ltr">&lt;<a href="mailto:ipmajhi@alaska.edu" target="_blank">ipmajhi@alaska.edu</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Hi Mary,<br><br></div>I looked at the plot , it is creating a plot where it is masking value over India, but I want value over India only with rest of the world masked out,I am attaching the plot with this email.Could you suggest the required changes. Thank you in advance.<br><br></div><div class="gmail_extra"><br><div class="gmail_quote"><span class="">On Fri, Mar 17, 2017 at 7:07 AM, Mary Haley <span dir="ltr">&lt;<a href="mailto:haley@ucar.edu" target="_blank">haley@ucar.edu</a>&gt;</span> wrote:<br></span><div><div class="h5"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_default" style="font-size:small">Hi Ipshita,</div><div class="gmail_default" style="font-size:small"><span style="font-size:12.8px"><br></span></div><div class="gmail_default" style="font-size:small"><span style="font-size:12.8px">The &quot;conform&quot; function doesn&#39;t do the masking. It simply propagates a 2D array to a 3D array. </span></div><div class="gmail_default" style="font-size:small"><span style="font-size:12.8px"><br></span></div><div class="gmail_default" style="font-size:small"><span style="font-size:12.8px">You need to take this 2D mask array, conform it to 3D, and then apply it to your original &quot;ts&quot; data in order to &quot;mask&quot; the locations in &quot;ts&quot; where the values are 1:</span></div><div class="gmail_default" style="font-size:small"><span style="font-size:12.8px"><br></span></div><div class="gmail_default" style="font-size:small"><div style="font-size:12.8px"><font face="monospace, monospace">;---Create a 2D mask array using India shapefile outlines</font></div><div style="font-size:12.8px"><font face="monospace, monospace"><span style="font-size:12.8px">  ts_mask_2d = mask_data_with_India_country(</span><span style="font-size:12.8px">I<wbr>ndia_code,ts(0,:,:))<br></span><br></font></div><div style="font-size:12.8px"><span style="font-size:12.8px"><font face="monospace, monospace">;---Conform this 2D array to a 3D array</font></span></div><div style="font-size:12.8px"><font face="monospace, monospace">  ts_mask_3d = conform(ts,ts_mask_2d,(/1,2/))</font></div><div style="font-size:12.8px"><font face="monospace, monospace"><br></font></div><div style="font-size:12.8px"><div style="font-size:12.8px"><font face="monospace, monospace">;---Apply the 3D mask to &quot;ts&quot;</font></div><div style="font-size:12.8px"><font face="monospace, monospace">  tm_3d = mask(ts,ts_mask_3d,1)    ; Keep ts when ts_mask_3d equal 1 </font></div><div style="font-size:12.8px"><font face="monospace, monospace">  </font></div><div><br></div></div></div><div class="gmail_default" style="font-size:small"><span style="font-size:12.8px">The tm_3d array should now be the one that you can average.</span></div><div class="gmail_default" style="font-size:small"><span style="font-size:12.8px"><br></span></div><div class="gmail_default" style="font-size:small"><span style="font-size:12.8px">By the way, there are some things you can do to clean up your code a little.</span></div><div class="gmail_default" style="font-size:small"><span style="font-size:12.8px"><br></span></div><div class="gmail_default" style="font-size:small"><span style="font-size:12.8px">[1] You don&#39;t need to use a &quot;do&quot; loop to calculate the average.  You can use &quot;dim_avg_n&quot; or &quot;dim_avg_n_Wrap&quot; (preserves metadata):</span></div><div class="gmail_default" style="font-size:small"><span style="font-size:12.8px"><br></span></div><div class="gmail_default"><div class="gmail_default"><span style="font-size:12.8px">prec_avg = dim_avg_n_Wrap(tm_3d,(/1,2/))</span></div><div class="gmail_default"><span style="font-size:12.8px"><br></span></div><div class="gmail_default">[2] You generally don&#39;t ever need to use &quot;new&quot; unless you need to populate that array inside a do loop by indexing it, or you just need to create an empty array for some reason.</div><div class="gmail_default"><br></div><div class="gmail_default">So, you can replace this code:</div><span><div class="gmail_default"><span style="font-size:12.8px"><br></span></div><div class="gmail_default"><span style="font-size:12.8px"> precp_mon_avg = new((/100,12/),&quot;float&quot;)</span></div><div class="gmail_default"><span style="font-size:12.8px"><br></span></div><div class="gmail_default"><span style="font-size:12.8px"> precp_mon_avg=reshape(prec_av<wbr>g,(/100,12/))</span></div><div class="gmail_default"><span style="font-size:12.8px"><br></span></div></span><div class="gmail_default"><span style="font-size:12.8px">with just one line:</span></div><div class="gmail_default"><span style="font-size:12.8px"><br></span></div><div class="gmail_default"><div class="gmail_default"><span style="font-size:12.8px"><font face="monospace, monospace"> precp_mon_avg = reshape(prec_avg,(/100,12/))</font></span></div><div class="gmail_default"><br></div><div class="gmail_default"><font face="arial, helvetica, sans-serif"><span style="font-size:12.8px">--Mary</span></font></div><div class="gmail_default"><font face="arial, helvetica, sans-serif"><span style="font-size:12.8px"><br></span></font></div><div><span style="font-size:12.8px"></span></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="m_-3799616607633107931h5">On Thu, Mar 16, 2017 at 1:48 PM, Ipshita Majhi <span dir="ltr">&lt;<a href="mailto:ipmajhi@alaska.edu" target="_blank">ipmajhi@alaska.edu</a>&gt;</span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="m_-3799616607633107931h5"><div dir="ltr"><div>Dear NCL Users,</div><div><br></div><div>I am using shapefile to analyze data over India, thanks to Mary got the shape file working and then also got the data conformed. But when I am doing area average for each month the values are the same . I am not sure what is going wrong. I am pasting my code in this email.</div><div><br></div><div><br></div><div>;*****************************<wbr>*******************</div><div>load &quot;$NCARG_ROOT/lib/ncarg/nclscri<wbr>pts/csm/gsn_code.ncl&quot; </div><div>load &quot;$NCARG_ROOT/lib/ncarg/nclscri<wbr>pts/csm/gsn_csm.ncl&quot; </div><div>load &quot;$NCARG_ROOT/lib/ncarg/nclscri<wbr>pts/csm/contributed.ncl&quot; </div><div>load &quot;$NCARG_ROOT/lib/ncarg/nclscri<wbr>pts/csm/shea_util.ncl&quot; </div><div>;*****************************<wbr>*******************</div><div>;Read data to plot and mask</div><div>;*****************************<wbr>******************* </div><div>function mask_data_with_India_country(c<wbr>ountry_code,data) </div><div>begin</div><div><br></div><div>mask_start_time = get_cpu_time()     ; For timing results</div><div><br></div><div>;---Convert rectilinear grid to 2D grid, but laid out as 1D array.</div><div>  dims  = dimsizes(data)</div><div>  lat1d = ndtooned(conform_dims(dims,dat<wbr>a&amp;$data!0$,0))</div><div>  lon1d = ndtooned(conform_dims(dims,dat<wbr>a&amp;$data!1$,1))</div><div><br></div><div>  shapefile_name = &quot;IND_adm0.shx&quot;</div><div> ;---Open shapefile and read lat/lon values.</div><div>  sfilename = &quot;IND_adm0.shp&quot;</div><div>  f = addfile(sfilename,&quot;r&quot;)</div><div>  segments = f-&gt;segments</div><div>  geometry = f-&gt;geometry</div><div>  segsDims = dimsizes(segments)</div><div>  geomDims = dimsizes(geometry)</div><div><br></div><div>;---Read global attributes  </div><div>  geom_segIndex = f@geom_segIndex</div><div>  geom_numSegs  = f@geom_numSegs</div><div>  segs_xyzIndex = f@segs_xyzIndex</div><div>  segs_numPnts  = f@segs_numPnts</div><div>  numFeatures   = geomDims(0)</div><div>  </div><div>  lon  = f-&gt;x</div><div>  lat  = f-&gt;y</div><div>  nlatlon = dimsizes(lat)</div><div>  </div><div>  min_lat = min(lat)</div><div>  max_lat = max(lat)</div><div>  min_lon = min(lon)</div><div>  max_lon = max(lon)</div><div><br></div><div>  print(&quot;=======================<wbr>===========================&quot;)</div><div>  print(&quot;Shapefile : &quot;  + sfilename)</div><div>  print(&quot;min_lat &quot; + min_lat)</div><div>  print(&quot;max_lat &quot; + max_lat)</div><div>  print(&quot;min_lon &quot; + min_lon)</div><div>  print(&quot;max_lon &quot; + max_lon)</div><div>  </div><div>   ii_latlon = ind(min_lat.le.lat1d.and.lat1d<wbr>.le.max_lat.and.\</div><div>                  min_lon.le.lon1d.and.lon1d.le.<wbr>max_lon)</div><div>  nii = dimsizes(ii_latlon)</div><div>  print(nii + &quot; values to check&quot;)</div><div>  print(numFeatures + &quot; feature(s) to traverse with a maximum of &quot; + \</div><div>        nlatlon + &quot; points&quot;)</div><div>  </div><div>  </div><div> ;---Create array to hold new data mask, and set all values to 0 initially.</div><div>  data_mask_1d = new(dimsizes(lat1d),integer)</div><div>  data_mask_1d = 0</div><div><br></div><div>;</div><div>; This is the loop that checks every point in lat1d/lon1d to see if it</div><div>; is inside or outside of the country. If it is inside, then data_mask_1d</div><div>; will be set to 1.</div><div>;</div><div>  ikeep = 0    ; Counter to see how many points were found inside the country</div><div>  do n=0,nii-1</div><div>    ii = ii_latlon(n)</div><div>    is_inside = False</div><div>    i = 0</div><div>    do while(.<a href="http://not.is_inside.and.i.lt" target="_blank">not.is_inside.and.i.lt</a>.<wbr>numFeatures)</div><div>       startSegment = geometry(i, geom_segIndex)</div><div>       numSegments  = geometry(i, geom_numSegs)</div><div>       do seg=startSegment, startSegment+numSegments-1</div><div>         startPT = segments(seg, segs_xyzIndex)</div><div>         endPT   = startPT + segments(seg, segs_numPnts) - 1</div><div>         if(data_mask_1d(ii).ne.1.and.<wbr>\</div><div>            gc_inout(lat1d(ii),lon1d(ii),\</div><div>                     lat(startPT:endPT),lon(startP<wbr>T:endPT))) then</div><div>           data_mask_1d(ii) = 1</div><div>           ikeep = ikeep+1</div><div>           is_inside = True</div><div>           continue</div><div>         end if</div><div>       end do</div><div>       i = i + 1</div><div>    end do</div><div>  end do</div><div>  print(ikeep + &quot; values kept&quot;)</div><div>  print(&quot;=======================<wbr>===========================&quot;) </div><div><br></div><div>; Create a 2D data array of same size as original data,</div><div>; but with appropriate values masked.</div><div>;</div><div>  data_mask = (where(onedtond(data_mask_1d,d<wbr>ims).eq.1,data,\</div><div>              data@_FillValue))</div><div>  copy_VarMeta(data,data_mask)      ; Copy all metadata</div><div><br></div><div>;---Print timings</div><div>  mask_end_time = get_cpu_time()</div><div>  print(&quot;Elapsed time in CPU second for &#39;mask_data_with_India_country&#39; = &quot; + \</div><div>         (mask_end_time-mask_start_tim<wbr>e))</div><div><br></div><div>  return(data_mask)</div><div>end</div><div>  </div><div>;-----------------------------<wbr>------------------------------<wbr>-----------</div><div>; Main code</div><div>;-----------------------------<wbr>------------------------------<wbr>-----------</div><div><br></div><div>;---Shapefile to use for masking. Using India (IND)</div><div>India_code = &quot;IND&quot;</div><div>India_name = &quot;IND&quot; + &quot;_adm0&quot;</div><div>India_shp  = &quot;IND_adm0.shp&quot;</div><div><br></div><div>;---Read lat/lon off shapefile</div><div> s         = addfile(India_shp,&quot;r&quot;)</div><div> slat      = s-&gt;y</div><div> slon      = s-&gt;x</div><div><br></div><div>;---Read precipitation data to contour and mask</div><div>  filename = &quot;<a href="http://b.e11.B1850C5CN.f09_g16.005.cam.h0.PRECC.070001-079912.nc" target="_blank">b.e11.B1850C5CN.f09_g16.005.c<wbr>am.h0.PRECC.070001-079912.nc</a>&quot;</div><div>  f        =  addfile(filename,&quot;r&quot;)</div><div>  ts       = f-&gt;PRECC(:,:,:)</div><div>  </div><div><br></div><div>;---Mask &quot;ts&quot; against India shapefile outlines</div><div>  ts_mask = mask_data_with_India_country(I<wbr>ndia_code,ts(0,:,:))</div><div>  tm_3d=conform(ts,ts_mask,(/1,2<wbr>/))</div><div><br></div><div>  </div><div><br></div><div>  tm_3d@_FillValue = ts@_FillValue</div><div>  </div><div>;  copy_VarAtts(ts,tm_3d)                    </div><div>  copy_VarCoords(ts,tm_3d)</div><div><br></div><div> print(tm_3d(100,{25},{70}))</div><div> print(tm_3d(110,{25},{74}))</div><div><br></div><div> printVarSummary(tm_3d)</div><div> printMinMax(tm_3d, True)</div><div><br></div><div> prec_avg = new((/1200/),&quot;float&quot;)</div><div><br></div><div> do ii =0,1199</div><div>  </div><div> prec_avg(ii) = avg(tm_3d(ii,:,:))</div><div><br></div><div> end do</div><div><br></div><div> precp_mon_avg = new((/100,12/),&quot;float&quot;)</div><div><br></div><div> precp_mon_avg=reshape(prec_av<wbr>g,(/100,12/))</div><div><br></div><div><br></div><div> precp_mon = dim_avg_n(precp_mon_avg,0)</div><div><br></div><div> print(precp_mon)</div><span class="m_-3799616607633107931m_-6771172571199963920HOEnZb"><font color="#888888"><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div>  </div><div><br></div><div><br></div>-- <br><div class="m_-3799616607633107931m_-6771172571199963920m_-2794746058020893777gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div><div><div>******************************<wbr>******************************<wbr>******************************<wbr>*************************</div><div><span style="font-size:small">&quot;I slept and dreamt that life was joy. I awoke and saw that life was service. I acted and behold, service was joy.&quot; - Rabindranath Tagore</span><br></div><div><span style="font-size:small">******************************<wbr>******************************<wbr>******************************<wbr>**************************</span></div><div><br></div><div>Ipshita Majhi<br></div>PhD Candidate<br></div>University of Alaska , Fairbanks<br></div>Atmospheric Science Department<br></div><a href="tel:(907)%20978-4220" value="+19079784220" target="_blank">(907)978-4220</a> <a href="mailto:ipmajhi@alaska.edu" target="_blank">ipmajhi@alaska.edu</a><br></div></div></div></div>
</font></span></div>
<br></div></div>______________________________<wbr>_________________<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/mailma<wbr>n/listinfo/ncl-talk</a><br>
<br></blockquote></div><br></div>
</blockquote></div></div></div><div><div class="h5"><br><br clear="all"><br>-- <br><div class="m_-3799616607633107931gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div><div><div>******************************<wbr>******************************<wbr>******************************<wbr>*************************</div><div><span style="font-size:small">&quot;I slept and dreamt that life was joy. I awoke and saw that life was service. I acted and behold, service was joy.&quot; - Rabindranath Tagore</span><br></div><div><span style="font-size:small">******************************<wbr>******************************<wbr>******************************<wbr>**************************</span></div><div><br></div><div>Ipshita Majhi<br></div>PhD Candidate<br></div>University of Alaska , Fairbanks<br></div>Atmospheric Science Department<br></div><a href="tel:(907)%20978-4220" value="+19079784220" target="_blank">(907)978-4220</a> <a href="mailto:ipmajhi@alaska.edu" target="_blank">ipmajhi@alaska.edu</a><br></div></div></div></div>
</div></div></div>
</blockquote></div><br></div>