Skip to content
Snippets Groups Projects
Commit 04df28a7 authored by Demo Milab's avatar Demo Milab
Browse files

ejercicio datos taller

parent cfdaf215
No related branches found
No related tags found
No related merge requests found
data/
.ipynb_checkpoints/
src/
amplitude.png

30.6 KiB

%% Cell type:markdown id:a4d202eb-9d92-4857-b2af-aba670f9090b tags:
# Análisis de Datos Racimo Tormenta
%% Cell type:markdown id:52bc4c5f-1baf-4e8f-8a1d-0b0e51fa8695 tags:
Notebook para el análisis de datos del proyecto racimo tormenta
%% Cell type:markdown id:d277a53c-cccc-4996-8944-620130575372 tags:
## Librerias
%% Cell type:markdown id:e96a3270-365f-4f90-9d5a-d2673f176f11 tags:
Importar las librerias necesarias para el análisis e interacciones de los datos
%% Cell type:code id:5d4d6478-6b92-46a4-a849-7fc4d5a3b119 tags:
``` python
import numpy as np
import matplotlib.pylab as plt
import scipy
from scipy import stats
from scipy.fftpack import fftfreq, irfft, rfft
import sys
import os
from matplotlib import cm
from matplotlib.colors import ListedColormap, LinearSegmentedColormap
import math
import datetime as datetime
import time
import matplotlib.dates as md
%matplotlib inline
sys.getdefaultencoding()
```
%% Cell type:markdown id:97659221-fc8b-4e1a-b6d9-2ef11fd5410c tags:
## Descargar datos
%% Cell type:markdown id:50ba171a-6d09-4d7a-90ff-b76ce313b09d tags:
Ir a https://dataverse.redclara.net/dataverseuser.xhtml?selectTab=apiTokenTab y copiar el **API Token** para definirlo en el siguiente campo
%% Cell type:code id:89723f93-88a1-4bd4-ac98-76a5d4e75c48 tags:
``` python
%env API_TOKEN=42401228-20ba-4de1-a889-6aa8ccd89087
```
%% Cell type:code id:bf02c4eb-bfed-478e-8315-2ca2bfbe6470 tags:
``` python
%env SERVER_URL=https://dataverse.redclara.net
%env PERSISTENT_ID=doi:10.21348/FK2/EIQEXC
%env VERSION=DRAFT
!curl -L -O -J -H "X-Dataverse-key:$API_TOKEN" $SERVER_URL/api/access/dataset/:persistentId/?persistentId=$PERSISTENT_ID
```
%% Cell type:markdown id:52c1b78f-5325-4a21-9933-d25cf8e927ab tags:
Descomprimir archivo de datos
%% Cell type:code id:0c7d3e5e-31fe-42b7-be08-e9c9f793a7da tags:
``` python
!unzip dataverse_files.zip
!rm dataverse_files.zip
!rm MANIFEST.TXT
```
%% Cell type:markdown id:960fdf13-2b85-4fca-a1fb-ed0aa016e468 tags:
## Calibración del detector
%% Cell type:markdown id:3b8b509b-c69e-4c8d-98e2-6f9d7cc825c3 tags:
Calibración de las mediciones del detector
%% Cell type:markdown id:440109c1-3272-4702-9a0c-fd5e2b8e6b1d tags:
### Vista preliminar de los datos de calibración
%% Cell type:markdown id:4ed58223-c091-4de4-b9c7-2cf4b3dd674f tags:
Cargar datos en formato *array numpy*
%% Cell type:code id:78a31578-66f3-4363-80b4-856aabede298 tags:
``` python
data = np.loadtxt('data/Lighting_2021_04_13_00_4.dat', comments='#')
```
%% Cell type:markdown id:7c03fdf2-6a4a-4d30-87e3-3b025a43f6c1 tags:
Describir datos
%% Cell type:code id:6733522b-ab2c-4b49-841a-a45bc4dfd377 tags:
``` python
from scipy import stats
stats.describe(data)
```
%% Cell type:markdown id:c754f55c-22ff-46b9-a414-7fa07a4497d6 tags:
### Amplitud y frecuencia de la señal
%% Cell type:markdown id:badb8819-ef05-4e8c-867c-8b4a03f554d2 tags:
Función para graficar amplitud y frecuencia.
%% Cell type:code id:05f9f0b6-dd57-4e7e-92ca-4718cf85808e tags:
``` python
def Lightning_Analysis(data, dt, Np):
mean = np.mean(data[:,1])
sigma = np.std(data[:,1])
peaks = []
MTFt = [] # Multiple-termination flash (MTF) relative times
MTSt = [] # Multiple.termination stroke (MTS) relative times
MTSv = []
T_count = 0 # Terminations counter
MTFc = 1 # MTF counter
MTSc = 0 # MTS counter
N = len(data)
MTSw = 1e-3 # time window for differentiating MTF and MTS events
threshold = mean + 5*sigma # Peak threshold
# Termination identification
for i in range(N):
if (data[i,1] > threshold):
T_count += 1
peaks.append(i)
t1 = data[i,0]
if T_count > 1:
Td = t1 - t0
if Td > MTSw:
MTFt.append(Td)
MTFc += 1
MTSv.append(MTSc)
MTSc = 0
else:
MTSt.append(Td)
MTSc += 1
t0 = t1
print (u'Terminations above 5\u03C3 = %d\n' %T_count)
print (u'Number of strokes = %d\n' %MTFc)
s = data[:,1]
Y = np.fft.fft(s)
N = len(Y)/2+1
fa = 1.0/dt
X = np.linspace(0, fa/2, int(N), endpoint=True)
sfft = np.abs(Y[:int(N)])
print('Sample Time = %.5f s' % dt)
print('Frequency = %.2f Hz' % fa)
sfft = np.array(sfft)
pos = int(np.where(sfft[1:-1] == np.amax(sfft[1:-1]))[0])
frec_pico = 868.35 # X[pos+1]
print ("Maximum frequency = %.2f Hz" %frec_pico)
if T_count >= Np:
# Signal plot
plt.figure(figsize = (16,4))
plt.subplot(1,2,1)
plt.plot(data[:,0], data[:,1])
plt.axhline(threshold, color='red')
plt.xlabel('Time [s]', fontsize = 20)
plt.ylabel('Amplitude [ADC]', fontsize = 20)
plt.savefig("amplitude.png", dpi=150)
# Spectrum plotting
plt.subplot(1,2,2)
plt.axvline(frec_pico, color='red')
plt.loglog(X, sfft)
plt.xlabel('Frequency [Hz]', fontsize = 20)
plt.axis([1e-1,1e5,1e1,1e7])
plt.grid()
plt.show()
return frec_pico, peaks, MTFt, MTSt, T_count, MTFc, MTSv
```
%% Cell type:markdown id:4da5b0b2-97c4-455b-b9f9-028e817f3fa9 tags:
Graficar
%% Cell type:code id:66982705-0db4-43d7-8bd0-22ec2ae7b907 tags:
``` python
dt = 10e-6 # sampling period
Np = 0. # filter signals per number of peaks above 5 sigma
fp1, peaks1, MTFt, MTSt, pN1, MTFc, MTSc = Lightning_Analysis(data, dt, Np) # Returns maximum peak frequency and peak positions
```
%% Cell type:markdown id:eccbba23-922e-430b-816b-41ca7212cae5 tags:
### Escalar señal y enfocar
%% Cell type:markdown id:c15cc352-a82b-404d-9a47-09860ae25950 tags:
Definir parámetros de escalado y foco
%% Cell type:code id:aaea4b8a-0639-479e-8f66-ad59fb179ac8 tags:
``` python
R1 = 0
R2 = 200
P = 5
```
%% Cell type:markdown id:5a59c5cb-19db-4872-9046-6f0471498da0 tags:
Crear nuevo *array* de datos.
%% Cell type:code id:0d8a87c1-5ab1-4e9f-b2d8-8d10572ebad8 tags:
``` python
newdata = data[R1:R2]*[1,P]
```
%% Cell type:markdown id:6abec110-ecf4-44e0-b88c-3a3a94b8b4aa tags:
Graficar señal escalada
%% Cell type:code id:53d02f88-4245-4b93-a2c4-7da1cb9c4448 tags:
``` python
dt = 10e-6 # sampling period
Np = 0. # filter signals per number of peaks above 5 sigma
fp1, peaks1, MTFt, MTSt, pN1, MTFc, MTSc = Lightning_Analysis(newdata, dt, Np) # Returns maximum peak frequency and peak positions
```
%% Cell type:markdown id:6cd50a1f-6003-4002-a1aa-f98af31ffcab tags:
### Publicar nuevos datos en dataverse
%% Cell type:markdown id:78c305e5-7806-432c-8ba6-f179d1f6d863 tags:
Conectar a dataverse
%% Cell type:code id:c365000c-82c3-4972-aa80-ad7d76801b43 tags:
``` python
from dataverse import Connection
API_TOKEN = os.environ['API_TOKEN']
host = 'dataverse.redclara.net' # All clients >4.0 are supported
# Conexión a repositorio
connection = Connection(host, API_TOKEN)
# Selección de dataverse a user (storm para Racimo Tormenta)
dataverse = connection.get_dataverse('storm')
```
%% Cell type:markdown id:2f189374-3ad4-4bfc-a11a-67f2c8a0e6eb tags:
Guardar *array* en nuevo archivo
%% Cell type:code id:ae9b7fd8-9850-4d7e-ab2e-0397c266416b tags:
``` python
np.savetxt('data/Lighting_2021_04_13_00_4-'+connection.token+'.dat', newdata)
```
%% Cell type:code id:0a1f7583-ad98-4aba-a67e-518af2319572 tags:
``` python
dataset = dataverse.get_dataset_by_title('Lighting_2021_04_13')
```
%% Cell type:code id:71796740-f56e-4dfa-8d8e-4fac88d4a762 tags:
``` python
dataset.upload_filepath('data/Lighting_2021_04_13_00_4-'+connection.token+'.dat')
```
%% Cell type:code id:e1d125e8-8bf1-464b-b012-cc648cc8691d tags:
``` python
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment