Пример #1
0
def calculate_discharge(params, mask, pnet):
    '''
    Calculate the accumulated water discharge 
    '''
    ldd = ascraster.Asciigrid(ascii_file=params.ldd, mask=mask, numtype=int)
    if params.ldebug: print(params.ldd + " has been read.")

    landarea = ascraster.Asciigrid(ascii_file=params.landarea,
                                   mask=mask,
                                   numtype=float)
    if params.ldebug: print(params.landarea + " has been read.")

    water = ascraster.duplicategrid(landarea)
    # Calculate accumulated water flux of each grid cell to the mouth of the river basin. Make discharge in km3
    # Convert pnet [mm/yr] to km3 by multiplying with landarea and 10e-6 (conversion from mm to km)

    for icell in range(landarea.length):
        wt = pnet.get_data(icell, 0.0) * landarea.get_data(icell, 0.0) * 1.0e-6
        water.set_data(icell, wt)

    discharge = accuflux.accuflux(ldd, water, negative_flux_possible=1)

    # Write discharge to output file:
    discharge.write_ascii_file(os.path.join(params.outputdir, "discharge.asc"))

    return discharge
Пример #2
0
def do_it(listelem):
    filename = listelem[0]
    num = listelem[1]
    base = "lala"
    if (num < 10):
        base = base + "000" + str(num)
    elif (num < 100):
        base = base + "00" + str(num)
    elif (num < 1000):
        base = base + "0" + str(num)
    else:
        base = base + str(num)

    grid1 = ascraster.Asciigrid(ascii_file=filename)

    isogrid = ascraster.Asciigrid(ascii_file="../gcountry.map")
    for icell in range(isogrid.length):
        iso = isogrid.get_data(icell)
        if (iso != None):
            # Make Greenland nodata
            if (int(float(iso)) == 304):
                grid1.set_data(icell, 0.0)

    grid = grid1.resize(3)
    #grid = grid1
    #grid.write_ascii_file(str(num) +".asc")

    #grid.save_as_bitmap("qq_"+str(num)+".png",minV=0.0001,maxV=maxV,mode="RGB",color=(1,0,0),number_of_classes = 150, classtype="log")
    grid.save_as_bitmap(base + ".png",minV=0.0,maxV=maxV,mode="RGB",color=(1,1,1),number_of_classes = 100, classtype="colorset.txt",\
                        nodatacolor=(225,225,225))

    # Put year in the image

    img = Image.open(base + ".png")
    draw = ImageDraw.Draw(img)
    font = ImageFont.truetype(
        "/usr/share/fonts/dejavu/DejaVuSansCondensed-BoldOblique.ttf", 60)
    draw.text((10, 30), str(5 * (num - 1) + 1900), (0, 0, 0), font=font)
    font = ImageFont.truetype(
        "/usr/share/fonts/dejavu/DejaVuSansCondensed-BoldOblique.ttf", 30)
    draw.text((10, 980), "pbl.nl", (100, 100, 100), font=font)

    slider(img, (200, 1070),
           1800,
           5 * (num - 1) + 1900,
           1900,
           2000,
           pos_text=True)

    img.save(base + ".png")

    print(filename + " is ready.")
Пример #3
0
def calculate(params, mask, residence_time_grid, lake_icell_dict):
    '''
    This function calculates the hydraulic load in the water bodies.
    '''

    # Depth water of waterbody in metres (result of residence time function)
    depth_grid = ascraster.Asciigrid(ascii_file=os.path.join(params.outputdir,"depth_waterbody_grid.asc"),\
                                              mask=mask,numtype=float)

    hydraulic_load_grid = ascraster.duplicategrid(residence_time_grid)
    hydraulic_load_grid.add_values(hydraulic_load_grid.length * [0.0])
    for icell in range(hydraulic_load_grid.length):
        depth = depth_grid.get_data(icell, 0.0)
        resi_time = residence_time_grid.get_data(icell, 0.0)
        try:
            Hl = depth / resi_time
        except ZeroDivisionError:
            Hl = 0.0
        hydraulic_load_grid.set_data(icell, Hl)

    # Write hydraulic load to output file:
    hydraulic_load_grid.write_ascii_file(
        os.path.join(params.outputdir, "hydraulic_load.asc"))

    return hydraulic_load_grid
Пример #4
0
def create_ncmask_from_ascii_file(asciifile):
    '''
    Returns a mask (numpy array of booleans) corresponding to ASCII file
    Grid cells with no data values in ASCII files are masked
    And a dictionnary containing properties of the ASCII grid (nb of rows, cols, xllcorner, yllcorner and cellsize)
    '''

    # Open ASCII file
    ascii_map = ascraster.Asciigrid(ascii_file=asciifile)

    # Create empty mask (nothing is masked yet)
    netcdf_mask = np.zeros((ascii_map.nrows, ascii_map.ncols), dtype=bool)

    # Create dictionnary containing map's properties
    ascii_map_prop = {}
    ascii_map_prop['ncols'] = ascii_map.ncols
    ascii_map_prop['nrows'] = ascii_map.nrows
    ascii_map_prop['xllcorner'] = ascii_map.xllcorner
    ascii_map_prop['yllcorner'] = ascii_map.yllcorner
    ascii_map_prop['cellsize'] = ascii_map.cellsize

    # For each cell, mask if no data in the ASCII grid
    #print 'Length of ASCII map  ' + str(ascii_map.length)
    #print 'ASCII map nodata value  ' + str(ascii_map.nodata_value)
    for icell in xrange(ascii_map.length):
        #print 'data ' + str(icell) + ': ' + str(map.get_data(icell))
        ilat, ilon = ascii_map.get_row_col_from_index(icell)

        #print 'ASCII map value for cell ' + str(icell) + '  ' + str(ascii_map.get_data(icell))
        if (ascii_map.get_data(icell) == None):
            netcdf_mask[ilat, ilon] = True

        #print 'netcdf_mask  ' + str(netcdf_mask)

    return netcdf_mask, ascii_map_prop
Пример #5
0
def ncgrid_from_asciifile(asciifile, nlat, nlon, netcdf_mask, fill_value=None):
    '''
    Creates an masked numpy array grid (to be included in NETCDF file)
    from an ASCII file
    '''

    # Open ASCII grid
    ascii_grid = ascraster.Asciigrid(ascii_file=asciifile, numtype=float)

    # Create empty masked numpy array
    netcdf_grid = np.ma.array(np.zeros((nlat, nlon)),
                              mask=netcdf_mask,
                              fill_value=fill_value)

    # Fill numpy array with values from the ASCII grid
    #for item in range(ascii_grid.length):
    #ilat,ilon = ascii_grid.get_row_col_from_index(item)
    for ilat in range(nlat):
        for ilon in range(nlon):
            item = ascii_grid.get_index_from_row_col(ilat, ilon)
            if not (netcdf_mask[ilat, ilon]):  #TEST
                if (ascii_grid.get_data(item) == None):
                    print('Oops! None value replaced by zero')
                    netcdf_grid[ilat, ilon] = 0.
                else:
                    netcdf_grid[ilat, ilon] = ascii_grid.get_data(item)

    return netcdf_grid
Пример #6
0
def aggregate(dict, name_id_grid, mask, filename, sep, keyname="basinid"):
    '''
    Aggreggate a dictionary of Mouth objects to another id grid and write the aggregation to output file.
    '''

    # Make a copy of the dictionary for the output
    new_dict = {}

    # Read id_grid
    id_grid = ascraster.Asciigrid(ascii_file=name_id_grid,
                                  mask=mask,
                                  numtype=int)

    for key in list(dict.keys()):
        ind = dict[key].index_grid
        if (ind == None):
            # There is no spatial location in isogrid found for this class.
            id = -9999
        else:
            id = int(id_grid.get_data(ind, -9999))
        try:
            new_dict[id].sum(dict[key])
        except KeyError:
            new_dict[id] = dict[key].copy()

    # Make a correction on the name
    for key in list(new_dict.keys()):
        setattr(new_dict[key], keyname, key)

    # Aggregate to global level
    for key in list(new_dict.keys()):
        try:
            new_dict["Total"].sum(new_dict[key])
        except KeyError:
            new_dict["Total"] = new_dict[key].copy()

    # Write all information of the river basin to output file:
    fp = open(filename, "w")
    for key in list(new_dict.keys()):
        new_dict[key].write_header(fp, sep=sep)
        break
    write_dict.write_dict(fp_in=fp,
                          filename=None,
                          dic=new_dict,
                          headerkey="",
                          sep=sep,
                          lkey=0,
                          ldebug=1,
                          fatal=0)
    fp.close()
Пример #7
0
def make_pointer(params,rivmask=None):
    '''
    Make a list of Pointer items with all information from each cell.
    '''
    # Read ldd map 
    lddmap = ascraster.Asciigrid(ascii_file=params.ldd,mask = rivmask,numtype=int)

    # Calculate the order of each cell
    ordermap = accuflux.determine_order(lddmap)

    # Make a pointer for the calculation order
    pointer1 = []
    for icell in range(ordermap.length):
        pointer1.append([iround(ordermap.get_data(icell)),icell])
    # Sort the pointer array so that lowest order is first calculated.
    pointer1.sort()

    # Create a dummy grid which has no mask to get the lat-lon numbers.
    dummy = ascraster.Asciigrid(ncols = lddmap.ncols, nrows = lddmap.nrows, \
                                xllcorner = lddmap.xllcorner, yllcorner = lddmap.yllcorner,\
                                cellsize = lddmap.cellsize)

    # Put all in a list of Pointer objects
    pointer_class = []
    for item in range(len(pointer1)):
        local_index = pointer1[item][1]
        iorder =  pointer1[item][0]
        if (rivmask == None):
            index_cell = local_index
        else:
            index_cell = rivmask[local_index]
        ilat,ilon = dummy.get_row_col_from_index(index_cell)
        pointer_class.append(Pointer(index_cell=index_cell,local_index=local_index,\
                                     ilat=ilat,ilon=ilon,iorder=iorder))

    return pointer_class
Пример #8
0
def check_nodata(filename, mask):
    '''
    This functions tests whether there are no nodata values given in this grid.
    When nodata is found, it will raise an error.
    '''

    # Open grid file
    grid = ascraster.Asciigrid(ascii_file=filename, mask=mask)

    # Check whether there is nodata.
    for icell in range(grid.length):
        val = grid.get_data(icell)
        if (val == None):
            # This should not happpen!
            # This should be corrected.
            raise MyError(filename +
                          " has nodata values which are not allowed.")
Пример #9
0
    def grid_add_scalar(self,label,filename,minimum=None, maximum=None):
        '''
        Make a new grid file for a sensitivity analyse. Filename is changed to output directory.
        '''
        if (self.label_present(label)):
            print(label + ' is found.')
            # Store old filename
            filename_old  = filename
            filename  = os.path.join(self.options.outputdir,os.path.basename(filename_old))
            # Read input file
            grid=ascraster.Asciigrid(ascii_file=filename_old)
            grid.add(float(getattr(self.options,label)),minimum=minimum,maximum=maximum)
            grid.write_ascii_file(filename)
            del grid
        else:
            print(label + ' is NOT found.')

        # Return new file name
        return filename
Пример #10
0
def read_netcdf(netcdf_filename, varname, outputdir, outfile, year):
    '''
    Read out of data "varname" out of netcdf file.
    If year==None, then all years are written to outputdirectory. 
    '''

    # Location and name specification of netcdf files.
    #pcglobwb_dir = "PCR_Routing/netCDF"
    #basename_netcdf = "pcrglobwb_CRU21_30min_"
    #extension_netcdf = "_yearly.nc"
    #outputdir = "pcr_routing_out"

    # Get the command line arguments
    #year = sys.argv[1]
    #varname = sys.argv[2]
    #outfile = sys.argv[3]
    #extensie =sys.argv[4]

    #extension_netcdf = extensie + extension_netcdf

    if (not os.path.isdir(outputdir)):
        os.mkdir(outputdir)

    #netcdf_file = os.path.join(pcglobwb_dir,basename_netcdf + varname + extension_netcdf)
    output_file = os.path.join(os.path.join(outputdir, str(year)), outfile)

    if (not os.path.isfile(netcdf_filename)):
        print("***** ERROR *****")
        print("File is not found.")
        print(("Looked for file: " + netcdf_filename))
        print("***** ERROR *****")
        raise IOError("File " + netcdf_filename + " is not found")

    # Load netcdf file
    nc = DS(netcdf_filename)

    # Check whether varname is in the netcdf file
    if (not varname in list(nc.variables.keys())):
        # There is a problem, varname is not available.
        print((varname + " is not found in file: " + netcdf_filename))
        print(("Parameters available: " +
               ",".join(map(str, list(nc.variables.keys())))))
        raise IOError("Parameter " + varname + " is not found in file " +
                      netcdf_filename)

    # Get the time settings of the nc file
    try:
        cdftime = utime(nc.variables['time'].units,
                        nc.variables['time'].calendar)
        start_year = cdftime.num2date(nc.variables['time'][0]).year
        end_year = cdftime.num2date(nc.variables['time'][-1]).year
    except AttributeError:
        # File is not time dependent.
        # Close netcdf file
        nc.close()
        read_netcdf_constant(netcdf_filename, varname, outputdir, outfile)

    if (year < start_year or year > end_year):
        print("***** ERROR *****")
        print(("Year: " + str(year) + " is not found in netcdf file " +
               netcdf_filename))
        print(("Years can be given between " + str(start_year) + " and " +
               str(end_year)))
        print("***** ERROR *****")
        raise IOError("Year " + str(year) +
                      " is outside the year range in file " + netcdf_filename)
    else:
        # Find the year in the netcdf file
        for i in range(len(nc.variables['time'])):
            if (cdftime.num2date(nc.variables['time'][i]).year == int(year)):
                iyear = i
                break
        else:
            print("***** ERROR *****")
            print(("Specified year " + str(year) +
                   " is not found in netcdf file " + netcdf_filename))
            print("Years available: ")
            for i in range(len(cdftime.num2date(nc.variables['time']))):
                print(cdftime.num2date(nc.variables['time'][i]).year)
            raise IOError("Year " + str(int(year)) + " is not found in file " +
                          netcdf_filename)

    # Get the variable.
    nc_var = nc.variables[varname][iyear, :, :]
    lat = nc.variables["latitude"][:]
    lon = nc.variables["longitude"][:]
    nrows = len(lat)
    ncols = len(lon)

    # Close netcdf file
    nc.close()

    dim = nc_var.shape

    if (dim[0] != nrows or dim[1] != ncols):
        print(("File: " + netcdf_filename + " is not consistent."))
        print(("In parameter file: nrows = ", nrows, " and ncols = ", ncols))
        print(("In netcdf file: nrows = ", dim[0], " and ncols = ", dim[1]))

    # General settings for ascii grid format
    ncols = 720
    nrows = 360
    xll = -180
    yll = -90
    cellsize = 0.5
    nodata_value = -9999

    # Create a raster without a mask and filled with nodata.
    out_grid = ascraster.Asciigrid(ncols=ncols,nrows=nrows,\
                               xllcorner=xll, \
                               yllcorner = yll, \
                               cellsize = cellsize,\
                               nodata_value = nodata_value,\
                               numtype=float)

    # Put the nc_var data into an ascraster object
    for i in range(nrows):
        for j in range(ncols):
            # Check whether there is no data
            if (type(nc_var[i, j]) != numpy.ma.core.MaskedConstant):
                icell = out_grid.get_index_from_coordin(lon[j], lat[i])
                out_grid.set_data(icell, nc_var[i, j])

    if (not os.path.isdir(os.path.join(outputdir, year))):
        os.mkdir(os.path.join(outputdir, year))

    out_grid.write_ascii_file(output_file)
Пример #11
0
def read_netcdf_constant(netcdf_filename, varname, outputdir, outfile):
    '''
    Read out of data "varname" out of netcdf file.
    If year==None, then all years are written to outputdirectory. 
    '''

    if (not os.path.isdir(outputdir)):
        os.mkdir(outputdir)

    # Make output file
    output_file = os.path.join(outputdir, outfile)

    if (not os.path.isfile(netcdf_filename)):
        print("***** ERROR *****")
        print("File is not found.")
        print(("Looked for file: " + netcdf_filename))
        print("***** ERROR *****")
        raise IOError("File " + netcdf_filename + " is not found")

    # Load netcdf file
    nc = DS(netcdf_filename)

    # Check whether varname is in the netcdf file
    if (not varname in list(nc.variables.keys())):
        # There is a problem, varname is not available.
        print((varname + " is not found in file: " + netcdf_filename))
        print(("Parameters available: " +
               ",".join(map(str, list(nc.variables.keys())))))
        raise IOError("Parameter " + varname + " is not found in file " +
                      netcdf_filename)

    # Get the variable.
    nc_var = nc.variables[varname][:, :]
    try:
        lat = nc.variables["latitude"][:]
    except KeyError:
        lat = nc.variables["lat"][:]
    try:
        lon = nc.variables["longitude"][:]
    except KeyError:
        lon = nc.variables["lon"][:]
    nrows = len(lat)
    ncols = len(lon)

    # Close netcdf file
    nc.close()

    dim = nc_var.shape

    if (dim[0] != nrows or dim[1] != ncols):
        print(("File: " + netcdf_filename + " is not consistent."))
        print(("In parameter file: nrows = ", nrows, " and ncols = ", ncols))
        print(("In netcdf file: nrows = ", dim[0], " and ncols = ", dim[1]))

    # General settings for ascii grid format
    ncols = 720
    nrows = 360
    xll = -180
    yll = -90
    cellsize = 0.5
    nodata_value = -9999

    # Create a raster without a mask and filled with nodata.
    out_grid = ascraster.Asciigrid(ncols=ncols,nrows=nrows,\
                               xllcorner=xll, \
                               yllcorner = yll, \
                               cellsize = cellsize,\
                               nodata_value = nodata_value,\
                               numtype=float)

    # Put the nc_var data into an ascraster object
    for i in range(nrows):
        for j in range(ncols):
            # Check whether there is no data
            if (type(nc_var[i, j]) != numpy.ma.core.MaskedConstant):
                icell = out_grid.get_index_from_coordin(lon[j], lat[i])
                out_grid.set_data(icell, nc_var[i, j])

    if (not os.path.isdir(os.path.join(outputdir, year))):
        os.mkdir(os.path.join(outputdir, year))

    out_grid.write_ascii_file(output_file)
Пример #12
0
def calculate(iriv, lock, params, species, proc, sources):
    '''
    Main function to calculate a riverbasin or a set of riverbasins.
    '''
    # Get the river id's which are being calculated.
    if (type(iriv) == list):
        rivlist = iriv
    else:
        rivlist = [iriv]
    print("Rivernumbers: ", rivlist)

    # Make a dictionary with river ids for each cell in the mask.
    rivernum = {}
    # Make mask for this collection of basins.
    rivmask = []
    cellnumber = 0

    # Read grid file with all basin ids without mask.
    basin = ascraster.Asciigrid(ascii_file=params.basin, numtype=int)

    # Read grid file with all basin ids.
    for icell in range(basin.length):
        id = basin.get_data(icell)
        if (id != None):
            if (iround(id) in rivlist):
                rivmask.append(icell)
                rivernum[cellnumber] = iround(id)
                cellnumber += 1

    # Set jobid for this collection of riverbasins.
    jobid = rivlist[0]
    print("Reading mask for riverids with jobid", jobid)

    # Read ldd map
    lddmap = ascraster.Asciigrid(ascii_file=params.ldd,
                                 mask=rivmask,
                                 numtype=int)
    print("Reading ldd for riverid ", jobid)

    # Make a pointer for the calculation order
    pointer1 = pointer_class.make_pointer(params, rivmask=rivmask)

    # Set temp output directory for this river basin.
    # First check the temp directory
    #params.tmp_dir = directory.ensure(os.path.join(params.outputdir, params.tmp_dir))

    #unique_jobid = str(int(random.random()*1000))+"_"+str(jobid)
    tmpdir = os.path.join(params.tmp_dir, str(jobid))

    # Create temp output directory for this riverbasin
    #if os.path.exists(tmpdir):
    # Remove old directory.
    #    my_sys.my_rmdir(tmpdir)
    # Make new temporary output directory
    os.mkdir(tmpdir)

    print(tmpdir)

    # Create photosynthese files for each species which needs photosynthese.
    if (len(params.totalphotindex) > 0):
        photo_list = []
        for ispec in params.totalphotindex:
            specname = species[ispec].get_name()
            fp = open(os.path.join(params.outputdir,\
                            os.path.splitext(os.path.basename(params.photosyn_file))[0]\
                             + "_" + specname + ".pkl"),"rb")

            photo_list.append(list(general_func.pickleLoader(fp)))
            fp.close()

        for item in range(len(pointer1)):
            icell = pointer1[item].get_index()
            # Get the latitude from the cell
            lon, lat = basin.get_coordin_from_index(icell)
            ind = iround((90.0 - lat + 0.5 * basin.cellsize) / basin.cellsize)
            fp_out = open(
                os.path.join(
                    tmpdir,
                    "photo_" + str(pointer1[item].get_local_index()) + ".pkl"),
                "wb")
            for i in range(len(params.totalphotindex)):
                pickle.dump(photo_list[i][ind][1:], fp_out, -1)
            fp_out.close()

    # Read the constant channel_width of PCRGLOBWB. # added in volume_depth.... calculations
    channel_width_grid = ascraster.Asciigrid(ascii_file=params.channel_width,\
                                             mask=rivmask,numtype=float)

    # Read the slopes of the cells
    slopemap = ascraster.Asciigrid(ascii_file=params.slope,\
                                   mask=rivmask,numtype=float)

    if params.lfloodplains == 1:
        norders = params.norder + 1
    else:
        norders = params.norder

    #outlakes_dict = make_outlakes_dict.calculate(params,pointer1)

    # Get all data from the input files with multiprocessing.
    if (params.ncpu_read_files > 1):

        # Make preparations for multiprocessing this part of the code.
        # Use the local processing power and use the multiprocessing module of python.
        import multiprocessing as mp

        # Set the number of cpu that will be used.
        number_of_cpu = min(mp.cpu_count(), params.ncpu_read_files)

        # Make a list of jobs which are active
        jobs = []

        outlakes_dict = make_outlakes_dict.calculate(params, pointer1)

        # Calculate volume and depth.
        #p=mp.Process(target= volume_depth_netcdf.calculate, args=(params,rivmask,tmpdir,pointer1,str(jobid),True))
        #p=mp.Process(target= volumes_depths.calculate, args=(params,rivmask,tmpdir,pointer1,outlakes_dict,str(jobid),True))
        p = mp.Process(target=vol_depth_vel.calculate,
                       args=(params, rivmask, tmpdir, pointer1, outlakes_dict,
                             str(jobid), True))  #LV 14-02-2018
        # Add the process handler to the jobs list.
        jobs.append(p)
        # Start the process
        general_func.start_process(p, jobs, number_of_cpu)

        # Read lake/reservoir id's for the whole period and put this time dependent object.
        p=mp.Process(target= get_dynamic_gridinfo.get_dynamic_gridinfo,\
                     args=(params,pointer1,os.path.join(params.water_inputdir,params.lakeid),params.lakeid_varname,None,\
                           os.path.join(tmpdir,"lakeid_"+str(jobid)+".pkl")))
        # Add the process handler to the jobs list.
        jobs.append(p)
        # Start the process
        general_func.start_process(p, jobs, number_of_cpu)

        # Read lake/reservoir outflow id's for the whole period and put this time dependent object.
        p=mp.Process(target= get_dynamic_gridinfo.get_dynamic_gridinfo,\
                     args=(params,pointer1,os.path.join(params.water_inputdir,params.endo_lakes),params.endo_lakes_varname,None,\
                           os.path.join(tmpdir,"endo_lakes_"+str(jobid)+".pkl")))
        # Add the process handler to the jobs list.
        jobs.append(p)
        # Start the process
        general_func.start_process(p, jobs, number_of_cpu)

        # Read endoreic lake/reservoir id's for the whole period and put this time dependent object.
        p=mp.Process(target= get_dynamic_gridinfo.get_dynamic_gridinfo,\
                     args=(params,pointer1,os.path.join(params.water_inputdir,params.outlakeid),params.outlakeid_varname,None,\
                           os.path.join(tmpdir,"outlakeid_"+str(jobid)+".pkl")))
        # Add the process handler to the jobs list.
        jobs.append(p)
        # Start the process
        general_func.start_process(p, jobs, number_of_cpu)

        # Read the discharge for the whole period and put this time dependent object km3/yr.
        p=mp.Process(target= get_dynamic_gridinfo.get_dynamic_gridinfo,\
                     args=(params,pointer1,os.path.join(params.water_inputdir,params.discharge),params.discharge_varname,None,\
                           os.path.join(tmpdir,"discharge_"+str(jobid)+".pkl")))
        # Add the process handler to the jobs list.
        jobs.append(p)
        # Start the process
        general_func.start_process(p, jobs, number_of_cpu)

        # Read the surface water area of lakes and resrvoirs m2
        p=mp.Process(target= get_dynamic_gridinfo.get_dynamic_gridinfo,\
                     args=(params,pointer1,os.path.join(params.water_inputdir,params.water_area),params.water_area_varname,None,\
                           os.path.join(tmpdir,"water_area_"+str(jobid)+".pkl")))
        # Add the process handler to the jobs list.
        jobs.append(p)
        # Start the process
        general_func.start_process(p, jobs, number_of_cpu)

        # Read the runoff for the whole period and put this time dependent object mm/yr.
        p=mp.Process(target= get_dynamic_gridinfo.get_dynamic_gridinfo,\
                     args=(params,pointer1,os.path.join(params.water_inputdir,params.pnet),params.pnet_varname,None,\
                           os.path.join(tmpdir,"runoff_"+str(jobid)+".pkl")))
        # Add the process handler to the jobs list.
        jobs.append(p)
        # Start the process
        general_func.start_process(p, jobs, number_of_cpu)

        # Read the temperature for the whole period and put this time dependent object in degree Celcius.
        p=mp.Process(target= get_dynamic_gridinfo.get_dynamic_gridinfo,\
                     args=(params,pointer1,os.path.join(params.water_inputdir,params.temperature),params.temperature_varname,None,\
                           os.path.join(tmpdir,"temperature_"+str(jobid)+".pkl")))
        # Add the process handler to the jobs list.
        jobs.append(p)
        # Start the process
        general_func.start_process(p, jobs, number_of_cpu)

        # Read the global radiation for the whole period and put this time dependent object in W/m2.
        p=mp.Process(target= get_dynamic_gridinfo.get_dynamic_gridinfo, args=(params,pointer1,\
                             os.path.join(params.water_inputdir,params.global_radiation),params.global_radiation_varname,None,\
                             os.path.join(tmpdir,"global_radiation_"+str(jobid)+".pkl")))
        # Add the process handler to the jobs list.
        jobs.append(p)
        # Start the process
        general_func.start_process(p, jobs, number_of_cpu)

        # Read the low vegetation fraction for the whole period and put this time dependent object in dimensionless
        p=mp.Process(target= get_dynamic_gridinfo.get_dynamic_gridinfo, args=(params,pointer1,\
                             os.path.join(params.water_inputdir, '..', 'other', params.low_veg_fr),params.low_veg_fr_varname,None,\
                             os.path.join(tmpdir,"low_veg_fr_"+str(jobid)+".pkl")))
        # Add the process handler to the jobs list.
        jobs.append(p)
        # Start the process
        general_func.start_process(p, jobs, number_of_cpu)

        # Read the high vegetation fraction for the whole period and put this time dependent object in dimensionless
        p=mp.Process(target= get_dynamic_gridinfo.get_dynamic_gridinfo, args=(params,pointer1,\
                             os.path.join(params.water_inputdir, '..', 'other', params.high_veg_fr),params.high_veg_fr_varname,None,\
                             os.path.join(tmpdir,"high_veg_fr_"+str(jobid)+".pkl")))
        # Add the process handler to the jobs list.
        jobs.append(p)
        # Start the process
        general_func.start_process(p, jobs, number_of_cpu)

        # Read loading for all the sources
        for isrc in range(len(sources)):
            filename = os.path.join(params.load_inputdir,
                                    sources[isrc].get_val('name') + '.nc')
            temp_distrib = None
            if ('temporal' in sources[isrc].get_attrib()):
                temp_distrib = sources[isrc].get_val('temporal')
            p=mp.Process(target= get_dynamic_gridinfo.get_dynamic_gridinfo,\
                         args=(params,pointer1,filename,sources[isrc].get_val('name'),temp_distrib,\
                               os.path.join(tmpdir,sources[isrc].get_val('name')+"_"+str(jobid)+".pkl")))
            # Add the process handler to the jobs list.
            jobs.append(p)
            # Start the process
            general_func.start_process(p, jobs, number_of_cpu)

        # Wait until all jobs are finished.
        for p in jobs:
            p.join()

        # Get all the content of the files which dumped to file.
        # LV 06-02-2017: added volume and depth of water in floodplains
        #volume_cells = general_func.get_content_rm(os.path.join(tmpdir,"volume_"+str(jobid)+".pkl"))
        #depth_cells = general_func.get_content_rm(os.path.join(tmpdir,"depth_"+str(jobid)+".pkl"))
        volume_cells = general_func.get_content_rm(
            os.path.join(tmpdir, "volume_c_" + str(jobid) + ".pkl"))
        depth_cells = general_func.get_content_rm(
            os.path.join(tmpdir, "depth_c_" + str(jobid) + ".pkl"))
        volume_fp_cells = general_func.get_content_rm(
            os.path.join(tmpdir, "volume_fp_" + str(jobid) + ".pkl"))
        depth_fp_cells = general_func.get_content_rm(
            os.path.join(tmpdir, "depth_fp_" + str(jobid) + ".pkl"))
        vel_cells = general_func.get_content_rm(
            os.path.join(tmpdir,
                         "velocity_" + str(jobid) + ".pkl"))  #LV 14-02-2018
        water_area_cells = general_func.get_content_rm(
            os.path.join(tmpdir,
                         "water_area_" + str(jobid) + ".pkl"))  #LV 29-11-2017
        runoff_cells = general_func.get_content_rm(
            os.path.join(tmpdir, "runoff_" + str(jobid) + ".pkl"))
        lakeid_cells = general_func.get_content_rm(
            os.path.join(tmpdir, "lakeid_" + str(jobid) + ".pkl"))
        outlakeid_cells = general_func.get_content_rm(
            os.path.join(tmpdir, "outlakeid_" + str(jobid) + ".pkl"))
        endo_lakes_cells = general_func.get_content_rm(
            os.path.join(tmpdir, "endo_lakes_" + str(jobid) + ".pkl"))
        discharge_cells = general_func.get_content_rm(
            os.path.join(tmpdir, "discharge_" + str(jobid) + ".pkl"))
        temperature_cells = general_func.get_content_rm(
            os.path.join(tmpdir, "temperature_" + str(jobid) + ".pkl"))
        globrad_cells = general_func.get_content(
            os.path.join(tmpdir, "globrad_" + str(jobid) + ".pkl"))
        low_veg_fr_cells = general_func.get_content(
            os.path.join(tmpdir, "low_veg_fr_" + str(jobid) + ".pkl"))
        high_veg_fr_cells = general_func.get_content(
            os.path.join(tmpdir, "high_veg_fr_" + str(jobid) + ".pkl"))

        srcload = []
        for isrc in range(len(sources)):
            filename = os.path.join(
                tmpdir,
                sources[isrc].get_val('name') + "_" + str(jobid) + ".pkl")
            srcload.append(general_func.get_content(filename))

    else:

        outlakes_dict = make_outlakes_dict.calculate(params, pointer1)

        # Calculate volume and depth.
        #volume_cells,depth_cells = volume_depth_netcdf.calculate(params,rivmask,tmpdir,pointer1,str(jobid),False)
        #volume_cells,depth_cells, volume_fp_cells, depth_fp_cells = volumes_depths.calculate(params,rivmask,tmpdir,pointer1,outlakes_dict,str(jobid),False)
        volume_cells, depth_cells, volume_fp_cells, depth_fp_cells, vel_cells = vol_depth_vel.calculate(
            params, rivmask, tmpdir, pointer1, outlakes_dict)  #LV 14-02-2018
        #print "vel_cells[3]  " + str(vel_cells[3])

        # Read surface water area of lakes and reservoirs m2
        water_area_cells = get_dynamic_gridinfo.get_dynamic_gridinfo(
            params, pointer1,
            os.path.join(params.water_inputdir, params.water_area),
            params.water_area_varname)  #LV 29-11-2017

        # Read the runoff for the whole period and put this time dependent object mm/yr.
        runoff_cells = get_dynamic_gridinfo.get_dynamic_gridinfo(
            params, pointer1, os.path.join(params.water_inputdir, params.pnet),
            params.pnet_varname)

        # Read the lake information for the whole period and put this time dependent object.
        lakeid_cells = get_dynamic_gridinfo.get_dynamic_gridinfo(
            params, pointer1, os.path.join(params.water_inputdir,
                                           params.lakeid),
            params.lakeid_varname)
        outlakeid_cells = get_dynamic_gridinfo.get_dynamic_gridinfo(
            params, pointer1,
            os.path.join(params.water_inputdir, params.outlakeid),
            params.outlakeid_varname)
        endo_lakes_cells = get_dynamic_gridinfo.get_dynamic_gridinfo(
            params, pointer1,
            os.path.join(params.water_inputdir, params.endo_lakes),
            params.endo_lakes_varname)

        # Read the discharge for the whole period and put this time dependent object km3/yr.
        discharge_cells = get_dynamic_gridinfo.get_dynamic_gridinfo(
            params, pointer1,
            os.path.join(params.water_inputdir, params.discharge),
            params.discharge_varname)

        # Read the temperature for the whole period and put this time dependent object in degree Celcius.
        temperature_cells = get_dynamic_gridinfo.get_dynamic_gridinfo(
            params, pointer1,
            os.path.join(params.water_inputdir, params.temperature),
            params.temperature_varname)

        # Read the global radiation for the whole period and put this time dependent object in degree W/m2
        globrad_cells = get_dynamic_gridinfo.get_dynamic_gridinfo(
            params, pointer1,
            os.path.join(params.water_inputdir, params.global_radiation),
            params.global_radiation_varname)

        low_veg_fr_cells = get_dynamic_gridinfo.get_dynamic_gridinfo(
            params, pointer1,
            os.path.join(params.water_inputdir, params.low_veg_fr),
            params.low_veg_fr_varname)

        high_veg_fr_cells = get_dynamic_gridinfo.get_dynamic_gridinfo(
            params, pointer1,
            os.path.join(params.water_inputdir, params.high_veg_fr),
            params.high_veg_fr_varname)

        # Read loads from all sources and apply temporal pattern
        srcload = []
        for isrc in range(len(sources)):
            filename = os.path.join(params.load_inputdir,
                                    sources[isrc].get_val('name') + '.nc')
            temp_distrib = None
            if ('temporal' in sources[isrc].get_attrib()):
                temp_distrib = sources[isrc].get_val('temporal')
            srcload.append(get_dynamic_gridinfo.get_dynamic_gridinfo(params,pointer1,filename,\
                                                                     sources[isrc].get_val('name'),\
                                                                     temp_distrib=temp_distrib))

    # Create strahler argument list if gridded chracteristics
    args_strahler_cell = None
    if (params.lstrahlergrids):
        args_strahler = define_subgrid_streamorder.define_subgrid_streamorder_riverbasin(
            params, rivmask)

    # Make preparations for multiprocessing of riverbasin dynamic calculations.
    number_of_cpu = general_func.get_number_of_cores(params, len(pointer1))

    if (params.ncpu_riverbasin > 1 and number_of_cpu > 1):
        # Make preparations for multiprocessing this part of the code.
        # Use the local processing power and use the multiprocessing module of python.
        import multiprocessing as mp

        # Set the number of cpu that will be used.
        number_of_cpu = min(mp.cpu_count(), number_of_cpu + 1)

        # Make a list of jobs which are active
        jobs = []

        # Prev_order is needed to find out which cells can be run at the same time. Cells of the same order can run at the same time.
        prev_order = pointer1[0].get_iorder()

        # Make lock for write and reading loads.
        lock_cell = {}
    else:
        number_of_cpu = 1

    # Read start file and extract information for all cells in this riverbasin and
    # store all information of the cells in separate input files.
    # In case of lspinup == 1 there is no startup file, so we have to create one ourselves.
    if ((params.lspinup == -1) or (params.lspinup == 0)):
        lfound = False
        # Open output file for first cell of this riverbasin.
        icell_prev = pointer1[0].get_index()
        local_cell_number = rivmask.index(icell_prev)
        fp_out = open(os.path.join(tmpdir,
                                   str(local_cell_number) + "_-1.pkl"), "wb")

        # Make a dictionary which indicates whether a river is found or not.
        lfound = {}
        for i in rivlist:
            lfound[i] = False

        # Read the startup file of all the riverbasins.
        with open(params.startup_file, 'rb') as fp:
            # Read header and skip this
            header_skip = pickle.load(fp)

            # Read header and skip this
            general_func.pickleLoader(fp)

            for line in general_func.pickleLoader(fp):
                # Find first line with the riverid iriv, then lfound will be true.
                if (line[0] in rivlist):
                    # Now the start of the riverbasin is found.
                    lfound[iround(line[0])] = True
                    icell = line[1]
                    if (icell != icell_prev):
                        # Now there is another cell found. So close the previous file and open a new one for the new cell.
                        fp_out.close()
                        local_cell_number = rivmask.index(icell)
                        fp_out = open(
                            os.path.join(tmpdir,
                                         str(local_cell_number) + "_-1.pkl"),
                            "wb")

                    # Put starting year in the file by overwriting the cellnumber.
                    line[1] = params.starttime
                    # Write cell info (starttime, and info about all the specs to output file.
                    pickle.dump(line[1:], fp_out, -1)
                    icell_prev = icell

            fp_out.close()

        lerror = False
        for key in rivlist:
            if (not lfound[key]):
                print("Startup file has no information on riverbasin " +
                      str(key))
                lerror = True
        if (lerror):
            raise MyError("Startup file has no information for riverbasins.")

    else:
        # We have to make an inital condition
        lsteady = True
        timeperiod = -1
        yearstart = params.starttime
        yearend = min(yearstart + params.outputtime, params.endtime)
        #lakes_dict = {}
        #outlakes_dict = {}
        # Loop over all cells:
        for item in range(len(pointer1)):
            #icell = pointer1[item][1]
            icell = pointer1[item].get_local_index()
            direction = lddmap.get_data(icell)
            next_cell = accuflux.goto_nextcell(icell,
                                               direction,
                                               lddmap.ncols,
                                               mask=rivmask)

            # Load is the time array of  all the sources
            load = []
            for isrc in range(len(sources)):
                load.append(srcload[isrc][icell])

            # Check whether cell is a lake/reservoir.
            lakeid = interpolate_list.stepwise(yearstart,
                                               lakeid_cells[icell],
                                               extrapol=1)[-1]
            if (lakeid > 0):
                outlakeid = interpolate_list.stepwise(yearstart,
                                                      outlakeid_cells[icell],
                                                      extrapol=1)[-1]
                endo_lake = interpolate_list.stepwise(yearstart,
                                                      endo_lakes_cells[icell],
                                                      extrapol=1)[-1]
                llake = True
                llakeout = (outlakeid > 0.)
                lendolake = (endo_lake > 0.)
                #lakes_dict[icell] = iround(lakeid)
                #if (llakeout or lendolake):
                #    outlakes_dict[iround(lakeid)] = icell
            else:
                llake = False
                llakeout = False
                lendolake = False

            if (number_of_cpu > 1):

                try:
                    qq = lock_cell[next_cell]
                except KeyError:
                    lock_cell[next_cell] = mp.Lock()

                if (prev_order != pointer1[item].get_iorder()):
                    # We have to wait untill all processes are ready, before we can continue
                    for p in jobs:
                        p.join()
                    prev_order = pointer1[item].get_iorder()
                    jobs = []

                # Define the process with cellid "icell", return value p is process "handler"
                width = channel_width_grid.get_data(icell)
                slope = slopemap.get_data(icell)

                if (params.lstrahlergrids):
                    args_strahler_cell = args_strahler[icell]
                p=mp.Process(target= dgnm_cell.calculate_cell,\
                             args=(lsteady,lock_cell[next_cell],icell,params,species,proc,sources,\
                                   tmpdir,next_cell,yearstart,yearend,timeperiod,\
                                   runoff_cells[icell],load,temperature_cells[icell],\
                                   globrad_cells[icell], discharge_cells[icell],\
                                   volume_cells[icell],water_area_cells[icell],depth_cells[icell],width,slope,\
                                   volume_fp_cells[icell],depth_fp_cells[icell],vel_cells[icell],\
                                   low_veg_fr_cells[icell],high_veg_fr_cells[icell],llake,llakeout,lendolake,args_strahler_cell))

                # Add the process handler to the jobs list.
                jobs.append(p)
                # Start the process
                general_func.start_process(p, jobs, number_of_cpu)

            else:
                # Do the job one at the time.
                width = channel_width_grid.get_data(icell)
                slope = slopemap.get_data(icell)

                if (params.lstrahlergrids):
                    args_strahler_cell = args_strahler[icell]
                dgnm_cell.calculate_cell(lsteady,None,icell,params,species,proc,sources,\
                                              tmpdir,\
                                              next_cell,yearstart,yearend,timeperiod,\
                                              runoff_cells[icell],load,temperature_cells[icell],\
                                              globrad_cells[icell], discharge_cells[icell],\
                                              volume_cells[icell],water_area_cells[icell],depth_cells[icell],width,slope,\
                                              volume_fp_cells[icell],depth_fp_cells[icell],vel_cells[icell],\
                                              low_veg_fr_cells[icell],high_veg_fr_cells[icell],llake,llakeout,lendolake,args_strahler_cell=args_strahler_cell)

        if (number_of_cpu > 1):
            # Wait until all jobs are finished.
            for p in jobs:
                p.join()

        # Put cell information of the last time step into the world map.
        data_block = []
        data_block1 = []
        outputlist = []
        startoutputlist = []
        conclist = []
        budlist = []
        argslist = []
        data_block2 = []
        data_block3 = []

        # Read the pickled data for individual cells and store in one list (per river basin)
        for item in range(len(pointer1)):
            icell = pointer1[item].get_local_index()

            # For amounts
            with open(
                    os.path.join(tmpdir,
                                 str(icell) + "_" + str(timeperiod) + ".pkl"),
                    "rb") as fp:
                for line in general_func.pickleLoader(fp):
                    data_block.append(line)
            fp.close()
            # Store only the last output time of the cell.
            for i in range(len(data_block) - norders, len(data_block)):
                # Put riverid and cellnumber in front of the line and remove the time.
                linestart = [rivernum[icell], rivmask[icell]]
                linestart.extend(data_block[i][1:])
                startoutputlist.append(linestart)
                # To be able to print species amounts/concentrations for all orders
                if (params.allorders == 1):
                    outputlist.append(linestart)
                else:
                    if (i == (len(data_block) - 1)):
                        outputlist.append(linestart)

            # For concentrations
            with open(
                    os.path.join(
                        tmpdir,
                        "conc_" + str(icell) + "_" + str(timeperiod) + ".pkl"),
                    "rb") as fp:
                for line in general_func.pickleLoader(fp):
                    data_block1.append(line)
            fp.close()
            for i in range(len(data_block1) - norders, len(data_block1)):
                # Put riverid and cellnumber in front of the line and remove the time.
                linestart = [rivernum[icell], rivmask[icell]]
                linestart.extend(data_block1[i][1:])
                # To be able to print species amounts/concentrations for all orders
                if (params.allorders == 1):
                    conclist.append(linestart)
                else:
                    if (i == (len(data_block1) - 1)):
                        conclist.append(linestart)

            # For interactions
            if (params.lbudget == 1):
                with open(
                        os.path.join(
                            tmpdir, "budget_" + str(icell) + "_" +
                            str(timeperiod) + ".pkl"), "rb") as fp:
                    for line in general_func.pickleLoader(fp):
                        data_block2.append(line)
                fp.close()
                for i in range(len(data_block2) - norders, len(data_block2)):
                    # Put riverid and cellnumber in front of the line and remove the time.
                    linestart = [rivernum[icell], rivmask[icell]]
                    linestart.extend(data_block2[i][1:])
                    budlist.append(linestart)

            # For arguments
            if (params.largs == 1):
                with open(
                        os.path.join(
                            tmpdir, "arguments_" + str(icell) + "_" +
                            str(timeperiod) + ".pkl"), "rb") as fp:
                    for line in general_func.pickleLoader(fp):
                        data_block3.append(line)
                fp.close()
                for i in range(len(data_block3) - norders, len(data_block3)):
                    # Put riverid and cellnumber in front of the line and remove the time.
                    linestart = [rivernum[icell], rivmask[icell]]
                    linestart.extend(data_block3[i][1:])
                    argslist.append(linestart)

        # Write all output of this riverbasin to the output file.
        stryearstart = general_func.stryear(yearstart)
        # Aquire lock
        file_locking.aquire_lock(params,
                                 lock,
                                 jobid,
                                 locktext="riverbasin" + stryearstart)

        fp = None
        if (params.allorders == 1):
            fp = open(
                os.path.join(params.outputdir,
                             stryearstart + "_allorders.pkl"), "ab")
        else:
            fp = open(os.path.join(params.outputdir, stryearstart + ".pkl"),
                      "ab")
        for i in range(len(outputlist)):
            pickle.dump(outputlist[i], fp, -1)
        fp.close()

        fp = open(
            os.path.join(params.outputdir, "start" + stryearstart + ".pkl"),
            "ab")
        for i in range(len(startoutputlist)):
            pickle.dump(startoutputlist[i], fp, -1)
        fp.close()

        fp = None
        if (params.allorders == 1):
            fp = open(
                os.path.join(params.outputdir,
                             "conc_" + stryearstart + "_allorders.pkl"), "ab")
        else:
            fp = open(
                os.path.join(params.outputdir,
                             "conc_" + stryearstart + ".pkl"), "ab")

        for i in range(len(conclist)):
            pickle.dump(conclist[i], fp, -1)
        fp.close()

        if (params.lbudget == 1):
            fp = open(
                os.path.join(params.outputdir,
                             "budget_" + stryearstart + ".pkl"), "ab")
            for i in range(len(budlist)):
                pickle.dump(budlist[i], fp, -1)
            fp.close()

        if (params.largs == 1):
            fp = open(
                os.path.join(params.outputdir,
                             "arguments_" + stryearstart + ".pkl"), "ab")
            for i in range(len(argslist)):
                pickle.dump(argslist[i], fp, -1)
            fp.close()

        # Release lock
        file_locking.release_lock(params,
                                  lock,
                                  locktext="riverbasin" + stryearstart)

    # Now we can start with the dynamic calculations.
    starttime_calculate = time.time()

    print("Starting dynamic calculations...")
    timeperiod = -1
    lsteady = False
    yearstart = params.starttime
    yearend = min(yearstart + params.outputtime, params.endtime)
    while not (yearstart > params.endtime):
        # Make a counter for each time period.
        timeperiod += 1
        lrestart = ((timeperiod+1)%iround(params.restartnumber) == 0) or\
                   (yearend == params.endtime)

        #lakes_dict = {}
        #outlakes_dict = {}
        # Loop over all cells:
        for item in range(len(pointer1)):
            icell = pointer1[item].get_local_index()
            direction = lddmap.get_data(icell)
            next_cell = accuflux.goto_nextcell(icell,
                                               direction,
                                               lddmap.ncols,
                                               mask=rivmask)

            # Load is the time array of  all the sources
            load = []
            for isrc in range(len(sources)):
                load.append(srcload[isrc][icell])

            # Check whether cell is a lake/reservoir.
            lakeid = interpolate_list.stepwise(yearstart,
                                               lakeid_cells[icell],
                                               extrapol=1)[-1]
            if (lakeid > 0):
                outlakeid = interpolate_list.stepwise(yearstart,
                                                      outlakeid_cells[icell],
                                                      extrapol=1)[-1]
                endo_lake = interpolate_list.stepwise(yearstart,
                                                      endo_lakes_cells[icell],
                                                      extrapol=1)[-1]
                llake = True
                llakeout = (outlakeid > 0.)
                lendolake = (endo_lake > 0.)
                #lakes_dict[icell] = iround(lakeid)
                #if (llakeout or lendolake):
                #    outlakes_dict[iround(lakeid)] = icell
            else:
                llake = False
                llakeout = False
                lendolake = False

            if (number_of_cpu > 1):
                #print "Submit nu ",icell
                try:
                    qq = lock_cell[next_cell]
                except KeyError:
                    lock_cell[next_cell] = mp.Lock()

                #if (prev_order != pointer1[item][0]):
                if (prev_order != pointer1[item].get_iorder()):
                    # We have to wait untill all processes are ready, before we can continue
                    for p in jobs:
                        p.join()
                    prev_order = pointer1[item].get_iorder()
                    jobs = []

                # Define the process with cellid "icell", return value p is process "handler"
                width = channel_width_grid.get_data(icell)
                slope = slopemap.get_data(icell)
                if (params.lstrahlergrids):
                    args_strahler_cell = args_strahler[icell]
                p=mp.Process(target= dgnm_cell.calculate_cell, args=(lsteady,lock_cell[next_cell],\
                                                     icell,params,species,proc,sources,\
                                                     tmpdir,next_cell,yearstart,yearend,timeperiod,\
                                                     runoff_cells[icell],load,temperature_cells[icell],\
                                                     globrad_cells[icell], discharge_cells[icell],\
                                                     volume_cells[icell],water_area_cells[icell],depth_cells[icell],\
                                                     width, slope, \
                                                     volume_fp_cells[icell],depth_fp_cells[icell],vel_cells[icell],\
                                                     low_veg_fr_cells[icell],high_veg_fr_cells[icell],llake,llakeout,lendolake,args_strahler_cell))

                # Add the process handler to the jobs list.
                jobs.append(p)
                # Start the process
                general_func.start_process(p, jobs, number_of_cpu)

            else:
                # Do the job one at the time.
                width = channel_width_grid.get_data(icell)
                slope = slopemap.get_data(icell)
                if (params.lstrahlergrids):
                    args_strahler_cell = args_strahler[icell]
                dgnm_cell.calculate_cell(lsteady,None,icell,params,species,proc,sources,\
                                              tmpdir,\
                                              next_cell,yearstart,yearend,timeperiod,\
                                              runoff_cells[icell],load,temperature_cells[icell],\
                                              globrad_cells[icell], discharge_cells[icell],\
                                              volume_cells[icell],water_area_cells[icell],depth_cells[icell],\
                                              width, slope, \
                                              volume_fp_cells[icell],depth_fp_cells[icell],vel_cells[icell],\
                                              low_veg_fr_cells[icell],high_veg_fr_cells[icell],llake,llakeout,lendolake,args_strahler_cell=args_strahler_cell)

        if (number_of_cpu > 1):
            # Wait until all jobs are finished.
            for p in jobs:
                p.join()

        # Distribute the outflow of lake or reservoir to all the cells of the waterbody
        # Overwrite the information of the cells which are not the outflow of a waterbody.
        #for icell in lakes_dict:
        #    id = lakes_dict[icell]
        #    if (icell != outlakes_dict[id]):
        #        # This is a cell which must be overwritten with the outflow cell.
        #        outflowcell = outlakes_dict[id]
        #        my_sys.my_copyfile(os.path.join(tmpdir,str(outflowcell)+"_"+str(timeperiod)+".pkl"),\
        #                           os.path.join(tmpdir,str(icell)+"_"+str(timeperiod)+".pkl"))
        #        my_sys.my_copyfile(os.path.join(tmpdir,"conc_"+str(outflowcell)+"_"+str(timeperiod)+".pkl"),\
        #                           os.path.join(tmpdir,"conc_"+str(icell)+"_"+str(timeperiod)+".pkl"))
        #        if (params.lbudget == 1):
        #            my_sys.my_copyfile(os.path.join(tmpdir,"budget_"+str(outflowcell)+"_"+str(timeperiod)+".pkl"),\
        #                               os.path.join(tmpdir,"budget_"+str(icell)+"_"+str(timeperiod)+".pkl")) #LV 17-07-2017

        # Put cell information of the last time step into the world map.
        data_block3 = []
        data_block2 = []
        data_block1 = []
        data_block = []
        outputlist = []
        startoutputlist = []
        conclist = []
        budlist = []
        argslist = []
        for item in range(len(pointer1)):
            icell = pointer1[item].get_local_index()
            with open(
                    os.path.join(tmpdir,
                                 str(icell) + "_" + str(timeperiod) + ".pkl"),
                    "rb") as fp:
                for line in general_func.pickleLoader(fp):
                    data_block.append(line)
            fp.close()
            # Store only the last output time of the cell.
            if (lrestart):
                for i in range(len(data_block) - norders, len(data_block)):
                    # Put riverid and cellnumber in front of the line and remove the time.
                    linestart = [rivernum[icell], rivmask[icell]]
                    linestart.extend(data_block[i][1:])
                    startoutputlist.append(linestart)

            for i in range(len(data_block) - norders, len(data_block)):
                # Put riverid and cellnumber in front of the line and remove the time.
                linestart = [rivernum[icell], rivmask[icell]]
                linestart.extend(data_block[i][1:])
                if (params.allorders == 1):
                    outputlist.append(linestart)
                else:
                    if (i == (len(data_block) - 1)):
                        outputlist.append(linestart)

            with open(
                    os.path.join(
                        tmpdir,
                        "conc_" + str(icell) + "_" + str(timeperiod) + ".pkl"),
                    "rb") as fp:
                for line in general_func.pickleLoader(fp):
                    data_block1.append(line)
            fp.close()

            for i in range(len(data_block1) - norders, len(data_block1)):
                # Put riverid and cellnumber in front of the line and remove the time.
                linestart = [rivernum[icell], rivmask[icell]]
                linestart.extend(data_block1[i][1:])
                if (params.allorders == 1):
                    conclist.append(linestart)
                else:
                    if (i == (len(data_block1) - 1)):
                        conclist.append(linestart)

            if (params.lbudget == 1):
                with open(
                        os.path.join(
                            tmpdir, "budget_" + str(icell) + "_" +
                            str(timeperiod) + ".pkl"), "rb") as fp:
                    for line in general_func.pickleLoader(fp):
                        data_block2.append(line)
                fp.close()
                for i in range(len(data_block2) - norders, len(data_block2)):
                    # Put riverid and cellnumber in front of the line and remove the time.
                    linestart = [rivernum[icell], rivmask[icell]]
                    linestart.extend(data_block2[i][1:])
                    budlist.append(linestart)
                    #print(linestart)

            if (params.largs == 1):
                with open(
                        os.path.join(
                            tmpdir, "arguments_" + str(icell) + "_" +
                            str(timeperiod) + ".pkl"), "rb") as fp:
                    for line in general_func.pickleLoader(fp):
                        data_block3.append(line)
                fp.close()
                for i in range(len(data_block3) - norders, len(data_block3)):
                    # Put riverid and cellnumber in front of the line and remove the time.
                    linestart = [rivernum[icell], rivmask[icell]]
                    linestart.extend(data_block3[i][1:])
                    argslist.append(linestart)

        # Write all output of this riverbasin to the output file.
        # Aquire lock
        strtime = general_func.stryear(yearend)
        file_locking.aquire_lock(params,
                                 lock,
                                 jobid,
                                 locktext="riverbasin" + strtime)
        fp = None
        if (params.allorders == 1):
            fp = open(
                os.path.join(params.outputdir, strtime + "_allorders.pkl"),
                "ab")
        else:
            fp = open(os.path.join(params.outputdir, strtime + ".pkl"), "ab")
        for i in range(len(outputlist)):
            pickle.dump(outputlist[i], fp, -1)
        fp.close()

        if (lrestart):
            fp = open(
                os.path.join(params.outputdir, "start" + strtime + ".pkl"),
                "ab")
            for i in range(len(startoutputlist)):
                pickle.dump(startoutputlist[i], fp, -1)
            fp.close()

        fp = None
        if (params.allorders == 1):
            fp = open(
                os.path.join(params.outputdir,
                             "conc_" + strtime + "_allorders.pkl"), "ab")
        else:
            fp = open(
                os.path.join(params.outputdir, "conc_" + strtime + ".pkl"),
                "ab")
        for i in range(len(conclist)):
            pickle.dump(conclist[i], fp, -1)
        fp.close()

        if (params.lbudget == 1):
            fp = open(
                os.path.join(params.outputdir, "budget_" + strtime + ".pkl"),
                "ab")
            for i in range(len(budlist)):
                pickle.dump(budlist[i], fp, -1)
            fp.close()

        if (params.largs == 1):
            fp = open(
                os.path.join(params.outputdir,
                             "arguments_" + strtime + ".pkl"), "ab")
            for i in range(len(argslist)):
                pickle.dump(argslist[i], fp, -1)
            fp.close()

        # Release lock
        file_locking.release_lock(params,
                                  lock,
                                  locktext="riverbasin" + strtime)

        print(" All processes are ready for river " + str(jobid) +
              " for time " + strtime + ".")
        # Goto next periode.
        yearstart = yearend
        yearend = min(yearstart + params.outputtime, params.endtime)
        if (abs(yearstart - yearend) < params.timestep):
            # This was the last timestep.
            break

    # Remove the temporary directory with the output of this riverbasin
    my_sys.my_rmdir(tmpdir)

    endtime_calculate = time.time()
    print('Total time (in s) to calculate dynamic processes in riverbasin ' +
          str(iriv) + ':  ' + str(endtime_calculate - starttime_calculate))
Пример #13
0
def calculate(params):

    #print("The critical N concentration in surface water is", params.crit_sw)

    # load needed variables for calculations
    nfix_ara = ascraster.Asciigrid(
        ascii_file=params.filename_nfixation_cropland,
        numtype=float,
        mask=params.mask)
    nfix_igl = ascraster.Asciigrid(ascii_file=params.filename_nfixation_intgl,
                                   numtype=float,
                                   mask=params.mask)
    nallo = ascraster.Asciigrid(
        ascii_file=params.filename_n_point_alloch_matter,
        numtype=float,
        mask=params.mask)
    nww = ascraster.Asciigrid(ascii_file=params.filename_n_point_wastewater,
                              numtype=float,
                              mask=params.mask)
    naqua = ascraster.Asciigrid(ascii_file=params.filename_n_point_aquaculture,
                                numtype=float,
                                mask=params.mask)
    ndep_sw = ascraster.Asciigrid(
        ascii_file=params.filename_n_point_dep_surfacewater,
        numtype=float,
        mask=params.mask)
    nfix_egl = ascraster.Asciigrid(ascii_file=os.path.join(
        params.inputdir, "nfix_grass_ext.asc"),
                                   numtype=float,
                                   mask=params.mask)
    nfix_nat = ascraster.Asciigrid(ascii_file=os.path.join(
        params.inputdir, "nfix_nat.asc"),
                                   numtype=float,
                                   mask=params.mask)
    nup_egl = ascraster.Asciigrid(ascii_file=os.path.join(
        params.inputdir, "n_up_grass_ext.asc"),
                                  numtype=float,
                                  mask=params.mask)
    q = ascraster.Asciigrid(ascii_file=os.path.join(params.inputdir, "q.asc"),
                            numtype=float,
                            mask=params.mask)
    npoint_tot = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "npoint_tot.asc"),
                                     numtype=float,
                                     mask=params.mask)
    nero_tot = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nero_tot.asc"),
                                   numtype=float,
                                   mask=params.mask)
    nload_fixed_ag = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nload_fixed_ag.asc"),
                                         numtype=float,
                                         mask=params.mask)
    nload_fixed_nat = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nload_fixed_nat.asc"),
                                          numtype=float,
                                          mask=params.mask)
    nload_var_ag = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nload_var_ag.asc"),
                                       numtype=float,
                                       mask=params.mask)
    nload_var_nat = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nload_var_nat.asc"),
                                        numtype=float,
                                        mask=params.mask)
    fle_ag = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "fle_ag.asc"),
                                 numtype=float,
                                 mask=params.mask)
    fle_nat = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "fle_nat.asc"),
                                  numtype=float,
                                  mask=params.mask)
    fsro_ag = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "fsro_ag.asc"),
                                  numtype=float,
                                  mask=params.mask)
    fsro_nat = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "fsro_nat.asc"),
                                   numtype=float,
                                   mask=params.mask)
    frnup_ara = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "frnup_ara.asc"),
                                    numtype=float,
                                    mask=params.mask)
    frnup_igl = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "frnup_igl.asc"),
                                    numtype=float,
                                    mask=params.mask)
    fgw_rec_le_ag = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "fgw_rec_le_ag.asc"),
                                        numtype=float,
                                        mask=params.mask)
    fgw_rec_le_nat = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "fgw_rec_le_nat.asc"),
                                         numtype=float,
                                         mask=params.mask)
    nox_em = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nox_em.asc"),
                                 numtype=float,
                                 mask=params.mask)
    nh3_tot_egl = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nh3_tot_egl.asc"),
                                      numtype=float,
                                      mask=params.mask)
    nh3_ef_man_ara = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nh3_ef_man_ara.asc"),
                                         numtype=float,
                                         mask=params.mask)
    nh3_ef_man_igl = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nh3_ef_man_igl.asc"),
                                         numtype=float,
                                         mask=params.mask)
    nh3_ef_fer_ara = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nh3_ef_fer_ara.asc"),
                                         numtype=float,
                                         mask=params.mask)
    nh3_ef_fer_igl = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nh3_ef_fer_igl.asc"),
                                         numtype=float,
                                         mask=params.mask)
    nh3_ef_man_fer_ara = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nh3_ef_man_fer_ara.asc"),
                                             numtype=float,
                                             mask=params.mask)
    nh3_ef_man_fer_igl = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nh3_ef_man_fer_igl.asc"),
                                             numtype=float,
                                             mask=params.mask)
    frnfe_ara = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "frnfe_ara.asc"),
                                    numtype=float,
                                    mask=params.mask)
    frnfe_igl = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "frnfe_igl.asc"),
                                    numtype=float,
                                    mask=params.mask)
    frn_ara = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "frn_ara.asc"),
                                  numtype=float,
                                  mask=params.mask)
    frn_igl = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "frn_igl.asc"),
                                  numtype=float,
                                  mask=params.mask)
    fara = ascraster.Asciigrid(ascii_file=os.path.join(params.outputdir,
                                                       "fara.asc"),
                               numtype=float,
                               mask=params.mask)
    figl = ascraster.Asciigrid(ascii_file=os.path.join(params.outputdir,
                                                       "figl.asc"),
                               numtype=float,
                               mask=params.mask)
    fegl = ascraster.Asciigrid(ascii_file=os.path.join(params.outputdir,
                                                       "fegl.asc"),
                               numtype=float,
                               mask=params.mask)
    fnat = ascraster.Asciigrid(ascii_file=os.path.join(params.outputdir,
                                                       "fnat.asc"),
                               numtype=float,
                               mask=params.mask)
    nup_max_ara = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nup_max_ara.asc"),
                                      numtype=float,
                                      mask=params.mask)
    nup_max_igl = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nup_max_igl.asc"),
                                      numtype=float,
                                      mask=params.mask)
    nin_max_ara = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nin_max_ara.asc"),
                                      numtype=float,
                                      mask=params.mask)
    nin_max_igl = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nin_max_igl.asc"),
                                      numtype=float,
                                      mask=params.mask)
    nman_egl = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nman_egl.asc"),
                                   numtype=float,
                                   mask=params.mask)

    ## One grid
    one_grid = ascraster.duplicategrid(nfix_ara)
    for i in range(one_grid.length):
        one_grid.set_data(i, 1.0)

    # calculate Nload,crit,sw =Q*Nconc,sw(crit)
    nload_crit_sw = ascraster.duplicategrid(q)
    nload_crit_sw.multiply(params.crit_sw)
    fileout = os.path.join(params.outputdir, "nload_crit_sw.asc")
    nload_crit_sw.write_ascii_file(fileout,
                                   output_nodata_value=-9999,
                                   compress=params.lcompress)
    print_debug(nload_crit_sw, "nload_crit_sw =")

    # calculate fixed N load to surface water
    nload_fixed_tot = ascraster.duplicategrid(nallo)
    nload_fixed_tot.add(nero_tot)
    nload_fixed_tot.add(nload_fixed_nat)
    print_debug(nload_fixed_tot, "nload_fixed_tot =")

    nload_var_ag_nat = ascraster.duplicategrid(nload_var_ag)
    nload_var_ag_nat.add(nload_var_nat)

    nload_var_other = ascraster.duplicategrid(nww)
    nload_var_other.add(naqua)
    nload_var_other.add(ndep_sw)
    nload_var_other.add(
        nload_fixed_ag
    )  # is zero if combined with scenario 1 (all ag. N load = variable)

    nload_var_tot = ascraster.duplicategrid(nload_var_ag_nat)
    nload_var_tot.add(nload_var_other)

    nload_var_ag_nat_percent = ascraster.duplicategrid(nload_var_ag_nat)
    nload_var_ag_nat_percent.divide(nload_var_tot, default_nodata_value=-9999)

    nload_var_other_percent = ascraster.duplicategrid(nload_var_other)
    nload_var_other_percent.divide(nload_var_tot, default_nodata_value=-9999)

    nload_var_crit_tot = ascraster.duplicategrid(nload_crit_sw)
    nload_var_crit_tot.substract(nload_fixed_tot)

    nload_var_crit_ag_nat = ascraster.duplicategrid(nload_var_crit_tot)
    nload_var_crit_ag_nat.multiply(nload_var_ag_nat_percent)

    nload_var_crit_other = ascraster.duplicategrid(nload_var_crit_tot)
    nload_var_crit_other.multiply(nload_var_other_percent)

    ## Parameter 'v'
    # 'ara'
    v_ara_part1 = ascraster.duplicategrid(fgw_rec_le_ag)
    v_ara_part1.multiply(fle_ag)
    v_ara_part2 = ascraster.duplicategrid(v_ara_part1)
    v_ara_part2.multiply(frnup_ara)
    v_ara_part3 = ascraster.duplicategrid(v_ara_part2)
    v_ara_part3.multiply(fsro_ag)
    v_ara_part4 = ascraster.duplicategrid(fgw_rec_le_ag)
    v_ara_part4.multiply(fle_ag)
    v_ara_part4.multiply(fsro_ag)
    v_ara = ascraster.duplicategrid(v_ara_part1)
    v_ara.substract(v_ara_part2)
    v_ara.add(v_ara_part3)
    v_ara.substract(v_ara_part4)
    v_ara.add(fsro_ag)
    print_debug(v_ara, "v_ara =")
    # 'igl'
    v_igl_part1 = ascraster.duplicategrid(fgw_rec_le_ag)
    v_igl_part1.multiply(fle_ag)
    v_igl_part2 = ascraster.duplicategrid(v_igl_part1)
    v_igl_part2.multiply(frnup_igl)
    v_igl_part3 = ascraster.duplicategrid(v_igl_part2)
    v_igl_part3.multiply(fsro_ag)
    v_igl_part4 = ascraster.duplicategrid(fgw_rec_le_ag)
    v_igl_part4.multiply(fle_ag)
    v_igl_part4.multiply(fsro_ag)
    v_igl = ascraster.duplicategrid(v_igl_part1)
    v_igl.substract(v_igl_part2)
    v_igl.add(v_igl_part3)
    v_igl.substract(v_igl_part4)
    v_igl.add(fsro_ag)
    print_debug(v_igl, "v_igl =")

    ## parameter 'w'
    # 'egl'
    w_egl_part1 = ascraster.duplicategrid(fgw_rec_le_ag)
    w_egl_part1.multiply(fle_ag)
    w_egl_part2 = ascraster.duplicategrid(w_egl_part1)
    w_egl_part2.multiply(fsro_ag)
    w_egl = ascraster.duplicategrid(w_egl_part1)
    w_egl.substract(w_egl_part2)
    w_egl.add(fsro_ag)
    print_debug(w_egl, "w_egl =")
    # 'nat'
    w_nat_part1 = ascraster.duplicategrid(fgw_rec_le_nat)
    w_nat_part1.multiply(fle_nat)
    w_nat_part2 = ascraster.duplicategrid(w_nat_part1)
    w_nat_part2.multiply(fsro_nat)
    w_nat = ascraster.duplicategrid(w_nat_part1)
    w_nat.substract(w_nat_part2)
    w_nat.add(fsro_nat)
    print_debug(w_nat, "w_nat =")

    ## parameter 'y1'
    y1_part1 = ascraster.duplicategrid(nfix_ara)
    y1_part1.multiply(v_ara)
    y1_part2 = ascraster.duplicategrid(nfix_igl)
    y1_part2.multiply(v_igl)
    y1_part3 = ascraster.duplicategrid(nfix_egl)
    y1_part3.add(nman_egl)
    y1_part3.multiply(w_egl)
    y1_part4 = ascraster.duplicategrid(nfix_nat)
    y1_part4.multiply(w_nat)
    y1 = ascraster.duplicategrid(y1_part1)
    y1.add(y1_part2)
    y1.add(y1_part3)
    y1.add(y1_part4)
    print_debug(y1, "y1 =")

    ## parameter 'y2'
    y2_part1 = ascraster.duplicategrid(fara)
    y2_part1.multiply(v_ara)
    y2_part2 = ascraster.duplicategrid(figl)
    y2_part2.multiply(v_igl)
    y2_part3 = ascraster.duplicategrid(fegl)
    y2_part3.multiply(w_egl)
    y2_part4 = ascraster.duplicategrid(fnat)
    y2_part4.multiply(w_nat)
    y2 = ascraster.duplicategrid(y2_part1)
    y2.add(y2_part2)
    y2.add(y2_part3)
    y2.add(y2_part4)
    print_debug(y2, "y2 =")

    # calculate parameter 'z'
    # 'ara' (LET OP: for z_ara, we use frn_igl!)
    one_min_frn_igl = ascraster.duplicategrid(one_grid)
    one_min_frn_igl.substract(frn_igl)
    z_ara = ascraster.duplicategrid(frn_igl)
    z_ara.divide(one_min_frn_igl, default_nodata_value=-9999)
    print_debug(z_ara, "z_ara =")

    # 'igl' (LET OP: for z_igl, we use frn_ara!)
    one_min_frn_ara = ascraster.duplicategrid(one_grid)
    one_min_frn_ara.substract(frn_ara)
    z_igl = ascraster.duplicategrid(frn_ara)
    z_igl.divide(one_min_frn_ara, default_nodata_value=-9999)
    print_debug(z_igl, "z_igl =")

    # calculate parameter 'x'
    # ara
    x_ara_part1 = ascraster.duplicategrid(y2)
    x_ara_part1.multiply(nh3_ef_man_fer_ara)
    x_ara = ascraster.duplicategrid(y2)
    x_ara.multiply(nh3_ef_man_fer_igl)
    x_ara.add(v_igl)
    x_ara.multiply(z_ara)
    x_ara.add(x_ara_part1)
    x_ara.add(v_ara)
    print_debug(x_ara, "x_ara =")

    # igl
    x_igl_part1 = ascraster.duplicategrid(y2)
    x_igl_part1.multiply(nh3_ef_man_fer_igl)
    x_igl = ascraster.duplicategrid(y2)
    x_igl.multiply(nh3_ef_man_fer_ara)
    x_igl.add(v_ara)
    x_igl.multiply(z_igl)
    x_igl.add(x_igl_part1)
    x_igl.add(v_igl)
    print_debug(x_igl, "x_igl =")

    ## calculate critical N input from manure

    ## OPTION2 - for grid cells with BOTH 'ara' AND 'igl'
    # 'ara'
    numerator_V2_ara = ascraster.duplicategrid(nload_var_crit_ag_nat)
    n1_V2_ara = ascraster.duplicategrid(nox_em)
    n1_V2_ara.add(nh3_tot_egl)
    n1_V2_ara.multiply(y2)
    numerator_V2_ara.substract(y1)
    numerator_V2_ara.substract(n1_V2_ara)

    one_min_frnfe_ara = ascraster.duplicategrid(one_grid)
    one_min_frnfe_ara.substract(frnfe_ara)
    frnfe_division_ara = ascraster.duplicategrid(frnfe_ara)
    frnfe_division_ara.divide(one_min_frnfe_ara, default_nodata_value=-9999)
    denominator_V2_ara = ascraster.duplicategrid(frnfe_division_ara)
    denominator_V2_ara.add(one_grid)
    denominator_V2_ara.multiply(x_ara)

    nman_crit_sw_V2_ara = ascraster.duplicategrid(numerator_V2_ara)
    nman_crit_sw_V2_ara.divide(denominator_V2_ara, default_nodata_value=-9999)

    fileout = os.path.join(params.outputdir, "nman_crit_sw_V2_ara.asc")
    nman_crit_sw_V2_ara.write_ascii_file(fileout,
                                         output_nodata_value=-9999,
                                         compress=params.lcompress)

    # 'igl'
    numerator_V2_igl = ascraster.duplicategrid(numerator_V2_ara)

    one_min_frnfe_igl = ascraster.duplicategrid(one_grid)
    one_min_frnfe_igl.substract(frnfe_igl)
    frnfe_division_igl = ascraster.duplicategrid(frnfe_igl)
    frnfe_division_igl.divide(one_min_frnfe_igl, default_nodata_value=-9999)
    denominator_V2_igl = ascraster.duplicategrid(frnfe_division_igl)
    denominator_V2_igl.add(one_grid)
    denominator_V2_igl.multiply(x_igl)

    nman_crit_sw_V2_igl = ascraster.duplicategrid(numerator_V2_igl)
    nman_crit_sw_V2_igl.divide(denominator_V2_igl, default_nodata_value=-9999)

    fileout = os.path.join(params.outputdir, "nman_crit_sw_V2_igl.asc")
    nman_crit_sw_V2_igl.write_ascii_file(fileout,
                                         output_nodata_value=-9999,
                                         compress=params.lcompress)

    ## OPTION 2 - for grid cells with EITHER 'ara' OR 'igl'
    # 'ara'
    numerator_V1_ara = ascraster.duplicategrid(numerator_V2_ara)
    n1_V1_ara = ascraster.duplicategrid(nup_egl)
    n1_V1_ara.multiply(fgw_rec_le_ag)
    n1_V1_ara.multiply(fle_ag)
    numerator_V1_ara.add(n1_V1_ara)

    d1_V1_ara = ascraster.duplicategrid(nh3_ef_man_ara)
    d1_V1_ara.multiply(y2)
    d1_V1_ara.add(v_ara)
    d2_V1_ara = ascraster.duplicategrid(nh3_ef_fer_ara)
    d2_V1_ara.multiply(y2)
    d2_V1_ara.add(v_ara)
    d2_V1_ara.multiply(frnfe_division_ara)
    denominator_V1_ara = ascraster.duplicategrid(d1_V1_ara)
    denominator_V1_ara.add(d2_V1_ara)

    nman_crit_sw_V1_ara = ascraster.duplicategrid(numerator_V1_ara)
    nman_crit_sw_V1_ara.divide(denominator_V1_ara, default_nodata_value=-9999)

    fileout = os.path.join(params.outputdir, "nman_crit_sw_V1_ara.asc")
    nman_crit_sw_V1_ara.write_ascii_file(fileout,
                                         output_nodata_value=-9999,
                                         compress=params.lcompress)

    # 'igl'
    numerator_V1_igl = ascraster.duplicategrid(numerator_V1_ara)

    d1_V1_igl = ascraster.duplicategrid(nh3_ef_man_igl)
    d1_V1_igl.multiply(y2)
    d1_V1_igl.add(v_igl)
    d2_V1_igl = ascraster.duplicategrid(nh3_ef_fer_igl)
    d2_V1_igl.multiply(y2)
    d2_V1_igl.add(v_ara)
    d2_V1_igl.multiply(frnfe_division_igl)
    denominator_V1_igl = ascraster.duplicategrid(d1_V1_igl)
    denominator_V1_igl.add(d2_V1_igl)

    nman_crit_sw_V1_igl = ascraster.duplicategrid(numerator_V1_igl)
    nman_crit_sw_V1_igl.divide(denominator_V1_igl, default_nodata_value=-9999)

    fileout = os.path.join(params.outputdir, "nman_crit_sw_V1_igl.asc")
    nman_crit_sw_V1_igl.write_ascii_file(fileout,
                                         output_nodata_value=-9999,
                                         compress=params.lcompress)

    # MAKE GRID WITH CRITICAL INPUTS FROM MANURE DEPENDING ON OPTION
    # ara
    nman_crit_sw_ara = ascraster.duplicategrid(nman_crit_sw_V1_ara)
    for icell in range(fara.length):
        f_ara = fara.get_data(icell)
        f_igl = figl.get_data(icell)
        nman_ara2 = nman_crit_sw_V2_ara.get_data(icell)
        if (f_ara is None or f_igl is None):
            continue
        elif (f_ara > 0 and f_igl > 0):
            nman_ara = nman_ara2
        elif (f_ara == 0 and f_igl > 0):
            nman_ara = 0
        else:
            continue

        nman_crit_sw_ara.set_data(icell, nman_ara)

    for icell in range(nman_crit_sw_ara.length):
        nman_ara3 = nman_crit_sw_ara.get_data(icell)
        if (nman_ara3 is None):
            continue
        if (nman_ara3 < 0):
            nman_ara = 0
        else:
            continue
        nman_crit_sw_ara.set_data(icell, nman_ara)

    fileout = os.path.join(params.outputdir, "nman_crit_sw_ara.asc")
    nman_crit_sw_ara.write_ascii_file(fileout,
                                      output_nodata_value=-9999,
                                      compress=params.lcompress)
    print_debug(nman_crit_sw_ara, "nman_crit_sw_ara =")

    # igl
    nman_crit_sw_igl = ascraster.duplicategrid(nman_crit_sw_V1_igl)
    for icell in range(figl.length):
        f_igl = figl.get_data(icell)
        f_ara = fara.get_data(icell)
        nman_igl2 = nman_crit_sw_V2_igl.get_data(icell)
        if (f_ara is None or f_igl is None):
            continue
        elif (f_ara > 0 and f_igl > 0):
            nman_igl = nman_igl2
        elif (f_ara > 0 and f_igl == 0):
            nman_igl = 0
        else:
            continue

        nman_crit_sw_igl.set_data(icell, nman_igl)

    for icell in range(nman_crit_sw_igl.length):
        nman_igl3 = nman_crit_sw_igl.get_data(icell)
        if (nman_igl3 is None):
            continue
        if (nman_igl3 < 0):
            nman_igl = 0
        else:
            continue
        nman_crit_sw_igl.set_data(icell, nman_igl)

    fileout = os.path.join(params.outputdir, "nman_crit_sw_igl.asc")
    nman_crit_sw_igl.write_ascii_file(fileout,
                                      output_nodata_value=-9999,
                                      compress=params.lcompress)
    print_debug(nman_crit_sw_igl, "nman_crit_sw_igl =")

    ## * Critical N input from fertilizer *
    # 'ara'
    nfer_crit_sw_ara = ascraster.duplicategrid(nman_crit_sw_ara)
    nfer_crit_sw_ara.multiply(frnfe_division_ara)

    # set to zero for all cells with no ara but igl (to avoid error in calculating nh3 emissions / deposition for these cells)
    for icell in range(nfer_crit_sw_ara.length):
        f_ara = fara.get_data(icell)
        f_igl = figl.get_data(icell)
        nfer_ara = nfer_crit_sw_ara.get_data(icell)
        if (f_ara == 0 and f_igl > 0):
            nfer_ara = 0
        else:
            continue

        nfer_crit_sw_ara.set_data(icell, nfer_ara)
    fileout = os.path.join(params.outputdir, "nfer_crit_sw_ara.asc")
    nfer_crit_sw_ara.write_ascii_file(fileout,
                                      output_nodata_value=-9999,
                                      compress=params.lcompress)
    print_debug(nfer_crit_sw_ara, "nfer_crit_sw_ara =")

    # 'igl'
    nfer_crit_sw_igl = ascraster.duplicategrid(nman_crit_sw_igl)
    nfer_crit_sw_igl.multiply(frnfe_division_igl)

    # set to zero for all cells with no igl but ara (to avoid error in calculating nh3 emissions / deposition for these cells)
    for icell in range(nfer_crit_sw_igl.length):
        f_ara = fara.get_data(icell)
        f_igl = figl.get_data(icell)
        nfer_igl = nfer_crit_sw_igl.get_data(icell)
        if (f_igl == 0 and f_ara > 0):
            nfer_igl = 0
        else:
            continue

        nfer_crit_sw_igl.set_data(icell, nfer_igl)
    fileout = os.path.join(params.outputdir, "nfer_crit_sw_igl.asc")
    nfer_crit_sw_igl.write_ascii_file(fileout,
                                      output_nodata_value=-9999,
                                      compress=params.lcompress)
    print_debug(nfer_crit_sw_igl, "nfer_crit_sw_igl =")

    ## * Critical N inputs from fertilizer plus manure * ##
    # 'ara'
    nman_fer_crit_sw_ara = ascraster.duplicategrid(nman_crit_sw_ara)
    nman_fer_crit_sw_ara.add(nfer_crit_sw_ara)
    fileout = os.path.join(params.outputdir, "nman_fer_crit_sw_ara.asc")
    nman_fer_crit_sw_ara.write_ascii_file(fileout,
                                          output_nodata_value=-9999,
                                          compress=params.lcompress)
    # 'igl'
    nman_fer_crit_sw_igl = ascraster.duplicategrid(nman_crit_sw_igl)
    nman_fer_crit_sw_igl.add(nfer_crit_sw_igl)
    fileout = os.path.join(params.outputdir, "nman_fer_crit_sw_igl.asc")
    nman_fer_crit_sw_igl.write_ascii_file(fileout,
                                          output_nodata_value=-9999,
                                          compress=params.lcompress)

    ## * NH3 emissions at critical N input *
    # 'ara'
    nh3em_man_crit_sw_ara = ascraster.duplicategrid(nman_crit_sw_ara)
    nh3em_man_crit_sw_ara.multiply(nh3_ef_man_ara)
    nh3em_fer_crit_sw_ara = ascraster.duplicategrid(nfer_crit_sw_ara)
    nh3em_fer_crit_sw_ara.multiply(nh3_ef_fer_ara)
    nh3em_crit_sw_ara = ascraster.duplicategrid(nh3em_man_crit_sw_ara)
    nh3em_crit_sw_ara.add(nh3em_fer_crit_sw_ara)
    print_debug(nh3em_crit_sw_ara, "nh3em_crit_sw_ara =")
    # 'igl'
    nh3em_man_crit_sw_igl = ascraster.duplicategrid(nman_crit_sw_igl)
    nh3em_man_crit_sw_igl.multiply(nh3_ef_man_igl)
    nh3em_fer_crit_sw_igl = ascraster.duplicategrid(nfer_crit_sw_igl)
    nh3em_fer_crit_sw_igl.multiply(nh3_ef_fer_igl)
    nh3em_crit_sw_igl = ascraster.duplicategrid(nh3em_man_crit_sw_igl)
    nh3em_crit_sw_igl.add(nh3em_fer_crit_sw_igl)
    print_debug(nh3em_crit_sw_igl, "nh3em_crit_sw_igl =")

    ## * N deposition at critical N input *
    ndep_crit_sw_tot = ascraster.duplicategrid(nh3em_crit_sw_ara)
    ndep_crit_sw_tot.add(nh3em_crit_sw_igl)
    ndep_crit_sw_tot.add(nox_em)
    ndep_crit_sw_tot.add(nh3_tot_egl)

    fileout = os.path.join(params.outputdir, "ndep_crit_sw_tot.asc")
    ndep_crit_sw_tot.write_ascii_file(fileout,
                                      output_nodata_value=-9999,
                                      compress=params.lcompress)
    print_debug(ndep_crit_sw_tot, "ndep_crit_sw_tot =")

    # 'ara'
    ndep_crit_sw_ara = ascraster.duplicategrid(ndep_crit_sw_tot)
    ndep_crit_sw_ara.multiply(fara)
    print_debug(ndep_crit_sw_ara, "ndep_crit_sw_ara =")
    # 'igl'
    ndep_crit_sw_igl = ascraster.duplicategrid(ndep_crit_sw_tot)
    ndep_crit_sw_igl.multiply(figl)
    print_debug(ndep_crit_sw_igl, "ndep_crit_sw_igl =")
    # 'nat'
    ndep_crit_sw_nat = ascraster.duplicategrid(ndep_crit_sw_tot)
    ndep_crit_sw_nat.multiply(fnat)
    print_debug(ndep_crit_sw_nat, "ndep_crit_sw_nat =")
    # 'egl'
    ndep_crit_sw_egl = ascraster.duplicategrid(ndep_crit_sw_tot)
    ndep_crit_sw_egl.multiply(fegl)
    print_debug(ndep_crit_sw_egl, "ndep_crit_sw_egl =")

    ## * Total critical N inputs *
    # 'ara'
    nin_crit_sw_ara = ascraster.duplicategrid(nman_crit_sw_ara)
    nin_crit_sw_ara.add(nfer_crit_sw_ara)
    nin_crit_sw_ara.add(ndep_crit_sw_ara)
    nin_crit_sw_ara.add(nfix_ara)
    fileout = os.path.join(params.outputdir, "nin_crit_sw_ara.asc")
    nin_crit_sw_ara.write_ascii_file(fileout,
                                     output_nodata_value=-9999,
                                     compress=params.lcompress)
    print_debug(nin_crit_sw_ara, "nin_crit_sw_ara =")
    # 'igl'
    nin_crit_sw_igl = ascraster.duplicategrid(nman_crit_sw_igl)
    nin_crit_sw_igl.add(nfer_crit_sw_igl)
    nin_crit_sw_igl.add(ndep_crit_sw_igl)
    nin_crit_sw_igl.add(nfix_igl)
    fileout = os.path.join(params.outputdir, "nin_crit_sw_igl.asc")
    nin_crit_sw_igl.write_ascii_file(fileout,
                                     output_nodata_value=-9999,
                                     compress=params.lcompress)
    print_debug(nin_crit_sw_igl, "nin_crit_sw_igl =")
    # 'ara+igl'
    nin_crit_sw_araigl = ascraster.duplicategrid(nin_crit_sw_ara)
    nin_crit_sw_araigl.add(nin_crit_sw_igl)
    fileout = os.path.join(params.outputdir, "nin_crit_sw_araigl.asc")
    nin_crit_sw_araigl.write_ascii_file(fileout,
                                        output_nodata_value=-9999,
                                        compress=params.lcompress)
    # 'nat'
    nin_crit_sw_nat = ascraster.duplicategrid(ndep_crit_sw_nat)
    nin_crit_sw_nat.add(nfix_nat)
    print_debug(nin_crit_sw_nat, "nin_crit_sw_nat =")
    # 'egl'
    nin_crit_sw_egl = ascraster.duplicategrid(ndep_crit_sw_egl)
    nin_crit_sw_egl.add(nfix_egl)
    nin_crit_sw_egl.add(nman_egl)
    print_debug(nin_crit_sw_egl, "nin_crit_sw_egl =")

    ## * N surface runoff at critical N inputs *
    # 'ara'
    nsro_crit_sw_ara = ascraster.duplicategrid(nin_crit_sw_ara)
    nsro_crit_sw_ara.multiply(fsro_ag)
    print_debug(nsro_crit_sw_ara, "nsro_crit_sw_ara =")
    # 'igl'
    nsro_crit_sw_igl = ascraster.duplicategrid(nin_crit_sw_igl)
    nsro_crit_sw_igl.multiply(fsro_ag)
    print_debug(nsro_crit_sw_igl, "nsro_crit_sw_igl =")
    # 'nat'
    nsro_crit_sw_nat = ascraster.duplicategrid(nin_crit_sw_nat)
    nsro_crit_sw_nat.multiply(fsro_nat)
    print_debug(nsro_crit_sw_nat, "nsro_crit_sw_nat =")
    # 'egl'
    nsro_crit_sw_egl = ascraster.duplicategrid(nin_crit_sw_egl)
    nsro_crit_sw_egl.multiply(fsro_ag)
    print_debug(nsro_crit_sw_egl, "nsro_crit_sw_egl =")

    ## * N uptake at critical N inputs *
    # 'ara'
    nup_crit_sw_ara = ascraster.duplicategrid(nin_crit_sw_ara)
    nup_crit_sw_ara.substract(nsro_crit_sw_ara)
    nup_crit_sw_ara.multiply(frnup_ara)
    fileout = os.path.join(params.outputdir, "nup_crit_sw_ara.asc")
    nup_crit_sw_ara.write_ascii_file(fileout,
                                     output_nodata_value=-9999,
                                     compress=params.lcompress)
    print_debug(nup_crit_sw_ara, "nup_crit_sw_ara =")
    # 'igl'
    nup_crit_sw_igl = ascraster.duplicategrid(nin_crit_sw_igl)
    nup_crit_sw_igl.substract(nsro_crit_sw_igl)
    nup_crit_sw_igl.multiply(frnup_igl)
    fileout = os.path.join(params.outputdir, "nup_crit_sw_igl.asc")
    nup_crit_sw_igl.write_ascii_file(fileout,
                                     output_nodata_value=-9999,
                                     compress=params.lcompress)
    print_debug(nup_crit_sw_igl, "nup_crit_sw_igl =")

    ## * NUE at critical N inputs *
    # 'ara'
    nue_crit_sw_ara = ascraster.duplicategrid(nup_crit_sw_ara)
    nue_crit_sw_ara.divide(nin_crit_sw_ara, default_nodata_value=-9999)
    fileout = os.path.join(params.outputdir, "nue_crit_sw_ara.asc")
    nue_crit_sw_ara.write_ascii_file(fileout,
                                     output_nodata_value=-9999,
                                     compress=params.lcompress)
    print_debug(nue_crit_sw_ara, "nue_crit_sw_ara =")
    # 'igl'
    nue_crit_sw_igl = ascraster.duplicategrid(nup_crit_sw_igl)
    nue_crit_sw_igl.divide(nin_crit_sw_igl, default_nodata_value=-9999)
    fileout = os.path.join(params.outputdir, "nue_crit_sw_igl.asc")
    nue_crit_sw_igl.write_ascii_file(fileout,
                                     output_nodata_value=-9999,
                                     compress=params.lcompress)
    print_debug(nue_crit_sw_igl, "nue_crit_sw_igl =")

    ## * Maximum uptake fraction  *
    # 'ara'
    fnup_max_sw_ara = ascraster.duplicategrid(nup_max_ara)
    fnup_max_sw_ara.divide(nup_crit_sw_ara)
    fileout = os.path.join(params.outputdir, "fnup_max_sw_ara.asc")
    fnup_max_sw_ara.write_ascii_file(fileout,
                                     output_nodata_value=-9999,
                                     compress=params.lcompress)
    print_debug(fnup_max_sw_ara, "fnup_max_sw_ara =")
    # 'igl'
    fnup_max_sw_igl = ascraster.duplicategrid(nup_max_igl)
    fnup_max_sw_igl.divide(nup_crit_sw_igl)
    fileout = os.path.join(params.outputdir, "fnup_max_sw_igl.asc")
    fnup_max_sw_igl.write_ascii_file(fileout,
                                     output_nodata_value=-9999,
                                     compress=params.lcompress)
    print_debug(fnup_max_sw_igl, "fnup_max_sw_igl =")

    ## * Correction factor for grid cells where Nup,crit > Nup,max *
    # 'ara'
    fnup_corr_sw_ara = ascraster.duplicategrid(nin_max_ara)
    fnup_corr_sw_ara.substract(nfix_ara)
    temp2_ara = ascraster.duplicategrid(nh3_tot_egl)
    temp2_ara.add(nox_em)
    temp2_ara.multiply(fara)
    fnup_corr_sw_ara.substract(temp2_ara)
    temp3_ara = ascraster.duplicategrid(nh3em_crit_sw_ara)
    temp3_ara.multiply(fara)
    temp3_ara.add(nman_crit_sw_ara)
    temp3_ara.add(nfer_crit_sw_ara)
    fnup_corr_sw_ara.divide(temp3_ara, default_nodata_value=-9999)
    fileout = os.path.join(params.outputdir, "fnup_corr_sw_ara.asc")
    fnup_corr_sw_ara.write_ascii_file(fileout,
                                      output_nodata_value=-9999,
                                      compress=params.lcompress)
    print_debug(fnup_corr_sw_ara, "fnup_corr_sw_ara =")
    # 'igl'
    fnup_corr_sw_igl = ascraster.duplicategrid(nin_max_igl)
    fnup_corr_sw_igl.substract(nfix_igl)
    temp2_igl = ascraster.duplicategrid(nh3_tot_egl)
    temp2_igl.add(nox_em)
    temp2_igl.multiply(figl)
    fnup_corr_sw_igl.substract(temp2_igl)
    temp3_igl = ascraster.duplicategrid(nh3em_crit_sw_igl)
    temp3_igl.multiply(figl)
    temp3_igl.add(nman_crit_sw_igl)
    temp3_igl.add(nfer_crit_sw_igl)
    fnup_corr_sw_igl.divide(temp3_igl, default_nodata_value=-9999)
    fileout = os.path.join(params.outputdir, "fnup_corr_sw_igl.asc")
    fnup_corr_sw_igl.write_ascii_file(fileout,
                                      output_nodata_value=-9999,
                                      compress=params.lcompress)
    print_debug(fnup_corr_sw_igl, "fnup_corr_sw_igl =")

    ########### FORWARD CALCULATIONS TO CHECK ###########
    ## * Critical N budget *
    # 'ara'
    nbud_crit_sw_ara = ascraster.duplicategrid(nin_crit_sw_ara)
    nbud_crit_sw_ara.substract(nup_crit_sw_ara)
    print_debug(nbud_crit_sw_ara, "nbud_crit_sw_ara =")
    # 'igl'
    nbud_crit_sw_igl = ascraster.duplicategrid(nin_crit_sw_igl)
    nbud_crit_sw_igl.substract(nup_crit_sw_igl)
    print_debug(nbud_crit_sw_igl, "nbud_crit_sw_igl =")
    # 'nat'
    nbud_crit_sw_nat = ascraster.duplicategrid(nin_crit_sw_nat)
    print_debug(nbud_crit_sw_nat, "nbud_crit_sw_nat =")
    # 'egl'
    nbud_crit_sw_egl = ascraster.duplicategrid(nin_crit_sw_egl)
    nbud_crit_sw_egl.substract(nup_egl)
    print_debug(nbud_crit_sw_egl, "nbud_crit_sw_egl =")

    ## * Critical leaching *
    # 'ara'
    nle_crit_sw_ara = ascraster.duplicategrid(nbud_crit_sw_ara)
    nle_crit_sw_ara.substract(nsro_crit_sw_ara)
    nle_crit_sw_ara.multiply(fle_ag)
    print_debug(nle_crit_sw_ara, "nle_crit_sw_ara =")
    # 'igl'
    nle_crit_sw_igl = ascraster.duplicategrid(nbud_crit_sw_igl)
    nle_crit_sw_igl.substract(nsro_crit_sw_igl)
    nle_crit_sw_igl.multiply(fle_ag)
    print_debug(nle_crit_sw_igl, "nle_crit_sw_igl =")
    # 'nat'
    nle_crit_sw_nat = ascraster.duplicategrid(nbud_crit_sw_nat)
    nle_crit_sw_nat.substract(nsro_crit_sw_nat)
    nle_crit_sw_nat.multiply(fle_nat)
    print_debug(nle_crit_sw_nat, "nle_crit_sw_nat =")
    # 'egl'
    nle_crit_sw_egl = ascraster.duplicategrid(nbud_crit_sw_egl)
    nle_crit_sw_egl.substract(nsro_crit_sw_egl)
    nle_crit_sw_egl.multiply(fle_ag)
    print_debug(nle_crit_sw_egl, "nle_crit_sw_egl =")

    ## * groundwater N load from recent inputs *
    # 'ara'
    ngw_rec_crit_sw_ara = ascraster.duplicategrid(nle_crit_sw_ara)
    ngw_rec_crit_sw_ara.multiply(fgw_rec_le_ag)
    print_debug(ngw_rec_crit_sw_ara, "ngw_rec_crit_sw_ara =")
    # 'igl'
    ngw_rec_crit_sw_igl = ascraster.duplicategrid(nle_crit_sw_igl)
    ngw_rec_crit_sw_igl.multiply(fgw_rec_le_ag)
    print_debug(ngw_rec_crit_sw_igl, "ngw_rec_crit_sw_igl =")
    # 'nat'
    ngw_rec_crit_sw_nat = ascraster.duplicategrid(nle_crit_sw_nat)
    ngw_rec_crit_sw_nat.multiply(fgw_rec_le_nat)
    print_debug(ngw_rec_crit_sw_nat, "ngw_rec_crit_sw_nat =")
    # 'egl'
    ngw_rec_crit_sw_egl = ascraster.duplicategrid(nle_crit_sw_egl)
    ngw_rec_crit_sw_egl.multiply(fgw_rec_le_ag)
    print_debug(ngw_rec_crit_sw_egl, "ngw_rec_crit_sw_egl =")

    ## * Variable critical N load to surface water *

    # 'ara'
    nload_var_crit_sw_ara = ascraster.duplicategrid(nsro_crit_sw_ara)
    nload_var_crit_sw_ara.add(ngw_rec_crit_sw_ara)
    print_debug(nload_var_crit_sw_ara, "nload_var_crit_sw_ara =")
    # 'igl'
    nload_var_crit_sw_igl = ascraster.duplicategrid(nsro_crit_sw_igl)
    nload_var_crit_sw_igl.add(ngw_rec_crit_sw_igl)
    print_debug(nload_var_crit_sw_igl, "nload_var_crit_sw_igl =")
    # 'nat'
    nload_var_crit_sw_nat = ascraster.duplicategrid(nsro_crit_sw_nat)
    nload_var_crit_sw_nat.add(ngw_rec_crit_sw_nat)
    print_debug(nload_var_crit_sw_nat, "nload_var_crit_sw_nat =")
    # 'egl'
    nload_var_crit_sw_egl = ascraster.duplicategrid(nsro_crit_sw_egl)
    nload_var_crit_sw_egl.add(ngw_rec_crit_sw_egl)
    print_debug(nload_var_crit_sw_egl, "nload_var_crit_sw_egl =")

    ## * calculate n load tot crit sw TEST *
    nload_crit_sw_test = ascraster.duplicategrid(nload_var_crit_sw_ara)
    nload_crit_sw_test.add(nload_var_crit_sw_igl)
    nload_crit_sw_test.add(nload_var_crit_sw_nat)
    nload_crit_sw_test.add(nload_var_crit_sw_egl)
    nload_crit_sw_test.add(nload_var_crit_other)
    nload_crit_sw_test.add(nload_fixed_tot)
    fileout = os.path.join(params.outputdir, "nload_crit_sw_test.asc")
    nload_crit_sw_test.write_ascii_file(fileout,
                                        output_nodata_value=-9999,
                                        compress=params.lcompress)

    ########### FORWARD CALCULATIONS TO CHECK ###########
    if icell_debug < 0:
        pass
    else:
        fw = nload_crit_sw_test.get_data(icell_debug)
        bw = nload_crit_sw.get_data(icell_debug)
        #print(fw)
        #print(bw)
        if fw is None:
            print(
                "FW/BW_TEST:_Forward_calculation_not_possible:_Nin,crit = None"
            )

        else:
            fw = round(fw, 2)
            bw = round(bw, 2)
            if fw == bw:
                print("FW/BW_TEST = SUCCESFUL")
            else:
                print("FW/BW_TEST = NOT_SUCCESFUL")
Пример #14
0
def calculate(params,mask,mouth_dict,basin,pnet,discharge):
    '''
    This function calculates the water area and the water body.
    Here the discharge of the mouth and qs is set and calculated.
    '''
    m2tokm2 = 1.0e-6
    mmtokm  = 1.0e-6
    kmtom   = 1.0e3

    # Read flooding areas as fraction of the cellarea.
    flooding_fraction = ascraster.Asciigrid(ascii_file=params.flooding_fraction,\
                                    mask=mask,numtype=float)

    # Read rivers and lakes/reservoirs areas as fraction of the cellarea.
    fraction_water_grid = ascraster.Asciigrid(ascii_file=params.fraction_water,\
                                  mask=mask,numtype=float)    

    # Read the cellarea
    cellarea_grid = ascraster.Asciigrid(ascii_file=params.cellarea,\
                                        mask=mask,numtype=float)
    cellarea_grid.multiply(m2tokm2)

    # Read lake/reservoir id
    lakeid_grid = ascraster.Asciigrid(ascii_file=params.lakeid,\
                                    mask=mask,numtype=int)

    # Calculate water area
    water_area = ascraster.duplicategrid(flooding_fraction)
    local_discharge = ascraster.duplicategrid(discharge)

    # Make nodata on all cells.
    water_area.add_values(water_area.length*[water_area.nodata_value])
    flooding_area = ascraster.duplicategrid(water_area)

    for icell in range(flooding_fraction.length):
        id = lakeid_grid.get_data(icell,-1)
        if (id > 0):
            waterbody_fraction_cell = fraction_water_grid.get_data(icell,0.0)
            flooding_fraction_cell  = 0.0
        else:
            flooding_fraction_cell = flooding_fraction.get_data(icell,0.0)
            waterbody_fraction_cell = fraction_water_grid.get_data(icell,0.0)
            waterbody_fraction_cell += flooding_fraction_cell
        if (waterbody_fraction_cell > 1.0):
            waterbody_fraction_cell = 1.0
        area = cellarea_grid.get_data(icell,0.0)
        water_area.set_data(icell,waterbody_fraction_cell * area)
        flooding_area.set_data(icell,flooding_fraction_cell * area)
        
        # Calculation of local discharge.
        discharge_cell = pnet.get_data(icell,0.0)
        if (discharge_cell > 0.):
            # Convert pnet [mm/yr] to km3 by multiplying with cellarea and 10e-6 (conversion from mm to km)
            flux = discharge_cell * area * mmtokm
            local_discharge.set_data(icell,flux)
        else:
            local_discharge.set_data(icell,0.0)

    # Take the discharge of the mouth of the river divided by the water area of the mouth cell. 
    for key in list(mouth_dict.keys()):
       mouth_dict[key].discharge_mouth = discharge.get_data(mouth_dict[key].index_grid,0.0)
       mouth_dict[key].waterarea = 0.0
       
    # Calculate the water body area for each river basin.
    # Create a start residence time with zero's.
    water_body = ascraster.duplicategrid(discharge)
    # Make nodata on all cells. As long as we can not calculate the waterarea, we use a waterbody of one instead of zero!
    water_body.add_values(water_body.length*[0])

    for icell in range(0,basin.length):
       basinid = basin.get_data(icell,-1)
       if (basinid > 0):
           try:
               area = water_area.get_data(icell,0.0)
               if (area > 0.0):                  
                   mouth_dict[int(basinid)].waterarea += area
                   water_body.set_data(icell,1)
                   mouth_dict[int(basinid)].flooding_area += flooding_area.get_data(icell,0.0)
           except KeyError:
               # We don't not do anything with this river.
               print("Basinid " + str(basinid) + " found in basinid map but not in river mouth file.")
    
    for key in list(mouth_dict.keys()):
       try:
           mouth_dict[key].qs = kmtom * mouth_dict[key].discharge_mouth/mouth_dict[key].waterarea
       except ZeroDivisionError:
            mouth_dict[key].qs  = 0.0

    # Write local discharge to output file
    local_discharge.write_ascii_file(os.path.join(params.outputdir,"discharge_local.asc"))

    return water_area, water_body
Пример #15
0
def get_netcdf_year_grid(netcdf_filename,
                         varname,
                         year,
                         params=None,
                         mask=None,
                         nodata_value=-9999.,
                         idim=0):
    '''
    Returns the whole grid at a given time.
    If prescribed (ncols,nrows,xll,yll and cellsize in params) then that format
    is returned. Also if mask is specified then masked grid is returned.
    If params is None, then netcdf as is is returned for that year.
    Nodata_value is used as default nodata value of the output grid.
    idim is the number of the fourth dimension. Only when the number of dimensions is four,
    you must specify the last dimension. Normal {time,lat,lon,ndim} as dimensions for this.
    '''
    # We have to open and close the filename
    if (not os.path.isfile(netcdf_filename)):
        raise MyError("File: " + netcdf_filename + " does not exist.")
    ncdata = Dataset(netcdf_filename, 'r')

    # Determine the years that are given for this parameter
    nctimes = ncdata.variables['time']
    years = convert_numdate2year(nctimes[:], nctimes.units)

    # Find begin and end point for this time period
    istart, iend = find_within_range(years, year, year)

    # Check whether one of the boundaries is equal to the year specified.
    # Range of 0.001 is used, because this is smaller than 0.5 day.
    if (math.fabs(years[istart] - year) < 0.001):
        ifound = istart
    elif (math.fabs(years[iend - 1] - year) < 0.001):
        ifound = iend - 1
    else:
        raise MyError("File: " + netcdf_filename + " does not have data for: " + str(year) +".",\
                      "Years found: " + ";".join(map(str,years)))

    # Assign the two dimension data for this parameter.
    try:
        if (len(ncdata.variables[varname].dimensions) == 3):
            vardata = ncdata.variables[varname][ifound, :, :]

        elif (len(ncdata.variables[varname].dimensions) == 4):
            try:
                vardata = ncdata.variables[varname][ifound, :, :, idim]
            except IndexError:
                raise MyError("File: " + netcdf_filename + " contains parameter: " + varname,\
                              "Dimension given (given with idim): " + str(idim) + " is not available.",\
                              "Available dimension must be between 0 and " + \
                              str(len(ncdata.variables[varname][ifound,0,0,:])-1))

        else:
            raise MyError("File: " + netcdf_filename + " contains parameter: " + varname + \
                          " with unknown dimensions.")
    except KeyError:
        raise MyError("File: " + netcdf_filename + " does not contain parameter: " + varname,\
                      "Parameters available: " + ";".join(map(str,list(ncdata.variables.keys())))+".")

    # Create a Asciigrid of this data
    # Get latitude
    try:
        lat = ncdata.variables['latitude'][:]
    except KeyError:
        try:
            lat = ncdata.variables['lat'][:]
        except KeyError:
            raise MyError("File: " + netcdf_filename + " does not contain parameter: lat or latitude.",\
                          "Parameters available: " + ";".join(map(str,list(ncdata.variables.keys())))+".")

    # Set number of rows
    nrows = len(lat)

    # Get longitude
    try:
        lon = ncdata.variables['longitude'][:]
    except KeyError:
        try:
            lon = ncdata.variables['lon'][:]
        except KeyError:
            raise MyError("File: " + netcdf_filename + " does not contain parameter: lon or longitude.",\
                          "Parameters available: " + ";".join(map(str,list(ncdata.variables.keys())))+".")
    # Set number of columns
    ncols = len(lon)

    # Get stepsize of the grid.
    stepsize_col = math.fabs((lon[-1] - lon[0]) / float(ncols - 1))
    stepsize_row = math.fabs((lat[-1] - lat[0]) / float(nrows - 1))

    if (math.fabs(stepsize_col - stepsize_row) > 0.0001):
        raise MyError("File: " + netcdf_filename +
                      " has not the same cellsize in lat - lon direction.")

    # Set cellsize and xllcorner and yllcorner
    cellsize = stepsize_col
    xllcorner = min(lon[-1], lon[0]) - 0.5 * cellsize
    yllcorner = min(lat[-1], lat[0]) - 0.5 * cellsize

    # Find out whether the data must be flipped.
    # We want to start left-upper corner and then go rowwise down.
    if (lat[0] < lat[-1]):
        # We have to flip (bottom to top and top to bottom)
        vardata_flip = np.flipud(vardata)
        vardata = vardata_flip
        print("Flipud is performed.")
    if (lon[-1] < lon[0]):
        # We have to flip (left-right)
        vardata_flip = np.fliplr(vardata)
        vardata = vardata_flip
        print("Fliplr is performed.")

    # Set nodata value to the specified value
    #vardata1 = vardata.filled([nodata_value])
    #vardata = vardata1
    # Reshape the two dimension grid into one dimensional list.
    try:
        vardata_list = list(
            np.reshape(vardata.filled([nodata_value]), nrows * ncols))
    except AttributeError:
        # Array is not masked.
        vardata_list = list(np.reshape(vardata, nrows * ncols))

    # Create output grid.
    if (params == None):
        outgrid = ascraster.Asciigrid(nrows=nrows,ncols=ncols,xllcorner=xllcorner,\
                                      yllcorner=yllcorner,cellsize=cellsize,\
                                      nodata_value = nodata_value)

    else:
        # Dependent on cellsize, choose the right parameters setting.
        if (math.fabs(cellsize - 0.5) < 0.0001):
            # Half degree settings.
            try:
                outgrid = ascraster.Asciigrid(nrows=params.nrows,ncols=params.ncols,xllcorner=params.xllcorner,\
                                          yllcorner=params.yllcorner,cellsize=params.cellsize,\
                                          nodata_value = nodata_value)
            except AttributeError:
                raise MyError("Set the attributes nrows, ncols, xllcorner, yllcorner and cellsize",\
                          "in the class params for the function manip_netcdf.get_netcdf_year_grid.")

        elif (math.fabs(cellsize - 0.08333) < 0.0001):
            # Five minutes settings.
            try:
                outgrid = ascraster.Asciigrid(nrows=params.nrows_5min,ncols=params.ncols_5min,xllcorner=params.xllcorner,\
                                          yllcorner=params.yllcorner,cellsize=params.cellsize_5min,\
                                          nodata_value = nodata_value)
            except AttributeError:
                raise MyError("Set the attributes nrows_5min, ncols_5min, xllcorner, yllcorner and cellsize_5min",\
                          "in the class params for the function manip_netcdf.get_netcdf_year_grid.")
        else:
            raise MyError("File: " + netcdf_filename + " does not have the expected spatial resolution.",\
                          "Read this file with params=None in function manip_netcdf.get_netcdf_year_grid.")

    # Put data into grid.
    outgrid.add_values(vardata_list)

    # Apply the mask when it is given
    if (mask != None):
        outgrid.apply_mask(mask)

    # Close file when we opened it here.
    ncdata.close()

    # Return years
    return outgrid
Пример #16
0
import my_sys

coordinate = [-81.25, 39.75]

# First argument is the input directory of the raster files.
inputdir = sys.argv[1]

if not os.path.isdir(inputdir):
    print("STOP: Input directory does not exist.")

# Get for a cell the data value for all grid files.
listdir = os.listdir(inputdir)

for file1 in sorted(listdir):
    filename = os.path.join(inputdir, file1)
    ldata = False
    if (os.path.splitext(file1)[1].upper() == ".ASC"):
        # Use this file
        ldata = True
    elif (os.path.splitext(file1)[1].upper() == ".GZ"):
        if (os.path.splitext(os.path.splitext(file)[0])[1].upper() == ".ASC"):
            ldata = True
    elif (os.path.splitext(file1)[1].upper() == ".MAP"):
        ldata = True
    # Do it.
    if (ldata):
        # Open grid
        grid = ascraster.Asciigrid(ascii_file=filename)
        ind = grid.get_index_from_coordin(coordinate[0], coordinate[1])
        print(file1 + ": " + str(grid.get_data(ind)))
Пример #17
0
def calculate_water_litho(params,mask):
    '''
    Calculate the total discharge for each litho class. 
    This is needed to fill in the table of litho_lib with information from Jens Hartmann
    '''
    pnet = ascraster.Asciigrid(ascii_file=params.pnet,mask=mask,numtype=float)
    if params.ldebug: print(params.pnet  + " has been read.")  

    landarea = ascraster.Asciigrid(ascii_file=params.landarea,mask=mask,numtype=float)
    if params.ldebug: print(params.landarea  + " has been read.")    
    
    # Calculate accumulated water flux of each grid cell to the mouth of the river basin. Make discharge in km3
    # Convert pnet [mm/yr] to km3 by multiplying with landarea and 10e-6 (conversion from mm to km)
    water = ascraster.duplicategrid(landarea)    
    for icell in range(landarea.length):
        wt = pnet.get_data(icell,0.0) * landarea.get_data(icell,0.0) * 1.0e-6
        water.set_data(icell,wt)

    # Compare first grid with another grid of the run
    equal = ascraster.compare_grids(params.pgrass,os.path.join(params.litho_dir,"nodatgrid.asc"))
    if (not equal):
        raise MyError("Litho maps don't have same header as other input files.")
        
    grid0 = ascraster.Asciigrid(ascii_file=os.path.join(params.litho_dir,"nodatgrid.asc"),\
                                mask=mask,numtype=float)

    # Check whether there are fractions given in grid0
    max_value = max(grid0.values)
    if (max_value > 1.0):
        raise MyError("Litho maps must be a fraction. Found values greater than 1 (found: " + str(max_value) + ").")
  
    # Make a default output list:
    out = len(litho_lib) * [0.0]
    area = len(litho_lib) * [0.0]

    # Loop over all the classes of litho map. Start with class 3 because first two are major water bodies and ice:
    for iclass in range(1,18):
        print(iclass)
        # Check header of input file
        equal = ascraster.compare_grids(params.pgrass,os.path.join(params.litho_dir,"grid" + str(iclass) + ".asc"))
        if (not equal):
            raise MyError("Litho maps don't have same header as other input files.")                

        grid = ascraster.Asciigrid(ascii_file=os.path.join(params.litho_dir,"grid" + str(iclass) + ".asc"),\
                                   mask=mask,numtype=float)
           
        print(os.path.join(params.litho_dir,"grid" + str(iclass) + ".asc") + " read.")
        for icell in range(0,grid0.length):
            grid0_cell = grid0.get_data(icell,0.0)
            if (grid0_cell < 1.0):
                # There is something to do.
                grid_cell = grid.get_data(icell,0.0)

                if (grid_cell > 0.0):
                    # There is something of this class in this cell:                       
                    out[iclass] += grid_cell * water.get_data(icell,0.0)
                    area[iclass] += grid_cell * landarea.get_data(icell,0.0)
            
            if (iclass == 1):
                out[0]  += grid0_cell * water.get_data(icell,0.0)
                area[0] += grid0_cell * landarea.get_data(icell,0.0)

    return out,area
Пример #18
0
        # Calculate the statistics.
        kappa, kappa_loc, kappa_histo = calculate_statistics(matrix_class)

        output_dict[min_num + iclass] = [kappa, kappa_loc, kappa_histo]

    return output_dict


# Source code to test this functions.

