iniciando

This commit is contained in:
2024-07-25 17:24:24 -03:00
commit dfea13c6c2
467 changed files with 13840436 additions and 0 deletions
Executable
+1
View File
@@ -0,0 +1 @@
__pycache__
+256
View File
@@ -0,0 +1,256 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "3baa2ead-4ec5-46a4-a098-faffc5f77d3e",
"metadata": {},
"outputs": [],
"source": [
"#bibliotecas\n",
"#!pip install pandas numpy\n",
"\n",
"import pandas as pd\n",
"import numpy as np\n",
"import os"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a7d16f5b-6486-4f88-bc7a-8586483ba363",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>CODIGO</th>\n",
" <th>DATA</th>\n",
" <th>VALOR</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1742017</td>\n",
" <td>1980-01-01</td>\n",
" <td>1.6</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1742017</td>\n",
" <td>1980-01-02</td>\n",
" <td>10.6</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" CODIGO DATA VALOR\n",
"0 1742017 1980-01-01 1.6\n",
"1 1742017 1980-01-02 10.6"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Carregar o DataFrame e pega a estacao mais proxima\n",
"\n",
"dados_df = pd.read_csv(\"leitura.csv\", sep=';', decimal=',', parse_dates=['DATA'], dayfirst=True)\n",
"dados_df.head(2)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "8e7c01f0-b13c-4552-96f6-0906abd07280",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>CODIGO</th>\n",
" <th>LATITUDE</th>\n",
" <th>LONGITUDE</th>\n",
" <th>NOME</th>\n",
" <th>1proxima</th>\n",
" <th>2proxima</th>\n",
" <th>3proxima</th>\n",
" <th>1codigo</th>\n",
" <th>2codigo</th>\n",
" <th>3codigo</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1742017</td>\n",
" <td>-17.845600</td>\n",
" <td>-42.075600</td>\n",
" <td>MALACACHETA</td>\n",
" <td>ÁGUA BOA</td>\n",
" <td>SANTA MARIA DO SUAÇUÍ</td>\n",
" <td>SÃO JOÃO EVANGELISTA</td>\n",
" <td>1742019</td>\n",
" <td>1842008</td>\n",
" <td>1842020</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1742019</td>\n",
" <td>-17.992201</td>\n",
" <td>-42.393902</td>\n",
" <td>ÁGUA BOA</td>\n",
" <td>SANTA MARIA DO SUAÇUÍ</td>\n",
" <td>MALACACHETA</td>\n",
" <td>SÃO JOÃO EVANGELISTA</td>\n",
" <td>1842008</td>\n",
" <td>1742017</td>\n",
" <td>1842020</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" CODIGO LATITUDE LONGITUDE NOME 1proxima \\\n",
"0 1742017 -17.845600 -42.075600 MALACACHETA ÁGUA BOA \n",
"1 1742019 -17.992201 -42.393902 ÁGUA BOA SANTA MARIA DO SUAÇUÍ \n",
"\n",
" 2proxima 3proxima 1codigo 2codigo 3codigo \n",
"0 SANTA MARIA DO SUAÇUÍ SÃO JOÃO EVANGELISTA 1742019 1842008 1842020 \n",
"1 MALACACHETA SÃO JOÃO EVANGELISTA 1842008 1742017 1842020 "
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"\n",
"# Converter mapeamento para DataFrame\n",
"estacao_df = pd.read_csv(\"estacao_mais_proxima.csv\", encoding='utf-8', sep=';', decimal=',')\n",
"estacao_df.head(2)\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "fb6a0bc3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dados atualizados:\n"
]
}
],
"source": [
"# Copiar dados_df para uma variável para atualização\n",
"AtualizaDados = dados_df.copy()\n",
"\n",
"# Caminho do arquivo de log no diretório atual\n",
"log_file_path = os.path.join(os.getcwd(), 'atualizacao_dados.txt')\n",
"\n",
"# Abrir o arquivo de log para escrita\n",
"with open(log_file_path, 'w') as log_file:\n",
" # Iterar sobre AtualizaDados para atualizar os valores\n",
" for index, row in AtualizaDados.iterrows():\n",
" valor = row['VALOR']\n",
" data = row['DATA']\n",
" if valor <= -1:\n",
" log_file.write(f\"Código: {row['CODIGO']}, Data: {row['DATA']}, Valor: {valor}\\n\")\n",
"\n",
" codigo_filtrado = row['CODIGO']\n",
" linhas_filtradas = estacao_df.loc[estacao_df['CODIGO'] == codigo_filtrado]\n",
" \n",
" # Iterar sobre os códigos próximos (1codigo, 2codigo, 3codigo)\n",
" for i in range(1, 4): # Considerando as três primeiras colunas de códigos\n",
" codigo_proximo = linhas_filtradas[f'{i}codigo'].iloc[0]\n",
" filtro = (dados_df['CODIGO'] == codigo_proximo) & (dados_df['DATA'] == data)\n",
" valor_proximo = dados_df.loc[filtro, 'VALOR'].values\n",
" \n",
" log_file.write(f\"{i}codigo: {codigo_proximo} Valor: {valor_proximo}\\n\")\n",
" \n",
" # Verificar se o valor encontrado não é menor ou igual a -1\n",
" if len(valor_proximo) > 0 and valor_proximo[0] > -1:\n",
" AtualizaDados.loc[index, 'VALOR'] = valor_proximo[0]\n",
"\n",
" log_file.write(f\"Valor atual {row['VALOR']} - Valor atualizado para: {valor_proximo[0]}\\n\")\n",
" break # Parar o loop se encontrar um valor válido\n",
" \n",
" log_file.write(\"---------------------------------------------------\\n\")\n",
"\n",
"# Exibir mensagem após completar o processo\n",
"print(\"Dados atualizados:\")\n",
"AtualizaDados.to_csv(\"dados_atualizados_mais_proximo.csv\", sep=\";\", encoding='utf-8', decimal=',', index=False)\n",
"#print(AtualizaDados)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
+38
View File
@@ -0,0 +1,38 @@
import carregar
import processar
def mostrar_menu():
print("Menu:")
print("1 - Carregar arquivo")
print("3 - Deletar csv gerando no menu 1")
print("11 - Estacao mais proxima")
print("20 - completar valores com -1")
print("0 - Sair")
def main():
while True:
mostrar_menu()
opcao = input("Escolha uma opção: ")
if opcao == "1":
carregar.carregar_arquivo()
elif opcao == "3":
carregar.deletar_arquivo_gerado()
elif opcao == "11":
carregar.carregar_arquivoxls_mais_proxima()
elif opcao == "20":
processar.processar_arquivos()
elif opcao == "0":
print("Saindo...")
break
else:
print("Opção inválida, tente novamente.")
if __name__ == "__main__":
main()
+177
View File
@@ -0,0 +1,177 @@
import os
import pandas as pd
import numpy as np
from time import sleep
def deletar_arquivo_gerado():
print("deltando arquivo...")
diretorio = 'data' # nome da pasta principal
pastas = listar_pastas(diretorio)
for pasta in pastas:
print(f"Encontrada pasta: {pasta}")
output_csv = f"./{pasta}.csv"
deletar_arquivo(output_csv)
def deletar_arquivo(caminho_arquivo):
try:
os.remove(caminho_arquivo)
print(f"Arquivo {caminho_arquivo} deletado com sucesso.")
except FileNotFoundError:
print(f"O arquivo {caminho_arquivo} não foi encontrado.")
except PermissionError:
print(f"Sem permissão para deletar o arquivo {caminho_arquivo}.")
except Exception as e:
print(f"Ocorreu um erro ao tentar deletar o arquivo {caminho_arquivo}: {e}")
def carregar_arquivo():
print("Carregando arquivo...")
diretorio = 'data' # nome da pasta principal
pastas = listar_pastas(diretorio)
for pasta in pastas:
print(f"Encontrada pasta: {pasta}")
# Chamar a função
diretorio = f"./data/{pasta}/dados"
print(diretorio)
output_csv = f"./dados_{pasta}.csv"
processar_arquivos_txt(diretorio, output_csv)
def listar_pastas(diretorio):
pastas = [nome for nome in os.listdir(diretorio) if os.path.isdir(os.path.join(diretorio, nome))]
return pastas
def carregar_arquivoxls_mais_proxima():
print("Carregando arquivo...")
diretorio = 'data' # nome da pasta principal
pastas = listar_pastas(diretorio)
for pasta in pastas:
print(f"Encontrada pasta: {pasta}")
diretorio = f"./data/{pasta}/{pasta}.xls"
output_csv = f"./proxima_{pasta}.csv"
processar_arquivos_xls(diretorio, output_csv)
print(output_csv)
sleep(3)
processar_arquivos_xls2(output_csv)
# Função para calcular a distância euclidiana
def calcular_distancia(lat1, lon1, lat2, lon2):
return np.sqrt((lat1 - lat2) ** 2 + (lon1 - lon2) ** 2)
def processar_arquivos_xls(arquivo, output_csv):
db = pd.read_excel(arquivo, decimal='.')
db.columns = db.columns.str.strip()
df = pd.DataFrame()
# Verificar a existência das colunas antes de acessar
if 'CODIGO' in db.columns and 'LATITUDE' in db.columns and 'LONGITUDE' in db.columns and 'NOME' in db.columns:
df['CODIGO'] = db['CODIGO']
df['LATITUDE'] = db['LATITUDE']
df['LONGITUDE'] = db['LONGITUDE']
df['NOME'] = db['NOME']
else:
print("Uma ou mais colunas esperadas não estão presentes no DataFrame.")
df.to_csv(output_csv, sep=";", encoding='utf-8', decimal=',', index=False)
def processar_arquivos_xls2(output_csv):
df = pd.read_csv(output_csv, encoding='utf-8', sep=';', decimal=',')
df['1p'] = ''
df['2p'] = ''
df['3p'] = ''
df['4p'] = ''
df['5p'] = ''
df['1c'] = ''
df['2c'] = ''
df['3c'] = ''
df['4c'] = ''
df['5c'] = ''
df['LATITUDE'] = pd.to_numeric(df['LATITUDE'], errors='coerce')
df['LONGITUDE'] = pd.to_numeric(df['LONGITUDE'], errors='coerce')
# Iterar sobre cada linha para encontrar as três estações mais próximas
for i, row in df.iterrows():
lat1, lon1 = row['LATITUDE'], row['LONGITUDE']
distancias = df.apply(lambda x: calcular_distancia(lat1, lon1, x['LATITUDE'], x['LONGITUDE']), axis=1)
distancias[i] = np.inf # Excluir a própria estação
proximas = distancias.nsmallest(5).index # Pegar os índices das três menores distâncias************
df.at[i, '1p'] = df.at[proximas[0], 'NOME']
df.at[i, '2p'] = df.at[proximas[1], 'NOME']
df.at[i, '3p'] = df.at[proximas[2], 'NOME']
df.at[i, '4p'] = df.at[proximas[3], 'NOME']
df.at[i, '5p'] = df.at[proximas[4], 'NOME']
df.at[i, '1c'] = df.at[proximas[0], 'CODIGO']
df.at[i, '2c'] = df.at[proximas[1], 'CODIGO']
df.at[i, '3c'] = df.at[proximas[2], 'CODIGO']
df.at[i, '4c'] = df.at[proximas[3], 'CODIGO']
df.at[i, '5c'] = df.at[proximas[4], 'CODIGO']
df.to_csv(output_csv, sep=";", encoding='utf-8', decimal=',', index=False)
def processar_arquivos_txt(diretorio, output_csv):
# Lista para armazenar os DataFrames
lista_dfs = []
# Percorrer todos os arquivos no diretório
for arquivo in os.listdir(diretorio):
if arquivo.endswith(".txt"):
# Caminho completo do arquivo
caminho_arquivo = os.path.join(diretorio, arquivo)
# Nome do código a partir do nome do arquivo (sem a extensão .txt)
codigo = arquivo.split('.')[0]
print(f"Processando arquivo: {codigo}")
# Leitura do arquivo txt
df = pd.read_csv(caminho_arquivo, delim_whitespace=True, header=None)
# Renomear colunas
df.columns = ['Dia', 'Mes', 'Ano', 'Valor']
# Criar coluna de data a partir das colunas Dia, Mes, Ano
df['Data'] = pd.to_datetime(df['Ano'].astype(str) + '-' + df['Mes'].astype(str) + '-' + df['Dia'].astype(str), errors='coerce')
df = df.drop(columns=['Dia', 'Mes', 'Ano'])
# Selecionar colunas necessárias e adicionar a coluna Código
df_final = df[['Data', 'Valor']].copy()
df_final['Codigo'] = codigo
# Reordenar as colunas
df_final = df_final[['Codigo', 'Data', 'Valor']]
# Adicionar o DataFrame processado à lista
lista_dfs.append(df_final)
# Concatenar todos os DataFrames em um único DataFrame
df_resultado = pd.concat(lista_dfs, ignore_index=True)
# Salvar o DataFrame em um arquivo CSV
df_resultado.columns = df_resultado.columns.str.upper()
df_resultado.to_csv(output_csv, index=False, sep=";", decimal=",")
print(f"Dados salvos em {output_csv}")
File diff suppressed because it is too large Load Diff
+957335
View File
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,36 @@
2042000
2042014
2142000
2142001
2142002
2142004
2142006
2142007
2142008
2142009
2142014
2142077
2143000
2143001
2143003
2143011
2143013
2143016
2143017
2143018
2143019
2143020
2143021
2143022
2143023
2143062
2143070
2242028
2243008
2243202
2244035
2244036
2244038
2244039
2244047
2244058
Binary file not shown.
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+60
View File
@@ -0,0 +1,60 @@
1742017
1742019
1841001
1841003
1841011
1841019
1841020
1842004
1842005
1842007
1842008
1842020
1842026
1843011
1843012
1941000
1941004
1941005
1941006
1941011
1941018
1941019
1941021
1942002
1942006
1942008
1942029
1942030
1942031
1942032
1942048
1942058
1942075
1943001
1943002
1943003
1943007
1943008
1943025
1943027
1943100
2041008
2041048
2042008
2042010
2042011
2042016
2042017
2042018
2042031
2042040
2043009
2043010
2043011
2043014
2043025
2043026
2043027
2043059
2143003
Binary file not shown.
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff

Some files were not shown because too many files have changed in this diff Show More