Note that there are some explanatory texts on larger screens.

plurals
  1. POwriting a loop for upscaling precipitation for USA
    text
    copied!<p>I am writing a code to calculate the mean amount of precipitation for different regions of conterminous USA. My total data has 300 times 120 (lon*lat) grids in Netcdf format. I want to write a loop in R to take the average of each 10 by 10 number of grids and assign that value (average) to all of the grids inside the region and repeat this for the next region. At the end instead of a 120 by 300 grids I will have 12 by 30 grids. So this is kind a upscaling method I want to apply to my data. I can use a for-loop for each region separately but It makes my code very huge and I don’t want to do that. Any idea would be appreciated. Thanks. P.S: Here is the function I have written for one region (10by10) lat*lon.</p> <pre><code>upscaling &lt;- function(file, variable, start.time=1, count.time=1) { library(ncdf) # load ncdf library to manipulate ncdf data ncdata &lt;- open.ncdf(file); # open ncdf file lon &lt;- get.var.ncdf(ncdata, "lon"); lat &lt;- get.var.ncdf(ncdata, "lat"); time &lt;- get.var.ncdf(ncdata, "time"); start.lon &lt;- 1 end.lon &lt;- length(lon) start.lat &lt;- 1 end.lat &lt;- length(lat) count.lon &lt;- end.lon - start.lon + 1; # count number of longitude count.lat &lt;- end.lat - start.lat + 1; # count number of latitude dat &lt;- get.var.ncdf(ncdata, variable, start=c(start.lon, start.lat, 1), count=c(count.lon, count.lat, 1)) temp.data&lt;- array(0,dim=c(10,10)) for (i in 1:10) { for (j in 1:10) { temp.data &lt;- mean(dat[i,j,]) } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload