Tracer des courbes de propagation en python

Projet:

Récupérer des infos généré par CW Skimmer par exemple et en faire des courbes.

Voici le script version 1:

#!/usr/bin/python
import sys
import datetime, time
import numpy as np
from scipy.interpolate import spline
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
from itertools import groupby
import matplotlib.dates as mdates
from optparse import OptionParser
import pandas as pd


def Cwplot( fQRZ,finputfile,foutputfile,timeto,comment ):

		if timeto == 0:
			timeto=86400
			
		with open(finputfile) as f:
			data = f.read().splitlines()

		x=[]
		y=[]
		for row in data:
			if fQRZ in row:
				if (((datetime.datetime.utcnow() - datetime.datetime.strptime(row.split('   ')[0], '%d/%m/%Y %H:%M')).total_seconds())  <= timeto):
					x.append(float(datetime.datetime.strptime(row.split('   ')[0], '%d/%m/%Y %H:%M').strftime('%s')))
					y.append(float(row.split('   ')[3].split(' ')[0]))

		x_sm = np.array(x)
		y_sm = np.array(y)
		
		x_smooth = np.linspace(x_sm.min(), x_sm.max(), 100)
		
		y_smooth = spline(x, y, x_smooth)
		
		for index, item in enumerate(x):
			if 0 <= int(index + 1) < len(x):
				if ((x[int(index + 1)] - x[index]) > 50000):
					for indexb, itemb in enumerate(x_smooth):
						if((itemb > x[index]) and (itemb < x[int(index + 1)])):
							y_smooth[indexb]=0
							

		
		c_reg = np.polyfit(x_smooth, y_smooth, 20)     
		y_reg=np.polyval(c_reg,x_smooth)
		
		xdate=[]
		for row in x_smooth:
			xdate.append(datetime.datetime.fromtimestamp(row))
		
		xdatev=[]
		for row in x:
			xdatev.append(datetime.datetime.fromtimestamp(row))

		
		fig = plt.figure(figsize=(8.49, 6.09), dpi=100)
		fig.subplots_adjust(top=0.90,bottom=0.18)  #bottom=0.9, left=0.09, right=0.10)
		ax = fig.add_subplot(111)
		ax.set_title(fQRZ + " " + str(comment) + " updated @ " + datetime.datetime.utcnow().strftime('%d/%m/%Y %H:%M') )    
		ax.set_xlabel('Date and Time (GMT)')
		ax.set_ylabel('SNR (dB)')
		
		ax.plot(xdate,y_smooth, c='r', label='measures')
		ax.plot(xdatev, y, 'o', label='points')
		ax.plot(xdate,y_reg, c='b', label='regression')
		
		if ((min(float(s) for s in y_smooth)) < 0): 
			ax.set_ylim([0,y_smooth.max()])
		
		
		for item in ([ax.xaxis.label, ax.yaxis.label] +
			ax.get_xticklabels() + ax.get_yticklabels()):
			item.set_fontsize(8)
		
		ax.title.set_fontsize(14)
		
		labels = ax.get_xticklabels()
		plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%Y %H:%M'))
		
		plt.setp(labels, rotation=30, fontsize=8)
		ax.legend(loc='lower left', shadow=True, fontsize=8)

		fig.savefig(foutputfile, dpi=100,format='PNG', bits=2)   # save the figure to file
		plt.close(fig)    # close the figure


parser = OptionParser()
parser.remove_option("-h")
parser.add_option("-v", "--help", action="help")
parser.add_option("-q", "--qrz", dest="qrz", help ="QRZ")
parser.add_option("-o", "--output", dest="out", help ="output folder (write / at the end)") 
parser.add_option("-i", "--input", dest="inp", help ="log file input")
parser.add_option("-s", "--seconds", dest="seconds", help ="number of days")
parser.add_option("-m", "--minutes", dest="minutes", help ="number of days")
parser.add_option("-h", "--hours", dest="hours", help ="number of hours")
parser.add_option("-d", "--days", dest="days", help ="number of days")
parser.add_option("-x", "--say", dest="say", help ="show informations")
parser.add_option("-c", "--comment", dest="com", help ="show informations")

(options, args) = parser.parse_args()

vartime = int(0)
if options.seconds is not None:
	vartime = vartime + int(options.seconds)
if options.minutes is not None:
	vartime = vartime + int(options.minutes) * 60
if options.hours is not None:
	vartime = vartime + int(options.hours) * 60 * 60
if options.days is not None:
	vartime = vartime + int(options.days) * 24 * 60 * 60


	
if options.inp is not None and options.out is not None :	
	if options.qrz is not None:
		if options.out.endswith('/'):
			options.out = options.out + options.qrz + '.png'
		if options.out.endswith('png'):
			Cwplot(options.qrz, options.inp, options.out,vartime,options.com)
			
	else:
		if options.out.endswith('/'):
			with open(options.inp) as f:
				data = f.read().splitlines()
				
			listeQRZF=[]
			listeQRZ=[]
			for row in data:
					listeQRZF.append(row.split('   ')[2])
					
			listeQRZ=list(set(listeQRZF))
					
			for row in listeQRZ:
				if options.say is not None:
					print row + " " + str(listeQRZF.count(row))
					print 'duration ' + str(vartime) + ' ' +str(type(vartime))
				if listeQRZF.count(row) >= 10: 
					Cwplot( row,options.inp, options.out+row+".png",vartime, "")	

			
			
			

 

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.