71 lines
1.9 KiB
Go
71 lines
1.9 KiB
Go
package rain
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"path"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
"weatherstation/internal/database"
|
|
)
|
|
|
|
var (
|
|
// Matches .../<PRODUCT>/<YYYYMMDD>/<HH>/<mm>/<z>/<y>/<x>.bin
|
|
// Example: /tiles/China/CMPA_RT_China_0P01_HOR-PRE_GISJPG_Tiles/20251007/01/00/7/40/102.bin
|
|
tileRE = regexp.MustCompile(`(?i)/tiles/.+?/([^/]+)/([0-9]{8})/([0-9]{2})/([0-9]{2})/([0-9]+)/([0-9]+)/([0-9]+)\.bin$`)
|
|
)
|
|
|
|
// TileRef references a CMPA rain tile.
|
|
type TileRef struct {
|
|
Product string
|
|
DT time.Time // nominal time in Asia/Shanghai (UTC+8)
|
|
Z, Y, X int
|
|
}
|
|
|
|
// ParseCMPATileURL parses a CMPA tile URL/path and extracts product, time (UTC+8), z/y/x.
|
|
// The timestamp in the path is UTC; we convert to Asia/Shanghai by adding 8h.
|
|
func ParseCMPATileURL(u string) (TileRef, error) {
|
|
p := u
|
|
if i := strings.IndexAny(p, "?#"); i >= 0 {
|
|
p = p[:i]
|
|
}
|
|
p = path.Clean(p)
|
|
m := tileRE.FindStringSubmatch(p)
|
|
if len(m) == 0 {
|
|
return TileRef{}, fmt.Errorf("unrecognized CMPA tile path: %s", u)
|
|
}
|
|
product := m[1]
|
|
yyyymmdd := m[2]
|
|
hh := m[3]
|
|
mm := m[4]
|
|
z := mustAtoi(m[5])
|
|
y := mustAtoi(m[6])
|
|
x := mustAtoi(m[7])
|
|
|
|
// Parse as UTC then shift to CST(+8)
|
|
utcT, err := time.ParseInLocation("20060102 15 04", fmt.Sprintf("%s %s %s", yyyymmdd, hh, mm), time.UTC)
|
|
if err != nil {
|
|
return TileRef{}, fmt.Errorf("parse utc time: %w", err)
|
|
}
|
|
loc, _ := time.LoadLocation("Asia/Shanghai")
|
|
if loc == nil {
|
|
loc = time.FixedZone("CST", 8*3600)
|
|
}
|
|
dt := utcT.In(loc)
|
|
return TileRef{Product: product, DT: dt, Z: z, Y: y, X: x}, nil
|
|
}
|
|
|
|
func mustAtoi(s string) int { n, _ := strconv.Atoi(s); return n }
|
|
|
|
// StoreTileBytes parses the URL, computes metadata and upserts into rain_tiles.
|
|
func StoreTileBytes(ctx context.Context, urlOrPath string, data []byte) error {
|
|
ref, err := ParseCMPATileURL(urlOrPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
db := database.GetDB()
|
|
return database.UpsertRainTile(ctx, db, ref.Product, ref.DT, ref.Z, ref.Y, ref.X, 256, 256, data)
|
|
}
|