if (__name__ == "__main__"):

    import ascraster

    mapA = ascraster.Asciigrid(ncols = 4,nrows = 4, xllcorner = 0.0, yllcorner = 0.0, \
                               cellsize = 1.0, nodata_value = -1, numtype = int)
    mapB = ascraster.duplicategrid(mapA)
    mapA.values = [-1,-1,-1,-1, 1, 1, 1, 1,\
                    2, 2, 2, 2, 3, 3, 3, 3]
    mapB.values = [-1,-1,-1,-1, 1, 1, 1, 1,\
                    3, 2, 2, 2, 3, 3, 3, 3]
    print("Raster are equal.")
    print(compare_rasters(mapA, mapA))
    print("Cramer: ", calculate_cramer_v(mapA, mapA))
    print("Raster are equal. PER CLASS")
    print(compare_rasters_per_class(mapA, mapA))
    print("Raster are equal except one cell (3 versus 2).")
    print(compare_rasters(mapA, mapB))
    print(compare_rasters_per_class(mapA, mapB))
    print("Cramer: ", calculate_cramer_v(mapA, mapA))
    mapA.values = [-1,-1,-1,-1, 1, 1, 1, 1,\
Пример #19
0
def gridfile_total(filename,txt,fp=None):
    grid = ascraster.Asciigrid(ascii_file = filename,numtype = float)
    return print_total(grid,txt,fp=fp)
Пример #20
0
    print(unf_datatype("GLCT.unf2"))
    try:
        print(unf_datatype("GLCT.unf3"))
    except MyError as val:
        val.write()

    #filename = r'/home/arthur/mestverdeling/in1900/GAREALAND.UNF0'
    #dt,num = unf_datatype(filename)
    #area = np.fromfile(filename, dtype=dt).tolist()
    #print area[:10]
    #area = read_unffile(filename)
    #print area[:10]

    dummy = ascraster.Asciigrid(ncols=720,
                                nrows=360,
                                xllcorner=-180,
                                yllcorner=-90,
                                cellsize=0.5,
                                nodata_value=-1)

    # Create mask
    filename = r'/mnt/s/beusena/unf/GR.UNF2'
    gr = read_unffile(filename)
    filename = r'/mnt/s/beusena/unf/GC.UNF2'
    gc = read_unffile(filename)
    mask = []
    for item in range(len(gr)):
        ind = dummy.get_index_from_row_col(gr[item], gc[item])
        mask.append(ind)

    example_raster = ascraster.Asciigrid(ncols=720,
                                         nrows=360,
Пример #21
0
    def make_value_list(self,params,mask,list_attribute_names):
        '''
        The litho grid is given in classes. For each cell we know the fraction of that class in the cell.
        Grid0 is the nodata information. When grid0 is 1.0 then the cell is 100% nodata.
        A list with length of grids (normally length of mask) is returned with on each entry the
        value of the attribute given in the list_attribute_names (and also the order as given in this list).
        '''
        # Compare first grid with another grid of the run
        equal = ascraster.compare_grids(params.pgrass,os.path.join(params.litho_dir,"nodatgrid.asc"))
        if (not equal):
            raise MyError("Litho maps don't have same header as other input files.")
        
        grid0 = ascraster.Asciigrid(ascii_file=os.path.join(params.litho_dir,"nodatgrid.asc"),\
                                       mask=mask,numtype=float)

        # Check whether there are fractions given in grid0
        max_value = max(grid0.values)
        if (max_value > 1.0):
            raise MyError("Litho maps must be a fraction. Found values greater than 1 (found: " + str(max_value) + ").")

    
        # Make a default output list:
        out = []
        for i in range(grid0.length):
            out.append(len(list_attribute_names) * [0.0])
        
        # Loop over all the classes of litho map. Start with class 3 because first two are major water bodies and ice:
        for iclass in range(3,18):
            # Get the litho_class values for this list_attributes_names
            values = []
            for item in range(len(list_attribute_names)):
                values.append(float(getattr(litho_lib[iclass],list_attribute_names[item])))

            # Check header of input file
            equal = ascraster.compare_grids(params.pgrass,os.path.join(params.litho_dir,"grid" + str(iclass) + ".asc"))
            if (not equal):
                raise MyError("Litho maps don't have same header as other input files.")                

            grid = ascraster.Asciigrid(ascii_file=os.path.join(params.litho_dir,"grid" + str(iclass) + ".asc"),\
                                       mask=mask,numtype=float)
           
            for icell in range(0,grid0.length):
                grid0_cell = grid0.get_data(icell,0.0)
                if (grid0_cell < 1.0):
                    # There is something to do.
                    grid_cell = grid.get_data(icell,0.0)

                    if (grid_cell > 0.0):
                        # There is something of this class in this cell:                       
                        for item in range(len(list_attribute_names)):
                            out[icell][item] += grid_cell * values[item]

        correc = grid0.length * [0.0]
        
        # Correction loop to correct for the nodata of the cell
        for iclass in range(1,3):
            # Get the litho_class values for this list_attributes_names

            # Check header of input file
            equal = ascraster.compare_grids(params.pgrass,os.path.join(params.litho_dir,"grid" + str(iclass) + ".asc"))
            if (not equal):
                raise MyError("Litho maps don't have same header as other input files.")                

            grid = ascraster.Asciigrid(ascii_file=os.path.join(params.litho_dir,"grid" + str(iclass) + ".asc"),\
                                       mask=mask,numtype=float)
           
            for icell in range(0,grid0.length):
                grid0_cell = grid0.get_data(icell,0.0)
                if (grid0_cell < 1.0):
                    # There is something to do.
                    if (iclass == 1):
                        correc[icell] += grid0_cell

                    grid_cell = grid.get_data(icell,0.0)
                    if (grid_cell > 0.0):
                        correc[icell] += grid_cell

        # Make correction for the situation where there is water bodies or ice or a part of the cell is nodata.
        for icell in range(len(correc)):
            if (correc[icell] > 0.0):
                # There was ice or water bodies or part of the cell is nodata

                if (correc[icell] < 1.0):
                    for item in range(len(list_attribute_names)):
                        out[icell][item] /= (1.0 - correc[icell])
        return out
Пример #22
0
          photo_average(values, 7.01 / 365, 372.01 / 365), "Value expected: ",
          sum(values) / float(len(values)))
    print(photo_average(values, 0.0, 7.01 / 365), "Value expected: ",
          sum(range(1, 10)) / float(len(list(range(1, 10)))))
    print(photo_average(values, 360.01 / 365, 1.0), "Value expected: ",
          sum(values[360:]) / float(len(values[360:])))
    QQ = ((7.01 / 365.) * 5 +
          (1.0 - 360.01 / 365.) * 362.5) / (1.0 - (360.01 / 365.) +
                                            (7.01 / 365.))
    print(photo_average(values, 360.01 / 365, 372.01 / 365),
          "Value expected: ", QQ)
    if (len(params.phytoindex) > 0):
        make_photosyn(params, species)

        # Checking
        basin = ascraster.Asciigrid(ascii_file=params.basin, numtype=int)
        specname = species[params.phytoindex[0]].get_name()
        outputfile = os.path.join(
            params.outputdir,
            os.path.splitext(os.path.basename(params.photosyn_file))[0] + "_" +
            specname + ".pkl")
        fp = open(outputfile, "rb")
        photo = list(general_func.pickleLoader(fp))
        print(len(photo), params.photosyn_file)
        fp.close()
        lats = [89.75, 87.25, -60.25, 0.25, -89.75]
        for lat in lats:
            ind = iround((90.0 - lat + 0.5 * basin.cellsize) / basin.cellsize)
            print(lat, photo[ind][0])
    else:
        print("No species with photosyntheses.")
Пример #23
0
'''
import os
import sys
__general = os.path.join(os.getcwd(), 'trunk')
if os.path.exists(__general):
    sys.path.insert(0, __general)
    print(__general + " is added to the python search path for modules.")

import ascraster
import my_sys

# First three tests are on a grid with no nodata
# Test 1
# Multiply grid1 with a scalar of type integer
# Read ascii grid
grid1 = ascraster.Asciigrid(ascii_file='testgrid1.asc', numtype=int)
factor = 1
grid1_old = ascraster.duplicategrid(grid1)
grid1.multiply(factor)
# Grid1 and grid1_old must be the same
ltest1 = 1
for i in range(grid1_old.length):
    if (grid1.values[i] != grid1_old.values[i]):
        ltest1 = 0
        print('Test 1 is not a succes for item: ' + str(i) +
              ". Values found: " + str(grid1.values[i]) + " and " +
              str(grid1_old.values[i]))
if (ltest1 == 1):
    print("Test1 passed.")
else:
    print("Multiplying with factor 1")
Пример #24
0
def make_one_photosyn(arg):
    '''
    Makes the photosythese for one specie which needs it.
    Calculate this for each day and for just 1 year.
    Calculation is only done, when this file does not exist.
    '''
    params = arg[0]
    species = arg[1]
    ispec = arg[2]
    specname = species[ispec].get_name()
    inputfile = os.path.splitext(
        params.photosyn_file)[0] + "_" + specname + ".pkl"
    outputfile = os.path.join(params.outputdir,os.path.splitext(os.path.basename(\
                              params.photosyn_file))[0] + "_" + specname + ".pkl")

    if (os.path.isfile(inputfile)):
        # Check input file whether this file is belonging to this run by comparing W, kmax, J1 and eta.
        fp = open(inputfile, 'rb')
        # Read header and check this
        header = pickle.load(fp)
        fp.close()

        # First value is the alpha and second value is the kmax, J1 and eta.
        lerror = (header[0] != species[ispec].get_alpha())
        lerror = lerror or (header[1] != species[ispec].get_kmax())
        lerror = lerror or (header[2] != params.J1)
        lerror = lerror or (header[3] != params.eta)
        if (lerror):
            raise MyError("Photosyn_file: " + inputfile + " is not correct.",\
                          " Values in the file: " + " ".join(map(str,header)),\
                          " Values expected   : " + str(species[ispec].get_alpha()) + " " +\
                          str(species[ispec].get_kmax()) + " " + str(params.J1) + " " + str(params.eta))
        # Copy file to outputdirectory
        my_sys.my_copyfile(inputfile, outputfile)
        print("File " + inputfile + " copied.")
    else:
        print("Start calculating the photosynthese file for specie: ",
              species[ispec].get_name())
        # Calculate the photsyn for each lattitude and each day and store this on the output directory.
        # Determine the cellsize of the grid by reading a grid inputfile
        basin = ascraster.Asciigrid(ascii_file=params.basin, numtype=int)
        cellsize = basin.cellsize
        yllmin = basin.yllcorner
        yllmax = yllmin + cellsize * basin.nrows
        del basin

        # Open output file
        fp = open(outputfile, 'wb')
        # Make header of outputfile
        header = [
            species[ispec].get_alpha(), species[ispec].get_kmax(), params.J1,
            params.eta
        ]
        pickle.dump(header, fp, -1)
        yll = yllmax - 0.5 * cellsize
        while yllmin < yll:
            # First value is the lattitude
            values = [yll]
            for day in range(1, 366):
                val = photosynday(params.depth,day,species[ispec].get_kmax(),\
                      species[ispec].get_alpha(),yll,params.eta,params.J1)
                values.append(val)
            pickle.dump(values, fp, -1)
            # Go to next cell (lattitude)
            yll -= cellsize

        # Close file
        fp.close()
def calculate_subgrid_retention(params,mask,mouth_dict,basin,lake_icell_dict,load,\
                                water_body,pnet,vf):
    '''
    The subgrid retention is based on Wollheim (2006, GBC). This method is based on the stream order.
    In each cell, which is not a lake or reservoir, the subgrid retention is calculated.
    Only the local situation is used. For the load only the diffuse source from this cell are used. 
    The runoff is used (so only local water). Also the concentration effect or temperature effect on retention is not used here.
    '''
    # Make the subgrid stream order properties.
    # Setup the stream order river length,area and distribution and flowpath. Everything is stored in params.
    define_subgrid_streamorder(params)

    # Set the relation between concentration and net uptake velocity
    try:
        accuflux_retention.set_conc_relation_retention(params)
    except AttributeError:
        # This function is only for N and not for P
        pass

    # Make output rasters
    load_grid       = ascraster.duplicategrid(load)
    retention      = ascraster.duplicategrid(load)

    # Make nodata on all cells.
    retention.add_values(retention.length*[retention.nodata_value])
    retentionload  = ascraster.duplicategrid(retention)

    # Read water temperature
    temperature = ascraster.Asciigrid(ascii_file=params.water_temperature,mask=mask,numtype=float)

    # Loop over all cells:    
    for icell in range(load_grid.length):
        # Get mass flux at the cell.
        load_cell = load_grid.get_data(icell)
        if (load_cell == None):
            # No data value
            continue
        if load_cell < 0.0:
            print("Negative mass flow of " + str(load_cell) + " at cell: " + str(icell))           
            # Go to next cell.
            continue

        # Retention in cell
        water_body_cell = water_body.get_data(icell,0)
        if (water_body_cell > 0):
            runoff_cell = pnet.get_data(icell,0.0)
            # Check whether this cell is an outflow of a lake/reservoir
            try:            
                loutflow = lake_icell_dict[icell].outcell
            except KeyError:
                # No lake or reservoir
                loutflow = -1
            try:
                if (loutflow == -1):
                    # Normal cell, no lake or reservoir.
                    # Default temperature of 20C because then temperature has no effect.
                    temperature_cell = temperature.get_data(icell,20.0)
                    ret_cell = calculate_localretention(params,runoff_cell,vf,load_cell,temperature_cell)

                else:
                    #TODO: local retention when it is a lake or reservoir. For example as normal retention * fr_land.
                    # Lake or reservoir.
                    ret_cell = 0.0

            except OverflowError:
                raise OverflowError
        else:
            ret_cell = 0.0

        # Fill retention grid with retention value and retention load
        retention.set_data(icell,ret_cell)
        retentionload.set_data(icell,load_cell*ret_cell)

    # Write to output files
    retention.write_ascii_file(os.path.join(params.outputdir,"retention_subgrid.asc"))
    retentionload.write_ascii_file(os.path.join(params.outputdir,"removed_subgridload.asc"))

    # Write in mouth_dict database
    aggregate.aggregate_grid(basin,retentionload,mouth_dict,item="retentionload_subgrid")
    aggregate.aggregate_grid(basin,load_grid,mouth_dict,item="subgrid_load")
    # Put the retention_subgrid of the river in the mouth database
    for key in list(mouth_dict.keys()):
        try:
            mouth_dict[key].retention_subgrid = mouth_dict[key].retentionload_subgrid/mouth_dict[key].subgrid_load
        except ZeroDivisionError:
            mouth_dict[key].retention_subgrid = 0.0

    return retentionload,retention
Пример #26
0
def calculate(params):

    #print("The critical N deposition rate is " + str(params.crit_dep) + " kg N ha-1 yr-1")

    # load needed variables for calculations
    biome = ascraster.Asciigrid(ascii_file=os.path.join(
        params.inputdir, "gnlct.asc"),
                                numtype=float,
                                mask=params.mask)
    a_tot = ascraster.Asciigrid(ascii_file=os.path.join(
        params.inputdir, "a_tot.asc"),
                                numtype=float,
                                mask=params.mask)
    nfix_ara = ascraster.Asciigrid(
        ascii_file=params.filename_nfixation_cropland,
        numtype=float,
        mask=params.mask)
    nfix_igl = ascraster.Asciigrid(ascii_file=params.filename_nfixation_intgl,
                                   numtype=float,
                                   mask=params.mask)
    nox_em = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nox_em.asc"),
                                 numtype=float,
                                 mask=params.mask)
    nh3_tot_egl = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nh3_tot_egl.asc"),
                                      numtype=float,
                                      mask=params.mask)
    nh3_tot_igl = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nh3_tot_igl.asc"),
                                      numtype=float,
                                      mask=params.mask)
    nh3_tot_ara = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nh3_tot_ara.asc"),
                                      numtype=float,
                                      mask=params.mask)
    nh3_ef_man_ara = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nh3_ef_man_ara.asc"),
                                         numtype=float,
                                         mask=params.mask)
    nh3_ef_man_igl = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nh3_ef_man_igl.asc"),
                                         numtype=float,
                                         mask=params.mask)
    nh3_ef_fer_ara = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nh3_ef_fer_ara.asc"),
                                         numtype=float,
                                         mask=params.mask)
    nh3_ef_fer_igl = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nh3_ef_fer_igl.asc"),
                                         numtype=float,
                                         mask=params.mask)
    frnfe_ara = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "frnfe_ara.asc"),
                                    numtype=float,
                                    mask=params.mask)
    frnfe_igl = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "frnfe_igl.asc"),
                                    numtype=float,
                                    mask=params.mask)
    fara = ascraster.Asciigrid(ascii_file=os.path.join(params.outputdir,
                                                       "fara.asc"),
                               numtype=float,
                               mask=params.mask)
    figl = ascraster.Asciigrid(ascii_file=os.path.join(params.outputdir,
                                                       "figl.asc"),
                               numtype=float,
                               mask=params.mask)
    fsro_ag = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "fsro_ag.asc"),
                                  numtype=float,
                                  mask=params.mask)
    frnup_ara = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "frnup_ara.asc"),
                                    numtype=float,
                                    mask=params.mask)
    frnup_igl = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "frnup_igl.asc"),
                                    numtype=float,
                                    mask=params.mask)
    nup_max_ara = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nup_max_ara.asc"),
                                      numtype=float,
                                      mask=params.mask)
    nup_max_igl = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nup_max_igl.asc"),
                                      numtype=float,
                                      mask=params.mask)
    nin_max_ara = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nin_max_ara.asc"),
                                      numtype=float,
                                      mask=params.mask)
    nin_max_igl = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nin_max_igl.asc"),
                                      numtype=float,
                                      mask=params.mask)

    # make grid with critical N deposition per biome
    ndep_crit_ha = ascraster.duplicategrid(nfix_ara)
    # watch out: can ndep_crit_ha become both -9999 and -1 (NA value?)
    for i in range(ndep_crit_ha.length):
        ndep_crit_ha.set_data(i, -9999)

    for icell in range(biome.length):
        val = biome.get_data(icell)
        # Ice (7) or Hot desert (16)
        if (val == 7 or val == 16):
            cl = 5.0
        # Boreal forest (10), Cool coniferous forest (11) or scrubland (17)
        elif (val == 10 or val == 11 or val == 17):
            cl = 7.5
        # Tundra (8) or wooded tundra (9)or Warm mixed forest (14)
        elif (val == 8 or val == 9 or val == 14):
            cl = 10.0
        # Temperate mixed forest (12) Temperate deciduous forest (13)
        elif (val == 12 or val == 13):
            cl = 12.5
        # Savanna (18)
        elif (val == 18):
            cl = 15.0
        # Grassland/steppe (15)
        elif (val == 15):
            cl = 17.5
        # Tropical woodland (19) or Tropical forest (20)
        elif (val == 19 or val == 20):
            cl = 20
        # Biome can also have value 0 or none (-1)
        else:
            continue
        ndep_crit_ha.set_data(icell, cl)

    print_debug(biome, "biome =")

    fileout = os.path.join(params.outputdir, "ndep_crit_ha.asc")
    ndep_crit_ha.write_ascii_file(fileout,
                                  output_nodata_value=-9999,
                                  compress=params.lcompress)
    print_debug(ndep_crit_ha, "ndep_crit_ha =")

    ## * Total critical deposition *
    ndep_crit_tot = ascraster.duplicategrid(ndep_crit_ha)
    ndep_crit_tot.multiply(a_tot)
    fileout = os.path.join(params.outputdir, "ndep_crit_tot.asc")
    ndep_crit_tot.write_ascii_file(fileout,
                                   output_nodata_value=-9999,
                                   compress=params.lcompress)
    print_debug(ndep_crit_tot, "ndep_crit_tot =")

    ## * Critical NH3 emissions *
    nh3em_ara_igl = ascraster.duplicategrid(nh3_tot_ara)
    nh3em_ara_igl.add(nh3_tot_igl)
    # 'ara'
    nh3_fraction_ara = ascraster.duplicategrid(nh3_tot_ara)
    nh3_fraction_ara.divide(nh3em_ara_igl, default_nodata_value=-9999)
    nh3em_crit_ara = ascraster.duplicategrid(ndep_crit_tot)
    nh3em_crit_ara.substract(nox_em)
    nh3em_crit_ara.substract(nh3_tot_egl)
    nh3em_crit_ara.multiply(nh3_fraction_ara)
    print_debug(nh3em_crit_ara, "nh3em_crit_ara =")
    # 'igl'
    nh3_fraction_igl = ascraster.duplicategrid(nh3_tot_igl)
    nh3_fraction_igl.divide(nh3em_ara_igl, default_nodata_value=-9999)
    nh3em_crit_igl = ascraster.duplicategrid(ndep_crit_tot)
    nh3em_crit_igl.substract(nox_em)
    nh3em_crit_igl.substract(nh3_tot_egl)
    nh3em_crit_igl.multiply(nh3_fraction_igl)
    print_debug(nh3em_crit_igl, "nh3em_crit_igl =")

    ## * Critical N inputs from manure *
    one_grid = ascraster.duplicategrid(frnfe_ara)
    for i in range(one_grid.length):
        one_grid.set_data(i, 1.0)

    # 'ara'
    one_min_frnfe_ara = ascraster.duplicategrid(one_grid)
    one_min_frnfe_ara.substract(frnfe_ara)
    frnfe_division_ara = ascraster.duplicategrid(frnfe_ara)
    frnfe_division_ara.divide(one_min_frnfe_ara, default_nodata_value=-9999)

    denominator_ara = ascraster.duplicategrid(frnfe_division_ara)
    denominator_ara.multiply(nh3_ef_fer_ara)
    denominator_ara.add(nh3_ef_man_ara)

    nman_crit_dep_ara = ascraster.duplicategrid(nh3em_crit_ara)
    nman_crit_dep_ara.divide(denominator_ara, default_nodata_value=-9999)

    for icell in range(nman_crit_dep_ara.length):
        f_ara = fara.get_data(icell)
        f_igl = figl.get_data(icell)
        if (f_ara == 0 and f_igl > 0):
            nman_ara = 0
        else:
            continue
        nman_crit_dep_ara.set_data(icell, nman_ara)

    for icell in range(nman_crit_dep_ara.length):
        nman_ara3 = nman_crit_dep_ara.get_data(icell)
        if (nman_ara3 is None):
            continue
        if (nman_ara3 < 0):
            nman_ara = 0
        else:
            continue
        nman_crit_dep_ara.set_data(icell, nman_ara)

    fileout = os.path.join(params.outputdir, "nman_crit_dep_ara.asc")
    nman_crit_dep_ara.write_ascii_file(fileout,
                                       output_nodata_value=-9999,
                                       compress=params.lcompress)
    print_debug(nman_crit_dep_ara, "nman_crit_dep_ara =")

    # 'igl'
    one_min_frnfe_igl = ascraster.duplicategrid(one_grid)
    one_min_frnfe_igl.substract(frnfe_igl)
    frnfe_division_igl = ascraster.duplicategrid(frnfe_igl)
    frnfe_division_igl.divide(one_min_frnfe_igl, default_nodata_value=-9999)

    denominator_igl = ascraster.duplicategrid(frnfe_division_igl)
    denominator_igl.multiply(nh3_ef_fer_igl)
    denominator_igl.add(nh3_ef_man_igl)

    nman_crit_dep_igl = ascraster.duplicategrid(nh3em_crit_igl)
    nman_crit_dep_igl.divide(denominator_igl, default_nodata_value=-9999)

    for icell in range(nman_crit_dep_igl.length):
        f_ara = fara.get_data(icell)
        f_igl = figl.get_data(icell)
        if (f_igl == 0 and f_ara > 0):
            nman_igl = 0
        else:
            continue
        nman_crit_dep_igl.set_data(icell, nman_igl)

    for icell in range(nman_crit_dep_igl.length):
        nman_igl3 = nman_crit_dep_igl.get_data(icell)
        if (nman_igl3 is None):
            continue
        if (nman_igl3 < 0):
            nman_igl = 0
        else:
            continue
        nman_crit_dep_igl.set_data(icell, nman_igl)

    fileout = os.path.join(params.outputdir, "nman_crit_dep_igl.asc")
    nman_crit_dep_igl.write_ascii_file(fileout,
                                       output_nodata_value=-9999,
                                       compress=params.lcompress)
    print_debug(nman_crit_dep_igl, "nman_crit_dep_igl =")

    ## calculate critical N input from fertilizer
    # 'ara'
    nfer_crit_dep_ara = ascraster.duplicategrid(nman_crit_dep_ara)
    nfer_crit_dep_ara.multiply(frnfe_division_ara)
    for icell in range(nfer_crit_dep_ara.length):
        f_ara = fara.get_data(icell)
        f_igl = figl.get_data(icell)
        if (f_ara == 0 and f_igl > 0):
            nfer_ara = 0
        else:
            continue
        nfer_crit_dep_ara.set_data(icell, nfer_ara)

    fileout = os.path.join(params.outputdir, "nfer_crit_dep_ara.asc")
    nfer_crit_dep_ara.write_ascii_file(fileout,
                                       output_nodata_value=-9999,
                                       compress=params.lcompress)
    print_debug(nfer_crit_dep_ara, "nfer_crit_dep_ara =")

    # 'igl'
    nfer_crit_dep_igl = ascraster.duplicategrid(nman_crit_dep_igl)
    nfer_crit_dep_igl.multiply(frnfe_division_igl)
    for icell in range(nfer_crit_dep_igl.length):
        f_ara = fara.get_data(icell)
        f_igl = figl.get_data(icell)
        if (f_igl == 0 and f_ara > 0):
            nfer_igl = 0
        else:
            continue
        nfer_crit_dep_igl.set_data(icell, nfer_igl)

    fileout = os.path.join(params.outputdir, "nfer_crit_dep_igl.asc")
    nfer_crit_dep_igl.write_ascii_file(fileout,
                                       output_nodata_value=-9999,
                                       compress=params.lcompress)
    print_debug(nfer_crit_dep_igl, "nfer_crit_dep_igl =")

    ## * Critical N inputs from fertilizer plus manure * ##
    # 'ara'
    nman_fer_crit_dep_ara = ascraster.duplicategrid(nman_crit_dep_ara)
    nman_fer_crit_dep_ara.add(nfer_crit_dep_ara)
    fileout = os.path.join(params.outputdir, "nman_fer_crit_dep_ara.asc")
    nman_fer_crit_dep_ara.write_ascii_file(fileout,
                                           output_nodata_value=-9999,
                                           compress=params.lcompress)
    # 'igl'
    nman_fer_crit_dep_igl = ascraster.duplicategrid(nman_crit_dep_igl)
    nman_fer_crit_dep_igl.add(nfer_crit_dep_igl)
    fileout = os.path.join(params.outputdir, "nman_fer_crit_dep_igl.asc")
    nman_fer_crit_dep_igl.write_ascii_file(fileout,
                                           output_nodata_value=-9999,
                                           compress=params.lcompress)

    ## * NH3 emissions at critical N input *
    # 'ara'
    nh3em_man_crit_dep_ara = ascraster.duplicategrid(nman_crit_dep_ara)
    nh3em_man_crit_dep_ara.multiply(nh3_ef_man_ara)
    nh3em_fer_crit_dep_ara = ascraster.duplicategrid(nfer_crit_dep_ara)
    nh3em_fer_crit_dep_ara.multiply(nh3_ef_fer_ara)
    nh3em_crit_dep_ara = ascraster.duplicategrid(nh3em_fer_crit_dep_ara)
    nh3em_crit_dep_ara.add(nh3em_man_crit_dep_ara)
    print_debug(nh3em_crit_dep_ara, "nh3em_crit_dep_ara =")
    # 'igl'
    nh3em_man_crit_dep_igl = ascraster.duplicategrid(nman_crit_dep_igl)
    nh3em_man_crit_dep_igl.multiply(nh3_ef_man_igl)
    nh3em_fer_crit_dep_igl = ascraster.duplicategrid(nfer_crit_dep_igl)
    nh3em_fer_crit_dep_igl.multiply(nh3_ef_fer_igl)
    nh3em_crit_dep_igl = ascraster.duplicategrid(nh3em_fer_crit_dep_igl)
    nh3em_crit_dep_igl.add(nh3em_man_crit_dep_igl)
    print_debug(nh3em_crit_dep_igl, "nh3em_crit_dep_igl =")

    ## * N deposition at critical N inputs *
    ndep_crit_dep_tot = ascraster.duplicategrid(nh3em_crit_dep_ara)
    ndep_crit_dep_tot.add(nh3em_crit_dep_igl)
    ndep_crit_dep_tot.add(nox_em)
    ndep_crit_dep_tot.add(nh3_tot_egl)
    fileout = os.path.join(params.outputdir, "ndep_crit_dep_tot.asc")
    ndep_crit_dep_tot.write_ascii_file(fileout,
                                       output_nodata_value=-9999,
                                       compress=params.lcompress)
    print_debug(ndep_crit_dep_tot, "ndep_crit_dep_tot =")
    # 'ara'
    ndep_crit_dep_ara = ascraster.duplicategrid(ndep_crit_dep_tot)
    ndep_crit_dep_ara.multiply(fara)
    print_debug(ndep_crit_dep_ara, "ndep_crit_dep_ara =")
    # 'igl'
    ndep_crit_dep_igl = ascraster.duplicategrid(ndep_crit_dep_tot)
    ndep_crit_dep_igl.multiply(figl)
    print_debug(ndep_crit_dep_igl, "ndep_crit_dep_igl =")

    ## * Total critical N inputs *
    # 'ara'
    nin_crit_dep_ara = ascraster.duplicategrid(nman_crit_dep_ara)
    nin_crit_dep_ara.add(nfer_crit_dep_ara)
    nin_crit_dep_ara.add(ndep_crit_dep_ara)
    nin_crit_dep_ara.add(nfix_ara)
    fileout = os.path.join(params.outputdir, "nin_crit_dep_ara.asc")
    nin_crit_dep_ara.write_ascii_file(fileout,
                                      output_nodata_value=-9999,
                                      compress=params.lcompress)
    print_debug(nin_crit_dep_ara, "nin_crit_dep_ara =")
    # 'igl'
    nin_crit_dep_igl = ascraster.duplicategrid(nman_crit_dep_igl)
    nin_crit_dep_igl.add(nfer_crit_dep_igl)
    nin_crit_dep_igl.add(ndep_crit_dep_igl)
    nin_crit_dep_igl.add(nfix_igl)
    fileout = os.path.join(params.outputdir, "nin_crit_dep_igl.asc")
    nin_crit_dep_igl.write_ascii_file(fileout,
                                      output_nodata_value=-9999,
                                      compress=params.lcompress)
    print_debug(nin_crit_dep_igl, "nin_crit_dep_igl =")
    # 'ara+igl'
    nin_crit_dep_araigl = ascraster.duplicategrid(nin_crit_dep_ara)
    nin_crit_dep_araigl.add(nin_crit_dep_igl)
    fileout = os.path.join(params.outputdir, "nin_crit_dep_araigl.asc")
    nin_crit_dep_araigl.write_ascii_file(fileout,
                                         output_nodata_value=-9999,
                                         compress=params.lcompress)

    ## * N surface runoff at critical N inputs *
    # 'ara'
    nsro_crit_dep_ara = ascraster.duplicategrid(nin_crit_dep_ara)
    nsro_crit_dep_ara.multiply(fsro_ag)
    print_debug(nsro_crit_dep_ara, "nsro_crit_dep_ara =")
    # 'igl'
    nsro_crit_dep_igl = ascraster.duplicategrid(nin_crit_dep_igl)
    nsro_crit_dep_igl.multiply(fsro_ag)
    print_debug(nsro_crit_dep_igl, "nsro_crit_dep_igl =")

    ## * N uptake at critical N inputs *
    # 'ara'
    nup_crit_dep_ara = ascraster.duplicategrid(nin_crit_dep_ara)
    nup_crit_dep_ara.substract(nsro_crit_dep_ara)
    nup_crit_dep_ara.multiply(frnup_ara)
    fileout = os.path.join(params.outputdir, "nup_crit_dep_ara.asc")
    nup_crit_dep_ara.write_ascii_file(fileout,
                                      output_nodata_value=-9999,
                                      compress=params.lcompress)
    print_debug(nup_crit_dep_ara, "nup_crit_dep_ara =")
    # 'igl'
    nup_crit_dep_igl = ascraster.duplicategrid(nin_crit_dep_igl)
    nup_crit_dep_igl.substract(nsro_crit_dep_igl)
    nup_crit_dep_igl.multiply(frnup_igl)
    fileout = os.path.join(params.outputdir, "nup_crit_dep_igl.asc")
    nup_crit_dep_igl.write_ascii_file(fileout,
                                      output_nodata_value=-9999,
                                      compress=params.lcompress)
    print_debug(nup_crit_dep_igl, "nup_crit_dep_igl =")

    ## * NUE at critical N inputs *
    # 'ara'
    nue_crit_dep_ara = ascraster.duplicategrid(nup_crit_dep_ara)
    nue_crit_dep_ara.divide(nin_crit_dep_ara, default_nodata_value=-9999)
    fileout = os.path.join(params.outputdir, "nue_crit_dep_ara.asc")
    nue_crit_dep_ara.write_ascii_file(fileout,
                                      output_nodata_value=-9999,
                                      compress=params.lcompress)
    print_debug(nue_crit_dep_ara, "nue_crit_dep_ara =")
    # 'igl'
    nue_crit_dep_igl = ascraster.duplicategrid(nup_crit_dep_igl)
    nue_crit_dep_igl.divide(nin_crit_dep_igl, default_nodata_value=-9999)
    fileout = os.path.join(params.outputdir, "nue_crit_dep_igl.asc")
    nue_crit_dep_igl.write_ascii_file(fileout,
                                      output_nodata_value=-9999,
                                      compress=params.lcompress)
    print_debug(nue_crit_dep_igl, "nue_crit_dep_igl =")

    ## * Maximum uptake fraction  *
    # 'ara'
    fnup_max_dep_ara = ascraster.duplicategrid(nup_max_ara)
    fnup_max_dep_ara.divide(nup_crit_dep_ara)
    fileout = os.path.join(params.outputdir, "fnup_max_dep_ara.asc")
    fnup_max_dep_ara.write_ascii_file(fileout,
                                      output_nodata_value=-9999,
                                      compress=params.lcompress)
    print_debug(fnup_max_dep_ara, "fnup_max_dep_ara =")
    # 'igl'
    fnup_max_dep_igl = ascraster.duplicategrid(nup_max_igl)
    fnup_max_dep_igl.divide(nup_crit_dep_igl)
    fileout = os.path.join(params.outputdir, "fnup_max_dep_igl.asc")
    fnup_max_dep_igl.write_ascii_file(fileout,
                                      output_nodata_value=-9999,
                                      compress=params.lcompress)
    print_debug(fnup_max_dep_igl, "fnup_max_dep_igl =")

    ## * Correction factor for grid cells where Nup,crit > Nup,max *
    # 'ara'
    fnup_corr_dep_ara = ascraster.duplicategrid(nin_max_ara)
    fnup_corr_dep_ara.substract(nfix_ara)
    temp2_ara = ascraster.duplicategrid(nh3_tot_egl)
    temp2_ara.add(nox_em)
    temp2_ara.multiply(fara)
    fnup_corr_dep_ara.substract(temp2_ara)
    temp3_ara = ascraster.duplicategrid(nh3em_crit_dep_ara)
    temp3_ara.multiply(fara)
    temp3_ara.add(nman_crit_dep_ara)
    temp3_ara.add(nfer_crit_dep_ara)
    fnup_corr_dep_ara.divide(temp3_ara, default_nodata_value=-9999)
    fileout = os.path.join(params.outputdir, "fnup_corr_dep_ara.asc")
    fnup_corr_dep_ara.write_ascii_file(fileout,
                                       output_nodata_value=-9999,
                                       compress=params.lcompress)
    print_debug(fnup_corr_dep_ara, "fnup_corr_dep_ara =")
    # 'igl'
    fnup_corr_dep_igl = ascraster.duplicategrid(nin_max_igl)
    fnup_corr_dep_igl.substract(nfix_igl)
    temp2_igl = ascraster.duplicategrid(nh3_tot_egl)
    temp2_igl.add(nox_em)
    temp2_igl.multiply(figl)
    fnup_corr_dep_igl.substract(temp2_igl)
    temp3_igl = ascraster.duplicategrid(nh3em_crit_dep_igl)
    temp3_igl.multiply(figl)
    temp3_igl.add(nman_crit_dep_igl)
    temp3_igl.add(nfer_crit_dep_igl)
    fnup_corr_dep_igl.divide(temp3_igl, default_nodata_value=-9999)
    fileout = os.path.join(params.outputdir, "fnup_corr_dep_igl.asc")
    fnup_corr_dep_igl.write_ascii_file(fileout,
                                       output_nodata_value=-9999,
                                       compress=params.lcompress)
    print_debug(fnup_corr_dep_igl, "fnup_corr_dep_igl =")

    ########### FORWARD CALCULATIONS TO CHECK ###########
    if icell_debug < 0:
        pass
    else:
        fw = ndep_crit_dep_tot.get_data(icell_debug)
        bw = ndep_crit_tot.get_data(icell_debug)
        if fw is None:
            print(
                "FW/BW_TEST:_Forward_calculation_not_possible:_Nin,crit = None"
            )

        else:
            fw = round(fw, 4)
            bw = round(bw, 4)
            if fw == bw:
                print("FW/BW_TEST = SUCCESFUL")
            else:
                print("FW/BW_TEST = NOT_SUCCESFUL")
Пример #27
0
def calculate(params):

    #print("The critical N deposition rate is " + str(params.crit_dep) + " kg N ha-1 yr-1")
    
    # load needed variables for calculations
    biome            = ascraster.Asciigrid(ascii_file=os.path.join(params.inputdir,"gnlct.asc")            ,numtype=float,mask=params.mask)
    a_tot            = ascraster.Asciigrid(ascii_file=os.path.join(params.inputdir,"a_tot.asc")            ,numtype=float,mask=params.mask)
    nox_em           = ascraster.Asciigrid(ascii_file=os.path.join(params.outputdir,"nox_em.asc")          ,numtype=float,mask=params.mask)
    nh3_tot_egl      = ascraster.Asciigrid(ascii_file=os.path.join(params.outputdir,"nh3_tot_egl.asc")     ,numtype=float,mask=params.mask)
    nh3_ef_man_agri  = ascraster.Asciigrid(ascii_file=os.path.join(params.outputdir,"nh3_ef_man_agri.asc") ,numtype=float,mask=params.mask)
    nh3_ef_fert_agri = ascraster.Asciigrid(ascii_file=os.path.join(params.outputdir,"nh3_ef_fert_agri.asc"),numtype=float,mask=params.mask)
    frnfe_agri       = ascraster.Asciigrid(ascii_file=os.path.join(params.outputdir,"frnfe_agri.asc")      ,numtype=float,mask=params.mask)
    fagri            = ascraster.Asciigrid(ascii_file=os.path.join(params.outputdir,"fagri.asc")           ,numtype=float,mask=params.mask)
    n_fix_agri       = ascraster.Asciigrid(ascii_file=os.path.join(params.outputdir,"n_fix_agri.asc")      ,numtype=float,mask=params.mask)
    fsro_ag          = ascraster.Asciigrid(ascii_file=os.path.join(params.outputdir,"fsro_ag.asc")         ,numtype=float,mask=params.mask)
    frnup_agri       = ascraster.Asciigrid(ascii_file=os.path.join(params.outputdir,"frnup_agri.asc")      ,numtype=float,mask=params.mask)
    n_up_max         = ascraster.Asciigrid(ascii_file=os.path.join(params.outputdir,"n_up_max.asc")        ,numtype=float,mask=params.mask)
    n_in_max         = ascraster.Asciigrid(ascii_file=os.path.join(params.outputdir,"n_in_max.asc")        ,numtype=float,mask=params.mask)   
    
    # make grid with critical N deposition per biome
    ndep_crit_ha = ascraster.duplicategrid(n_fix_agri)
    # watch out: can ndep_crit_ha become both -9999 and -1 (NA value?)
    for i in range(ndep_crit_ha.length):
        ndep_crit_ha.set_data(i,-9999)
          
    for icell in range(biome.length):
        val = biome.get_data(icell)
        # Ice (7) or Hot desert (16)
        if (val == 7 or val == 16):
            cl = 5.0
        # Boreal forest (10), Cool coniferous forest (11) or scrubland (17)
        elif (val == 10 or val == 11 or val == 17):
            cl = 7.5
        # Tundra (8) or wooded tundra (9)or Warm mixed forest (14)
        elif (val == 8 or val == 9 or val == 14):
            cl = 10.0
        # Temperate mixed forest (12) Temperate deciduous forest (13) 
        elif (val == 12 or val == 13):
            cl = 12.5
        # Savanna (18)
        elif (val == 18):
            cl = 15.0
        # Grassland/steppe (15)
        elif (val == 15):
            cl = 17.5                    
        # Tropical woodland (19) or Tropical forest (20)
        elif (val == 19 or val == 20):
            cl = 20
        # Biome can also have value 0 or none (-1)
        else:
            continue        
        ndep_crit_ha.set_data(icell,cl) 
    
    print_debug(biome,"The biome ID is") 
    
    fileout = os.path.join(params.outputdir,"ndep_crit_ha.asc")
    ndep_crit_ha.write_ascii_file(fileout,output_nodata_value=-9999,compress=params.lcompress)
    print_debug(ndep_crit_ha,"The critical N deposition per hectare is") 
    
    # calculate total critical deposition: Ndep,tot(crit) = Ndep,crit,ha * A
    ndep_crit_tot = ascraster.duplicategrid(ndep_crit_ha)
    ndep_crit_tot.multiply(a_tot)
    #fileout = os.path.join(params.outputdir,"ndep_crit_tot.asc")
    #ndep_crit_tot.write_ascii_file(fileout,output_nodata_value=-9999,compress=params.lcompress)
    print_debug(ndep_crit_tot,"The total critical N deposition is")    
   
    # calculate critical N input from manure
    nh3_em_crit_agri = ascraster.duplicategrid(ndep_crit_tot)
    nh3_em_crit_agri.substract(nox_em)
    nh3_em_crit_agri.substract(nh3_tot_egl)

    one_grid = ascraster.duplicategrid(frnfe_agri)
    for i in range(one_grid.length):
        one_grid.set_data(i,1.0)
        
    one_min_frnfe = ascraster.duplicategrid(one_grid)
    one_min_frnfe.substract(frnfe_agri)
    frnfe_division = ascraster.duplicategrid(frnfe_agri)
    frnfe_division.divide(one_min_frnfe, default_nodata_value = -9999)
    
    denominator = ascraster.duplicategrid(frnfe_division)
    denominator.multiply(nh3_ef_fert_agri)
    denominator.add(nh3_ef_man_agri)
 
    nman_crit_dep = ascraster.duplicategrid(nh3_em_crit_agri)
    nman_crit_dep.divide(denominator, default_nodata_value = -9999)
    
    for icell in range (nman_crit_dep.length):
        nman = nman_crit_dep.get_data(icell)
        if (nman is None):
            continue
        if (nman < 0):
            man = 0
        else:
            continue
        nman_crit_dep.set_data(icell,man)
    
    fileout = os.path.join(params.outputdir,"nman_crit_dep.asc")
    nman_crit_dep.write_ascii_file(fileout,output_nodata_value=-9999,compress=params.lcompress)
    print_debug(nman_crit_dep,"The critical N input from manure for the N deposition criterion is")
    
    # calculate critical N input from fertilizer
    nfert_crit_dep = ascraster.duplicategrid(nman_crit_dep)
    nfert_crit_dep.multiply(frnfe_division)
    fileout = os.path.join(params.outputdir,"nfert_crit_dep.asc")
    nfert_crit_dep.write_ascii_file(fileout,output_nodata_value=-9999,compress=params.lcompress)
    print_debug(nfert_crit_dep,"The critical N input from fertilizer for the N deposition criterion is")
    
    # calculate related N deposition
    nh3em_man_crit_dep = ascraster.duplicategrid(nman_crit_dep)
    nh3em_man_crit_dep.multiply(nh3_ef_man_agri)

    fileout = os.path.join(params.outputdir,"nh3em_man_crit_dep.asc")
    nh3em_man_crit_dep.write_ascii_file(fileout,output_nodata_value=-9999,compress=params.lcompress)
    
    nh3em_fert_crit_dep = ascraster.duplicategrid(nfert_crit_dep)
    nh3em_fert_crit_dep.multiply(nh3_ef_fert_agri)
    
    fileout = os.path.join(params.outputdir,"nh3em_fert_crit_dep.asc")
    nh3em_fert_crit_dep.write_ascii_file(fileout,output_nodata_value=-9999,compress=params.lcompress)
    
    nh3em_crit_dep = ascraster.duplicategrid(nh3em_fert_crit_dep)
    nh3em_crit_dep.add(nh3em_man_crit_dep)
    #
    fileout = os.path.join(params.outputdir,"nh3em_crit_dep.asc")
    nh3em_crit_dep.write_ascii_file(fileout,output_nodata_value=-9999,compress=params.lcompress)
    #
    #nh3em_crit_dep.add(nh3_tot_egl)
    ndep_crit_dep_tot = ascraster.duplicategrid(nh3em_crit_dep)
    ndep_crit_dep_tot.add(nox_em)
    ndep_crit_dep_tot.add(nh3_tot_egl)
    print_debug(ndep_crit_dep_tot,"The total critical N deposition for the N deposition criterion is")
    ndep_crit_dep_agri = ascraster.duplicategrid(ndep_crit_dep_tot)
    ndep_crit_dep_agri.multiply(fagri)
    print_debug(ndep_crit_dep_agri,"The critical N deposition on agricultural land for the N deposition criterion is")
    
    # calculate total critical N inputs wrt N deposition
    nin_crit_dep_agri = ascraster.duplicategrid(nman_crit_dep)
    nin_crit_dep_agri.add(nfert_crit_dep)
    nin_crit_dep_agri.add(ndep_crit_dep_agri)
    nin_crit_dep_agri.add(n_fix_agri)
    fileout = os.path.join(params.outputdir,"nin_crit_dep_agri.asc")
    nin_crit_dep_agri.write_ascii_file(fileout,output_nodata_value=-9999,compress=params.lcompress)
    print_debug(nin_crit_dep_agri,"The total critical input to agriclture for the N deposition criterion is")
    
    # calculate N surface runoff at critical N inputs deposition
    nsro_crit_dep_agri = ascraster.duplicategrid(nin_crit_dep_agri)
    nsro_crit_dep_agri.multiply(fsro_ag)
    print_debug(nsro_crit_dep_agri,"The critical N surface runoff for the N deposition criterion is")
    
    # calculate N uptake at critical N inputs deposition
    nup_crit_dep_agri = ascraster.duplicategrid(nin_crit_dep_agri)
    nup_crit_dep_agri.substract(nsro_crit_dep_agri)
    nup_crit_dep_agri.multiply(frnup_agri)
    fileout = os.path.join(params.outputdir,"nup_crit_dep_agri.asc")
    nup_crit_dep_agri.write_ascii_file(fileout,output_nodata_value=-9999,compress=params.lcompress)
    print_debug(nup_crit_dep_agri,"The N uptake for the N deposition criterion is")
        
    # calculate implied NUE
    nue_crit_dep_agri = ascraster.duplicategrid(nup_crit_dep_agri)
    nue_crit_dep_agri.divide(nin_crit_dep_agri, default_nodata_value = -9999)
    #fileout = os.path.join(params.outputdir,"nue_crit_dep_agri.asc")
    #nue_crit_dep_agri.write_ascii_file(fileout,output_nodata_value=-9999,compress=params.lcompress)
    print_debug(nue_crit_dep_agri,"The NUE for the N deposition criterion is")
    
    # calculate maximum uptake fraction
    fnup_max_dep = ascraster.duplicategrid(n_up_max)
    fnup_max_dep.divide(nup_crit_dep_agri)
    fileout = os.path.join(params.outputdir,"fnup_max_dep.asc")
    fnup_max_dep.write_ascii_file(fileout,output_nodata_value=-9999,compress=params.lcompress)
    print_debug(fnup_max_dep,"The fraction maximum uptake / critical uptake for deposition is")
    
    # calculate correction factor for those cases where critical N uptake exceeds max. N uptake 
    fnup_corr_dep = ascraster.duplicategrid(n_in_max)
    fnup_corr_dep.substract(n_fix_agri)
    
    temp2 = ascraster.duplicategrid(nh3_tot_egl)
    temp2.add(nox_em)
    temp2.multiply(fagri)
    fnup_corr_dep.substract(temp2)
    
    temp3 = ascraster.duplicategrid(nh3em_crit_dep)
    temp3.multiply(fagri)
    temp3.add(nman_crit_dep)
    temp3.add(nfert_crit_dep)
    fnup_corr_dep.divide(temp3, default_nodata_value = -9999)
    
    fileout = os.path.join(params.outputdir,"fnup_corr_dep.asc")
    fnup_corr_dep.write_ascii_file(fileout,output_nodata_value=-9999,compress=params.lcompress)
    print_debug(fnup_corr_dep,"The correction factor for cases where Nup,crit>Nup,max is")
    
    
    ########### FORWARD CALCULATIONS TO CHECK ###########
    if icell_debug<0:
        pass
    else:
        fw = ndep_crit_dep_tot.get_data(icell_debug)
        bw = ndep_crit_tot.get_data(icell_debug)
        if fw is None:
            print("FW / BW TEST: Forward calculation not possible (Nin,crit=None)")
        
        else:
            fw = round(fw,4) 
            bw = round(bw,4)
            if fw == bw:
                print("FW / BW TEST: SUCCESFUL")
            else:
                print("FW / BW TEST: NOT SUCCESFUL")
    ############################################################################################
Пример #28
0
def calculate(params):

    print("nconc_le_crit_gw =", params.crit_gw)

    # load needed variables for calculations
    q = ascraster.Asciigrid(ascii_file=os.path.join(params.inputdir, "q.asc"),
                            numtype=float,
                            mask=params.mask)
    nle_ag = ascraster.Asciigrid(ascii_file=os.path.join(
        params.inputdir, "nle_ag.asc"),
                                 numtype=float,
                                 mask=params.mask)
    nfix_ara = ascraster.Asciigrid(
        ascii_file=params.filename_nfixation_cropland,
        numtype=float,
        mask=params.mask)
    nfix_igl = ascraster.Asciigrid(ascii_file=params.filename_nfixation_intgl,
                                   numtype=float,
                                   mask=params.mask)
    fsro_ag = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "fsro_ag.asc"),
                                  numtype=float,
                                  mask=params.mask)
    fara = ascraster.Asciigrid(ascii_file=os.path.join(params.outputdir,
                                                       "fara.asc"),
                               numtype=float,
                               mask=params.mask)
    figl = ascraster.Asciigrid(ascii_file=os.path.join(params.outputdir,
                                                       "figl.asc"),
                               numtype=float,
                               mask=params.mask)
    fegl = ascraster.Asciigrid(ascii_file=os.path.join(params.outputdir,
                                                       "fegl.asc"),
                               numtype=float,
                               mask=params.mask)
    fnat = ascraster.Asciigrid(ascii_file=os.path.join(params.outputdir,
                                                       "fnat.asc"),
                               numtype=float,
                               mask=params.mask)
    fle_ag = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "fle_ag.asc"),
                                 numtype=float,
                                 mask=params.mask)
    frnup_ara = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "frnup_ara.asc"),
                                    numtype=float,
                                    mask=params.mask)
    frnup_igl = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "frnup_igl.asc"),
                                    numtype=float,
                                    mask=params.mask)
    nup_max_ara = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nup_max_ara.asc"),
                                      numtype=float,
                                      mask=params.mask)
    nup_max_igl = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nup_max_igl.asc"),
                                      numtype=float,
                                      mask=params.mask)
    nox_em = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nox_em.asc"),
                                 numtype=float,
                                 mask=params.mask)
    nh3_tot_egl = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nh3_tot_egl.asc"),
                                      numtype=float,
                                      mask=params.mask)
    nh3_tot_igl = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nh3_tot_igl.asc"),
                                      numtype=float,
                                      mask=params.mask)
    nh3_tot_ara = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nh3_tot_ara.asc"),
                                      numtype=float,
                                      mask=params.mask)
    nh3_ef_man_ara = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nh3_ef_man_ara.asc"),
                                         numtype=float,
                                         mask=params.mask)
    nh3_ef_man_igl = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nh3_ef_man_igl.asc"),
                                         numtype=float,
                                         mask=params.mask)
    nh3_ef_fer_ara = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nh3_ef_fer_ara.asc"),
                                         numtype=float,
                                         mask=params.mask)
    nh3_ef_fer_igl = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nh3_ef_fer_igl.asc"),
                                         numtype=float,
                                         mask=params.mask)
    frnfe_ara = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "frnfe_ara.asc"),
                                    numtype=float,
                                    mask=params.mask)
    frnfe_igl = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "frnfe_igl.asc"),
                                    numtype=float,
                                    mask=params.mask)
    nin_max_ara = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nin_max_ara.asc"),
                                      numtype=float,
                                      mask=params.mask)
    nin_max_igl = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nin_max_igl.asc"),
                                      numtype=float,
                                      mask=params.mask)
    nle_ara = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nle_ara.asc"),
                                  numtype=float,
                                  mask=params.mask)
    nle_igl = ascraster.Asciigrid(ascii_file=os.path.join(
        params.outputdir, "nle_igl.asc"),
                                  numtype=float,
                                  mask=params.mask)

    ## * Critical N leaching *
    one_grid = ascraster.duplicategrid(q)
    for i in range(one_grid.length):
        one_grid.set_data(i, 1.0)
    one_min_fsro = ascraster.duplicategrid(one_grid)
    one_min_fsro.substract(fsro_ag)
    # 'ara'
    nle_crit_gw_ara = ascraster.duplicategrid(one_min_fsro)
    nle_crit_gw_ara.multiply(q)
    nle_crit_gw_ara.multiply(fara)
    nle_crit_gw_ara.multiply(params.crit_gw)
    fileout = os.path.join(params.outputdir, "nle_crit_gw_ara.asc")
    nle_crit_gw_ara.write_ascii_file(fileout,
                                     output_nodata_value=-9999,
                                     compress=params.lcompress)
    print_debug(nle_crit_gw_ara, "nle_crit_gw_ara =")

    # 'igl'
    nle_crit_gw_igl = ascraster.duplicategrid(one_min_fsro)
    nle_crit_gw_igl.multiply(q)
    nle_crit_gw_igl.multiply(figl)
    nle_crit_gw_igl.multiply(params.crit_gw)
    fileout = os.path.join(params.outputdir, "nle_crit_gw_igl.asc")
    nle_crit_gw_igl.write_ascii_file(fileout,
                                     output_nodata_value=-9999,
                                     compress=params.lcompress)
    print_debug(nle_crit_gw_igl, "nle_crit_gw_igl =")

    ## * Parameter v *
    # 'ara'
    v_ara_part1 = ascraster.duplicategrid(one_grid)
    v_ara_part1.substract(frnup_ara)
    v_ara_part2 = ascraster.duplicategrid(fsro_ag)
    v_ara_part2.multiply(frnup_ara)
    v_ara = ascraster.duplicategrid(v_ara_part1)
    v_ara.add(v_ara_part2)
    v_ara.substract(fsro_ag)
    v_ara.multiply(fle_ag)
    # 'igl'
    v_igl_part1 = ascraster.duplicategrid(one_grid)
    v_igl_part1.substract(frnup_igl)
    v_igl_part2 = ascraster.duplicategrid(fsro_ag)
    v_igl_part2.multiply(frnup_igl)
    v_igl = ascraster.duplicategrid(v_igl_part1)
    v_igl.add(v_igl_part2)
    v_igl.substract(fsro_ag)
    v_igl.multiply(fle_ag)

    ## * Critical N input from manure *
    ## OPTION 1 - for grid cells with EITHER 'ara' OR 'igl'
    # 'ara'
    num1_ara = ascraster.duplicategrid(nle_crit_gw_ara)
    num1_ara.divide(v_ara, default_nodata_value=-9999)
    num1_ara.substract(nfix_ara)
    num2_V1_ara = ascraster.duplicategrid(nox_em)
    num2_V1_ara.add(nh3_tot_egl)
    num2_V1_ara.multiply(fara)
    num_V1_ara = ascraster.duplicategrid(num1_ara)
    num_V1_ara.substract(num2_V1_ara)
    den1_ara = ascraster.duplicategrid(fara)
    den1_ara.multiply(nh3_ef_man_ara)
    den1_ara.add(one_grid)
    den2_ara = ascraster.duplicategrid(fara)
    den2_ara.multiply(nh3_ef_fer_ara)
    den2_ara.add(one_grid)
    one_min_frnfe_ara = ascraster.duplicategrid(one_grid)
    one_min_frnfe_ara.substract(frnfe_ara)
    frnfe_division_ara = ascraster.duplicategrid(frnfe_ara)
    frnfe_division_ara.divide(one_min_frnfe_ara, default_nodata_value=-9999)
    den2_ara.multiply(frnfe_division_ara)
    den_ara = ascraster.duplicategrid(den1_ara)
    den_ara.add(den2_ara)

    nman_crit_gw_V1_ara = ascraster.duplicategrid(num_V1_ara)
    nman_crit_gw_V1_ara.divide(den_ara, default_nodata_value=-9999)

    fileout = os.path.join(params.outputdir, "nman_crit_gw_V1_ara.asc")
    nman_crit_gw_V1_ara.write_ascii_file(fileout,
                                         output_nodata_value=-9999,
                                         compress=params.lcompress)

    # 'igl'
    num1_igl = ascraster.duplicategrid(nle_crit_gw_igl)
    num1_igl.divide(v_igl, default_nodata_value=-9999)
    num1_igl.substract(nfix_igl)
    num2_V1_igl = ascraster.duplicategrid(nox_em)
    num2_V1_igl.add(nh3_tot_egl)
    num2_V1_igl.multiply(figl)
    num_V1_igl = ascraster.duplicategrid(num1_igl)
    num_V1_igl.substract(num2_V1_igl)
    den1_igl = ascraster.duplicategrid(figl)
    den1_igl.multiply(nh3_ef_man_igl)
    den1_igl.add(one_grid)
    den2_igl = ascraster.duplicategrid(figl)
    den2_igl.multiply(nh3_ef_fer_igl)
    den2_igl.add(one_grid)
    one_min_frnfe_igl = ascraster.duplicategrid(one_grid)
    one_min_frnfe_igl.substract(frnfe_igl)
    frnfe_division_igl = ascraster.duplicategrid(frnfe_igl)
    frnfe_division_igl.divide(one_min_frnfe_igl, default_nodata_value=-9999)
    den2_igl.multiply(frnfe_division_igl)
    den_igl = ascraster.duplicategrid(den1_igl)
    den_igl.add(den2_igl)

    nman_crit_gw_V1_igl = ascraster.duplicategrid(num_V1_igl)
    nman_crit_gw_V1_igl.divide(den_igl, default_nodata_value=-9999)

    fileout = os.path.join(params.outputdir, "nman_crit_gw_V1_igl.asc")
    nman_crit_gw_V1_igl.write_ascii_file(fileout,
                                         output_nodata_value=-9999,
                                         compress=params.lcompress)

    ## OPTION2 - for grid cells with BOTH 'ara' AND 'igl'
    # 'ara'
    num2_V2_ara = ascraster.duplicategrid(nle_crit_gw_igl)
    num2_V2_ara.divide(nle_igl, default_nodata_value=-9999)
    num2_V2_ara.multiply(nh3_tot_igl)
    num2_V2_ara.add(nox_em)
    num2_V2_ara.add(nh3_tot_egl)
    num2_V2_ara.multiply(fara)
    num_V2_ara = ascraster.duplicategrid(num1_ara)
    num_V2_ara.substract(num2_V2_ara)

    nman_crit_gw_V2_ara = ascraster.duplicategrid(num_V2_ara)
    nman_crit_gw_V2_ara.divide(den_ara, default_nodata_value=-9999)

    fileout = os.path.join(params.outputdir, "nman_crit_gw_V2_ara.asc")
    nman_crit_gw_V2_ara.write_ascii_file(fileout,
                                         output_nodata_value=-9999,
                                         compress=params.lcompress)

    # 'igl'
    num2_V2_igl = ascraster.duplicategrid(nle_crit_gw_ara)
    num2_V2_igl.divide(nle_ara, default_nodata_value=-9999)
    num2_V2_igl.multiply(nh3_tot_ara)
    num2_V2_igl.add(nox_em)
    num2_V2_igl.add(nh3_tot_egl)
    num2_V2_igl.multiply(figl)
    num_V2_igl = ascraster.duplicategrid(num1_igl)
    num_V2_igl.substract(num2_V2_igl)

    nman_crit_gw_V2_igl = ascraster.duplicategrid(num_V2_igl)
    nman_crit_gw_V2_igl.divide(den_igl, default_nodata_value=-9999)

    fileout = os.path.join(params.outputdir, "nman_crit_gw_V2_igl.asc")
    nman_crit_gw_V2_igl.write_ascii_file(fileout,
                                         output_nodata_value=-9999,
                                         compress=params.lcompress)

    # MAKE GRID WITH CRITICAL INPUTS FROM MANURE DEPENDING ON OPTION
    # ara
    nman_crit_gw_ara = ascraster.duplicategrid(nman_crit_gw_V1_ara)
    for icell in range(fara.length):
        f_ara = fara.get_data(icell)
        f_igl = figl.get_data(icell)
        nman_ara2 = nman_crit_gw_V2_ara.get_data(icell)
        nle = nle_ag.get_data(icell)
        if (f_ara is None or f_igl is None):
            continue
        elif (f_ara > 0 and f_igl > 0 and nle > 0):
            nman_ara = nman_ara2
        elif (f_ara == 0 and f_igl > 0):
            nman_ara = 0
        else:
            continue

        nman_crit_gw_ara.set_data(icell, nman_ara)

    for icell in range(nman_crit_gw_ara.length):
        nman_ara3 = nman_crit_gw_ara.get_data(icell)
        if (nman_ara3 is None):
            continue
        if (nman_ara3 < 0):
            nman_ara = 0
        else:
            continue
        nman_crit_gw_ara.set_data(icell, nman_ara)

    fileout = os.path.join(params.outputdir, "nman_crit_gw_ara.asc")
    nman_crit_gw_ara.write_ascii_file(fileout,
                                      output_nodata_value=-9999,
                                      compress=params.lcompress)
    print_debug(nman_crit_gw_ara, "nman_crit_gw_ara =")

    # igl
    nman_crit_gw_igl = ascraster.duplicategrid(nman_crit_gw_V1_igl)
    for icell in range(figl.length):
        f_igl = figl.get_data(icell)
        f_ara = fara.get_data(icell)
        nman_igl2 = nman_crit_gw_V2_igl.get_data(icell)
        nle = nle_ag.get_data(icell)
        if (f_ara is None or f_igl is None):
            continue
        elif (f_ara > 0 and f_igl > 0 and nle > 0):
            nman_igl = nman_igl2
        elif (f_ara > 0 and f_igl == 0):
            nman_igl = 0
        else:
            continue

        nman_crit_gw_igl.set_data(icell, nman_igl)

    for icell in range(nman_crit_gw_igl.length):
        nman_igl3 = nman_crit_gw_igl.get_data(icell)
        if (nman_igl3 is None):
            continue
        if (nman_igl3 < 0):
            nman_igl = 0
        else:
            continue
        nman_crit_gw_igl.set_data(icell, nman_igl)

    fileout = os.path.join(params.outputdir, "nman_crit_gw_igl.asc")
    nman_crit_gw_igl.write_ascii_file(fileout,
                                      output_nodata_value=-9999,
                                      compress=params.lcompress)
    print_debug(nman_crit_gw_igl, "nman_crit_gw_igl =")

    ## * Critical N input from fertilizer *
    # 'ara'
    nfer_crit_gw_ara = ascraster.duplicategrid(nman_crit_gw_ara)
    nfer_crit_gw_ara.multiply(frnfe_division_ara)

    # set to zero for all cells with no ara but igl (to avoid error in calculating nh3 emissions / deposition for these cells)
    for icell in range(nfer_crit_gw_ara.length):
        f_ara = fara.get_data(icell)
        f_igl = figl.get_data(icell)
        if (f_ara == 0 and f_igl > 0):
            nfer_ara = 0
        else:
            continue

        nfer_crit_gw_ara.set_data(icell, nfer_ara)
    fileout = os.path.join(params.outputdir, "nfer_crit_gw_ara.asc")
    nfer_crit_gw_ara.write_ascii_file(fileout,
                                      output_nodata_value=-9999,
                                      compress=params.lcompress)
    print_debug(nfer_crit_gw_ara, "nfer_crit_gw_ara =")

    # 'igl'
    nfer_crit_gw_igl = ascraster.duplicategrid(nman_crit_gw_igl)
    nfer_crit_gw_igl.multiply(frnfe_division_igl)

    # set to zero for all cells with no igl but ara (to avoid error in calculating nh3 emissions / deposition for these cells)
    for icell in range(nfer_crit_gw_igl.length):
        f_ara = fara.get_data(icell)
        f_igl = figl.get_data(icell)
        if (f_igl == 0 and f_ara > 0):
            nfer_igl = 0
        else:
            continue

        nfer_crit_gw_igl.set_data(icell, nfer_igl)
    fileout = os.path.join(params.outputdir, "nfer_crit_gw_igl.asc")
    nfer_crit_gw_igl.write_ascii_file(fileout,
                                      output_nodata_value=-9999,
                                      compress=params.lcompress)
    print_debug(nfer_crit_gw_igl, "nfer_crit_gw_igl =")

    ## * Critical N inputs from fertilizer plus manure * ##
    # 'ara'
    nman_fer_crit_gw_ara = ascraster.duplicategrid(nman_crit_gw_ara)
    nman_fer_crit_gw_ara.add(nfer_crit_gw_ara)
    fileout = os.path.join(params.outputdir, "nman_fer_crit_gw_ara.asc")
    nman_fer_crit_gw_ara.write_ascii_file(fileout,
                                          output_nodata_value=-9999,
                                          compress=params.lcompress)
    # 'igl'
    nman_fer_crit_gw_igl = ascraster.duplicategrid(nman_crit_gw_igl)
    nman_fer_crit_gw_igl.add(nfer_crit_gw_igl)
    fileout = os.path.join(params.outputdir, "nman_fer_crit_gw_igl.asc")
    nman_fer_crit_gw_igl.write_ascii_file(fileout,
                                          output_nodata_value=-9999,
                                          compress=params.lcompress)

    ## * NH3 emissions at critical N input *
    # 'ara'
    nh3em_man_crit_gw_ara = ascraster.duplicategrid(nman_crit_gw_ara)
    nh3em_man_crit_gw_ara.multiply(nh3_ef_man_ara)
    nh3em_fer_crit_gw_ara = ascraster.duplicategrid(nfer_crit_gw_ara)
    nh3em_fer_crit_gw_ara.multiply(nh3_ef_fer_ara)
    nh3em_crit_gw_ara = ascraster.duplicategrid(nh3em_man_crit_gw_ara)
    nh3em_crit_gw_ara.add(nh3em_fer_crit_gw_ara)
    print_debug(nh3em_crit_gw_ara, "nh3em_crit_gw_ara =")
    # 'igl'
    nh3em_man_crit_gw_igl = ascraster.duplicategrid(nman_crit_gw_igl)
    nh3em_man_crit_gw_igl.multiply(nh3_ef_man_igl)
    nh3em_fer_crit_gw_igl = ascraster.duplicategrid(nfer_crit_gw_igl)
    nh3em_fer_crit_gw_igl.multiply(nh3_ef_fer_igl)
    nh3em_crit_gw_igl = ascraster.duplicategrid(nh3em_man_crit_gw_igl)
    nh3em_crit_gw_igl.add(nh3em_fer_crit_gw_igl)
    print_debug(nh3em_crit_gw_igl, "nh3em_crit_gw_igl =")

    ## * N deposition at critical N input *
    ndep_crit_gw_tot = ascraster.duplicategrid(nh3em_crit_gw_ara)
    ndep_crit_gw_tot.add(nh3em_crit_gw_igl)
    ndep_crit_gw_tot.add(nox_em)
    ndep_crit_gw_tot.add(nh3_tot_egl)

    fileout = os.path.join(params.outputdir, "ndep_crit_gw_tot.asc")
    ndep_crit_gw_tot.write_ascii_file(fileout,
                                      output_nodata_value=-9999,
                                      compress=params.lcompress)
    print_debug(ndep_crit_gw_tot, "ndep_crit_gw_tot =")

    # OPTION 1 - for grid cells with EITHER ara OR igl
    # 'ara'
    ndep_crit_gw_V1_ara = ascraster.duplicategrid(ndep_crit_gw_tot)
    ndep_crit_gw_V1_ara.multiply(fara)
    # 'igl'
    ndep_crit_gw_V1_igl = ascraster.duplicategrid(ndep_crit_gw_tot)
    ndep_crit_gw_V1_igl.multiply(figl)

    # OPTION 2 - for grid cells with BOTH ara + igl
    # 'ara'
    ndep_crit_gw_V2_ara = ascraster.duplicategrid(nle_crit_gw_igl)
    ndep_crit_gw_V2_ara.divide(nle_igl, default_nodata_value=-9999)
    ndep_crit_gw_V2_ara.multiply(nh3_tot_igl)
    ndep_crit_gw_V2_ara.add(nh3em_crit_gw_ara)
    ndep_crit_gw_V2_ara.add(nox_em)
    ndep_crit_gw_V2_ara.add(nh3_tot_egl)
    ndep_crit_gw_V2_ara.multiply(fara)
    # 'igl'
    ndep_crit_gw_V2_igl = ascraster.duplicategrid(nle_crit_gw_ara)
    ndep_crit_gw_V2_igl.divide(nle_ara, default_nodata_value=-9999)
    ndep_crit_gw_V2_igl.multiply(nh3_tot_ara)
    ndep_crit_gw_V2_igl.add(nh3em_crit_gw_igl)
    ndep_crit_gw_V2_igl.add(nox_em)
    ndep_crit_gw_V2_igl.add(nh3_tot_egl)
    ndep_crit_gw_V2_igl.multiply(figl)

    # MAKE ONE GRID FOR N DEPOSITION
    # 'ara'
    ndep_crit_gw_ara = ascraster.duplicategrid(ndep_crit_gw_V1_ara)
    for icell in range(ndep_crit_gw_ara.length):
        f_ara = fara.get_data(icell)
        f_igl = figl.get_data(icell)
        ndep_ara2 = ndep_crit_gw_V2_ara.get_data(icell)
        nle = nle_ag.get_data(icell)
        if (f_ara is None or f_igl is None):
            continue
        elif (f_ara > 0 and f_igl > 0 and nle > 0):
            ndep_ara = ndep_ara2
        else:
            continue

        ndep_crit_gw_ara.set_data(icell, ndep_ara)
    fileout = os.path.join(params.outputdir, "ndep_crit_gw_ara.asc")
    ndep_crit_gw_ara.write_ascii_file(fileout,
                                      output_nodata_value=-9999,
                                      compress=params.lcompress)
    print_debug(ndep_crit_gw_ara, "ndep_crit_gw_ara =")

    # 'igl'
    ndep_crit_gw_igl = ascraster.duplicategrid(ndep_crit_gw_V1_igl)
    for icell in range(ndep_crit_gw_igl.length):
        f_ara = fara.get_data(icell)
        f_igl = figl.get_data(icell)
        ndep_igl2 = ndep_crit_gw_V2_igl.get_data(icell)
        nle = nle_ag.get_data(icell)
        if (f_ara is None or f_igl is None):
            continue
        elif (f_ara > 0 and f_igl > 0 and nle > 0):
            ndep_igl = ndep_igl2
        else:
            continue

        ndep_crit_gw_igl.set_data(icell, ndep_igl)
    fileout = os.path.join(params.outputdir, "ndep_crit_gw_igl.asc")
    ndep_crit_gw_igl.write_ascii_file(fileout,
                                      output_nodata_value=-9999,
                                      compress=params.lcompress)
    print_debug(ndep_crit_gw_igl, "ndep_crit_gw_igl =")

    ##TEST: Total N deposition as sum of depositions#
    ndep_crit_gw_egl = ascraster.duplicategrid(ndep_crit_gw_tot)
    ndep_crit_gw_egl.multiply(fegl)
    ndep_crit_gw_nat = ascraster.duplicategrid(ndep_crit_gw_tot)
    ndep_crit_gw_nat.multiply(fnat)

    ndep_crit_gw_tot_TEST = ascraster.duplicategrid(ndep_crit_gw_ara)
    ndep_crit_gw_tot_TEST.add(ndep_crit_gw_igl)
    ndep_crit_gw_tot_TEST.add(ndep_crit_gw_egl)
    ndep_crit_gw_tot_TEST.add(ndep_crit_gw_nat)

    fileout = os.path.join(params.outputdir, "ndep_crit_gw_tot_TEST.asc")
    ndep_crit_gw_tot_TEST.write_ascii_file(fileout,
                                           output_nodata_value=-9999,
                                           compress=params.lcompress)
    print_debug(ndep_crit_gw_tot_TEST, "ndep_crit_gw_tot_TEST =")

    ## * Total critical N inputs *
    # 'ara'
    nin_crit_gw_ara = ascraster.duplicategrid(nman_crit_gw_ara)
    nin_crit_gw_ara.add(nfer_crit_gw_ara)
    nin_crit_gw_ara.add(ndep_crit_gw_ara)
    nin_crit_gw_ara.add(nfix_ara)
    fileout = os.path.join(params.outputdir, "nin_crit_gw_ara.asc")
    nin_crit_gw_ara.write_ascii_file(fileout,
                                     output_nodata_value=-9999,
                                     compress=params.lcompress)
    print_debug(nin_crit_gw_ara, "nin_crit_gw_ara =")
    # 'igl'
    nin_crit_gw_igl = ascraster.duplicategrid(nman_crit_gw_igl)
    nin_crit_gw_igl.add(nfer_crit_gw_igl)
    nin_crit_gw_igl.add(ndep_crit_gw_igl)
    nin_crit_gw_igl.add(nfix_igl)
    fileout = os.path.join(params.outputdir, "nin_crit_gw_igl.asc")
    nin_crit_gw_igl.write_ascii_file(fileout,
                                     output_nodata_value=-9999,
                                     compress=params.lcompress)
    print_debug(nin_crit_gw_igl, "nin_crit_gw_igl =")
    # 'ara+igl'
    nin_crit_gw_araigl = ascraster.duplicategrid(nin_crit_gw_ara)
    nin_crit_gw_araigl.add(nin_crit_gw_igl)
    fileout = os.path.join(params.outputdir, "nin_crit_gw_araigl.asc")
    nin_crit_gw_araigl.write_ascii_file(fileout,
                                        output_nodata_value=-9999,
                                        compress=params.lcompress)

    ## * N surface runoff at critical N inputs *
    # 'ara'
    nsro_crit_gw_ara = ascraster.duplicategrid(nin_crit_gw_ara)
    nsro_crit_gw_ara.multiply(fsro_ag)
    print_debug(nsro_crit_gw_ara, "nsro_crit_gw_ara =")
    # 'igl'
    nsro_crit_gw_igl = ascraster.duplicategrid(nin_crit_gw_igl)
    nsro_crit_gw_igl.multiply(fsro_ag)
    print_debug(nsro_crit_gw_igl, "nsro_crit_gw_igl =")

    ## * N uptake at critical N inputs *
    # 'ara'
    nup_crit_gw_ara = ascraster.duplicategrid(nin_crit_gw_ara)
    nup_crit_gw_ara.substract(nsro_crit_gw_ara)
    nup_crit_gw_ara.multiply(frnup_ara)
    fileout = os.path.join(params.outputdir, "nup_crit_gw_ara.asc")
    nup_crit_gw_ara.write_ascii_file(fileout,
                                     output_nodata_value=-9999,
                                     compress=params.lcompress)
    print_debug(nup_crit_gw_ara, "nup_crit_gw_ara =")
    # 'igl'
    nup_crit_gw_igl = ascraster.duplicategrid(nin_crit_gw_igl)
    nup_crit_gw_igl.substract(nsro_crit_gw_igl)
    nup_crit_gw_igl.multiply(frnup_igl)
    fileout = os.path.join(params.outputdir, "nup_crit_gw_igl.asc")
    nup_crit_gw_igl.write_ascii_file(fileout,
                                     output_nodata_value=-9999,
                                     compress=params.lcompress)
    print_debug(nup_crit_gw_igl, "nup_crit_gw_igl =")

    ## * NUE at critical N inputs *
    # 'ara'
    nue_crit_gw_ara = ascraster.duplicategrid(nup_crit_gw_ara)
    nue_crit_gw_ara.divide(nin_crit_gw_ara, default_nodata_value=-9999)
    fileout = os.path.join(params.outputdir, "nue_crit_gw_ara.asc")
    nue_crit_gw_ara.write_ascii_file(fileout,
                                     output_nodata_value=-9999,
                                     compress=params.lcompress)
    print_debug(nue_crit_gw_ara, "nue_crit_gw_ara =")
    # 'igl'
    nue_crit_gw_igl = ascraster.duplicategrid(nup_crit_gw_igl)
    nue_crit_gw_igl.divide(nin_crit_gw_igl, default_nodata_value=-9999)
    fileout = os.path.join(params.outputdir, "nue_crit_gw_igl.asc")
    nue_crit_gw_igl.write_ascii_file(fileout,
                                     output_nodata_value=-9999,
                                     compress=params.lcompress)
    print_debug(nue_crit_gw_igl, "nue_crit_gw_igl =")

    ## * Maximum uptake fraction  *
    # 'ara'
    fnup_max_gw_ara = ascraster.duplicategrid(nup_max_ara)
    fnup_max_gw_ara.divide(nup_crit_gw_ara)
    fileout = os.path.join(params.outputdir, "fnup_max_gw_ara.asc")
    fnup_max_gw_ara.write_ascii_file(fileout,
                                     output_nodata_value=-9999,
                                     compress=params.lcompress)
    print_debug(fnup_max_gw_ara, "fnup_max_gw_ara =")
    # 'igl'
    fnup_max_gw_igl = ascraster.duplicategrid(nup_max_igl)
    fnup_max_gw_igl.divide(nup_crit_gw_igl)
    fileout = os.path.join(params.outputdir, "fnup_max_gw_igl.asc")
    fnup_max_gw_igl.write_ascii_file(fileout,
                                     output_nodata_value=-9999,
                                     compress=params.lcompress)
    print_debug(fnup_max_gw_igl, "fnup_max_gw_igl =")

    ## * Correction factor for grid cells where Nup,crit > Nup,max *
    # 'ara'
    fnup_corr_gw_ara = ascraster.duplicategrid(nin_max_ara)
    fnup_corr_gw_ara.substract(nfix_ara)
    temp2_ara = ascraster.duplicategrid(nh3_tot_egl)
    temp2_ara.add(nox_em)
    temp2_ara.multiply(fara)
    fnup_corr_gw_ara.substract(temp2_ara)
    temp3_ara = ascraster.duplicategrid(nh3em_crit_gw_ara)
    temp3_ara.multiply(fara)
    temp3_ara.add(nman_crit_gw_ara)
    temp3_ara.add(nfer_crit_gw_ara)
    fnup_corr_gw_ara.divide(temp3_ara, default_nodata_value=-9999)
    fileout = os.path.join(params.outputdir, "fnup_corr_gw_ara.asc")
    fnup_corr_gw_ara.write_ascii_file(fileout,
                                      output_nodata_value=-9999,
                                      compress=params.lcompress)
    print_debug(fnup_corr_gw_ara, "fnup_corr_gw_ara =")
    # 'igl'
    fnup_corr_gw_igl = ascraster.duplicategrid(nin_max_igl)
    fnup_corr_gw_igl.substract(nfix_igl)
    temp2_igl = ascraster.duplicategrid(nh3_tot_egl)
    temp2_igl.add(nox_em)
    temp2_igl.multiply(figl)
    fnup_corr_gw_igl.substract(temp2_igl)
    temp3_igl = ascraster.duplicategrid(nh3em_crit_gw_igl)
    temp3_igl.multiply(figl)
    temp3_igl.add(nman_crit_gw_igl)
    temp3_igl.add(nfer_crit_gw_igl)
    fnup_corr_gw_igl.divide(temp3_igl, default_nodata_value=-9999)
    fileout = os.path.join(params.outputdir, "fnup_corr_gw_igl.asc")
    fnup_corr_gw_igl.write_ascii_file(fileout,
                                      output_nodata_value=-9999,
                                      compress=params.lcompress)
    print_debug(fnup_corr_gw_igl, "fnup_corr_gw_igl =")

    ########### Checking degree of exceedance of critical N leaching by FIXED N inputs ############
    # Calculate N leaching caused by N fixation alone
    nle_nfix_gw_ara = ascraster.duplicategrid(nfix_ara)
    nle_nfix_gw_ara.multiply(v_ara)
    nle_nfix_gw_igl = ascraster.duplicategrid(nfix_igl)
    nle_nfix_gw_igl.multiply(v_igl)
    nle_nfix_gw = ascraster.duplicategrid(nle_nfix_gw_ara)
    nle_nfix_gw.add(nle_nfix_gw_igl)
    fileout = os.path.join(params.outputdir, "nle_nfix_gw.asc")
    nle_nfix_gw.write_ascii_file(fileout,
                                 output_nodata_value=-9999,
                                 compress=params.lcompress)
    #print_debug(nle_nfix_gw,"The N leaching caused by N fixation alone is")
    # Calculate N leaching caused by NOx emissions alone
    nle_nox_gw_ara = ascraster.duplicategrid(nox_em)
    nle_nox_gw_ara.multiply(fara)
    nle_nox_gw_ara.multiply(v_ara)
    nle_nox_gw_igl = ascraster.duplicategrid(nox_em)
    nle_nox_gw_igl.multiply(figl)
    nle_nox_gw_igl.multiply(v_igl)
    nle_nox_gw = ascraster.duplicategrid(nle_nox_gw_ara)
    nle_nox_gw.add(nle_nox_gw_igl)
    fileout = os.path.join(params.outputdir, "nle_nox_gw.asc")
    nle_nox_gw.write_ascii_file(fileout,
                                output_nodata_value=-9999,
                                compress=params.lcompress)
    #print_debug(nle_nox_gw,"The N leaching caused by NOx emissions alone is")
    # Calculate N leaching caused by NH3,egl emissions alone
    nle_nh3egl_gw_ara = ascraster.duplicategrid(nh3_tot_egl)
    nle_nh3egl_gw_ara.multiply(fara)
    nle_nh3egl_gw_ara.multiply(v_ara)
    nle_nh3egl_gw_igl = ascraster.duplicategrid(nh3_tot_egl)
    nle_nh3egl_gw_igl.multiply(figl)
    nle_nh3egl_gw_igl.multiply(v_igl)
    nle_nh3egl_gw = ascraster.duplicategrid(nle_nh3egl_gw_ara)
    nle_nh3egl_gw.add(nle_nh3egl_gw_igl)
    fileout = os.path.join(params.outputdir, "nle_nh3egl_gw.asc")
    nle_nh3egl_gw.write_ascii_file(fileout,
                                   output_nodata_value=-9999,
                                   compress=params.lcompress)
    #print_debug(nle_nh3egl_gw,"The N leaching caused by NH3 emissions from ext grassland alone is")
    #############################################################################################

    ########### FORWARD CALCULATIONS TO CHECK ###########
    ## * Critical N budget *
    # 'ara'
    nbud_crit_gw_ara = ascraster.duplicategrid(nin_crit_gw_ara)
    nbud_crit_gw_ara.substract(nup_crit_gw_ara)
    print_debug(nbud_crit_gw_ara, "nbud_crit_gw_ara =")
    # 'igl'
    nbud_crit_gw_igl = ascraster.duplicategrid(nin_crit_gw_igl)
    nbud_crit_gw_igl.substract(nup_crit_gw_igl)
    print_debug(nbud_crit_gw_igl, "nbud_crit_gw_igl =")

    ## * Critical leaching *
    # 'ara'
    nle_crit_gw_test_ara = ascraster.duplicategrid(nbud_crit_gw_ara)
    nle_crit_gw_test_ara.substract(nsro_crit_gw_ara)
    nle_crit_gw_test_ara.multiply(fle_ag)
    fileout = os.path.join(params.outputdir, "nle_crit_gw_test_ara.asc")
    nle_crit_gw_test_ara.write_ascii_file(fileout,
                                          output_nodata_value=-9999,
                                          compress=params.lcompress)
    print_debug(nle_crit_gw_test_ara, "nle_crit_gw_test_ara =")
    # 'igl'
    nle_crit_gw_test_igl = ascraster.duplicategrid(nbud_crit_gw_igl)
    nle_crit_gw_test_igl.substract(nsro_crit_gw_igl)
    nle_crit_gw_test_igl.multiply(fle_ag)
    fileout = os.path.join(params.outputdir, "nle_crit_gw_test_igl.asc")
    nle_crit_gw_test_igl.write_ascii_file(fileout,
                                          output_nodata_value=-9999,
                                          compress=params.lcompress)
    print_debug(nle_crit_gw_test_igl, "nle_crit_gw_test_igl =")

    ## *TEST IF FORWARD CALCULATIONS EQUAL BACKWARD CALLCULATION*
    # 'ara'
    if icell_debug < 0:
        pass
    else:
        fw_ara = nle_crit_gw_test_ara.get_data(icell_debug)
        bw_ara = nle_crit_gw_ara.get_data(icell_debug)
        if fw_ara is None:
            print(
                "FW/BW_TEST:_Forward_calculation_not_possible:_Nin,crit = None"
            )

        else:
            fw_ara = round(fw_ara, 4)
            bw_ara = round(bw_ara, 4)
            if fw_ara == bw_ara:
                print("FW/BW_TEST_ARABLE_LAND = SUCCESFUL")
            else:
                print("FW/BW_TEST_ARABLE_LAND = NOT_SUCCESFUL")
    # 'igl'
    if icell_debug < 0:
        pass
    else:
        fw_igl = nle_crit_gw_test_igl.get_data(icell_debug)
        bw_igl = nle_crit_gw_igl.get_data(icell_debug)
        if fw_igl is None:
            print(
                "FW/BW_TEST:_Forward_calculation_not_possible:_Nin,crit = None"
            )

        else:
            fw_igl = round(fw_igl, 4)
            bw_igl = round(bw_igl, 4)
            if fw_igl == bw_igl:
                print("FW/BW_TEST_INTENSIVE_GRASSLAND = SUCCESFUL")
            else:
                print("FW/BW_TEST_INTENSIVE_GRASSLAND = NOT_SUCCESFUL")
Пример #29
0
def calculate(params, mask, mouth_dict, lake_icell_dict, basin, discharge,
              lddmap, Pload):
    '''
    This function calculates the retention in the grid cells.
    '''

    # Read in the PCGLOBWB files
    water_storage_file = params.water_storage
    water_storage = ascraster.Asciigrid(ascii_file=water_storage_file,\
                                    mask=mask,numtype=float)

    flooding_depth_file = params.flooding_depth
    flooding_depth = ascraster.Asciigrid(ascii_file=flooding_depth_file,\
                                    mask=mask,numtype=float)

    flooding_fraction = ascraster.Asciigrid(ascii_file=params.flooding_fraction,\
                                    mask=mask,numtype=float)

    fraction_water_grid = ascraster.Asciigrid(ascii_file=params.fraction_water,\
                                  mask=mask,numtype=float)

    cellarea_grid = ascraster.Asciigrid(ascii_file=params.cellarea,\
                                    mask=mask,numtype=float)

    lakeid_grid = ascraster.Asciigrid(ascii_file=params.lakeid,\
                                    mask=mask,numtype=int)

    outlakeid_grid = ascraster.Asciigrid(ascii_file=params.outlakeid,\
                                    mask=mask,numtype=int)

    water_area_grid = ascraster.Asciigrid(ascii_file=params.water_area,\
                                    mask=mask,numtype=int)

    # Water volume flooding area
    volume_flooding_grid = ascraster.duplicategrid(flooding_depth)
    volume_flooding_grid.multiply(flooding_fraction, default_nodata_value=0.0)
    volume_flooding_grid.multiply(cellarea_grid, default_nodata_value=0.0)
    volume_flooding_grid.write_ascii_file(
        os.path.join(params.outputdir, "volume_flooding_grid.asc"))

    # Water volume of river.
    volume_river_grid = ascraster.duplicategrid(water_storage)
    volume_river_grid.write_ascii_file(
        os.path.join(params.outputdir, "volume_river_grid.asc"))

    # Area water of river.
    area_river_grid = ascraster.duplicategrid(cellarea_grid)
    area_river_grid.multiply(fraction_water_grid, default_nodata_value=0.0)
    area_river_grid.write_ascii_file(
        os.path.join(params.outputdir, "area_river_grid.asc"))

    # Correct river volume and area when there is a reservoir or a lake.
    for icell in range(lakeid_grid.length):
        id = lakeid_grid.get_data(icell, -1)
        if (id > 0):
            # It is a lake or reservoir
            # Check whether it is a outlet of a lake or reservoir
            id = outlakeid_grid.get_data(icell, -1)
            if (id > 0):
                # It is an outlet. So put lake properties into the gridcell
                volume_river_grid.set_data(icell,
                                           water_storage.get_data(icell, 0.0))
                area_river_grid.set_data(icell,
                                         water_area_grid.get_data(icell, 0.0))
            else:
                # It is not an outlet. So remove the calculated river properties.
                volume_river_grid.set_data(icell, 0.0)
                area_river_grid.set_data(icell, 0.0)

    volume_river_grid.write_ascii_file(
        os.path.join(params.outputdir, "volume_river_grid_with_lakes.asc"))
    area_river_grid.write_ascii_file(
        os.path.join(params.outputdir, "area_river_grid_with_lakes.asc"))

    # Depth water of river in metres
    depth_river_grid = ascraster.duplicategrid(volume_river_grid)
    depth_river_grid.divide(area_river_grid, default_nodata_value=0.0)
    depth_river_grid.write_ascii_file(
        os.path.join(params.outputdir, "depth_river_grid.asc"))

    # Change water_storage from m3 into km3
    volume_river_grid.multiply(1.0e-9)
    volume_flooding_grid.multiply(1.0e-9)

    # Residence time is the volume divided by discharge.
    # Water storage is in km3. Discharge is km3/yr. So residence time is in years.
    residence_time_river_grid = ascraster.duplicategrid(volume_river_grid)
    residence_time_flooding_grid = ascraster.duplicategrid(
        volume_flooding_grid)

    # Calculate residence times by dividing water_storage through discharge.
    residence_time_river_grid.divide(discharge, default_nodata_value=0.0)
    residence_time_flooding_grid.divide(
        discharge,
        default_nodata_value=0.0,
        maximum=params.max_residence_time_per_cell)

    # If residence times are very high (e.g in dry areas),
    # set the residence time to a maximum
    # Apply maximum on gridcells which are not the outlet of a reservoir or lake.
    # In the flooding map there are no lakes/reservoirs
    for icell in range(residence_time_river_grid.length):
        resi = residence_time_river_grid.get_data(icell, -1)
        if (resi > params.max_residence_time_per_cell):
            # Check whether this is a outlet of a lake or reservoir
            try:
                outcell = lake_icell_dict[icell].outcell
                if (outcell == 1):
                    # Outlet of a lake/reservoir
                    if (resi > params.max_residence_time_per_lake):
                        resi = params.max_residence_time_per_lake
                else:
                    resi = params.max_residence_time_per_cell
                residence_time_river_grid.set_data(icell, resi)
            except KeyError:
                # This is no reservoir or lake
                residence_time_river_grid.set_data(
                    icell, params.max_residence_time_per_cell)
    residence_time_river_grid.write_ascii_file(
        os.path.join(params.outputdir, "residence_time_river_grid.asc"))
    residence_time_flooding_grid.write_ascii_file(
        os.path.join(params.outputdir, "residence_time_flooding_grid.asc"))

    # Put residence time of the water body (lakes and reservoirs) in the grid.
    for icell in lake_icell_dict:
        if (lake_icell_dict[icell].outcell == 1):
            lake_icell_dict[
                icell].residence_time = residence_time_river_grid.get_data(
                    icell, 0.0)

    # Calculate the water volume in the grid cell
    volume_grid = ascraster.duplicategrid(volume_flooding_grid)
    volume_grid.add(volume_river_grid, default_nodata_value=0.0)
    volume_grid.write_ascii_file(
        os.path.join(params.outputdir, "volume_grid.asc"))

    # Calculate the average residence time with as weighing factors the volumes of the flooding - river
    residence_time_grid = ascraster.duplicategrid(volume_river_grid)
    residence_time_grid.multiply(residence_time_river_grid,
                                 default_nodata_value=0.0)
    temp_grid = ascraster.duplicategrid(volume_flooding_grid)
    temp_grid.multiply(residence_time_flooding_grid, default_nodata_value=0.0)
    residence_time_grid.add(temp_grid, default_nodata_value=0.0)
    residence_time_grid.divide(volume_grid, default_nodata_value=0.0)
    del temp_grid

    # Write residence_time to output file:
    residence_time_grid.write_ascii_file(
        os.path.join(params.outputdir, "residence_time.asc"))

    # Calculate the travel time of each grid cell to the river mouth
    traveltime = accuflux.accutime(lddmap, residence_time_grid,
                                   lake_icell_dict)
    traveltime.write_ascii_file(
        os.path.join(params.outputdir, "traveltime.asc"))

    # Aggregate traveltime over the river basin.
    aggregate.aggregate_grid(basin,
                             traveltime,
                             mouth_dict,
                             item="sum_traveltime")

    # Calculate the average traveltime for each river basin
    for key in list(mouth_dict.keys()):
        try:
            mouth_dict[key].avg_traveltime = mouth_dict[
                key].sum_traveltime / mouth_dict[key].number_of_cells
        except ZeroDivisionError:
            mouth_dict[key].avg_traveltime = 0.0
        except AttributeError:
            pass

    # Calculate the hydraulic load (Hl = depth/residence_time)
    hydraulic_load_flooding_grid = ascraster.duplicategrid(flooding_depth)
    hydraulic_load_flooding_grid.divide(residence_time_flooding_grid,
                                        default_nodata_value=0.0)
    hydraulic_load_river_grid = ascraster.duplicategrid(depth_river_grid)
    hydraulic_load_river_grid.divide(residence_time_river_grid,
                                     default_nodata_value=0.0)
    hydraulic_load_river_grid.write_ascii_file(
        os.path.join(params.outputdir, "hydraulic_load_river.asc"))
    hydraulic_load_flooding_grid.write_ascii_file(
        os.path.join(params.outputdir, "hydraulic_load_flooding.asc"))
    # Calculate the average hydraulic load with as weighing factors the volumes of the flooding - river
    hydraulic_load_grid = ascraster.duplicategrid(volume_river_grid)
    hydraulic_load_grid.multiply(hydraulic_load_river_grid,
                                 default_nodata_value=0.0)
    temp_grid = ascraster.duplicategrid(volume_flooding_grid)
    temp_grid.multiply(hydraulic_load_flooding_grid, default_nodata_value=0.0)
    hydraulic_load_grid.add(temp_grid, default_nodata_value=0.0)
    hydraulic_load_grid.divide(volume_grid, default_nodata_value=0.0)
    del temp_grid

    # Write hydraulic load to output file:
    hydraulic_load_grid.write_ascii_file(
        os.path.join(params.outputdir, "hydraulic_load.asc"))

    temperature_grid = ascraster.Asciigrid(ascii_file=params.water_temperature,\
                                      mask=mask,numtype=float)

    # Read endoreic lakes
    endo_lakes_grid = ascraster.Asciigrid(ascii_file=params.endo_lakes,\
                                      mask=mask,numtype=int)

    # Calculate the retention for rivers.
    retention_river_grid = ascraster.duplicategrid(volume_river_grid)
    for icell in range(retention_river_grid.length):
        id = endo_lakes_grid.get_data(icell, 0)
        if (id > 0):
            # This is an endoreic lake. So the calculation
            retention_river_grid.set_data(icell, 1.0)
            continue

        vol = volume_river_grid.get_data(icell, -1)
        if (vol > 0):
            temperature = temperature_grid.get_data(icell, 20.0)
            hydraulic_load = hydraulic_load_river_grid.get_data(icell, 0.0)
            ret = calculate_retention(params, hydraulic_load, vol, temperature)
            retention_river_grid.set_data(icell, ret)
        else:
            retention_river_grid.set_data(icell, 0.0)

    # Calculate the retention for flood planes.
    retention_flooding_grid = ascraster.duplicategrid(volume_flooding_grid)
    for icell in range(retention_flooding_grid.length):
        vol = volume_flooding_grid.get_data(icell, -1)
        if (vol > 0):
            temperature = temperature_grid.get_data(icell, 20.0)
            hydraulic_load = hydraulic_load_flooding_grid.get_data(icell, 0.0)
            ret = calculate_retention(params, hydraulic_load, vol, temperature)
            retention_flooding_grid.set_data(icell, ret)
        else:
            retention_flooding_grid.set_data(icell, 0.0)

    # Calculate the average retention with as weighing factors the volumes of the flooding - river
    retention_grid = ascraster.duplicategrid(volume_river_grid)
    retention_grid.multiply(retention_river_grid, default_nodata_value=0.0)
    temp_grid = ascraster.duplicategrid(volume_flooding_grid)
    temp_grid.multiply(retention_flooding_grid, default_nodata_value=0.0)
    retention_grid.add(temp_grid, default_nodata_value=0.0)
    retention_grid.divide(water_storage, default_nodata_value=0.0)
    del temp_grid

    # Set the lake database
    for icell in range(outlakeid_grid.length):
        if (id > 0):
            # It is an outlet. So put lake properties into the gridcell
            # Recalculate the temperature
            temp = 0.0
            number_of_cells = len(lake_icell_dict[icell].cells)
            for item in range(number_of_cells):
                icell_lake = lake_icell_dict[icell].cells[item]
                temp += temperature.get_data(icell_lake, 0.0)
            # Take average over the cells.
            temperature_cell = temp / float(number_of_cells)
            ret_cell = retention_river_grid.get_data(icell, 0.0)
            setattr(lake_icell_dict[icell], "retention", ret_cell)
            setattr(lake_icell_dict[icell], "fraction_water", -99)
            setattr(lake_icell_dict[icell], "temperature", temperature_cell)

    accuPload = P_accuflux(params, mask, lake_icell_dict, lddmap, Pload,
                           retention_grid)

    return residence_time_grid, traveltime, retention_grid, accuPload, hydraulic_load_grid
Пример #30
0
# ******************************************************
## Revision "$LastChangedDate: 2018-06-01 15:05:44 +0200 (Fri, 01 Jun 2018) $"
## Date "$LastChangedRevision: 1 $"
## Author "$LastChangedBy: arthurbeusen $"
## URL "$HeadURL: https://pbl.sliksvn.com/generalcode/trunk/showimg.py $"
## Copyright 2017, PBL Netherlands Environmental Assessment Agency and Utrecht University.
## Reuse permitted under Gnu Public License, GPL v3.
# ******************************************************

import ascraster
import sys

# Read ascii grid
grid = ascraster.Asciigrid(ascii_file=sys.argv[1])
grid.save_as_bitmap('qq.jpg')