<commit_before>#! /usr/bin/make -f

ifndef configure.mk
include configure.mk
endif

insolation.mk:=1

# Check this is a YYYY-MM-DD setup
$(call hasDD)


# Define some .PHONY raster interpolation targets
$(foreach p,FAO_Rso Rso Rs K Dk Bk sretr ssetr ssha,$(eval $(call grass_raster_shorthand,$(p))))

# Sunrise/Sunset parameters are taken from r.solpos
${rast}/sretr ${rast}/ssetr ${rast}/ssha:
	r.solpos date=$(MAPSET) sretr=sretr ssetr=ssetr ssha=ssha

############################################################################
# Calculations of the CIMIS data, are still handled outside this file
# I will think about inserting them into this setup.
# This includes K,Dk,Bk,Trb,Trd
############################################################################
insolation: ${rast}/Bc ${rast}/Dc ${rast}/Gc ${rast}/Trd ${rast}/Trd ${rast}/FAO_Rso ${rast}/Rs ${rast}/Rso

clean:: clean-tmp
	g.remove rast=Rs,B,D,Bc,Dc,Bk,Dk,Rso,G,K,Bc,Dc,Gc,Trb,Trd,FAO_Rso &> /dev/null
	g.mremove -f rast=[knp]????


# Wh/m^2 day -> MJ/m^2 day
${rast}/Rso: ${rast}/Gc
	r.mapcalc "Rso=(Gc*0.0036)" &> /dev/null; 
	@r.colors map=Rso rast=Rso@default_colors > /dev/null

${rast}/Rs: ${rast}/G
	r.mapcalc "Rs=(G*0.0036)" &> /dev/null; 
	@r.colors map=Rs rast=Rso@default_colors > /dev/null


########################################################################
# This is the CIMIS method of calculating the Extraterrestrial Radiation
#############################################################################
.PHONY: FAO_Rso
FAO_Rso: ${rast}/FAO_Rso

${rast}/FAO_Rso: ${rast}/ssha
	$(call NOMASK)
	eval `r.solpos -r date=$(MAPSET)`; \
	r.mapcalc "FAO_Rso=(0.0036)*(0.75+0.00002*'Z@2km')*$$etrn*24/3.14159*\
	((ssha*3.14159/180)*sin(latitude_deg@2km)*sin($$declin)\
	+cos(latitude_deg@2km)*cos($$declin)*sin(ssha))" &> /dev/null
	@r.colors map=$(notdir $<) rast=Rso@default_colors > /dev/null


visHHMM:=$(shell cg.daylight.intervals --exists --int=60 delim=' ' filename='vis%hh%mm' date=$(MAPSET) sretr=sretr ssetr=ssetr)
HHMM:=$(subst vis,,$(visHHMM))
kXXXX:=$(patsubst %,k%,$(HHMM))

##############################################################################
# Calculation of Albedo.  We need:
# k=1 cloud value (max brightness on 9x9)
# albedo for each hour
##############################################################################

#cloud-cover::

define r.cloud_cover

#cloud-cover:: $(rast)/cloud$(1)
#$(eval $(call grass_shorthand_names,n$(1)))
#$(eval $(call grass_shorthand_names,p$(1)))
#$(eval $(call grass_shorthand_names,k$(1)))
#$(eval $(call grass_shorthand_names,cloud$(1)))

$(rast)/vis$(1)_9:$(rast)/vis$(1)
	$(call NOMASK)
	r.neighbors --q size=9 input=vis${1} output=vis${1}_9 method=average

$(rast)/vis$(1)_m:$(rast)/vis$(1)
	$(call NOMASK)
	r.neighbors --q size=5 input=vis${1} output=vis${1}_m method=median

# We should look at replacing this with a simpler method (like top 2%)
$(etc)/maxmedian/vis$(1): $(rast)/vis$(1)_m
	[[ -d $(etc)/maxmedian ]] || mkdir -p $(etc)/maxmedian
	max=$$$$(for p in `cg.proximate.mapsets --past=14 rast=vis$(1)_m --delim=' '`; do \
	  r.info -r $$$$p | sed -n -e 's/max=// p'; \
	done | sort -n | tail -1); \
	echo $$$$max > $$@

