From 5b234aa7badf65ce1ddc57b12625a12317d10eca Mon Sep 17 00:00:00 2001 From: mattbk Date: Mon, 23 Mar 2026 21:41:11 -0500 Subject: [PATCH] Process state data from fccULSloader. --- bin/fcc-query.R | 103 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 90 insertions(+), 13 deletions(-) diff --git a/bin/fcc-query.R b/bin/fcc-query.R index fcd6486..d400c1e 100644 --- a/bin/fcc-query.R +++ b/bin/fcc-query.R @@ -8,27 +8,104 @@ library(tidyr) library(janitor) library(USAboundaries) +# Update local database +system("cd python3 fccULSloader/src/fcc_tool.py --update") + +# Get current date +now <- Sys.Date() + con <- dbConnect(SQLite(), "fccULSloader/src/data/fcc_data.db") am <- tbl(con, "AM") en <- tbl(con, "EN") -a <- dbGetQuery(con, "SELECT COUNT(*) FROM EN LEFT JOIN HD ON +a_raw <- dbGetQuery(con, "SELECT COUNT(*) FROM EN LEFT JOIN HD ON EN.unique_system_identifier = HD.unique_system_identifier - WHERE license_status = 'A';") #%>% -#janitor::clean_names() %>% -#head(2) -#filter(status_code == "A") %>% -#summarize(count = n()) #%>% -#collect() + WHERE license_status = 'A';") -print(a) +total_active <- a_raw %>% rename(count = `COUNT(*)`) %>% + mutate(state_name = "TOTAL") -b <- dbGetQuery(con, "SELECT UPPER(state), COUNT(*) FROM EN LEFT JOIN HD ON +print(total_active) + +# Query active licenes by state +b_raw <- dbGetQuery(con, "SELECT UPPER(state), COUNT(*) FROM EN LEFT JOIN HD ON EN.unique_system_identifier = HD.unique_system_identifier WHERE license_status = 'A' - GROUP BY UPPER(state);") %>% - left_join(state_codes %>% filter(state_abbr != ""), - by = c("UPPER(state)" = "state_abbr")) + GROUP BY UPPER(state);") + +active_by_state <- b_raw %>% + left_join(state_codes %>% filter(state_abbr != ""), + by = c("UPPER(state)" = "state_abbr")) %>% + rename(abbrev = `UPPER(state)`, + count = `COUNT(*)`) %>% + mutate(state_name = case_when(abbrev == "AA" ~ "Armed Forces America", + abbrev == "AE" ~ "Armed Forces Europe", + abbrev == "AP" ~ "Armed Forces Pacific", + abbrev == "VI" ~ "Virgin Islands", + abbrev == "UM" ~ "US Minor Islands", + abbrev == "" ~ "Other*", + TRUE ~ state_name)) %>% + bind_rows(total_active) + +print(active_by_state) + +# Query active licenes by state, by class +d_raw <- dbGetQuery(con, "SELECT UPPER(state), operator_class, COUNT(*) FROM EN LEFT JOIN HD ON + EN.unique_system_identifier = HD.unique_system_identifier LEFT JOIN AM ON + EN.unique_system_identifier = AM.unique_system_identifier + WHERE license_status = 'A' + GROUP BY UPPER(state), operator_class;") + +active_state_class <- d_raw %>% + left_join(state_codes %>% filter(state_abbr != ""), + by = c("UPPER(state)" = "state_abbr")) %>% + rename(abbrev = `UPPER(state)`, + count = `COUNT(*)`) %>% + mutate(state_name = case_when(abbrev == "AA" ~ "Armed Forces America", + abbrev == "AE" ~ "Armed Forces Europe", + abbrev == "AP" ~ "Armed Forces Pacific", + abbrev == "VI" ~ "Virgin Islands", + abbrev == "UM" ~ "US Minor Islands", + abbrev == "" ~ "Other*", + TRUE ~ state_name)) %>% + #bind_rows(total_active) %>% + select(-state_code, -abbrev, -jurisdiction_type) %>% + mutate(operator_class = case_when(operator_class == "A" ~ "Advanced", + operator_class == "E" ~ "Extra", + operator_class == "G" ~ "General", + operator_class == "N" ~ "Novice", + operator_class %in% c("T","P") ~ "Tech")) %>% + pivot_wider(names_from = operator_class, + values_from = count) %>% + mutate(Date = now) %>% + rename("State/Territory" = state_name) %>% + select(Date, `State/Territory`, Novice, Tech, General, Advanced, Extra, `NA`) %>% + mutate("Tech-Plus" = NA, + .before = General) %>% + rowwise() %>% mutate("Total" = sum(c_across(Novice:`NA`), + na.rm = T)) %>% + select(-`NA`) %>% + group_by(Date) %>% + bind_rows(summarise(., across(where(is.numeric), sum, na.rm = T), + across(where(is.character), ~'TOTAL'))) %>% + mutate(Tech_and_TechPlus = NA, + Conditional = NA, + Club = NA, + Military = NA, + Multiple = NA, + Repeater = NA, + GMRS = NA, + source_name = "fccULSloader", + source_detail = "w1cdn.net" +) + +# Append to table +write.table(active_state_class, file = "out/fccULSloader-fcc-licenses-scraped.csv", sep = ",", + append = TRUE, quote = FALSE, + col.names = F, row.names = FALSE, + na = "") + + + -print(b)