package radarfetch import ( "encoding/json" "fmt" "os" "path/filepath" ) type Bounds struct { West float64 `json:"west"` South float64 `json:"south"` East float64 `json:"east"` North float64 `json:"north"` } type Sources struct { NmcHTML string `json:"nmc_html"` NmcImg string `json:"nmc_img"` CmaBin string `json:"cma_bin"` } type Files struct { HTML string `json:"html"` PNG string `json:"png"` BIN string `json:"bin"` Metadata string `json:"metadata"` CMAPNG string `json:"cma_png"` } type Sizes struct { PNG int64 `json:"png"` BIN int64 `json:"bin"` } type Metadata struct { TimestampLocal string `json:"timestamp_local"` Date int `json:"date"` Hour int `json:"hour"` Minute int `json:"minute"` Z int `json:"z"` Y int `json:"y"` X int `json:"x"` Bounds Bounds `json:"bounds"` ResDeg float64 `json:"res_deg"` Sources Sources `json:"sources"` Files Files `json:"files"` Sizes Sizes `json:"sizes"` CreatedAt string `json:"created_at"` } func WriteMetadata(path string, m *Metadata) error { b, err := json.MarshalIndent(m, "", " ") if err != nil { return err } return os.WriteFile(path, b, 0o644) } func UpdateLatest(root string, curDir string, m *Metadata) error { latest := filepath.Join(root, "latest") if err := os.MkdirAll(latest, 0o755); err != nil { return err } // Write latest.json b, _ := json.MarshalIndent(struct { Dir string `json:"dir"` Meta *Metadata `json:"meta"` }{Dir: curDir, Meta: m}, "", " ") _ = os.WriteFile(filepath.Join(latest, "latest.json"), b, 0o644) copyFile := func(name string) { dst := filepath.Join(latest, name) src := filepath.Join(curDir, name) data, e2 := os.ReadFile(src) if e2 == nil { _ = os.WriteFile(dst, data, 0o644) } } copyFile("nmc_chinaall.png") copyFile("nmc_huanan.png") copyFile("nmc_nanning.png") copyFile("metadata.json") copyFile(fmt.Sprintf("%d-%d-%d.bin", m.Z, m.Y, m.X)) if m.Files.CMAPNG != "" { copyFile(filepath.Base(m.Files.CMAPNG)) } return nil }