# We should look at replacing this with a simpler method (like top 2%)
$(etc)/max/vis$(1): $(rast)/vis$(1)_9
	[[ -d $(etc)/max ]] || mkdir -p $(etc)/max
	max=$$$$(for p in `cg.proximate.mapsets --past=14 rast=vis$(1)_9 --delim=' '`; do \
	  r.info -r $$$$p | sed -n -e 's/max=// p'; \
	done | sort -n | tail -1); \
	echo $$$$max > $$@

$(rast)/p$(1): $(rast)/vis$(1)
	maps=`cg.proximate.mapsets --quote --delim=',' --past=14 rast=vis$(1)`; \
	r.mapcalc "p$(1)=min($$$$maps)" &> /dev/null

$(rast)/cloud$(1): $(rast)/p$(1) $(etc)/max/vis$(1)
	$(call NOMASK)
	max=`cat $(etc)/max/vis$(1)`; \
	r.mapcalc "cloud$(1)=int(if(isnull(p$(1)),if(vis$(1)>$$$$max,1,vis$(1)/$$$$max),if(vis$(1)>$$$$max,1,(vis$(1)-p$(1))/($$$$max-p$(1))))*255)" &> /dev/null

$(rast)/n$(1): $(rast)/p$(1) $(etc)/max/vis$(1)
	$(call NOMASK) 
	max=`cat $(etc)/max/vis$(1)`; \
	r.mapcalc "n$(1)=if($$$$max>p$(1),($$$$max-vis$(1))/($$$$max-p$(1)),1.2)" &> /dev/null

$(rast)/k$(1): $(rast)/n$(1)
	$(call NOMASK)
	r.mapcalc 'k$(1)=if((n$(1)>1.2),1.2,if((n$(1)>0.2),n$(1),(if((n$(1)>-0.1),(5.0/3.0)*n$(1)*n$(1)+(1.0/3.0)*n$(1)+(1.0/15.0),0.05))))' &> /dev/null
<commit_msg>Guard against null values in vis<commit_after>#! /usr/bin/make -f

ifndef configure.mk
include configure.mk
endif

insolation.mk:=1

# Check this is a YYYY-MM-DD setup
$(call hasDD)


# Define some .PHONY raster interpolation targets
$(foreach p,FAO_Rso Rso Rs K Dk Bk sretr ssetr ssha,$(eval $(call grass_raster_shorthand,$(p))))

# Sunrise/Sunset parameters are taken from r.solpos
${rast}/sretr ${rast}/ssetr ${rast}/ssha:
	r.solpos date=$(MAPSET) sretr=sretr ssetr=ssetr ssha=ssha

############################################################################
# Calculations of the CIMIS data, are still handled outside this file
# I will think about inserting them into this setup.
# This includes K,Dk,Bk,Trb,Trd
############################################################################
insolation: ${rast}/Bc ${rast}/Dc ${rast}/Gc ${rast}/Trd ${rast}/Trd ${rast}/FAO_Rso ${rast}/Rs ${rast}/Rso

clean:: clean-tmp
	g.remove rast=Rs,B,D,Bc,Dc,Bk,Dk,Rso,G,K,Bc,Dc,Gc,Trb,Trd,FAO_Rso &> /dev/null
	g.mremove -f rast=[knp]????


# Wh/m^2 day -> MJ/m^2 day
${rast}/Rso: ${rast}/Gc
	r.mapcalc "Rso=(Gc*0.0036)" &> /dev/null; 
	@r.colors map=Rso rast=Rso@default_colors > /dev/null

${rast}/Rs: ${rast}/G
	r.mapcalc "Rs=(G*0.0036)" &> /dev/null; 
	@r.colors map=Rs rast=Rso@default_colors > /dev/null


########################################################################
# This is the CIMIS method of calculating the Extraterrestrial Radiation
#############################################################################
.PHONY: FAO_Rso
FAO_Rso: ${rast}/FAO_Rso

