In our first verses we have David acquainting thunder and lightning and hail as the Lord working against his enemies and the enemies of Israel. We find things like this al the way back as far as between Israel and Pharaoh in Egypt. Does God control the weather for the benefit of His people? He can! My niece Linda lives in Florida and heard an urgent tornado warning. Linda and her daughter got into the bathroom tub and prayed. Theirs and a neighbors house were the only ones untouched by the tornado in the whole block. Here it is though that David could look at the weather as God's protection when needed.
I will go on to verse 20 as God is rewarding David being rewarded according to his own practical righteousness. Does that mean he was sinless. We only need to read the story to find out that was not true. But he was careful to observe His judgments and statutes. He sought to keep himself from iniquity.
The last verse 24 he speaks of the Lord's recompense to him for his righteous life. We can't use this as a works salvation, for we are saved by grace alone. Once we are saved God does honor a godly life.