Somehow I missed this formula. But the issue remained. Why S2A different method of calculation? Why in the formula has the value of 0.0172 (the Earth angular velocity (radians/day))? All other methods were wrong? They give a completely different result.

Ok. The issue is resolved:
d(t) = 1/ ((1-0.01673cos(0.0172(t-2)))^2) = 1/d^2
d(t) – this is value from metadata!
Distance of Earth-Sun: d = SQRT(1/d(t)).

Can you please explain how to get the values to calculate the TOA or BOA reflectance ?
L = (rToa * e0__SOLAR_IRRADIANCE_For_band * cos(Z__Sun_Angles_Grid_Zenith_Values)) / (PI * U__earth_sun_distance_correction_factor) (Equation 1)
I know that :
rToa is the band we need to calculate its reflectance
e0__SOLAR_IRRADIANCE_For_band can be obtained from the metadata as below for Band 4 for example :

U__earth_sun_distance_correction_factor from the metadata as below :

But how to obtain:
1- cos(Z__Sun_Angles_Grid_Zenith_Values))
2- PI ( what is it ?)

I have calculated the d(t) according to the equation :

As t= 0.98358 (for the day for day 358, the earth-sun distance is 1.00992).
d(t) = 1.03431890515045328251315
But do not know where to insert d(t) in the (Equation 1)

Another question regarding :
(Equation 2)

Another question, why using the Equation 1 and not Equation 2 for calculating the reflectance? Also would be more appreciate if you could give an example of how to fine the parameters in Equation 2 from Sentinel 2 metadata.

Data Sentinel-2A L1C MSI already have TOA Reflectance. According to the documentation, You must divide the pixel value by the value of QUANTIFICATION_VALUE:

rTOA = DN / QUANTIFICATION_VALUE

Possible also some deviation in DN from the desired range. For example:

For BOA Reflectance – use, for example, SEN2COR package:

d = SQRT(1/d(t)) or d =SQRT(1/U)
ESUN = SOLAR_IRRADIANCE for Band
Theta = Z__Sun_Angles_Grid_Zenith_Values – the interpolated value from the matrix from the metadata:
GRANULE \ … granule.xml, tags: “Tile_Angles Sun_Angles_Grid Zenith… Values_List…”

The correct expression for the radiance should be the following:
L = (rToa * e0__SOLAR_IRRADIANCE_For_band * cos(Z__Sun_Angles_Grid_Zenith_Values) * U__earth_sun_distance_correction_factor) / PI
because U=1/d^2

But in the program sen2tree has the following lines:

===================

def refl2rad(self, indataArr):
‘’’ Converts the reflectance to radiance.
:param indataArray: the digital numbers representing TOA reflectance.
:type indataArray: a 2 dimensional numpy array (row x column) of type unsigned int 16.
:return: the pixel data converted to radiance.
:rtype: a 2 dimensional numpy array (row x column) of type unsigned int 16, representing radiance.

Additional inputs from L1 user Product_Image_Characteristics metadata:
* QUANTIFICATION_VALUE: the scaling factor for converting DN to reflectance.
* U: the earth sun distance correction factor.
* SOLAR_IRRADIANCE: the mean solar exoatmospheric irradiances for each band.
Additional inputs from L1 tile Geometric_Info metadata:
* Sun_Angles_Grid.Zenith.Values: the interpolated zenith angles grid.
'''
# This converts TOA reflectance to radiance:
nrows = self.config.nrows
ncols = self.config.ncols
# The digital number (DN) as float:
DN = indataArr.astype(float32)
xp = L3_XmlParser(self.config, 'UP1C')
pic = xp.getTree('General_Info', 'Product_Image_Characteristics')
qv = pic.QUANTIFICATION_VALUE
c0 = 0
# The quantification value for the DN from metadata:
c1 = float32(qv.text)
# TOA reflectance:
rtoa = float32(c0 + DN / c1)
rc = pic.Reflectance_Conversion
# The earth sun distance correction factor,
# apparently already squared:
u2 = float32(rc.U.text)
# The solar irradiance:
si = rc.Solar_Irradiance_List.SOLAR_IRRADIANCE
e0 = float32(si[self._bandIndex].text)
# The solar zenith array:
x = arange(nrows, dtype=float32) / (nrows-1) * self.config.solze_arr.shape[0]
y = arange(ncols, dtype=float32) / (ncols-1) * self.config.solze_arr.shape[1]
szi = rectBivariateSpline(x,y,self.config.solze_arr)
rad_szi = radians(szi)
sza = float32(cos(rad_szi))
rtoa_e0_sza = float32(rtoa * sza * e0)

???–>> pi_u2 = float32(pi * u2 )

# Finally, calculate the radiance and return array as unsigned int, this is multiplied by 100,
# to keep resolution - glymur only allows integer integer values for storage.
L = (rtoa_e0_sza / pi_u2 ) * 100.0
return (L + 0.5).astype(uint16)

===================

Link:

It seems to me that this formula is correct:
L = (UrToaESUN*cos(theta))/PI;
And in their program - error.

L = (UrToaESUN*cos(theta))/PI is correct expression, but you’ve written in other posts L = (rToa * e0__SOLAR_IRRADIANCE_For_band * cos(Z__Sun_Angles_Grid_Zenith_Values)) / (PI * U__earth_sun_distance_correction_factor)