${rast}/FAO_Rso: ${rast}/ssha
	$(call NOMASK)
	eval `r.solpos -r date=$(MAPSET)`; \
	r.mapcalc "FAO_Rso=(0.0036)*(0.75+0.00002*'Z@2km')*$$etrn*24/3.14159*\
	((ssha*3.14159/180)*sin(latitude_deg@2km)*sin($$declin)\
	+cos(latitude_deg@2km)*cos($$declin)*sin(ssha))" &> /dev/null
	@r.colors map=$(notdir $<) rast=Rso@default_colors > /dev/null


visHHMM:=$(shell cg.daylight.intervals --exists --int=60 delim=' ' filename='vis%hh%mm' date=$(MAPSET) sretr=sretr ssetr=ssetr)
HHMM:=$(subst vis,,$(visHHMM))
kXXXX:=$(patsubst %,k%,$(HHMM))

##############################################################################
# Calculation of Albedo.  We need:
# k=1 cloud value (max brightness on 9x9)
# albedo for each hour
##############################################################################

#cloud-cover::

define r.cloud_cover

#cloud-cover:: $(rast)/cloud$(1)
#$(eval $(call grass_shorthand_names,n$(1)))
#$(eval $(call grass_shorthand_names,p$(1)))
#$(eval $(call grass_shorthand_names,k$(1)))
#$(eval $(call grass_shorthand_names,cloud$(1)))

$(rast)/vis$(1)_9:$(rast)/vis$(1)
	$(call NOMASK)
	r.neighbors --q size=9 input=vis${1} output=vis${1}_9 method=average

$(rast)/vis$(1)_m:$(rast)/vis$(1)
	$(call NOMASK)
	r.neighbors --q size=5 input=vis${1} output=vis${1}_m method=median

# We should look at replacing this with a simpler method (like top 2%)
$(etc)/maxmedian/vis$(1): $(rast)/vis$(1)_m
	[[ -d $(etc)/maxmedian ]] || mkdir -p $(etc)/maxmedian
	max=$$$$(for p in `cg.proximate.mapsets --past=14 rast=vis$(1)_m --delim=' '`; do \
	  r.info -r $$$$p | sed -n -e 's/max=// p'; \
	done | sort -n | tail -1); \
	echo $$$$max > $$@

# We should look at replacing this with a simpler method (like top 2%)
$(etc)/max/vis$(1): $(rast)/vis$(1)_9
	[[ -d $(etc)/max ]] || mkdir -p $(etc)/max
	max=$$$$(for p in `cg.proximate.mapsets --past=14 rast=vis$(1)_9 --delim=' '`; do \
	  r.info -r $$$$p | sed -n -e 's/max=// p'; \
	done | sort -n | tail -1); \
	echo $$$$max > $$@

$(rast)/p$(1): $(rast)/vis$(1)
	maps=`cg.proximate.mapsets --quote --delim=',' --past=14 rast=vis$(1)`;\
	r.mapcalc "p$(1)=if(isnull(min($$$$maps)),vis$(1),min($$$$maps))" &> /dev/null

$(rast)/cloud$(1): $(rast)/p$(1) $(etc)/max/vis$(1)
	$(call NOMASK)
	max=`cat $(etc)/max/vis$(1)`; \
	r.mapcalc "cloud$(1)=int(if(vis$(1)>$$$$max,1,(vis$(1)-p$(1))/($$$$max-p$(1)))*255)" &> /dev/null

$(rast)/n$(1): $(rast)/p$(1) $(etc)/max/vis$(1)
	$(call NOMASK) 
	max=`cat $(etc)/max/vis$(1)`; \
	r.mapcalc "n$(1)=if($$$$max>p$(1),($$$$max-vis$(1))/($$$$max-p$(1)),1.2)" &> /dev/null

$(rast)/k$(1): $(rast)/n$(1)
	$(call NOMASK)
	r.mapcalc 'k$(1)=if((n$(1)>1.2),1.2,if((n$(1)>0.2),n$(1),(if((n$(1)>-0.1),(5.0/3.0)*n$(1)*n$(1)+(1.0/3.0)*n$(1)+(1.0/15.0),0.05))))' &> /dev/null

{'fraction_non_alphanumeric': 0.3396619587867562, 'fraction_numerical': 0.03484602917341977, 'mean_word_length': 5.427827380952381, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3601374', 'n_tokens_mistral': 3778, 'n_tokens_neox': 3529, 'n_words': 663}