Skip to content

Instantly share code, notes, and snippets.

@pbaylis
Last active September 11, 2015 12:39
Show Gist options
  • Save pbaylis/5cc38e5c8415d87b9b28 to your computer and use it in GitHub Desktop.
Save pbaylis/5cc38e5c8415d87b9b28 to your computer and use it in GitHub Desktop.
Stata program to get heat index for given dry bulb temperature and relative humidity
program define gen_heat_index
* Calculat heat index
* See https://en.wikipedia.org/wiki/Heat_index. Note that there is no exact heat index formula
* since it is defined by the table. There are approximations, but this code uses the table itself.
* The only modification is that I cap at 140.
syntax, DRYbulbtemp(varname) RELativehumidity(varname) [GENerate(string)]
if "`generate'" == "" {
local generate "heat_index"
}
// Load heat index table
preserve
insheet using "heatindex_long.csv", comma clear
save "heatindex.dta", replace
restore
// Round T to nearest 2, RH to nearest 5
capture drop t_tomerge rh_tomerge
gen t_tomerge = round(`drybulbtemp',2)
gen rh_tomerge = round(`relativehumidity',5)
// Merge in table of heat indices
merge m:1 rh_tomerge t_tomerge using "~/Dropbox/01_Work/01_Current/moodtemp/data/heatindex.dta", keep(1 3) nogen
// High extremes
replace heat_index = 140 if t_tomerge > 110 & rh_tomerge >= 40
// Low extremes
replace heat_index = `drybulbtemp' if t_tomerge < 80 | rh_tomerge < 40
// Double check missing
replace heat_index =. if t_tomerge ==. | rh_tomerge ==.
rename heat_index `generate'
drop t_tomerge rh_tomerge
end
RH_tomerge T_tomerge heat_index
40 80 80
45 80 80
50 80 81
55 80 81
60 80 82
65 80 82
70 80 83
75 80 84
80 80 84
85 80 85
90 80 86
95 80 86
100 80 87
40 82 81
45 82 82
50 82 83
55 82 84
60 82 84
65 82 85
70 82 86
75 82 88
80 82 89
85 82 90
90 82 91
95 82 93
100 82 95
40 84 83
45 84 84
50 84 85
55 84 86
60 84 88
65 84 89
70 84 90
75 84 92
80 84 94
85 84 96
90 84 98
95 84 100
100 84 103
40 86 85
45 86 87
50 86 88
55 86 89
60 86 91
65 86 93
70 86 95
75 86 97
80 86 100
85 86 102
90 86 105
95 86 108
100 86 112
40 88 88
45 88 89
50 88 91
55 88 93
60 88 95
65 88 98
70 88 100
75 88 103
80 88 106
85 88 110
90 88 113
95 88 117
100 88 121
40 90 91
45 90 93
50 90 95
55 90 97
60 90 100
65 90 103
70 90 105
75 90 109
80 90 113
85 90 117
90 90 122
95 90 127
100 90 132
40 92 94
45 92 96
50 92 99
55 92 101
60 92 105
65 92 108
70 92 112
75 92 116
80 92 121
85 92 126
90 92 131
95 92 140
100 92 140
40 94 97
45 94 100
50 94 103
55 94 106
60 94 110
65 94 114
70 94 119
75 94 124
80 94 129
85 94 135
90 94 140
95 94 140
100 94 140
40 96 101
45 96 104
50 96 108
55 96 112
60 96 116
65 96 121
70 96 126
75 96 132
80 96 140
85 96 140
90 96 140
95 96 140
100 96 140
40 98 105
45 98 109
50 98 113
55 98 117
60 98 123
65 98 128
70 98 134
75 98 140
80 98 140
85 98 140
90 98 140
95 98 140
100 98 140
40 100 109
45 100 114
50 100 118
55 100 124
60 100 129
65 100 136
70 100 140
75 100 140
80 100 140
85 100 140
90 100 140
95 100 140
100 100 140
40 102 114
45 102 119
50 102 124
55 102 130
60 102 137
65 102 140
70 102 140
75 102 140
80 102 140
85 102 140
90 102 140
95 102 140
100 102 140
40 104 119
45 104 124
50 104 131
55 104 137
60 104 140
65 104 140
70 104 140
75 104 140
80 104 140
85 104 140
90 104 140
95 104 140
100 104 140
40 106 124
45 106 130
50 106 137
55 106 140
60 106 140
65 106 140
70 106 140
75 106 140
80 106 140
85 106 140
90 106 140
95 106 140
100 106 140
40 108 130
45 108 137
50 108 140
55 108 140
60 108 140
65 108 140
70 108 140
75 108 140
80 108 140
85 108 140
90 108 140
95 108 140
100 108 140
40 110 136
45 110 140
50 110 140
55 110 140
60 110 140
65 110 140
70 110 140
75 110 140
80 110 140
85 110 140
90 110 140
95 110 140
100 110 140
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment