From ce33d6a221957612a884b00f0c782c75a352b5b3 Mon Sep 17 00:00:00 2001 From: Avinash Dwarapu Date: Sun, 7 Feb 2016 11:13:19 +0000 Subject: [PATCH] Use the refactoring in main.go --- dscovr/dscovr.go | 78 -------------------------------- himawari/himawari.go | 66 --------------------------- main.go | 105 +++++++++++++++++-------------------------- 3 files changed, 42 insertions(+), 207 deletions(-) delete mode 100644 dscovr/dscovr.go delete mode 100644 himawari/himawari.go diff --git a/dscovr/dscovr.go b/dscovr/dscovr.go deleted file mode 100644 index e463106..0000000 --- a/dscovr/dscovr.go +++ /dev/null @@ -1,78 +0,0 @@ -package dscovr - -import ( - "bytes" - "encoding/json" - "fmt" - "image" - "io/ioutil" - "net/http" - "time" - - _ "image/jpeg" // Decode JPEG images from imagePath -) - -const ( - updatePath = "http://epic.gsfc.nasa.gov/api/images.php" - updateFormat = "2006-01-02 15:04:05" - imagePath = "http://epic.gsfc.nasa.gov/epic-archive/jpg/%s.jpg" -) - -// Latest image name. -func Latest() (string, error) { - // Get the response from the url. - res, err := http.Get(updatePath) - if err != nil { - return "", err - } - defer res.Body.Close() - - // Convert the response to bytes. - body, err := ioutil.ReadAll(res.Body) - if err != nil { - return "", err - } - - // Convert from JSON. - var ts []struct { - Image string `json:"image"` - Date string `json:"date"` - } - if err := json.Unmarshal(body, &ts); err != nil { - return "", err - } - - // Now we iterate through the slice and find the newest image. - latestImage := "" - latestDate := time.Unix(0, 0) - for _, obj := range ts { - objDate, err := time.Parse(updateFormat, obj.Date) - if err != nil { - return "", err - } - if objDate.After(latestDate) { - latestImage = obj.Image - latestDate = objDate - } - } - - return latestImage, nil -} - -// GetImage returns the image with the given name. No stitching this time! -func GetImage(name string) (image.Image, error) { - // Construct the url. - buf := new(bytes.Buffer) - fmt.Fprintf(buf, imagePath, name) - - // Get the response from the url. - res, err := http.Get(buf.String()) - if err != nil { - return nil, err - } - defer res.Body.Close() - - // Create an image from the response. - img, _, err := image.Decode(res.Body) - return img, err -} diff --git a/himawari/himawari.go b/himawari/himawari.go deleted file mode 100644 index 03eb347..0000000 --- a/himawari/himawari.go +++ /dev/null @@ -1,66 +0,0 @@ -package himawari - -import ( - "bytes" - "encoding/json" - "fmt" - "image" - "io/ioutil" - "net/http" - "time" - - _ "image/png" // Decode PNG images from imagePath -) - -const ( - updatePath = "http://himawari8-dl.nict.go.jp/himawari8/img/D531106/latest.json" - updateFormat = "2006-01-02 15:04:05" - imagePath = "http://himawari8.nict.go.jp/img/D531106/%dd/550/%s_%d_%d.png" - imageFormat = "2006/01/02/150405" -) - -// Latest image time. -func Latest() (*time.Time, error) { - // Get the response from the url. - res, err := http.Get(updatePath) - if err != nil { - return nil, err - } - defer res.Body.Close() - - // Convert the response to bytes. - body, err := ioutil.ReadAll(res.Body) - if err != nil { - return nil, err - } - - // Convert to JSON and get the date string. - ts := &struct { - Date string `json:"date"` - }{} - if err := json.Unmarshal(body, ts); err != nil { - return nil, err - } - - // Parse the date string. - latestTime, err := time.Parse(updateFormat, ts.Date) - return &latestTime, err -} - -// GridAt returns the image at a certain row and column. -func GridAt(t *time.Time, depth, row, col int) (image.Image, error) { - // Construct the url. - buf := new(bytes.Buffer) - fmt.Fprintf(buf, imagePath, depth, t.Format(imageFormat), row, col) - - // Get the image from the url. - res, err := http.Get(buf.String()) - if err != nil { - return nil, err - } - defer res.Body.Close() - - // Create an image from the response. - img, _, err := image.Decode(res.Body) - return img, err -} diff --git a/main.go b/main.go index 96e610e..e5cc9b0 100644 --- a/main.go +++ b/main.go @@ -2,13 +2,11 @@ package main import ( "flag" - "image" "log" - "sync" "time" "github.com/avinashbot/himawari/background" - "github.com/avinashbot/himawari/himawari" + "github.com/avinashbot/himawari/download" ) const ( @@ -17,85 +15,66 @@ const ( ) var ( - depth int - every int64 - once bool - dsc bool + satellite string + depth int + every time.Duration ) func init() { - flag.IntVar(&depth, "depth", 4, "Resolution of the image. One of 4, 8, 16, 20.") - flag.Int64Var(&every, "every", 600, "Re-run every x seconds.") - flag.BoolVar(&once, "once", false, "Set the background and exit.") - flag.BoolVar(&dsc, "dscovr", false, "Use DSCOVR imagery. It's not geostationary though.") -} - -func run(t *time.Time) error { - // Make the image grid. - m := make([][]image.Image, depth) - for i := range m { - m[i] = make([]image.Image, depth) - } - - // Download images in parallel. Woo go! - log.Println("Starting download...") - startTime := time.Now() - var wg sync.WaitGroup - var err error - for i := 0; i < depth; i++ { - for j := 0; j < depth; j++ { - wg.Add(1) - go func(i, j int) { - defer wg.Done() - m[i][j], err = himawari.GridAt(t, depth, i, j) - }(i, j) - } - } - wg.Wait() - log.Printf("Done! Downloading images took %s.\n", time.Now().Sub(startTime)) - - // Join the pieces and set the background image. - // A depth=20 crashed my VM around this part, so watch out. - var img image.Image - img = background.Join(m, gridSize*depth, gridSize*depth) - img = background.Expand(img, 16/9) // FIXME - - log.Println("Setting image as background...") - return background.Set(img) + flag.StringVar(&satellite, "satellite", "himawari", `The satellite to use: "himawari" or "dscovr".`) + flag.IntVar(&depth, "depth", 4, "Resolution of the Himawari image. One of 4, 8, 16, 20.") + flag.DurationVar(&every, "every", 0, "Time to wait between each rerun.") } func main() { flag.Parse() - // Start off with a dummy time. - t := time.Unix(0, 0) + // Set the satellite. + var dl download.Downloader + switch satellite { + case "himawari": + dl = download.Himawari{Depth: depth} + case "dscovr": + dl = download.Dscovr{} + default: + log.Fatalln("Satellite not recognized. Exiting.") + } - // Run the program. - for ticker := time.NewTicker(time.Duration(every) * time.Second); ; <-ticker.C { - // Get the latest timestamp. - newt, err := himawari.Latest() + // Start off with a zero time. + for lastTime := (time.Time{}); ; time.Sleep(every) { + // Get the filename to the latest image. + filename, err := dl.ModifiedSince(lastTime) if err != nil { - continue // The update server threw an error. Try later. + log.Println(err) } - log.Printf("The latest image is at %s.\n", newt) - - // Skip if the latest time hasn't changed. - if newt.Equal(t) { - log.Println("The image has not changed. Waiting...") + if filename == "" { + log.Println("No changes since last time. Trying again later...") continue } - // If it has, run it. - if err = run(newt); err != nil { + // There is new image out. Download it. + log.Println("Starting download...") + benchmarkTime := time.Now() + img, err := dl.Download(filename) + if err != nil { log.Println(err) continue } + log.Printf("Done! Download took %s.\n", time.Now().Sub(benchmarkTime)) + + // Set the image as the background. + // This one's a serious error, so break if it happens. + log.Println("Setting image as background...") + if err := background.Set(img); err != nil { + log.Println(err) + break + } - // Everything has succeeded, set the time to the latest image time. - t = *newt + // Success. Replace lastTime with the current time. + lastTime = time.Now() - // If we're only running this once, exit. - if once { + // If we're only doing this once, quit. + if every == 0 { break } }