Area weighted seasonal cummulative precipitation extraction based on crop yeild area file

I am new to R and have to calculate area weighted seasonal (Kharif= day 151-304 and Rabi= 305-90) cumulative precipitation over indus basin. Mean where there is some crop fraction (ranges from 0-1.57 for different crop types) area value is true either in full or a part of a cell, then precipitation only for that particular part of the grid will be used as mask to calculate cumulative precipitation

I have crop fractional area (cropfrac) file (.nc and .bin formats) with 30 time steps (1 value per year) with 32 layers for 32 crop types. I have daily precipitation data from 1981-2010 in ncdf 4 format. I have ascii files for areas (i.e. Indus, Ganges river basins).

To my understanding, first I have to read cropfrac file at all time steps and at all grid points (lat, lon) using loop and if there is some cropfractional area exist then I have to call precipitation data again for all time steps and al grid points, multiply it with cropfraction and grid size, sum precipitation over all time steps in a grid and finally divide by total crop and grid area. But how to make a connection in all these steps is missing.

Can anybody help or suggest me.

Thanking in advance and looking forward to receiving quick help and suggestions, please
direct contact ID:

Right now this question is pretty broad – I suggest you try to break it down into smaller pieces to try and make it easier for the community to help you with the parts that you're stuck on.

In general, it's much easier to discuss code by looking at the code itself. Could you turn what you have done thus far into a self-contained reprex (short for minimal reproducible example)? It will help us help you if we can be sure we're all working with/looking at the same stuff.

Currently, the best way to install reprex is:

# install.packages("devtools")

If you've never heard of a reprex before, you might want to start by reading the help page. The reprex dos and don'ts are also useful.

If you run into problems with access to your clipboard, you can specify an outfile for the reprex, and then copy and paste the contents into the forum.

reprex::reprex(input = "fruits_stringdist.R", outfile = "")

For pointers specific to the community site, check out the reprex FAQ, linked to below.

Dear Mara,

Thanks for your kind email and nice suggestion to get help from reprex

I have installed reprex and now on the clipboard.

I tried to make code with split parts i.e. to read cftfrac file, read
precipitation file but still not sure how to run loop first to identify
grids step by step and where there is some cropfractional area exist then
call precipitation data and to write as

My specific task is: first I have to read ascii file for a specific basin
say basin name= Indus and basinnrs= 1.

Then I have to read cropfractional area file (which contains
croppefractional area in some grids and is different for different crop
types) which contains 30 time steps for all grid points for 32 crop types.

nyearcft <- 30
Ncellcft <- lat*lon
nbandscft <- 32

for (year in 1:nyear)
for (i in 1:ncell)
for (k in 1:nbands)

then I have to apply some if statement i.e.
if(cropfractional area = true, read prec and write)

My precipitation file is a daily data in .nc format with 65975 gridcells.

nyearspre <- 30 (1981-2010)
ncells <- xval*yval

how to make a link between years/gridcells (i,j)/32 layers with
precipitation in each grid at all timesteps. after precipitation only for
cropfractional area grids, I want to cumulate for seasons

my code is attached herewith for your kind reference please,

kind reply or guidance is appreciated in advance

Thanks and best regards