<commit_before>package clocky

import (
	"fmt"
	"io"
	"math"
	"os"
	"regexp"
	"strconv"
	"strings"
	"template"
	"xml"

	"appengine"
	"appengine/memcache"
)

// WindChill returns the Celsius wind chill (2001 North American
// formula) for a given air temperature in degrees Celsius and a wind
// speed in km/h.
func WindChill(temp, speed float64) *float64 {
	if temp > 10 || speed <= 4.8 {
		return nil
	}
	chill := 13.12 + 0.6215*temp - 11.37*math.Pow(speed, 0.16) + 0.3965*temp*math.Pow(speed, 0.16)
	return &chill
}

func Cardinal(degrees int) string {
	switch {
	case degrees < 0:
		break
	case degrees < 23:
		return "N"
	case degrees < 68:
		return "NW"
	case degrees < 113:
		return "W"
	case degrees < 158:
		return "SW"
	case degrees < 203:
		return "S"
	case degrees < 248:
		return "SE"
	case degrees < 293:
		return "E"
	case degrees < 338:
		return "NE"
	case degrees < 360:
		return "N"
	}
	return ""
}

func Conditions(w io.Writer, c appengine.Context) {
	item, err := memcache.Get(c, "conditions")
	if err != nil {
		c.Errorf("%s", err)
		return
	}

	var dir string
	var speed, temp, chill *float64

	for _, line := range strings.Split(string(item.Value), "\n") {
		if len(line) != 116 || line[0] == '#' {
			continue
		}
		// FTPC1 is a C-MAN automated buoy near Crissy Field.
		if line[:5] != "FTPC1" {
			continue
		}
		if n, err := strconv.Atoi(strings.TrimSpace(line[40:43])); err != nil || n < 0 || n > 359 {
			c.Errorf("weather: bad wind direction in %q", line)
		} else {
			dir = Cardinal(n)
		}
		if n, err := strconv.Atof64(strings.TrimSpace(line[44:49])); err != nil {
			c.Errorf("weather: bad wind speed in %q", line)
		} else {
			n *= 3.6 // m/s to km/h
			speed = &n
		}
		if n, err := strconv.Atof64(strings.TrimSpace(line[87:92])); err != nil {
			c.Errorf("weather: bad temp in %q", line)
		} else {
			temp = &n
		}
		break
	}
	if temp != nil && speed != nil {
		chill = WindChill(*temp, *speed)
	}

	io.WriteString(w, `<div class=header>`)
	if temp != nil {
		// Don't round this, since we are using the value
		// directly from the data, not a converted value like
		// wind speed or a derived value like wind chill.
		fmt.Fprintf(w, `<span class=larger>%.1f°</span> `, *temp)
	}
	switch {
	case speed == nil:
		// Output nothing.
	case chill != nil:
		fmt.Fprintf(w, `wind chill %.1f°`, *chill+0.05)
	case *speed > 1:
		fmt.Fprintf(w, ` %s wind %d&thinsp;km/h`, dir, int(*speed+0.5))
	default:
		io.WriteString(w, `wind calm`)
	}
	io.WriteString(w, `</div>`)
}

var (
	nbspRegexp   = regexp.MustCompile(` [0-9]+\.`)
	thinspRegexp = regexp.MustCompile(`[0-9] (am|pm|km/h)`)
)<commit_msg>Only display wind chill if it's at least 1° below temperature.<commit_after>package clocky

import (
	"fmt"
	"io"
	"math"
	"os"
	"regexp"
	"strconv"
	"strings"
	"template"
	"xml"

	"appengine"
	"appengine/memcache"
)

// WindChill returns the Celsius wind chill (2001 North American
// formula) for a given air temperature in degrees Celsius and a wind
// speed in km/h.
func WindChill(temp, speed float64) *float64 {
	if temp > 10 || speed <= 4.8 {
		return nil
	}
	chill := 13.12 + 0.6215*temp - 11.37*math.Pow(speed, 0.16) + 0.3965*temp*math.Pow(speed, 0.16)
	return &chill
}

func Cardinal(degrees int) string {
	switch {
	case degrees < 0:
		break
	case degrees < 23:
		return "N"
	case degrees < 68:
		return "NW"
	case degrees < 113:
		return "W"
	case degrees < 158:
		return "SW"
	case degrees < 203:
		return "S"
	case degrees < 248:
		return "SE"
	case degrees < 293:
		return "E"
	case degrees < 338:
		return "NE"
	case degrees < 360:
		return "N"
	}
	return ""
}

func Conditions(w io.Writer, c appengine.Context) {
	item, err := memcache.Get(c, "conditions")
	if err != nil {
		c.Errorf("%s", err)
		return
	}

	var dir string
	var speed, temp, chill *float64

	for _, line := range strings.Split(string(item.Value), "\n") {
		if len(line) != 116 || line[0] == '#' {
			continue
		}
		// FTPC1 is a C-MAN automated buoy near Crissy Field.
		if line[:5] != "FTPC1" {
			continue
		}
		if n, err := strconv.Atoi(strings.TrimSpace(line[40:43])); err != nil || n < 0 || n > 359 {
			c.Errorf("weather: bad wind direction in %q", line)
		} else {
			dir = Cardinal(n)
		}
		if n, err := strconv.Atof64(strings.TrimSpace(line[44:49])); err != nil {
			c.Errorf("weather: bad wind speed in %q", line)
		} else {
			n *= 3.6 // m/s to km/h
			speed = &n
		}
		if n, err := strconv.Atof64(strings.TrimSpace(line[87:92])); err != nil {
			c.Errorf("weather: bad temp in %q", line)
		} else {
			temp = &n
		}
		break
	}
	if temp != nil && speed != nil {
		chill = WindChill(*temp, *speed)
	}

	io.WriteString(w, `<div class=header>`)
	if temp != nil {
		// Don't round this, since we are using the value
		// directly from the data, not a converted value like
		// wind speed or a derived value like wind chill.
		fmt.Fprintf(w, `<span class=larger>%.1f°</span> `, *temp)
	}
	switch {
	case speed == nil:
		// Output nothing.
	case chill != nil && *chill < *temp-1:
		fmt.Fprintf(w, `wind chill %.1f°`, *chill+0.05)
	case *speed > 1:
		fmt.Fprintf(w, ` %s wind %d&thinsp;km/h`, dir, int(*speed+0.5))
	default:
		io.WriteString(w, `wind calm`)
	}
	io.WriteString(w, `</div>`)
}

var (
	nbspRegexp   = regexp.MustCompile(` [0-9]+\.`)
	thinspRegexp = regexp.MustCompile(`[0-9] (am|pm|km/h)`)
)
{'fraction_non_alphanumeric': 0.16669798909979328, 'fraction_numerical': 0.03984213493704191, 'mean_word_length': 3.141634241245136, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '184129', 'n_tokens_mistral': 2390, 'n_tokens_neox': 2098, 'n_words': 608}