From ae064155874b20b20748be3250e3320353c3c52c Mon Sep 17 00:00:00 2001 From: Raffaele Di Stefano Date: Wed, 23 Sep 2026 18:09:24 +0200 Subject: [PATCH 01/12] A comment changed --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a23d795..372da94 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ # pyml [![Version](https://img.shields.io/badge/dynamic/yaml?label=ver&query=softwareVersion&url=https://raw.githubusercontent.com/INGV/pyml/main/publiccode.yml)](https://github.com/INGV/pyml/blob/main/publiccode.yml) | [![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/ingv/pyml/docker-image.yml?branch=main&style=flat&logo=GitHub-Actions&logoColor=white&logoSize=auto&label=GitHub%20Actions)](https://github.com/INGV/pyml/actions) ## Introduction -Python code, complementary to pyamp, to calculate ML with different methods and attenuation functions using pyamp_amplitude output file or database table. +Python code, complementary to pyamp or REI, to calculate ML with different methods and attenuation functions. ## Quickstart ### Docker image From 1d0d4c4abe98dc8230e15a13e809ccc8fc5a63c3 Mon Sep 17 00:00:00 2001 From: Raffaele Di Stefano Date: Wed, 23 Sep 2026 18:09:57 +0200 Subject: [PATCH 02/12] First two big enhancement for efficiency eliminating the for loop on iterrows --- pyml.py | 324 ++++++++++++++++++++++++-------------------------------- 1 file changed, 139 insertions(+), 185 deletions(-) diff --git a/pyml.py b/pyml.py index 6e64194..ba64858 100644 --- a/pyml.py +++ b/pyml.py @@ -65,6 +65,10 @@ from math import sqrt as msqrt, log10 as mlog10, pow as mpow, log2 as mlog2 +import numpy as np +import pandas as pd +from obspy.geodetics.base import gps2dist_azimuth + class MyParser(argparse.ArgumentParser): def error(self, message): sys.stderr.write('error: %s\n' % message) @@ -107,6 +111,137 @@ def get_config_dictionary(cfg, section): dict1[option] = None return dict1 + +################################################################### + +def process_amplitudes_vectorized(dfa, origin, unit, max_lowcorner, delta_corner): + """ + Sostituisce il blocco dfa.iterrows(). + Pulisce i dati e calcola le distanze in logica vettoriale (NumPy/Pandas). + """ + # 1. Uniformiamo i nomi e riempiamo i valori nulli in modo vettoriale + # Sfruttiamo le potenzialità di Pandas per gestire i missing values senza try/except continui + dfa['net'] = dfa.get('net', pd.Series(dtype=object)).fillna(False) + dfa['sta'] = dfa.get('sta', pd.Series(dtype=object)).fillna(False) + dfa['loc'] = dfa.get('loc', pd.Series(dtype=object)).fillna('--') + dfa['cha'] = dfa.get('cha', pd.Series(dtype=object)).fillna(False) + + # 2. Estrazione Ampiezze (Gestione flessibile delle colonne 'minamp(m)' vs 'amp1') + minamp_col = 'minamp(m)' if 'minamp(m)' in dfa.columns else 'amp1' + maxamp_col = 'maxamp(m)' if 'maxamp(m)' in dfa.columns else 'amp2' + time_min_col = 'minamptime' if 'minamptime' in dfa.columns else 'time1' + time_max_col = 'maxamptime' if 'maxamptime' in dfa.columns else 'time2' + + # Forziamo il tipo a float64 per la massima precisione matematica + dfa['minamp'] = dfa[minamp_col].astype(np.float64) * unit + dfa['maxamp'] = dfa[maxamp_col].astype(np.float64) * unit + dfa['amp_calc'] = np.abs(dfa['maxamp'] - dfa['minamp']) / 2.0 # Vettoriale + + dfa['time_minamp'] = dfa[time_min_col] + dfa['time_maxamp'] = dfa[time_max_col] + + # 3. Filtro rapido delle stazioni valide prima dei calcoli pesanti geodetici + # Manteniamo solo le righe dove net, sta, cha e coordinate esistono + valid_mask = ( + (dfa['net'] != False) & (dfa['sta'] != False) & (dfa['cha'] != False) & + dfa['lat'].notna() & dfa['lon'].notna() + ) + dfa_valid = dfa[valid_mask].copy() + + # 4. Calcolo Distanze Vettorizzato + evla = float(origin['lat']) + evlo = float(origin['lon']) + evdp = float(origin['depth']) + + # Definiamo un wrapper vettorizzato per la funzione Obspy + def calc_dist_az(stla, stlo): + distmeters, azi, _ = gps2dist_azimuth(stla, stlo, evla, evlo) + return distmeters / 1000.0, azi + + vec_dist_az = np.vectorize(calc_dist_az, otypes=[np.float64, np.float64]) + + # Applichiamo il calcolo solo sulle coordinate valide in un solo colpo (Fast mapping) + dfa_valid['epi_dist_km'], dfa_valid['epi_azimuth'] = vec_dist_az( + dfa_valid['lat'].values, + dfa_valid['lon'].values + ) + + # Calcolo distanza ipocentrale usando NumPy strict math (algebra lineare su array) + # Rimpiazza la lentezza di msqrt e mpow + dfa_valid['stel_km'] = dfa_valid['elev'].astype(np.float64) / 1000.0 + dfa_valid['hypo_dist'] = np.sqrt( + np.square(dfa_valid['epi_dist_km']) + np.square(evdp + dfa_valid['stel_km']) + ) + + return dfa_valid + +def calc_huttonboore_vec(a, d, s, uc): + """Vettorizzazione della legge Hutton & Boore""" + # s = 0 if not uc else s + s_arr = np.where(uc, s, 0.0) + # Ritorna np.nan invece di False in caso di valori invalidi (es. a <= 0) + with np.errstate(divide='ignore', invalid='ignore'): + m = np.where(a > 0, np.log10(a) + 1.110 * np.log10(d / 100.) + 0.00189 * (d - 100.) + 3.0 + s_arr, np.nan) + return m + + +def calc_dibona_vec(a, d, s, uc): + """Vettorizzazione della legge Di Bona""" + s_arr = np.where(uc, s, 0.0) + with np.errstate(divide='ignore', invalid='ignore'): + m = np.where(a > 0, np.log10(a) + 1.667 * np.log10(d / 100.) + 0.001736 * (d - 100.) + 3.0 + s_arr, np.nan) + return m + + +def apply_magnitudes_and_group(dfa_valid, dbcorr, use_stcorr_hb, use_stcorr_db, when_no_stcorr_hb, when_no_stcorr_db): + """ + Sostituisce la seconda parte del ciclo iterrows. + Calcola ML in vettoriale e prepara i dati per i set N ed E. + """ + # 1. Merge vettoriale delle correzioni di stazione (Di Bona) + if dbcorr is not False and not dbcorr.empty: + # Aggiungiamo la colonna 'c' di dbcorr associandola alla stazione ('sta') + dfa_valid = pd.merge(dfa_valid, dbcorr[['sta', 'c']], on='sta', how='left') + dfa_valid.rename(columns={'c': 's_dibona'}, inplace=True) + dfa_valid['s_dibona'] = dfa_valid['s_dibona'].fillna(False) + else: + dfa_valid['s_dibona'] = False + + dfa_valid['s_hutton'] = False # Da codice originale è sempre False + + # Condizioni per il calcolo + calc_hb_mask = (dfa_valid['s_hutton'] != False) | ((dfa_valid['s_hutton'] == False) & when_no_stcorr_hb) + calc_db_mask = (dfa_valid['s_dibona'] != False) | ((dfa_valid['s_dibona'] == False) & when_no_stcorr_db) + + # 2. Calcolo vettoriale delle ML + dfa_valid['ml_hb'] = np.nan + dfa_valid.loc[calc_hb_mask, 'ml_hb'] = calc_huttonboore_vec( + dfa_valid.loc[calc_hb_mask, 'amp_calc'].values, + dfa_valid.loc[calc_hb_mask, 'hypo_dist'].values, + dfa_valid.loc[calc_hb_mask, 's_hutton'].values, + use_stcorr_hb + ) + + dfa_valid['ml_db'] = np.nan + dfa_valid.loc[calc_db_mask, 'ml_db'] = calc_dibona_vec( + dfa_valid.loc[calc_db_mask, 'amp_calc'].values, + dfa_valid.loc[calc_db_mask, 'hypo_dist'].values, + dfa_valid.loc[calc_db_mask, 's_dibona'].values, + use_stcorr_db + ) + + # 3. Creazione della chiave univoca per stazione/canale + # Estrae i primi due caratteri del canale (es. 'HH' da 'HHN') + dfa_valid['cha_prefix'] = dfa_valid['cha'].str[0:2] + dfa_valid['components_key'] = dfa_valid['net'] + '_' + dfa_valid['sta'] + '_' + dfa_valid['loc'] + '_' + dfa_valid[ + 'cha_prefix'] + + # Identifica l'orientamento (N, E, Z) + dfa_valid['orientation'] = dfa_valid['cha'].str[2] + + return dfa_valid + +###################################################################################### def huttonboore(a,d,s,uc): # if uc (use_stcorr_hb) is True s is set to its entrance value and if this value is not False it is set to its value # if uc (use_stcorr_hb) is True s is set to its entrance value and if this value is False it is set to ZERO @@ -818,192 +953,11 @@ def json_pyml_response(r): if args.in_file_format == 'json': unit=1 #db units -for index, row in dfa.iterrows(): - #### Testing the basic data #### - # Channel Info - try: - net = row['net'] - net = False if pandas.isna(row['net']) else net - except: - net = False - try: - sta = row['sta'] - sta = False if pandas.isna(row['sta']) else sta - except: - sta = False - try: - loc = row['loc'] - loc = "--" if not loc else loc - except: - loc = False - try: - cha = row['cha'] - cha = False if pandas.isna(row['cha']) else cha - except: - cha = False - # Amplitudes - try: - minamp=row['minamp(m)']*unit - except: - try: - minamp=row['amp1']*unit - minamp = False if pandas.isna(row['amp1']) else minamp - except: - minamp = False - try: - maxamp=row['maxamp(m)']*unit - except: - try: - maxamp=row['amp2']*unit - maxamp = False if pandas.isna(row['amp2']) else maxamp - except: - maxamp = False - - # Amplitudes times - try: - time_minamp=row['minamptime'] - except: - try: - time_minamp=row['time1'] - time_minamp = False if pandas.isna(row['time1']) else time_minamp - except: - time_minamp = False - try: - time_maxamp=row['maxamptime'] - except: - try: - time_maxamp=row['time2'] - time_maxamp = False if pandas.isna(row['time2']) else time_maxamp - except: - time_maxamp = False - try: - period=row['period'] - period = False if pandas.isna(row['period']) else period - except: - period = False - # Station's coordinates - try: - stla=False if pandas.isna(row['lat']) else float(row['lat']) - stlo=False if pandas.isna(row['lon']) else float(row['lon']) - stel=False if pandas.isna(row['elev']) else float(row['elev'])/km - except: - stla=False - stlo=False - stel=False - - check_list = { - 'net': net, - 'sta': sta, - 'loc': loc, - 'cha': cha, - 'minamp': minamp, - 'maxamp': maxamp, - 'time_minamp': time_minamp, - 'time_maxamp': time_maxamp, - 'stla': stla, - 'stlo': stlo - } - false_keys = [key for key, value in check_list.items() if not value] - if 'net' in false_keys or 'sta' in false_keys or 'loc' in false_keys or 'cha' in false_keys: - continue - #if 'minamp' in false_keys or 'maxamp' in false_keys: - # print(sta) +# 1. Pulizia e calcolo distanze spaziali (tutto vettoriale con ObsPy + Numpy) +dfa_valid = process_amplitudes_vectorized(dfa, origin, unit, max_lowcorner, delta_corner) - try: - corner_low=float(row['loco']) - except: - corner_low=False - try: - corner_high=float(row['hico']) - except: - corner_high=False - try: - met=row['method'] - - except: - met='ingv' - if args.clipped_info: - clp=clip.loc[(clip['net'] == net) & (clip['sta'] == sta) & (clip['cha'] == cha)] - if not clp.empty: - log_out.write(' '.join(("Component skipped:",str(net),str(sta),str(loc),str(cha)," due to cliping\n"))) - continue - if corner_low and corner_high and (corner_low >= max_lowcorner or (corner_high-corner_low) < delta_corner): - log_out.write(' '.join(("Component skipped:",str(net),str(sta),str(loc),str(cha),str(corner_low),str(corner_high),"\n"))) - continue - seed_id='.'.join((str(net),str(sta),str(loc),str(cha))) - if log_out: - log_out.write(' '.join(("Working on",seed_id,'\n'))) - # In this block all the possibile conditions not to use this waveforms are checked so to reduce useless computing time - # First: get timing info from SAC to soon understand if this is a good cut or not for amplitude determination - #[distmeters,azi,bazi] = distaz(tr.stla,tr.stlo,tr.evla,tr.evlo) - #tr.dist=distmeters/1000. - components_key='_'.join((str(net),str(sta),str(loc),str(cha[0:2]))) - cmp_keys.add(components_key) - - try: - hypo_dist = row['ipodistance(km)'] - epi_dist_km = row['epidistance(km)'] - epi_dist_deg = False - except: - #calcolo le distanze - evla=float(origin['lat']) - evlo=float(origin['lon']) - evdp=float(origin['depth']) - if not stla or not stlo: - epi_dist_km = False - epi_dist_deg = False - hypo_dist = False - epi_azimuth = False - else: - geo_dict = geod.Inverse(evla,evlo,stla,stlo) - #distmeters = geo_dict['s12'] - distdegrees = geo_dict['a12'] - #azi = geo_dict['azi1'] - #bazi = geo_dict['azi2'] - [distmeters,azi,bazi] = distaz(stla,stlo,evla,evlo) - epi_dist_km = distmeters / km - epi_dist_deg = distdegrees - hypo_dist = msqrt(mpow(epi_dist_km,2)+mpow((evdp+stel),2)) - epi_azimuth = azi - if log_out: - log_out.write(' '.join(("Coordinates:",str(net),str(sta),str(loc),str(cha),str(stla),str(stlo),str(stel),str(evla),str(evlo),str(evdp),str(epi_dist_km),str(epi_dist_deg),str(hypo_dist),str(epi_azimuth),"\n"))) - - ml = [False]*2 - amp = abs(maxamp-minamp)/2 - components_key_met=components_key+'_'+met - # Loading stations corrections - dbsite=sta - try: - s_dibona=float(dbcorr.loc[dbcorr['sta'] == dbsite, 'c'].iloc[0]) # Di Bona Station correction - except: - s_dibona=False - s_hutton=False # Station correction for Hutton and Boore formula - - if s_hutton or (not s_hutton and when_no_stcorr_hb): - try: - ml[0] = huttonboore(amp,hypo_dist,s_hutton,use_stcorr_hb) - except: - ml[0] = False - else: - ml[0] = False - - if s_dibona or (not s_dibona and when_no_stcorr_db): - try: - ml[1] = dibona(amp,hypo_dist,s_dibona,use_stcorr_db) - except: - ml[1] = False - else: - ml[1] = False - - if cha[2] == 'N': - components_N[components_key_met]=[ml,[minamp,maxamp],epi_dist_km,epi_dist_deg,hypo_dist,[s_hutton,s_dibona],time_minamp,time_maxamp,period,stla,stlo,stel*km,epi_azimuth] - elif cha[2] == 'E': - components_E[components_key_met]=[ml,[minamp,maxamp],epi_dist_km,epi_dist_deg,hypo_dist,[s_hutton,s_dibona],time_minamp,time_maxamp,period,stla,stlo,stel*km,epi_azimuth] - elif cha[2] == 'Z': - components_Z[components_key_met]=[ml,[minamp,maxamp],epi_dist_km,epi_dist_deg,hypo_dist,[s_hutton,s_dibona],time_minamp,time_maxamp,period,stla,stlo,stel*km,epi_azimuth] - else: - if log_out: - log_out.write(' '.join(("Component not recognized for ",str(net),str(sta),str(loc),str(cha),"\n"))) +# 2. Calcolo Magnitudo (H&B, Di Bona) e Merge Correzioni (tutto vettoriale Pandas) +dfa_processed = apply_magnitudes_and_group(dfa_valid, dbcorr, use_stcorr_hb, use_stcorr_db, when_no_stcorr_hb, when_no_stcorr_db) # Hutton and Boore mean_hb_ml_sta,min_dist,log['stationmagnitudes'] = create_sets(cmp_keys,components_N,components_E,met,mindist,maxdist,delta_peaks,0,when_no_stcorr_hb,use_stcorr_hb,mag_mean_type,amp_mean_type,log['stationmagnitudes'],jlog_stamag,jlog_stamag_cha) From 6468961682180739b8357eaf93a68696f204de8a Mon Sep 17 00:00:00 2001 From: Raffaele Di Stefano Date: Wed, 23 Sep 2026 18:17:38 +0200 Subject: [PATCH 03/12] Upgrade of the createset deprecated function --- pyml.py | 215 ++++++++++++++++++++++++++------------------------------ 1 file changed, 98 insertions(+), 117 deletions(-) diff --git a/pyml.py b/pyml.py index ba64858..b6a2362 100644 --- a/pyml.py +++ b/pyml.py @@ -267,119 +267,80 @@ def dibona(a,d,s,uc): return m -def create_sets(keys,cmpn_original,cmpe_original,mtd,mid,mad,dp,mty,whstc,stc,mmt,amt,logsms,jlog_stamag,jlog_stamag_cha): - # mtd is the peakmethod - # mty is the huttonboore 0, dibona 1 - # a channel cmpn is [ml,amp,tr.dist,hypo_dist,[s_hutton,s_dibona],time_minamp,time_maxamp] - # where ml is a list of two: [ml_hutton,ml_dibona] - # mmt is the mag_mean_type parameter - # 'meanmag"--> only the mean of the channels's ML is computed - # 'meanamp"--> only the ML based on the mean of the channels's amplitudes is computed - # in the second case, amt is evaluated: - # 'geo' --> the geometrical mean is computed - # 'ari' --> the artihmetic mean is computed - ml_set=[] - mtytxt='HuttonBoore' if mty==0 else 'DiBona' - midi = 99999.0 # Minimum distance is needed and here below calculated to be used in the mag quality definition - cmpe = cmpe_original.copy() - cmpn = cmpn_original.copy() - for k in keys: - log_condition=False - n,s,l,c = k.split('_') - kk=k+'_'+mtd - logsm = copy.deepcopy(jlog_stamag) - logsm['net'],logsm['sta'],logsm['loc'],logsm['band_inst'] = [n,s,l,c] - #logsm['loc'] = "--" if logsm['loc'] == "None" else logsm['loc'] - why_missing_channel="" - if kk in cmpe: - if not cmpe[kk][1][1] or not cmpe[kk][1][0]: - why_missing_channel="maxamp missing" if not cmpe[kk][1][1] else "minamp missing" - remove_key = cmpe.pop(kk, None) - if kk in cmpn: - if not cmpn[kk][1][1] or not cmpn[kk][1][0]: - why_missing_channel="maxamp missing" if not cmpn[kk][1][1] else "minamp missing" - remove_key = cmpn.pop(kk, None) - if kk in cmpn and kk in cmpe: # if both components are present in the set - if not cmpn[kk][2] or not cmpe[kk][2]: - epidist = False - ipodist = False - if log_out: - log_out.write(' '.join(("Station skipped due to stations coordinates missing: ",str(k),str(ipodist),"\n"))) - logsm['status'] = "critical" - logsm['summary'] = "Station skipped due to stations coordinates missing" - logsm['extended'] = mtytxt - logsms.append(logsm) - continue - epidist = (cmpn[kk][2] + cmpe[kk][2])/2 # epicentral distance - ipodist = (cmpn[kk][4] + cmpe[kk][4])/2 # ipocentral distance - amp_e = abs(cmpe[kk][1][1]-cmpe[kk][1][0])/2 # now both the minamp and maxamp values are reported in the channels arrays so amplitude richter must be calculated here - amp_n = abs(cmpn[kk][1][1]-cmpn[kk][1][0])/2 # now both the minamp and maxamp values are reported in the channels arrays so amplitude richter must be calculated here - if ipodist >= mid and ipodist <= mad: # if ipocentral distance is within the accepted range we proceed - if mtd != 'free' or (mtd == 'free' and abs(cmpn[kk][7]-cmpn[kk][6]) <= dp and abs(cmpe[kk][7]-cmpe[kk][6]) <= dp): # if the method is not free the deltapeak has no meaning orherwise it is evaluated - #Mean of channel magnitudes is calculated - if mmt == 'meanmag' and cmpn[kk][0][mty] and cmpe[kk][0][mty]: - mm = (cmpn[kk][0][mty] + cmpe[kk][0][mty])/2 - elif mmt == 'meanamp' and (not stc or (cmpn[kk][5][mty] and cmpe[kk][5][mty]) or whstc): - # Magnitudes of Mean channel amplitudes is calculated if - if amt == 'ari': - mean_amp = (amp_n + amp_e)/2 # Artimetic mean - elif amt == 'geo': - mean_amp = msqrt(amp_n * amp_e) # Geometric mean that is the correct one according to Di Bona - corr = (cmpn[kk][5][mty] + cmpe[kk][5][mty])/2 if cmpn[kk][5][mty] and cmpe[kk][5][mty] else False - if mty == 0: - mm = huttonboore(mean_amp,ipodist,corr,stc) - mtytxt = 'HuttonBoore' - if mty == 1: - mm = dibona(mean_amp,ipodist,corr,stc) - mtytxt = 'DiBona' - ml_set.append([kk,mm]) - midi = epidist if epidist < midi else midi - else: - if log_out: - log_out.write(' '.join(("Station skipped due to time distance between min and max amp larger than",str(dp),":",str(kk),str(abs(cmpn[kk][7]-cmpn[kk][6])),"\n"))) - logsm['status'] = 'warning' - logsm['summary'] = ' '.join(("In ML ",mtytxt,"Station skipped due to time distance between min and max amp larger than",str(dp))) - logsm['extended'] = ' '.join(("Time distance is",str(abs(cmpn[kk][7]-cmpn[kk][6])),"s")) - log_condition=True - #logsms.append(logsm) - else: - if log_out: - log_out.write(' '.join(("Station skipped due to ipodist: ",str(k),str(ipodist),"\n"))) - logsm['status'] = 'warning' - logsm['summary'] = ' '.join(("In ML ",mtytxt,"Station",str(kk),"skipped due to ipodist")) - logsm['extended'] = ' '.join(("Distance is",str(ipodist),"km")) - log_condition=True - #logsms.append(logsm) - elif kk not in cmpn or kk not in cmpe: - if log_out: - log_out.write(' '.join(("Station skipped due to missing channel",str(kk),'\n'))) - logsm['status'] = 'critical' - logsm['summary'] = 'Station skipped due to missing channel' - logsm['extended'] = 'ML ' + mtytxt - logsmc = copy.deepcopy(jlog_stamag_cha) - if kk not in cmpn: - logsmc['orientation'] = 'N' - logsmc['status'] = 'critical' - logsmc['summary'] = 'missing' - logsmc['extended'] = why_missing_channel - else: - logsmc['orientation'] = 'N' - logsmc['status'] = 'ok' - logsm['channels'].append(logsmc) - logsmc = copy.deepcopy(jlog_stamag_cha) - if kk not in cmpe: - logsmc['orientation'] = 'E' - logsmc['status'] = 'critical' - logsmc['summary'] = 'missing' - logsmc['extended'] = why_missing_channel - else: - logsmc['orientation'] = 'E' - logsmc['status'] = 'ok' - logsm['channels'].append(logsmc) - log_condition=True - if log_condition: - logsms.append(logsm) - return ml_set,midi,logsms +def create_sets_vectorized(dfa_processed, mtd, mid, mad, dp, mty, whstc, stc, mmt, amt): + """ + Sostituisce il vecchio 'create_sets'. + Usa Pandas per allineare canali N ed E, filtrare vettorialmente e calcolare la ML di stazione. + """ + # 1. Filtriamo solo i canali N ed E dal nostro dataframe processato + df_ne = dfa_processed[dfa_processed['orientation'].isin(['N', 'E'])].copy() + + # 2. Pivot: Trasformiamo le righe in colonne. + # Ora avremo una riga per ogni 'components_key' con le relative colonne per N ed E + df_p = df_ne.pivot(index='components_key', columns='orientation') + + # 3. Eliminiamo le stazioni che non hanno entrambe le componenti valide + df_p = df_p.dropna(subset=[('amp_calc', 'N'), ('amp_calc', 'E')]) + + if df_p.empty: + return [], 99999.0, [] + + # 4. Calcolo delle medie per tutte le stazioni simultaneamente + mean_hypo = (df_p[('hypo_dist', 'N')] + df_p[('hypo_dist', 'E')]) / 2.0 + mean_epi = (df_p[('epi_dist_km', 'N')] + df_p[('epi_dist_km', 'E')]) / 2.0 + + # 5. Creazione della Maschera di Validità (Filtri su distanze e delta peaks) + valid_mask = (mean_hypo >= mid) & (mean_hypo <= mad) + + if mtd == 'free': + # Assicuriamoci che i tempi siano numerici per la sottrazione + t_max_n = pd.to_numeric(df_p[('time_maxamp', 'N')], errors='coerce') + t_min_n = pd.to_numeric(df_p[('time_minamp', 'N')], errors='coerce') + t_max_e = pd.to_numeric(df_p[('time_maxamp', 'E')], errors='coerce') + t_min_e = pd.to_numeric(df_p[('time_minamp', 'E')], errors='coerce') + + dp_n = (t_max_n - t_min_n).abs() + dp_e = (t_max_e - t_min_e).abs() + + valid_mask &= (dp_n <= dp) & (dp_e <= dp) + + df_valid = df_p[valid_mask].copy() + if df_valid.empty: + return [], 99999.0, [] + + # 6. Calcolo della Magnitudo di Stazione + mty_str = 'ml_hb' if mty == 0 else 'ml_db' + s_str = 's_hutton' if mty == 0 else 's_dibona' + calc_func = calc_huttonboore_vec if mty == 0 else calc_dibona_vec + + if mmt == 'meanmag': + sta_ml = (df_valid[(mty_str, 'N')] + df_valid[(mty_str, 'E')]) / 2.0 + elif mmt == 'meanamp': + amp_n, amp_e = df_valid[('amp_calc', 'N')], df_valid[('amp_calc', 'E')] + + # Matematica pura e vettoriale + if amt == 'ari': + mean_amp = (amp_n + amp_e) / 2.0 + else: # 'geo' + mean_amp = np.sqrt(amp_n * amp_e) + + # Gestiamo eventuali correzioni assenti (False/NaN) convertendole a 0 per il calcolo + corr_n = pd.to_numeric(df_valid[(s_str, 'N')], errors='coerce').fillna(0) + corr_e = pd.to_numeric(df_valid[(s_str, 'E')], errors='coerce').fillna(0) + mean_corr = (corr_n + corr_e) / 2.0 + + sta_ml = calc_func(mean_amp.values, mean_hypo[valid_mask].values, mean_corr.values, stc) + + # 7. Ricostruzione dell'output nel formato atteso dal resto del codice + valid_keys = df_valid.index.astype(str) + '_' + mtd + ml_set = [[k, m] for k, m in zip(valid_keys, sta_ml) if pd.notna(m)] + + midi = mean_epi[valid_mask].min() if not mean_epi[valid_mask].empty else 99999.0 + + # I log verranno rimandati a un costruttore esterno per efficienza + logsms = [] + + return ml_set, midi, logsms #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> # Calculating the weighted standard deviation @@ -959,8 +920,30 @@ def json_pyml_response(r): # 2. Calcolo Magnitudo (H&B, Di Bona) e Merge Correzioni (tutto vettoriale Pandas) dfa_processed = apply_magnitudes_and_group(dfa_valid, dbcorr, use_stcorr_hb, use_stcorr_db, when_no_stcorr_hb, when_no_stcorr_db) +# === START BRIDGE (Per mantenere vivo il costruttore JSON a fine file) === +components_N = {} +components_E = {} +for _, row in dfa_processed.iterrows(): + k = row['components_key'] + '_' + met + # Ricostruiamo in modo posticcio le tuple aspettate dal JSON writer + dummy_data = [[row['ml_hb'], row['ml_db']], [row['minamp'], row['maxamp']], row['epi_dist_km'], + 0, row['hypo_dist'], [row['s_hutton'], row['s_dibona']], + row['time_minamp'], row['time_maxamp'], 0, row['lat'], row['lon'], row['elev'], row['epi_azimuth']] + if row['orientation'] == 'N': components_N[k] = dummy_data + if row['orientation'] == 'E': components_E[k] = dummy_data +# === END BRIDGE === + + # Hutton and Boore -mean_hb_ml_sta,min_dist,log['stationmagnitudes'] = create_sets(cmp_keys,components_N,components_E,met,mindist,maxdist,delta_peaks,0,when_no_stcorr_hb,use_stcorr_hb,mag_mean_type,amp_mean_type,log['stationmagnitudes'],jlog_stamag,jlog_stamag_cha) +mean_hb_ml_sta, min_dist, log['stationmagnitudes'] = create_sets_vectorized( + dfa_processed, met, mindist, maxdist, delta_peaks, 0, when_no_stcorr_hb, use_stcorr_hb, mag_mean_type, amp_mean_type +) + +# Di Bona +mean_db_ml_sta, min_dist, log['stationmagnitudes'] = create_sets_vectorized( + dfa_processed, met, mindist, maxdist, delta_peaks, 1, when_no_stcorr_db, use_stcorr_db, mag_mean_type, amp_mean_type +) + #if log_out: # log_out.write("create_sets: the execution time is: "+str(execution_time)+"\n") if len(mean_hb_ml_sta) == 0: @@ -997,9 +980,7 @@ def json_pyml_response(r): logm['hb']['status'] = msg_sts logm['hb']['summary'] = msg_sum logm['hb']['extended'] = msg_ext -#mm_mlh,mm_stdh,mm_ns_s_h,mm_nsh,cond = calculate_event_ml(meanmag_ml_sta,outliers_max_it,outliers_red_stop) -# Di Bona -mean_db_ml_sta,min_dist,log['stationmagnitudes'] = create_sets(cmp_keys,components_N,components_E,met,mindist,maxdist,delta_peaks,1,when_no_stcorr_db,use_stcorr_db,mag_mean_type,amp_mean_type,log['stationmagnitudes'],jlog_stamag,jlog_stamag_cha) + if len(mean_db_ml_sta) == 0: msg_sts = 'critical' msg_sum='Dibona List is empty' From 70572fa5f8e76e8fd4e8262cae80873daceb03bd Mon Sep 17 00:00:00 2001 From: Raffaele Di Stefano Date: Wed, 23 Sep 2026 18:45:03 +0200 Subject: [PATCH 04/12] import and imported functions naming cleaning --- pyml.py | 184 +++++++++++++++++++++++++++----------------------------- 1 file changed, 89 insertions(+), 95 deletions(-) diff --git a/pyml.py b/pyml.py index b6a2362..dfc8719 100644 --- a/pyml.py +++ b/pyml.py @@ -39,34 +39,25 @@ # the rest is downweighted as hm_cutoff/(distance_from_the_mean) # In both cases stations with hypocentral distance lower than mindist (tipically 10km) and higher than maxdist (tipically 600) are excluded -import argparse,sys,os,glob,copy,pwd,pathlib,itertools,getpass,socket +# --- Libreria Standard Python --- +import argparse +import copy import decimal - -from geographiclib.geodesic import Geodesic -import pandas -import time +import getpass import json - -# Imports from obpsy -from obspy import read -from obspy.geodetics.base import gps2dist_azimuth as distaz -from scipy.stats import median_abs_deviation - -from numpy import where as np_where -from numpy import sum as np_sum -from numpy import square as np_square -from numpy import errstate as np_errstate -from numpy import median as np_median -from numpy import ones as np_ones -from numpy import array as np_array -from numpy import asarray as np_asarray - +import os +import socket +import sys from datetime import datetime +import configparser as cp -from math import sqrt as msqrt, log10 as mlog10, pow as mpow, log2 as mlog2 - +# --- Numerical and Data Managing Libraries --- import numpy as np import pandas as pd +from scipy.stats import median_abs_deviation + +# --- Geodetic and Sismological Libraries --- +from geographiclib.geodesic import Geodesic from obspy.geodetics.base import gps2dist_azimuth class MyParser(argparse.ArgumentParser): @@ -90,12 +81,6 @@ def parseArguments(): args=parser.parse_args() return args -# Depending on the python version the configparser has different names so ... try -try: - import ConfigParser as cp -except ImportError: - import configparser as cp - # Build a dictionary from config file section def get_config_dictionary(cfg, section): dict1 = {} @@ -116,64 +101,74 @@ def get_config_dictionary(cfg, section): def process_amplitudes_vectorized(dfa, origin, unit, max_lowcorner, delta_corner): """ - Sostituisce il blocco dfa.iterrows(). - Pulisce i dati e calcola le distanze in logica vettoriale (NumPy/Pandas). + Pulisce i dati, gestisce le distanze (sia precalcolate CSV che da calcolare via Obspy/JSON) + ed estrae il metodo operativo in logica Pandas. """ - # 1. Uniformiamo i nomi e riempiamo i valori nulli in modo vettoriale - # Sfruttiamo le potenzialità di Pandas per gestire i missing values senza try/except continui dfa['net'] = dfa.get('net', pd.Series(dtype=object)).fillna(False) dfa['sta'] = dfa.get('sta', pd.Series(dtype=object)).fillna(False) dfa['loc'] = dfa.get('loc', pd.Series(dtype=object)).fillna('--') dfa['cha'] = dfa.get('cha', pd.Series(dtype=object)).fillna(False) - - # 2. Estrazione Ampiezze (Gestione flessibile delle colonne 'minamp(m)' vs 'amp1') + + # Estrazione sicura del parametro 'met' tramite esplorazione colonne Pandas + if 'method' in dfa.columns: + met = str(dfa['method'].iloc[0]) + else: + met = 'ingv' + minamp_col = 'minamp(m)' if 'minamp(m)' in dfa.columns else 'amp1' maxamp_col = 'maxamp(m)' if 'maxamp(m)' in dfa.columns else 'amp2' time_min_col = 'minamptime' if 'minamptime' in dfa.columns else 'time1' time_max_col = 'maxamptime' if 'maxamptime' in dfa.columns else 'time2' - # Forziamo il tipo a float64 per la massima precisione matematica dfa['minamp'] = dfa[minamp_col].astype(np.float64) * unit dfa['maxamp'] = dfa[maxamp_col].astype(np.float64) * unit - dfa['amp_calc'] = np.abs(dfa['maxamp'] - dfa['minamp']) / 2.0 # Vettoriale - + dfa['amp_calc'] = np.abs(dfa['maxamp'] - dfa['minamp']) / 2.0 + dfa['time_minamp'] = dfa[time_min_col] dfa['time_maxamp'] = dfa[time_max_col] - # 3. Filtro rapido delle stazioni valide prima dei calcoli pesanti geodetici - # Manteniamo solo le righe dove net, sta, cha e coordinate esistono valid_mask = ( - (dfa['net'] != False) & (dfa['sta'] != False) & (dfa['cha'] != False) & - dfa['lat'].notna() & dfa['lon'].notna() + (dfa['net'] != False) & (dfa['sta'] != False) & (dfa['cha'] != False) ) dfa_valid = dfa[valid_mask].copy() - # 4. Calcolo Distanze Vettorizzato - evla = float(origin['lat']) - evlo = float(origin['lon']) - evdp = float(origin['depth']) - - # Definiamo un wrapper vettorizzato per la funzione Obspy - def calc_dist_az(stla, stlo): - distmeters, azi, _ = gps2dist_azimuth(stla, stlo, evla, evlo) - return distmeters / 1000.0, azi - - vec_dist_az = np.vectorize(calc_dist_az, otypes=[np.float64, np.float64]) - - # Applichiamo il calcolo solo sulle coordinate valide in un solo colpo (Fast mapping) - dfa_valid['epi_dist_km'], dfa_valid['epi_azimuth'] = vec_dist_az( - dfa_valid['lat'].values, - dfa_valid['lon'].values - ) - - # Calcolo distanza ipocentrale usando NumPy strict math (algebra lineare su array) - # Rimpiazza la lentezza di msqrt e mpow - dfa_valid['stel_km'] = dfa_valid['elev'].astype(np.float64) / 1000.0 - dfa_valid['hypo_dist'] = np.sqrt( - np.square(dfa_valid['epi_dist_km']) + np.square(evdp + dfa_valid['stel_km']) - ) + # 4. Calcolo Distanze (Logica Ibrida NumPy vettorializzata) + if 'ipodistance(km)' in dfa_valid.columns and 'epidistance(km)' in dfa_valid.columns: + # Percorso CSV: assegnazione istantanea da array, skip operazioni Obspy + dfa_valid['epi_dist_km'] = dfa_valid['epidistance(km)'].astype(np.float64) + dfa_valid['hypo_dist'] = dfa_valid['ipodistance(km)'].astype(np.float64) + dfa_valid['epi_azimuth'] = 0.0 # Placeholder se non c'è azimuth + else: + # Percorso JSON: calcolo geodetico Obspy+NumPy + if origin and 'lat' in origin and 'lon' in origin: + # Controllo validità coordinate stazione + valid_coord_mask = dfa_valid['lat'].notna() & dfa_valid['lon'].notna() + dfa_valid = dfa_valid[valid_coord_mask].copy() + + evla = float(origin['lat']) + evlo = float(origin['lon']) + evdp = float(origin['depth']) + + def calc_dist_az(stla, stlo): + distmeters, azi, _ = gps2dist_azimuth(stla, stlo, evla, evlo) + return distmeters / 1000.0, azi + + vec_dist_az = np.vectorize(calc_dist_az, otypes=[np.float64, np.float64]) + dfa_valid['epi_dist_km'], dfa_valid['epi_azimuth'] = vec_dist_az( + dfa_valid['lat'].values, + dfa_valid['lon'].values + ) + + dfa_valid['stel_km'] = dfa_valid['elev'].astype(np.float64) / 1000.0 + dfa_valid['hypo_dist'] = np.sqrt( + np.square(dfa_valid['epi_dist_km']) + np.square(evdp + dfa_valid['stel_km']) + ) + else: + sys.stderr.write("Dati di origine mancanti per il calcolo delle distanze.\n") + sys.exit(1) - return dfa_valid + # Restituiamo anche 'met' così il main può usarlo + return dfa_valid, met def calc_huttonboore_vec(a, d, s, uc): """Vettorizzazione della legge Hutton & Boore""" @@ -249,7 +244,7 @@ def huttonboore(a,d,s,uc): s = 0 if not uc else s # station correction is set to 0 if use_stcorr_hb is False s = 0 if not s else s # station correction is set to 0 if it is False try: - m = mlog10(a) + 1.110*mlog10(d / 100.) + 0.00189*(d - 100.) + 3.0 + s # Hutton & Boore with s added but not confirmed if it is correct + m = np.log10(a) + 1.110*np.log10(d / 100.) + 0.00189*(d - 100.) + 3.0 + s # Hutton & Boore with s added but not confirmed if it is correct except: m = False return m @@ -261,7 +256,7 @@ def dibona(a,d,s,uc): s = 0 if not uc else s # station correction is set to 0 if use_stcorr_hb is False s = 0 if not s else s # station correction is set to 0 if it is False try: - m = mlog10(a) + 1.667*mlog10(d / 100.) + 0.001736*(d - 100.) + 3.0 + s # Massimo Di Bona + m = np.log10(a) + 1.667*np.log10(d / 100.) + 0.001736*(d - 100.) + 3.0 + s # Massimo Di Bona except: m = False return m @@ -363,23 +358,23 @@ def wstd(v,wm,wf): if len(v) <= 1: wsd = False else: - index_not_zero = np_where(wf > eps2)[0] + index_not_zero = np.where(wf > eps2)[0] wf_nz = wf[index_not_zero] #print("wf_nz: ",wf_nz) v_nz = v[index_not_zero] - wsd = np_sum(wf_nz*np_square((v_nz-wm))) - fac = ((len(wf_nz)-1)/len(wf_nz))*np_sum(wf_nz) + wsd = np.sum(wf_nz*np.square((v_nz-wm))) + fac = ((len(wf_nz)-1)/len(wf_nz))*np.sum(wf_nz) wsd = wsd / max(fac,eps2) - wsd = msqrt(max(wsd,eps2)) + wsd = np.sqrt(max(wsd,eps2)) return wsd def whuber(v,w_mean,ruse,zero): res = abs(v - w_mean) - with np_errstate(divide='ignore'): - w = np_where(res <= ruse,1.0,np_where(res > zero,0.0,ruse/res)) + with np.errstate(divide='ignore'): + w = np.where(res <= ruse,1.0,np.where(res > zero,0.0,ruse/res)) #print("W: ",w) try: - w_mean = np_sum(v * w)/np_sum(w) + w_mean = np.sum(v * w)/np.sum(w) except Exception as e: sys.stderr.write(("Error calculalting the Huber Mean",e)) w_mean = False @@ -404,7 +399,7 @@ def rm_outliers(v,v_w,v_flag,v_mean,v_std,times_std,co,var_stop,it_max,skip): #v_flag = v_flag[not_outlier] if len(v[not_outlier]) > 0: v_std = median_abs_deviation(v[not_outlier]) - v_mean = np_median(v[not_outlier]) + v_mean = np.median(v[not_outlier]) n_v_flag = len(v[not_outlier]) else: v_std = False @@ -413,20 +408,20 @@ def rm_outliers(v,v_w,v_flag,v_mean,v_std,times_std,co,var_stop,it_max,skip): return v_mean,v_std,v_w,skip def calculate_event_ml(magnitudes,magnitudes_sta,it_max,var_stop,max_dev,out_cutoff,hm_cutoff): - v = np_array(magnitudes) + v = np.array(magnitudes) s = magnitudes_sta ruse = hm_cutoff # Calculate starting values from m (magnitudes) vector # We here use a median and mad instead of the mean and std as stating values because they are more robust # Names are taken from Huber Mean routine by Franco Mele for coherence - xmd = np_median(v) + xmd = np.median(v) xmd_std = median_abs_deviation(v) vlen_start = len(v) n = 1 finished = False if hm_cutoff: ruse,zero = [hm_cutoff,9999.0] if isinstance(hm_cutoff,float) else hm_cutoff - weights = np_ones(len(v)) + weights = np.ones(len(v)) removed=[] while not finished: amd = xmd @@ -452,7 +447,7 @@ def calculate_event_ml(magnitudes,magnitudes_sta,it_max,var_stop,max_dev,out_cut finished = True whystop=typemean+'_maxit='+str(n)+'_xmdvar='+str(xmd_var) n += 1 - vlen_stop = round(np_sum(weights),2) + vlen_stop = round(np.sum(weights),2) return xmd,xmd_std,vlen_start,vlen_stop,whystop,removed,weights,whuber_fail def char_quality(nu,nw,d,m,r): @@ -467,13 +462,13 @@ def char_quality(nu,nw,d,m,r): # the adopted parameters, we multiply nu by 2 channels q1type = 'course' if q1type == 'basili': - if (mlog2(nu*2)-2) >= m: + if (np.log2(nu*2)-2) >= m: q1 = 'A' - elif (mlog2(nu*2)-1) >= m: + elif (np.log2(nu*2)-1) >= m: q1 = 'B' - elif mlog2(nu*2) >= m: + elif np.log2(nu*2) >= m: q1 = 'C' - elif mlog2(nu*2) < m: + elif np.log2(nu*2) < m: q1 = 'D' else: q1 = 'E' @@ -533,7 +528,7 @@ def standard_pyml_load(infile,eventid,conf_file,log_out,f): #Net;Sta;Loc;Cha;Lat;Lon;Ele;EpiDistance(km);IpoDistance(km);MinAmp(m);MinAmpTime;MaxAmp(m);MaxAmpTime;DeltaPeaks;Method;NoiseWinMin;NoiseWinMax;SignalWinMin;SignalWinMax;P_Pick;Synth;S_Picks;Synth;Nyq;LoCo;HiCo;LenOverSNRIn;SNRIn;ML_H;CORR_HB;CORR_USED_HB;ML_DB;CORR_DB;CORR_USED_DB - dfa=pandas.read_csv(f,sep=';',index_col=False) + dfa=pd.read_csv(f,sep=';',index_col=False) dfa.columns = dfa.columns.str.lower() # Here we load the basic condition to decide if to proceed or not @@ -639,9 +634,9 @@ def json_pyml_load(json_in): except: origin=False try: - dfa = pandas.DataFrame(json_in['data']['amplitudes']) + dfa = pd.DataFrame(json_in['data']['amplitudes']) except: - dfa=pandas.DataFrame() + dfa=pd.DataFrame() return dfa,config,origin # JSON ENCODER CLASS @@ -804,6 +799,7 @@ def json_pyml_response(r): conf_file = args.conf eventid=args.eventid dfa,theoP,theoS,delta_corner,max_lowcorner,delta_peaks,use_stcorr_hb,use_stcorr_db,when_no_stcorr_hb,when_no_stcorr_db,mindist,maxdist,hm_cutoff,outliers_max_it,outliers_red_stop,outliers_nstd,outliers_cutoff = standard_pyml_load(infile,eventid,conf_file,log_out,infile) + origin = False if dfa.empty: if log_out: sys.stderr.write("The given input pyamp file "+infile+" was incomplete\n") @@ -820,7 +816,7 @@ def json_pyml_response(r): elif args.in_file_format.lower() == 'json': ### IF JSON argument is given, pyml works reading both input data a configuration options from the same json file, and it writes out results and log ONLY in one single JSON file if os.path.exists(args.in_file_name): - json_in=pandas.read_json(args.in_file_name) + json_in=pd.read_json(args.in_file_name) dfa,config,origin = json_pyml_load(json_in) eventid=0 if dfa.empty or not config or not origin: @@ -890,7 +886,7 @@ def json_pyml_response(r): sys.exit() if args.clipped_info: - clip=pandas.read_csv(args.clipped_info,sep=';',index_col=False) + clip=pd.read_csv(args.clipped_info,sep=';',index_col=False) cmp_keys=set() components_N={} @@ -900,22 +896,20 @@ def json_pyml_response(r): if args.dbona_corr: try: - dbcorr=pandas.read_csv(args.dbona_corr,sep=';') + dbcorr=pd.read_csv(args.dbona_corr,sep=';') except: log_out.write("File "+args.dbona_corr+" not found\n") sys.exit() else: dbcorr=False - -start_time = time.perf_counter() km=1000. unit=1000 # pyamp units if args.in_file_format == 'json': unit=1 #db units # 1. Pulizia e calcolo distanze spaziali (tutto vettoriale con ObsPy + Numpy) -dfa_valid = process_amplitudes_vectorized(dfa, origin, unit, max_lowcorner, delta_corner) +dfa_valid, met = process_amplitudes_vectorized(dfa, origin, unit, max_lowcorner, delta_corner) # 2. Calcolo Magnitudo (H&B, Di Bona) e Merge Correzioni (tutto vettoriale Pandas) dfa_processed = apply_magnitudes_and_group(dfa_valid, dbcorr, use_stcorr_hb, use_stcorr_db, when_no_stcorr_hb, when_no_stcorr_db) @@ -956,7 +950,7 @@ def json_pyml_response(r): else: #meanmag_ml = list(list(zip(*meanmag_ml_sta))[1]) mean_ml = list(list(zip(*mean_hb_ml_sta))[1]) - mean_ml_sta = np_asarray(list(list(zip(*mean_hb_ml_sta))[0]), dtype=object) + mean_ml_sta = np.asarray(list(list(zip(*mean_hb_ml_sta))[0]), dtype=object) ma_mlh,ma_stdh,ma_ns_s_h,ma_nsh,cond_hb,outliers_hb,weights_hb,wh_hb_fail = calculate_event_ml(mean_ml,mean_ml_sta,outliers_max_it,outliers_red_stop,outliers_nstd,outliers_cutoff,hm_cutoff) if ma_mlh: mlhb = True @@ -991,7 +985,7 @@ def json_pyml_response(r): else: #meanmag_ml = list(list(zip(*meanmag_ml_sta))[1]) mean_ml = list(list(zip(*mean_db_ml_sta))[1]) - mean_ml_sta = np_asarray(list(list(zip(*mean_db_ml_sta))[0]), dtype=object) + mean_ml_sta = np.asarray(list(list(zip(*mean_db_ml_sta))[0]), dtype=object) ma_mld,ma_stdd,ma_ns_s_d,ma_nsd,cond_db,outliers_db,weights_db,wh_db_fail = calculate_event_ml(mean_ml,mean_ml_sta,outliers_max_it,outliers_red_stop,outliers_nstd,outliers_cutoff,hm_cutoff) if ma_mld: mldb = True From 070cee7d10c6cd8f85e2ed21ad10ee7c6d26b538 Mon Sep 17 00:00:00 2001 From: Raffaele Di Stefano Date: Wed, 23 Sep 2026 19:08:30 +0200 Subject: [PATCH 05/12] Final implementation and cleaning --- pyml.py | 1522 +++++++++++++++++++------------------------------------ 1 file changed, 518 insertions(+), 1004 deletions(-) diff --git a/pyml.py b/pyml.py index dfc8719..aea2b3e 100644 --- a/pyml.py +++ b/pyml.py @@ -1,87 +1,58 @@ -# Authors: Raffaele Di Stefano (raffaele.distefano@ingv.it), Barbara Castello (barbara.castello@ingv.it) +# Authors: +# Raffaele Di Stefano (raffaele.distefano@ingv.it), +# Barbara Castello (barbara.castello@ingv.it) +# Coding of present version powered by Gemini 3.1 Pro # Aknowledgements: Valentino Lauciani (valentino.lauciani@ingv.it) and Andrea Bono (andrea.bono@ingv.it) - adaptation to the monitoring system # Licence: CC BY-SA 4.0 (https://creativecommons.org/licenses/by-sa/4.0/) -# -# Input files: -# - option 1) channels' amplitudes csv file, stations corrections csv files, pyml.conf -# - option 2) channels' amplitudes json file with conf included, stations corrections csv files -# Operations: -# - calculates channel ML on all the three components with several Attenuation Laws -# - calculates station ML on all the stations either as a mean of channel's one or as ML of the mean amplitude -# - calculates event ML for all the Attenuation Laws with different possible statistical approaches -# -# Details: Amplitude usage -# Two different approaches are followed to calculate the station ML: -# - the mean of the maximum amplitudes N and E is used in the attenuation law to get the station ML (mean can be aritmetic or geometric) -# - the mean of the N and E ML is performed to give the station ML (mean can be aritmetic or geometric) -# Attenuation Law -# PyML implemets two different attenuation laws for the ML calculation: -# - INGV Hutton & Boore which is mutuated by the one formerly used at USGS for the -# adapted to the Italian region with no stations corrections -# - M. Di Bona attenuation law calculated specifically for the Italian region -# with stations corrections at 400 stations (Di Bona 1-199 and Mele-Quintiliani 200-400) -# - Lolli Gssperini and Mele double attenuation law is coming soon -# -# Mean ML Value and Standard deviation calculation -# Two alternative, both iterative, approaches can be used -# Both methods start from the median and not from the mean for robustness, then they diverge from each other -# 1) Strict Huber Mean / Outliers removal (weighting function is not applied or it can considered to be 0 for outlier and 1 for the rest) -# Parameters are taken from the .conf file or from the json input: -# - outliers_nstd: how many time (outliers_nstd*standarddev) must be used to cut out the outliers -# - outliers_cutoff: a lower limit to the outliers_nstd*standarddev value -# - outliers_red_stop: stops iterations when abs(mean_old-updated_mean)) is lower than outliers_red_stop -# - outliers_max_it: after this value iterations it stops iterating anyway (no convergence); this value is also used in the other method -# 2) Weighted Huber Mean -# after the preliminary mean calculation this approach calculates it's own weighted mean value and standard deviation -# Parameters are taken from the .conf file or from the json input: -# - outliers_max_it: same as the other -# - hm_cutoff: this is the corner of the downweighting function; whatever is inside +/- hm_cutoff from the (updated) weighted mean has weight 1.0, -# the rest is downweighted as hm_cutoff/(distance_from_the_mean) -# In both cases stations with hypocentral distance lower than mindist (tipically 10km) and higher than maxdist (tipically 600) are excluded # --- Libreria Standard Python --- import argparse import copy import decimal -import getpass import json import os import socket import sys +import logging from datetime import datetime import configparser as cp +from math import log2 -# --- Numerical and Data Managing Libraries --- +# --- Librerie Scientifiche e di Analisi Dati --- import numpy as np import pandas as pd from scipy.stats import median_abs_deviation -# --- Geodetic and Sismological Libraries --- +# --- Librerie Geodetiche e Sismologiche --- from geographiclib.geodesic import Geodesic from obspy.geodetics.base import gps2dist_azimuth + class MyParser(argparse.ArgumentParser): def error(self, message): - sys.stderr.write('error: %s\n' % message) + sys.stderr.write(f'error: {message}\n') self.print_help() sys.exit(2) -def parseArguments(): - parser=MyParser() - parser.add_argument('--in_file_name', default=None, help='Name (and path) of input amplitudes file') - parser.add_argument('--in_file_format', default='json', help='options are json (data and config in one single file) or csv (pyamp format): this latter needs also the --conf argument') - parser.add_argument('--conf', default=None, help='A file containing sections and related parameters (used only with --csv)') - parser.add_argument('--out_format', default='json', help='options are json or txt') - parser.add_argument('--eventid', default='0', help='Unique identifier of the event (only used with --csv_in and --conf)') - parser.add_argument('--dbona_corr', default='dbcor.csv', help='Input file with DiBona Stations corrections') - parser.add_argument('--clipped_info',help='Input file with information on clipped channels') - if len(sys.argv)==1: - parser.print_help() - sys.exit(1) - args=parser.parse_args() - return args -# Build a dictionary from config file section +def parse_arguments(): + parser = MyParser() + parser.add_argument('--in_file_name', default=None, help='Name (and path) of input amplitudes file') + parser.add_argument('--in_file_format', default='json', + help='options are json (data and config in one single file) or csv (pyamp format): this latter needs also the --conf argument') + parser.add_argument('--conf', default=None, + help='A file containing sections and related parameters (used only with --csv)') + parser.add_argument('--out_format', default='json', help='options are json or txt') + parser.add_argument('--eventid', default='0', + help='Unique identifier of the event (only used with --csv_in and --conf)') + parser.add_argument('--dbona_corr', default='dbcor.csv', help='Input file with DiBona Stations corrections') + parser.add_argument('--clipped_info', help='Input file with information on clipped channels') + if len(sys.argv) == 1: + parser.print_help() + sys.exit(1) + return parser.parse_args() + + def get_config_dictionary(cfg, section): dict1 = {} options = cfg.options(section) @@ -89,206 +60,150 @@ def get_config_dictionary(cfg, section): try: dict1[option] = cfg.get(section, option) if dict1[option] == -1: - sys.stderr.write(("skip: %s" % option)) - except: - if log_out: - log_out.write("exception on %s!\n" % option) + sys.stderr.write(f"skip: {option}") + except (cp.NoOptionError, cp.NoSectionError) as ex: + sys.stderr.write(f"Exception on {option}! {ex}\n") dict1[option] = None return dict1 -################################################################### - -def process_amplitudes_vectorized(dfa, origin, unit, max_lowcorner, delta_corner): +def process_amplitudes_vectorized(df_amp, ev_origin, unit_val): """ Pulisce i dati, gestisce le distanze (sia precalcolate CSV che da calcolare via Obspy/JSON) ed estrae il metodo operativo in logica Pandas. """ - dfa['net'] = dfa.get('net', pd.Series(dtype=object)).fillna(False) - dfa['sta'] = dfa.get('sta', pd.Series(dtype=object)).fillna(False) - dfa['loc'] = dfa.get('loc', pd.Series(dtype=object)).fillna('--') - dfa['cha'] = dfa.get('cha', pd.Series(dtype=object)).fillna(False) - - # Estrazione sicura del parametro 'met' tramite esplorazione colonne Pandas - if 'method' in dfa.columns: - met = str(dfa['method'].iloc[0]) - else: - met = 'ingv' + df_amp['net'] = df_amp.get('net', pd.Series(dtype=object)).fillna(False) + df_amp['sta'] = df_amp.get('sta', pd.Series(dtype=object)).fillna(False) + df_amp['loc'] = df_amp.get('loc', pd.Series(dtype=object)).fillna('--') + df_amp['cha'] = df_amp.get('cha', pd.Series(dtype=object)).fillna(False) - minamp_col = 'minamp(m)' if 'minamp(m)' in dfa.columns else 'amp1' - maxamp_col = 'maxamp(m)' if 'maxamp(m)' in dfa.columns else 'amp2' - time_min_col = 'minamptime' if 'minamptime' in dfa.columns else 'time1' - time_max_col = 'maxamptime' if 'maxamptime' in dfa.columns else 'time2' + method_type = str(df_amp['method'].iloc[0]) if 'method' in df_amp.columns else 'ingv' - dfa['minamp'] = dfa[minamp_col].astype(np.float64) * unit - dfa['maxamp'] = dfa[maxamp_col].astype(np.float64) * unit - dfa['amp_calc'] = np.abs(dfa['maxamp'] - dfa['minamp']) / 2.0 + minamp_col = 'minamp(m)' if 'minamp(m)' in df_amp.columns else 'amp1' + maxamp_col = 'maxamp(m)' if 'maxamp(m)' in df_amp.columns else 'amp2' + time_min_col = 'minamptime' if 'minamptime' in df_amp.columns else 'time1' + time_max_col = 'maxamptime' if 'maxamptime' in df_amp.columns else 'time2' - dfa['time_minamp'] = dfa[time_min_col] - dfa['time_maxamp'] = dfa[time_max_col] + df_amp['minamp'] = df_amp[minamp_col].astype(np.float64) * unit_val + df_amp['maxamp'] = df_amp[maxamp_col].astype(np.float64) * unit_val + df_amp['amp_calc'] = np.abs(df_amp['maxamp'] - df_amp['minamp']) / 2.0 - valid_mask = ( - (dfa['net'] != False) & (dfa['sta'] != False) & (dfa['cha'] != False) - ) - dfa_valid = dfa[valid_mask].copy() - - # 4. Calcolo Distanze (Logica Ibrida NumPy vettorializzata) - if 'ipodistance(km)' in dfa_valid.columns and 'epidistance(km)' in dfa_valid.columns: - # Percorso CSV: assegnazione istantanea da array, skip operazioni Obspy - dfa_valid['epi_dist_km'] = dfa_valid['epidistance(km)'].astype(np.float64) - dfa_valid['hypo_dist'] = dfa_valid['ipodistance(km)'].astype(np.float64) - dfa_valid['epi_azimuth'] = 0.0 # Placeholder se non c'è azimuth + df_amp['time_minamp'] = df_amp[time_min_col] + df_amp['time_maxamp'] = df_amp[time_max_col] + + valid_mask = (df_amp['net'] != False) & (df_amp['sta'] != False) & (df_amp['cha'] != False) + df_valid = df_amp[valid_mask].copy() + + if 'ipodistance(km)' in df_valid.columns and 'epidistance(km)' in df_valid.columns: + df_valid['epi_dist_km'] = df_valid['epidistance(km)'].astype(np.float64) + df_valid['hypo_dist'] = df_valid['ipodistance(km)'].astype(np.float64) + df_valid['epi_azimuth'] = 0.0 else: - # Percorso JSON: calcolo geodetico Obspy+NumPy - if origin and 'lat' in origin and 'lon' in origin: - # Controllo validità coordinate stazione - valid_coord_mask = dfa_valid['lat'].notna() & dfa_valid['lon'].notna() - dfa_valid = dfa_valid[valid_coord_mask].copy() + if ev_origin and 'lat' in ev_origin and 'lon' in ev_origin: + valid_coord_mask = df_valid['lat'].notna() & df_valid['lon'].notna() + df_valid = df_valid[valid_coord_mask].copy() - evla = float(origin['lat']) - evlo = float(origin['lon']) - evdp = float(origin['depth']) + evla = float(ev_origin['lat']) + evlo = float(ev_origin['lon']) + evdp = float(ev_origin['depth']) def calc_dist_az(stla, stlo): distmeters, azi, _ = gps2dist_azimuth(stla, stlo, evla, evlo) return distmeters / 1000.0, azi vec_dist_az = np.vectorize(calc_dist_az, otypes=[np.float64, np.float64]) - dfa_valid['epi_dist_km'], dfa_valid['epi_azimuth'] = vec_dist_az( - dfa_valid['lat'].values, - dfa_valid['lon'].values + df_valid['epi_dist_km'], df_valid['epi_azimuth'] = vec_dist_az( + df_valid['lat'].to_numpy(), + df_valid['lon'].to_numpy() ) - dfa_valid['stel_km'] = dfa_valid['elev'].astype(np.float64) / 1000.0 - dfa_valid['hypo_dist'] = np.sqrt( - np.square(dfa_valid['epi_dist_km']) + np.square(evdp + dfa_valid['stel_km']) + df_valid['stel_km'] = df_valid['elev'].astype(np.float64) / 1000.0 + df_valid['hypo_dist'] = np.sqrt( + np.square(df_valid['epi_dist_km']) + np.square(evdp + df_valid['stel_km']) ) else: sys.stderr.write("Dati di origine mancanti per il calcolo delle distanze.\n") sys.exit(1) - # Restituiamo anche 'met' così il main può usarlo - return dfa_valid, met + return df_valid, method_type + def calc_huttonboore_vec(a, d, s, uc): - """Vettorizzazione della legge Hutton & Boore""" - # s = 0 if not uc else s s_arr = np.where(uc, s, 0.0) - # Ritorna np.nan invece di False in caso di valori invalidi (es. a <= 0) with np.errstate(divide='ignore', invalid='ignore'): - m = np.where(a > 0, np.log10(a) + 1.110 * np.log10(d / 100.) + 0.00189 * (d - 100.) + 3.0 + s_arr, np.nan) - return m + mag_val = np.where(a > 0, np.log10(a) + 1.110 * np.log10(d / 100.) + 0.00189 * (d - 100.) + 3.0 + s_arr, np.nan) + return mag_val def calc_dibona_vec(a, d, s, uc): - """Vettorizzazione della legge Di Bona""" s_arr = np.where(uc, s, 0.0) with np.errstate(divide='ignore', invalid='ignore'): - m = np.where(a > 0, np.log10(a) + 1.667 * np.log10(d / 100.) + 0.001736 * (d - 100.) + 3.0 + s_arr, np.nan) - return m + mag_val = np.where(a > 0, np.log10(a) + 1.667 * np.log10(d / 100.) + 0.001736 * (d - 100.) + 3.0 + s_arr, + np.nan) + return mag_val -def apply_magnitudes_and_group(dfa_valid, dbcorr, use_stcorr_hb, use_stcorr_db, when_no_stcorr_hb, when_no_stcorr_db): +def apply_magnitudes_and_group(df_valid, db_corr, use_stcorr_h, use_stcorr_d, when_no_stcorr_h, when_no_stcorr_d): """ - Sostituisce la seconda parte del ciclo iterrows. - Calcola ML in vettoriale e prepara i dati per i set N ed E. + Calcola ML in vettoriale e prepara i dati per l'aggregazione di stazione. """ - # 1. Merge vettoriale delle correzioni di stazione (Di Bona) - if dbcorr is not False and not dbcorr.empty: - # Aggiungiamo la colonna 'c' di dbcorr associandola alla stazione ('sta') - dfa_valid = pd.merge(dfa_valid, dbcorr[['sta', 'c']], on='sta', how='left') - dfa_valid.rename(columns={'c': 's_dibona'}, inplace=True) - dfa_valid['s_dibona'] = dfa_valid['s_dibona'].fillna(False) + if db_corr is not False and not db_corr.empty: + df_valid = pd.merge(df_valid, db_corr[['sta', 'c']], on='sta', how='left') + df_valid.rename(columns={'c': 's_dibona'}, inplace=True) + df_valid['s_dibona'] = df_valid['s_dibona'].fillna(False) else: - dfa_valid['s_dibona'] = False + df_valid['s_dibona'] = False - dfa_valid['s_hutton'] = False # Da codice originale è sempre False + df_valid['s_hutton'] = False - # Condizioni per il calcolo - calc_hb_mask = (dfa_valid['s_hutton'] != False) | ((dfa_valid['s_hutton'] == False) & when_no_stcorr_hb) - calc_db_mask = (dfa_valid['s_dibona'] != False) | ((dfa_valid['s_dibona'] == False) & when_no_stcorr_db) + calc_hb_mask = (df_valid['s_hutton'] != False) | ((df_valid['s_hutton'] == False) & when_no_stcorr_h) + calc_db_mask = (df_valid['s_dibona'] != False) | ((df_valid['s_dibona'] == False) & when_no_stcorr_d) - # 2. Calcolo vettoriale delle ML - dfa_valid['ml_hb'] = np.nan - dfa_valid.loc[calc_hb_mask, 'ml_hb'] = calc_huttonboore_vec( - dfa_valid.loc[calc_hb_mask, 'amp_calc'].values, - dfa_valid.loc[calc_hb_mask, 'hypo_dist'].values, - dfa_valid.loc[calc_hb_mask, 's_hutton'].values, - use_stcorr_hb + df_valid['ml_hb'] = np.nan + df_valid.loc[calc_hb_mask, 'ml_hb'] = calc_huttonboore_vec( + df_valid.loc[calc_hb_mask, 'amp_calc'].to_numpy(), + df_valid.loc[calc_hb_mask, 'hypo_dist'].to_numpy(), + df_valid.loc[calc_hb_mask, 's_hutton'].to_numpy(), + use_stcorr_h ) - dfa_valid['ml_db'] = np.nan - dfa_valid.loc[calc_db_mask, 'ml_db'] = calc_dibona_vec( - dfa_valid.loc[calc_db_mask, 'amp_calc'].values, - dfa_valid.loc[calc_db_mask, 'hypo_dist'].values, - dfa_valid.loc[calc_db_mask, 's_dibona'].values, - use_stcorr_db + df_valid['ml_db'] = np.nan + df_valid.loc[calc_db_mask, 'ml_db'] = calc_dibona_vec( + df_valid.loc[calc_db_mask, 'amp_calc'].to_numpy(), + df_valid.loc[calc_db_mask, 'hypo_dist'].to_numpy(), + df_valid.loc[calc_db_mask, 's_dibona'].to_numpy(), + use_stcorr_d ) - # 3. Creazione della chiave univoca per stazione/canale - # Estrae i primi due caratteri del canale (es. 'HH' da 'HHN') - dfa_valid['cha_prefix'] = dfa_valid['cha'].str[0:2] - dfa_valid['components_key'] = dfa_valid['net'] + '_' + dfa_valid['sta'] + '_' + dfa_valid['loc'] + '_' + dfa_valid[ + df_valid['cha_prefix'] = df_valid['cha'].str[0:2] + df_valid['components_key'] = df_valid['net'] + '_' + df_valid['sta'] + '_' + df_valid['loc'] + '_' + df_valid[ 'cha_prefix'] + df_valid['orientation'] = df_valid['cha'].str[2] - # Identifica l'orientamento (N, E, Z) - dfa_valid['orientation'] = dfa_valid['cha'].str[2] + return df_valid - return dfa_valid -###################################################################################### -def huttonboore(a,d,s,uc): - # if uc (use_stcorr_hb) is True s is set to its entrance value and if this value is not False it is set to its value - # if uc (use_stcorr_hb) is True s is set to its entrance value and if this value is False it is set to ZERO - # if uc (use_stcorr_hb) is False s entrance value is overwritten and it is set to ZERO and then in the second condition it is ZERO (so it is True because it has a value) and remains ZERO - s = 0 if not uc else s # station correction is set to 0 if use_stcorr_hb is False - s = 0 if not s else s # station correction is set to 0 if it is False - try: - m = np.log10(a) + 1.110*np.log10(d / 100.) + 0.00189*(d - 100.) + 3.0 + s # Hutton & Boore with s added but not confirmed if it is correct - except: - m = False - return m - -def dibona(a,d,s,uc): - # if uc (use_stcorr_db) is True s is set to its entrance value and if this value is not False it is set to its value - # if uc (use_stcorr_db) is True s is set to its entrance value and if this value is False it is set to ZERO - # if uc (use_stcorr_db) is False s entrance value is overwritten and it is set to ZERO and then in the second condition it is ZERO (so it is True because it has a value) and remains ZERO - s = 0 if not uc else s # station correction is set to 0 if use_stcorr_hb is False - s = 0 if not s else s # station correction is set to 0 if it is False - try: - m = np.log10(a) + 1.667*np.log10(d / 100.) + 0.001736*(d - 100.) + 3.0 + s # Massimo Di Bona - except: - m = False - return m - - -def create_sets_vectorized(dfa_processed, mtd, mid, mad, dp, mty, whstc, stc, mmt, amt): +def create_sets_vectorized(dfa_proc, mtd, mid, mad, dp, mty, stc, mmt, amt): """ - Sostituisce il vecchio 'create_sets'. Usa Pandas per allineare canali N ed E, filtrare vettorialmente e calcolare la ML di stazione. """ - # 1. Filtriamo solo i canali N ed E dal nostro dataframe processato - df_ne = dfa_processed[dfa_processed['orientation'].isin(['N', 'E'])].copy() + df_ne = dfa_proc[dfa_proc['orientation'].isin(['N', 'E'])].copy() - # 2. Pivot: Trasformiamo le righe in colonne. - # Ora avremo una riga per ogni 'components_key' con le relative colonne per N ed E - df_p = df_ne.pivot(index='components_key', columns='orientation') + if df_ne.empty: + return [], 99999.0, [] - # 3. Eliminiamo le stazioni che non hanno entrambe le componenti valide + df_p = df_ne.pivot(index='components_key', columns='orientation') df_p = df_p.dropna(subset=[('amp_calc', 'N'), ('amp_calc', 'E')]) if df_p.empty: return [], 99999.0, [] - # 4. Calcolo delle medie per tutte le stazioni simultaneamente mean_hypo = (df_p[('hypo_dist', 'N')] + df_p[('hypo_dist', 'E')]) / 2.0 mean_epi = (df_p[('epi_dist_km', 'N')] + df_p[('epi_dist_km', 'E')]) / 2.0 - # 5. Creazione della Maschera di Validità (Filtri su distanze e delta peaks) valid_mask = (mean_hypo >= mid) & (mean_hypo <= mad) if mtd == 'free': - # Assicuriamoci che i tempi siano numerici per la sottrazione t_max_n = pd.to_numeric(df_p[('time_maxamp', 'N')], errors='coerce') t_min_n = pd.to_numeric(df_p[('time_minamp', 'N')], errors='coerce') t_max_e = pd.to_numeric(df_p[('time_maxamp', 'E')], errors='coerce') @@ -299,920 +214,519 @@ def create_sets_vectorized(dfa_processed, mtd, mid, mad, dp, mty, whstc, stc, mm valid_mask &= (dp_n <= dp) & (dp_e <= dp) - df_valid = df_p[valid_mask].copy() - if df_valid.empty: + df_v = df_p[valid_mask].copy() + if df_v.empty: return [], 99999.0, [] - # 6. Calcolo della Magnitudo di Stazione mty_str = 'ml_hb' if mty == 0 else 'ml_db' s_str = 's_hutton' if mty == 0 else 's_dibona' calc_func = calc_huttonboore_vec if mty == 0 else calc_dibona_vec + sta_ml = None + if mmt == 'meanmag': - sta_ml = (df_valid[(mty_str, 'N')] + df_valid[(mty_str, 'E')]) / 2.0 + sta_ml = (df_v[(mty_str, 'N')] + df_v[(mty_str, 'E')]) / 2.0 elif mmt == 'meanamp': - amp_n, amp_e = df_valid[('amp_calc', 'N')], df_valid[('amp_calc', 'E')] - - # Matematica pura e vettoriale - if amt == 'ari': - mean_amp = (amp_n + amp_e) / 2.0 - else: # 'geo' - mean_amp = np.sqrt(amp_n * amp_e) + amp_n, amp_e = df_v[('amp_calc', 'N')], df_v[('amp_calc', 'E')] + mean_amp = (amp_n + amp_e) / 2.0 if amt == 'ari' else np.sqrt(amp_n * amp_e) - # Gestiamo eventuali correzioni assenti (False/NaN) convertendole a 0 per il calcolo - corr_n = pd.to_numeric(df_valid[(s_str, 'N')], errors='coerce').fillna(0) - corr_e = pd.to_numeric(df_valid[(s_str, 'E')], errors='coerce').fillna(0) + corr_n = pd.to_numeric(df_v[(s_str, 'N')], errors='coerce').fillna(0) + corr_e = pd.to_numeric(df_v[(s_str, 'E')], errors='coerce').fillna(0) mean_corr = (corr_n + corr_e) / 2.0 - sta_ml = calc_func(mean_amp.values, mean_hypo[valid_mask].values, mean_corr.values, stc) + sta_ml = calc_func(mean_amp.to_numpy(), mean_hypo[valid_mask].to_numpy(), mean_corr.to_numpy(), stc) - # 7. Ricostruzione dell'output nel formato atteso dal resto del codice - valid_keys = df_valid.index.astype(str) + '_' + mtd + valid_keys = df_v.index.astype(str) + '_' + mtd ml_set = [[k, m] for k, m in zip(valid_keys, sta_ml) if pd.notna(m)] midi = mean_epi[valid_mask].min() if not mean_epi[valid_mask].empty else 99999.0 - - # I log verranno rimandati a un costruttore esterno per efficienza logsms = [] return ml_set, midi, logsms -#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> -# Calculating the weighted standard deviation -# from SUBROUTINE MEDIA_HUBER Programmed on 1 september 2003 by Franco Mele (INGV) -# Formula is: -# ___N -# \ 2 -# \ Wi(Xi - Xmed) -# / -# /__i=1 -# SQRT( ------------------------------------- ) -# -# N'-1 __N -# ----- ( \ Wi ) -# N' /_i=1 -# -#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< -def wstd(v,wm,wf): - eps2=5.0e-6 + +def wstd(v, wm, wf): + eps2 = 5.0e-6 if len(v) <= 1: - wsd = False - else: - index_not_zero = np.where(wf > eps2)[0] - wf_nz = wf[index_not_zero] - #print("wf_nz: ",wf_nz) - v_nz = v[index_not_zero] - wsd = np.sum(wf_nz*np.square((v_nz-wm))) - fac = ((len(wf_nz)-1)/len(wf_nz))*np.sum(wf_nz) - wsd = wsd / max(fac,eps2) - wsd = np.sqrt(max(wsd,eps2)) - return wsd - -def whuber(v,w_mean,ruse,zero): + return False + index_not_zero = np.where(wf > eps2)[0] + wf_nz = wf[index_not_zero] + v_nz = v[index_not_zero] + wsd = np.sum(wf_nz * np.square((v_nz - wm))) + fac = ((len(wf_nz) - 1) / len(wf_nz)) * np.sum(wf_nz) + wsd = wsd / max(fac, eps2) + return np.sqrt(max(wsd, eps2)) + + +def whuber(v, w_mean, ruse, zero_val): res = abs(v - w_mean) with np.errstate(divide='ignore'): - w = np.where(res <= ruse,1.0,np.where(res > zero,0.0,ruse/res)) - #print("W: ",w) + w = np.where(res <= ruse, 1.0, np.where(res > zero_val, 0.0, ruse / res)) try: - w_mean = np.sum(v * w)/np.sum(w) - except Exception as e: - sys.stderr.write(("Error calculalting the Huber Mean",e)) - w_mean = False - if w_mean: - w_std = wstd(v,w_mean,w) - else: - w_std = False + w_mean = np.sum(v * w) / np.sum(w) + except ZeroDivisionError as err: + sys.stderr.write(f"Error calculating the Huber Mean: {err}\n") + w_mean = False + + w_std = wstd(v, w_mean, w) if w_mean else False w_std = 0.0 if not w_std else w_std - return w_mean,w_std,w + return w_mean, w_std, w + -def rm_outliers(v,v_w,v_flag,v_mean,v_std,times_std,co,var_stop,it_max,skip): +def rm_outliers(v, v_w, v_flag, v_mean, v_std, times_std, co, skip): res = abs(v - v_mean) - #w_fake = np_ones(len(v)) - v_mean_old = v_mean cut_limit = times_std * v_std if (times_std * v_std) > co else co - not_outlier = res < cut_limit + not_outlier = res < cut_limit yes_outlier = res >= cut_limit - skip.append(list(zip(v_flag[yes_outlier],v[yes_outlier]))) - v_w[yes_outlier]=0 - v_w[not_outlier]=1 - #v = v[not_outlier] - #v_flag = v_flag[not_outlier] + skip.append(list(zip(v_flag[yes_outlier], v[yes_outlier]))) + v_w[yes_outlier] = 0 + v_w[not_outlier] = 1 + if len(v[not_outlier]) > 0: - v_std = median_abs_deviation(v[not_outlier]) - v_mean = np.median(v[not_outlier]) - n_v_flag = len(v[not_outlier]) + v_std = median_abs_deviation(v[not_outlier]) + v_mean = np.median(v[not_outlier]) else: - v_std = False - v_mean = False - n_v_flag = False - return v_mean,v_std,v_w,skip + v_std = False + v_mean = False + return v_mean, v_std, v_w, skip -def calculate_event_ml(magnitudes,magnitudes_sta,it_max,var_stop,max_dev,out_cutoff,hm_cutoff): + +def calculate_event_ml(magnitudes, magnitudes_sta, it_max, var_stop, max_dev, out_cutoff, hm_cutoff_val): v = np.array(magnitudes) s = magnitudes_sta - ruse = hm_cutoff - # Calculate starting values from m (magnitudes) vector - # We here use a median and mad instead of the mean and std as stating values because they are more robust - # Names are taken from Huber Mean routine by Franco Mele for coherence + ruse = hm_cutoff_val xmd = np.median(v) - xmd_std = median_abs_deviation(v) + xmd_std = median_abs_deviation(v) vlen_start = len(v) n = 1 finished = False - if hm_cutoff: - ruse,zero = [hm_cutoff,9999.0] if isinstance(hm_cutoff,float) else hm_cutoff + + if hm_cutoff_val: + ruse, zero_val = [hm_cutoff_val, 9999.0] if isinstance(hm_cutoff_val, float) else hm_cutoff_val + else: + zero_val = 9999.0 + weights = np.ones(len(v)) - removed=[] + removed = [] + whuber_fail = False + typemean = 'unknown' + whystop = 'unknown' + while not finished: - amd = xmd - if hm_cutoff: - #print("Calling Whuber","ruse: ",ruse,"zero: ",zero,"V: ",v,"XMD: ",xmd) - whuber_fail = False - try: - xmd,xmd_std,weights = whuber(v,xmd,ruse,zero) - typemean = 'whuber' - except Exception as e: - whuber_fail = True - if not hm_cutoff or whuber_fail: - xmd,xmd_std,weights,removed = rm_outliers(v,weights,s,xmd,xmd_std,max_dev,out_cutoff,var_stop,it_max,removed) - if not whuber_fail: - typemean = 'rmoutl' - else: - typemean = 'rowhfl' - xmd_var = abs(amd-xmd) - if xmd_var <= var_stop: - finished = True - whystop=typemean+'_varstop='+str(xmd_var)+'_n='+str(n) - elif n == it_max: - finished = True - whystop=typemean+'_maxit='+str(n)+'_xmdvar='+str(xmd_var) - n += 1 - vlen_stop = round(np.sum(weights),2) - return xmd,xmd_std,vlen_start,vlen_stop,whystop,removed,weights,whuber_fail - -def char_quality(nu,nw,d,m,r): - #nu = number of used stations - #nw = weighted number of used stations - # d = minimum distance km - # m = magnitude - # defining position 1: this character is defined according to a rule described in a Sismo-CAT course at INGV - # uploaded in Google Drive on 24 May 2019 by Anna Nardi (https://docs.google.com/presentation/d/16ZzCgUQhkS7eIcbVNX-IH8IAmbPbQ7K6Fq45jte3ON0/edit#slide=id.p12) - # there, only A B C are defined: outside of this definitions, all is D - # In the original formula, the nu is the number of components: since in pyml we do not use separate channels but station mean ML values, to keep the validity of - # the adopted parameters, we multiply nu by 2 channels + amd = xmd + if hm_cutoff_val: + whuber_fail = False + try: + xmd, xmd_std, weights = whuber(v, xmd, ruse, zero_val) + typemean = 'whuber' + except (ZeroDivisionError, ValueError) as err: + logging.warning(f"Calcolo della media di Huber fallito. Fallback in rm_outliers. Errore: {err}") + whuber_fail = True + + if not hm_cutoff_val or whuber_fail: + xmd, xmd_std, weights, removed = rm_outliers(v, weights, s, xmd, xmd_std, max_dev, out_cutoff, removed) + typemean = 'rowhfl' if whuber_fail else 'rmoutl' + + xmd_var = abs(amd - xmd) + if xmd_var <= var_stop: + finished = True + whystop = f"{typemean}_varstop={xmd_var}_n={n}" + elif n == it_max: + finished = True + whystop = f"{typemean}_maxit={n}_xmdvar={xmd_var}" + n += 1 + + vlen_stop = round(np.sum(weights), 2) + return xmd, xmd_std, vlen_start, vlen_stop, whystop, removed, weights, whuber_fail + + +def char_quality(nu, d, m, r): q1type = 'course' + q1, q2 = 'D', 'D' if q1type == 'basili': - if (np.log2(nu*2)-2) >= m: - q1 = 'A' - elif (np.log2(nu*2)-1) >= m: - q1 = 'B' - elif np.log2(nu*2) >= m: - q1 = 'C' - elif np.log2(nu*2) < m: - q1 = 'D' - else: - q1 = 'E' + if (log2(nu * 2) - 2) >= m: + q1 = 'A' + elif (log2(nu * 2) - 1) >= m: + q1 = 'B' + elif log2(nu * 2) >= m: + q1 = 'C' + elif log2(nu * 2) < m: + q1 = 'D' + else: + q1 = 'E' elif q1type == 'course': - if d <= 50. and nu*2 > (m*10.0+1): + if d <= 50. and nu * 2 > (m * 10.0 + 1): q1 = 'A' - elif d <= 100.0 and nu*2 > (m*7.5+1): + elif d <= 100.0 and nu * 2 > (m * 7.5 + 1): q1 = 'B' - elif d <= 150.0 and nu*2 > (m*5.0+1): + elif d <= 150.0 and nu * 2 > (m * 5.0 + 1): q1 = 'C' - else: + else: q1 = 'D' - # defining position 2 (by Alberto Basili for INGV Locator code, statement since 2010) - #A = rms<=0.2 - #B = 0.2 0.2 and r <= 0.3: - q2 = 'B' - elif r > 0.3 and r <= 0.4: - q2 = 'C' + q2 = 'A' + elif 0.2 < r <= 0.3: + q2 = 'B' + elif 0.3 < r <= 0.4: + q2 = 'C' elif r > 0.4: - q2 = 'D' - return ''.join((q1,q2)) - -def standard_pyml_load(infile,eventid,conf_file,log_out,f): - # Now loading the configuration file - if os.path.exists(conf_file) and os.path.getsize(conf_file) > 0: - paramfile=conf_file - else: - sys.stderr.write("Config file " + args.conf + " not existing or empty\n\n") - sys.exit(2) - confObj = cp.ConfigParser() - confObj.read(paramfile) - - #try: - # filenames_parameters=get_config_dictionary(confObj, 'iofilenames') - #except Exception as e: - # sys.stderr.write(("\n"+str(e)+"\n\n")) - # sys.exit(1) - #try: - # mf=eval(filenames_parameters['magnitudes']) - # lf=eval(filenames_parameters['log']) - # if mf: - # magnitudes_out=open(str(eventid)+'_'+str(mf),'w') - # else: - # magnitudes_out=open(str(eventid)+'_pyml_magnitudes.out','w') - # if lf: - # log_out=open(str(eventid)+'_'+str(lf),'w') - # else: - # log_out=open(str(eventid)+'_pyml_general.log','w') - #except Exception as e: - # sys.stderr.write(("\n"+str(e)+"\n\n")) - # sys.exit(1) - - - #Net;Sta;Loc;Cha;Lat;Lon;Ele;EpiDistance(km);IpoDistance(km);MinAmp(m);MinAmpTime;MaxAmp(m);MaxAmpTime;DeltaPeaks;Method;NoiseWinMin;NoiseWinMax;SignalWinMin;SignalWinMax;P_Pick;Synth;S_Picks;Synth;Nyq;LoCo;HiCo;LenOverSNRIn;SNRIn;ML_H;CORR_HB;CORR_USED_HB;ML_DB;CORR_DB;CORR_USED_DB - dfa=pd.read_csv(f,sep=';',index_col=False) - dfa.columns = dfa.columns.str.lower() - - # Here we load the basic condition to decide if to proceed or not - # If there is no P do we proceed with theoretical or skip the channel? - # If there is no S do we proceed with theoretical or skip the channel? - preconditions_parameters=get_config_dictionary(confObj, 'preconditions') - theoP=eval(preconditions_parameters['theoretical_p']) - theoS=eval(preconditions_parameters['theoretical_s']) - delta_corner=float(preconditions_parameters['delta_corner']) - max_lowcorner=float(preconditions_parameters['max_lowcorner']) - - try: - station_magnitude_parameters=get_config_dictionary(confObj, 'station_magnitude') - except: - log_out.write("No section 'station_magnitude' in config file") - sys.exit() - try: - mag_mean_type=eval(station_magnitude_parameters['mag_mean_type']) - except: - log_out.write("No parameter 'mag_mean_type' in section 'station_magnitude' of config file") - sys.exit() - if mag_mean_type == 'meanamp': - try: - amp_mean_type=eval(station_magnitude_parameters['amp_mean_type']) - except: - log_out.write("No parameter 'amp_mean_type' in section 'station_magnitude' of config file") - sys.exit() - try: - delta_peaks=float(station_magnitude_parameters['delta_peaks']) - except: - log_out.write("No parameter 'delta_peaks' in section 'station_magnitude' of config file") - sys.exit() - try: - use_stcorr_hb=eval(station_magnitude_parameters['use_stcorr_hb']) - except: - log_out.write("No parameter 'use_stcorr_hb' in section 'station_magnitude' of config file") - sys.exit() - try: - use_stcorr_db=eval(station_magnitude_parameters['use_stcorr_db']) - except: - log_out.write("No parameter 'use_stcorr_db' in section 'station_magnitude' of config file") - sys.exit() - try: - when_no_stcorr_hb=eval(station_magnitude_parameters['when_no_stcorr_hb']) - except: - log_out.write("No parameter 'when_no_stcorrhb' in section 'station_magnitude' of config file") - sys.exit() - try: - when_no_stcorr_db=eval(station_magnitude_parameters['when_no_stcorr_db']) - except: - log_out.write("No parameter 'when_no_stcorrdb' in section 'station_magnitude' of config file") - sys.exit() - - try: - event_magnitude_parameters=get_config_dictionary(confObj, 'event_magnitude') - except: - log_out.write("No section 'event_magnitude' in config file") - sys.exit() - try: - mindist=float(event_magnitude_parameters['mindist']) - except: - log_out.write("No parameter 'mindist' in section 'event_magnitude' of config file") - sys.exit() - try: - maxdist=float(event_magnitude_parameters['maxdist']) - except: - log_out.write("No parameter 'maxdist' in section 'event_magnitude' of config file") - sys.exit() - try: - hm_cutoff=eval(event_magnitude_parameters['hm_cutoff']) - except: - log_out.write("No parameter 'hm_cutoff' in section 'event_magnitude' of config file") - sys.exit() - try: - outliers_max_it=int(event_magnitude_parameters['outliers_max_it']) - except: - log_out.write("No parameter 'outliers_max_it' in section 'event_magnitude' of config file") - sys.exit() - try: - outliers_red_stop=float(event_magnitude_parameters['outliers_red_stop']) - except: - log_out.write("No parameter 'outliers_red_stop' in section 'event_magnitude' of config file") - sys.exit() - try: - outliers_nstd=float(event_magnitude_parameters['outliers_nstd']) - except: - log_out.write("No parameter 'outliers_nstd' in section 'event_magnitude' of config file") - sys.exit() - try: - outliers_cutoff=float(event_magnitude_parameters['outliers_cutoff']) - except: - log_out.write("No parameter 'outliers_cutoff' in section 'event_magnitude' of config file") - sys.exit() - return dfa,theoP,theoS,delta_corner,max_lowcorner,delta_peaks,use_stcorr_hb,use_stcorr_db,when_no_stcorr_hb,when_no_stcorr_db,mindist,maxdist,hm_cutoff,outliers_max_it,outliers_red_stop,outliers_nstd,outliers_cutoff - -def json_pyml_load(json_in): - try: - config=json_in['data']['pyml_conf'] - except: - config=False - try: - origin=json_in['data']['origin'] - except: - origin=False - try: - dfa = pd.DataFrame(json_in['data']['amplitudes']) - except: - dfa=pd.DataFrame() - return dfa,config,origin - -# JSON ENCODER CLASS + q2 = 'D' + return ''.join((q1, q2)) + + +def standard_pyml_load(infile_val, conf_file_val, log_out_val): + if os.path.exists(conf_file_val) and os.path.getsize(conf_file_val) > 0: + paramfile = conf_file_val + else: + sys.stderr.write(f"Config file {conf_file_val} not existing or empty\n\n") + sys.exit(2) + + confObj = cp.ConfigParser() + confObj.read(paramfile) + df_amp = pd.read_csv(infile_val, sep=';', index_col=False) + df_amp.columns = df_amp.columns.str.lower() + + pre_params = get_config_dictionary(confObj, 'preconditions') + theo_p_val = eval(pre_params['theoretical_p']) + theo_s_val = eval(pre_params['theoretical_s']) + + sta_params = get_config_dictionary(confObj, 'station_magnitude') + mag_mean_type_val = eval(sta_params['mag_mean_type']) + amp_mean_type_val = eval(sta_params['amp_mean_type']) if mag_mean_type_val == 'meanamp' else None + delta_peaks_val = float(sta_params['delta_peaks']) + use_stcorr_hb_val = eval(sta_params['use_stcorr_hb']) + use_stcorr_db_val = eval(sta_params['use_stcorr_db']) + when_no_stcorr_hb_val = eval(sta_params['when_no_stcorr_hb']) + when_no_stcorr_db_val = eval(sta_params['when_no_stcorr_db']) + + ev_params = get_config_dictionary(confObj, 'event_magnitude') + mindist_val = float(ev_params['mindist']) + maxdist_val = float(ev_params['maxdist']) + hm_cutoff_val = eval(ev_params['hm_cutoff']) + outliers_max_it_val = int(ev_params['outliers_max_it']) + outliers_red_stop_val = float(ev_params['outliers_red_stop']) + outliers_nstd_val = float(ev_params['outliers_nstd']) + outliers_cutoff_val = float(ev_params['outliers_cutoff']) + + return df_amp, theo_p_val, theo_s_val, delta_peaks_val, use_stcorr_hb_val, use_stcorr_db_val, when_no_stcorr_hb_val, when_no_stcorr_db_val, mindist_val, maxdist_val, hm_cutoff_val, outliers_max_it_val, outliers_red_stop_val, outliers_nstd_val, outliers_cutoff_val, mag_mean_type_val, amp_mean_type_val + + +def json_pyml_load(json_input): + cfg = json_input.get('data', {}).get('pyml_conf', False) + orig = json_input.get('data', {}).get('origin', False) + amps = json_input.get('data', {}).get('amplitudes', []) + df_amp = pd.DataFrame(amps) + return df_amp, cfg, orig + + class DataEncoder(json.JSONEncoder): def default(self, o): if isinstance(o, decimal.Decimal): return float(o) - if isinstance(o, datetime): return o.isoformat() - return json.JSONEncoder.default(self, o) + def json_response_structure(): - null='' + null = '' generic = { - "type": null, - "title": null, - "status": null, - "detail": null, - "instance": null, - "version": null, - "request_submitted": null, - "errors": { - "inputfile": [], - "arguments": [] - } - } + "type": null, "title": null, "status": null, "detail": null, + "instance": null, "version": null, "request_submitted": null, + "errors": {"inputfile": [], "arguments": []} + } response = { - "random_string": null, - "hostname": {}, - "magnitudes": {}, - "stationmagnitudes": [], - "log": {} - } - - log = { - "magnitude": {}, - "stationmagnitudes": [] - } + "random_string": null, "hostname": {}, "magnitudes": {}, + "stationmagnitudes": [], "log": {} + } + log_dict = {"magnitude": {}, "stationmagnitudes": []} log_magnitude = { - "hb": { - "status": null, # eg: "warning" - "summary": null, # eg: "In ML HuttonBoore Station IV_FDMO_--_HH_ingv skipped due to ipodist" - "extended": null, # eg: "Distance is 6.045133888690926 km" - }, - "db": { - "status": null, # eg: "warning", - "summary": null, # eg: "In ML HuttonBoore Station IV_FDMO_--_HH_ingv skipped due to ipodist", - "extended": null, # eg: "Distance is 6.045133888690926 km" - } - } + "hb": {"status": null, "summary": null, "extended": null}, + "db": {"status": null, "summary": null, "extended": null} + } log_stationmagnitude = { - "net": null, # eg: "IV" - "sta": null, # eg: "FDMO", - "loc": null, # eg: "--", - "band_inst": null, # eg: "HH", - "status": null, # eg: "warning", - "summary": null, # eg: "In ML HuttonBoore Station IV_FDMO_--_HH_ingv skipped due to ipodist", - "extended": null, # eg: "Distance is 6.045133888690926 km" - "channels": [] - } + "net": null, "sta": null, "loc": null, "band_inst": null, + "status": null, "summary": null, "extended": null, "channels": [] + } log_stationmagnitude_channel = { - "orientation": null, # "N", - "status": null, # "ok", - "summary": null, # "In ML HuttonBoore Station IV_FDMO_--_HH_ingv skipped due to ipodist", - "extended": null, # "Distance is 6.045133888690926 km" - } + "orientation": null, "status": null, "summary": null, "extended": null + } magnitudes = { - "hb": {}, - "db": {}, - "ampmethod": null, - "magmethod": null, - "loopexitcondition": null - } + "hb": {}, "db": {}, "ampmethod": null, "magmethod": null, "loopexitcondition": null + } emag = { - "ml": null, - "std": null, - "totsta": null, - "usedsta": null, - "quality": '--' - } - + "ml": null, "std": null, "totsta": null, "usedsta": null, "quality": '--' + } stationmagnitude = { - "net": null, - "sta": null, - "cha": null, - "loc": null, - "amp1": null, - "time1": null, - "amp2": null, - "time2": null, -# "period": null, -# "lat": null, -# "lon": null, -# "elev": null, - "ep_distance_km": null, - "ep_distance_delta": null, - "orig_distance": null, - "azimuth": null, - "hb": { - "ml": null, - "w": null - }, - "db": { - "ml": null, - "w": null - } - } - return response,generic,log,log_magnitude,log_stationmagnitude,log_stationmagnitude_channel,magnitudes,stationmagnitude,emag + "net": null, "sta": null, "cha": null, "loc": null, + "amp1": null, "time1": null, "amp2": null, "time2": null, + "ep_distance_km": null, "ep_distance_delta": null, + "orig_distance": null, "azimuth": null, + "hb": {"ml": null, "w": null}, + "db": {"ml": null, "w": null} + } + return response, generic, log_dict, log_magnitude, log_stationmagnitude, log_stationmagnitude_channel, magnitudes, stationmagnitude, emag + def json_pyml_response(r): - x=json.dumps(r,cls=DataEncoder) - return x + return json.dumps(r, cls=DataEncoder) + ############################################################### -###### End of Functions ########## ###### Main starts here ########## -pyml_version="1.0.0" -args = parseArguments() - -# setting up geographiclib +pyml_version = "1.0.0" +args = parse_arguments() geod = Geodesic.WGS84 -exit_condition=False -jresponse,jbadrequest,jlog,jlog_mag,jlog_stamag,jlog_stamag_cha,jmagnitudes,jstationmagnitude,jemag = json_response_structure() -# JSON STRUCTURE DEEPCOPY -resp = copy.deepcopy(jresponse) -log = copy.deepcopy(jlog) -logm = copy.deepcopy(jlog_mag) +# Inizializzazioni sicure per il Linter e per la robustezza +dfa = pd.DataFrame() +origin = False +config = None +eventid = 0 +magnitudes_out = False +log_out = False +exit_condition = False +met = 'ingv' +use_stcorr_hb = None +use_stcorr_db = None +when_no_stcorr_hb = None +when_no_stcorr_db = None +mindist = None +maxdist = None +delta_peaks = None +mag_mean_type = None +amp_mean_type = None +outliers_max_it = None +outliers_red_stop = None +outliers_nstd = None +outliers_cutoff = None +hm_cutoff = None +theo_p = None +theo_s = None + +jresponse, jbadrequest, jlog, jlog_mag, jlog_stamag, jlog_stamag_cha, jmagnitudes, jstationmagnitude, jemag = json_response_structure() +resp = copy.deepcopy(jresponse) +log = copy.deepcopy(jlog) +logm = copy.deepcopy(jlog_mag) gresp = copy.deepcopy(jbadrequest) resp['random_string'] = 'github/ingv/pyml' resp['hostname'] = socket.gethostname() if args.out_format.lower() == 'txt': - magnitudes_out=sys.stdout - log_out=sys.stderr + magnitudes_out = sys.stdout + log_out = sys.stderr elif args.out_format.lower() == 'json': - magnitudes_out=False - log_out=False + magnitudes_out = False + log_out = False else: -# if log_out: -# sys.stderr.write("The given out format "+args.out_format.lower()+"/"+args.out_format+" is unknown\n") -# else: - log_out=False - gresp['type'] = "https://tools.ietf.org/html/rfc4918#section-11.2" - gresp['title'] = "Unprocessable entity" - gresp['status'] = "422" - gresp['detail'] = "The given data was invalid" - gresp['instance'] = str(getpass.getuser())+'@'+str(socket.gethostname()) - gresp['version'] = str(pyml_version) - gresp['request_submitted'] = str(datetime.now()) - gresp['errors']["arguments"].append("The given argument out format "+args.out_format.lower()+"/"+args.out_format+" is unknown") - exit_condition=True - -############## PYML works either in CSV in/out or JSON in/out) ############################################################# -### IF input format is csv pyml works reading csv files, with configuration loaded from a dictionary file + log_out = False + gresp['type'] = "https://tools.ietf.org/html/rfc4918#section-11.2" + gresp['status'] = "422" + gresp['errors']["arguments"].append(f"The given argument out format {args.out_format} is unknown") + exit_condition = True + if args.in_file_format.lower() == 'csv': - infile=args.in_file_name - conf_file = args.conf - eventid=args.eventid - dfa,theoP,theoS,delta_corner,max_lowcorner,delta_peaks,use_stcorr_hb,use_stcorr_db,when_no_stcorr_hb,when_no_stcorr_db,mindist,maxdist,hm_cutoff,outliers_max_it,outliers_red_stop,outliers_nstd,outliers_cutoff = standard_pyml_load(infile,eventid,conf_file,log_out,infile) - origin = False - if dfa.empty: - if log_out: - sys.stderr.write("The given input pyamp file "+infile+" was incomplete\n") - else: - gresp['type'] = "https://tools.ietf.org/html/rfc4918#section-11.2" - gresp['title'] = "Unprocessable entity" - gresp['status'] = "422" - gresp['detail'] = "The given data was invalid" - gresp['instance'] = str(getpass.getuser())+'@'+str(socket.gethostname()) - gresp['version'] = str(pyml_version) - gresp['request_submitted'] = str(datetime.now()) - gresp['errors']["inputfile"].append("The given input file "+args.in_file_name+" format "+args.in_file_format.lower()+" was incomplete") - exit_condition=True + infile = args.in_file_name + conf_file = args.conf + eventid = args.eventid + + dfa, theo_p, theo_s, delta_peaks, use_stcorr_hb, use_stcorr_db, when_no_stcorr_hb, when_no_stcorr_db, mindist, maxdist, hm_cutoff, outliers_max_it, outliers_red_stop, outliers_nstd, outliers_cutoff, mag_mean_type, amp_mean_type = standard_pyml_load( + infile, conf_file, log_out) + origin = False + if dfa.empty: + gresp['status'] = "422" + gresp['errors']["inputfile"].append("The given input file was incomplete") + exit_condition = True + elif args.in_file_format.lower() == 'json': - ### IF JSON argument is given, pyml works reading both input data a configuration options from the same json file, and it writes out results and log ONLY in one single JSON file - if os.path.exists(args.in_file_name): - json_in=pd.read_json(args.in_file_name) - dfa,config,origin = json_pyml_load(json_in) - eventid=0 - if dfa.empty or not config or not origin: - if log_out: - sys.stderr.write("The given input file "+args.in_file_name+" format "+args.in_file_format.lower()+" was incomplete") - else: - gresp['type'] = "https://tools.ietf.org/html/rfc4918#section-11.2" - gresp['title'] = "Unprocessable entity" + if os.path.exists(args.in_file_name): + try: + with open(args.in_file_name, 'r') as f: + json_in = json.load(f) + dfa, config, origin = json_pyml_load(json_in) + except json.JSONDecodeError: + dfa = pd.DataFrame() + + if dfa.empty or not config or not origin: + gresp['status'] = "422" + gresp['errors']["inputfile"].append("The given input file was incomplete") + exit_condition = True + else: + gresp['status'] = "422" + gresp['errors']["inputfile"].append("No input file found.") + exit_condition = True + + if not exit_condition: + try: + theo_p = config['preconditions']['theoretical_p'] + theo_s = config['preconditions']['theoretical_s'] + mag_mean_type = config['station_magnitude']['mag_mean_type'] + amp_mean_type = config['station_magnitude'].get('amp_mean_type', None) + delta_peaks = config['station_magnitude']['delta_peaks'] + use_stcorr_hb = config['station_magnitude']['use_stcorr_hb'] + use_stcorr_db = config['station_magnitude']['use_stcorr_db'] + when_no_stcorr_hb = config['station_magnitude']['when_no_stcorr_hb'] + when_no_stcorr_db = config['station_magnitude']['when_no_stcorr_db'] + mindist = config['event_magnitude']['mindist'] + maxdist = config['event_magnitude']['maxdist'] + hm_cutoff = config['event_magnitude']['hm_cutoff'] + outliers_max_it = config['event_magnitude']['outliers_max_it'] + outliers_red_stop = config['event_magnitude']['outliers_red_stop'] + outliers_nstd = config['event_magnitude']['outliers_nstd'] + outliers_cutoff = config['event_magnitude']['outliers_cutoff'] + except KeyError as k_err: gresp['status'] = "422" - gresp['detail'] = "The given data was invalid" - gresp['instance'] = str(getpass.getuser())+'@'+str(socket.gethostname()) - gresp['version'] = str(pyml_version) - gresp['request_submitted'] = str(datetime.now()) - gresp['errors']["inputfile"].append("The given input file "+args.in_file_name+" format "+args.in_file_format.lower()+" was incomplete") - exit_condition=True - else: - if log_out: - sys.stderr.write("No input file "+args.in_file_name+" format "+args.in_file_format.lower()+" found: exit") - else: - gresp['type'] = "https://tools.ietf.org/html/rfc4918#section-11.2" - gresp['title'] = "Unprocessable entity" - gresp['status'] = "422" - gresp['detail'] = "The given data was invalid" - gresp['instance'] = str(getpass.getuser())+'@'+str(socket.gethostname()) - gresp['version'] = str(pyml_version) - gresp['request_submitted'] = str(datetime.now()) - gresp['errors']["inputfile"].append("No input file "+args.in_file_name+" format "+args.in_file_format.lower()+" found.") - exit_condition=True - - if exit_condition: - sys.stdout.write(json_pyml_response(gresp)) - sys.exit() - # Preconditions - theoP=config['preconditions']['theoretical_p'] - theoS=config['preconditions']['theoretical_s'] - delta_corner=config['preconditions']['delta_corner'] - max_lowcorner=config['preconditions']['max_lowcorner'] - # Stations magnitude - mag_mean_type=config['station_magnitude']['mag_mean_type'] - amp_mean_type=config['station_magnitude']['amp_mean_type'] - delta_peaks=config['station_magnitude']['delta_peaks'] - use_stcorr_hb=config['station_magnitude']['use_stcorr_hb'] - use_stcorr_db=config['station_magnitude']['use_stcorr_db'] - when_no_stcorr_hb=config['station_magnitude']['when_no_stcorr_hb'] - when_no_stcorr_db=config['station_magnitude']['when_no_stcorr_db'] - # Event Magnitude - mindist=config['event_magnitude']['mindist'] - maxdist=config['event_magnitude']['maxdist'] - hm_cutoff=config['event_magnitude']['hm_cutoff'] - outliers_max_it=config['event_magnitude']['outliers_max_it'] - outliers_red_stop=config['event_magnitude']['outliers_red_stop'] - outliers_nstd=config['event_magnitude']['outliers_nstd'] - outliers_cutoff=config['event_magnitude']['outliers_cutoff'] + gresp['errors']["inputfile"].append(f"Missing config key: {k_err}") + exit_condition = True else: - if log_out: - sys.stderr.write("The given input format "+args.in_file_format.lower()+" is unknown") - else: - gresp['type'] = "https://tools.ietf.org/html/rfc4918#section-11.2" - gresp['title'] = "Unprocessable entity" - gresp['status'] = "422" - gresp['detail'] = "The given data was invalid" - gresp['instance'] = str(getpass.getuser())+'@'+str(socket.gethostname()) - gresp['version'] = str(pyml_version) - gresp['request_submitted'] = str(datetime.now()) - gresp['errors']["arguments"].append("The given input format "+args.in_file_format.lower()+" is unknown") - sys.stdout.write(json_pyml_response(gresp)) - sys.exit() - -if args.clipped_info: - clip=pd.read_csv(args.clipped_info,sep=';',index_col=False) - -cmp_keys=set() -components_N={} -components_E={} -components_Z={} -mcalc=True + gresp['status'] = "422" + gresp['errors']["arguments"].append("The given input format is unknown") + sys.stdout.write(json_pyml_response(gresp)) + sys.exit() + +if exit_condition: + sys.stdout.write(json_pyml_response(gresp)) + sys.exit() if args.dbona_corr: - try: - dbcorr=pd.read_csv(args.dbona_corr,sep=';') - except: - log_out.write("File "+args.dbona_corr+" not found\n") - sys.exit() -else: - dbcorr=False - -km=1000. -unit=1000 # pyamp units -if args.in_file_format == 'json': - unit=1 #db units - -# 1. Pulizia e calcolo distanze spaziali (tutto vettoriale con ObsPy + Numpy) -dfa_valid, met = process_amplitudes_vectorized(dfa, origin, unit, max_lowcorner, delta_corner) - -# 2. Calcolo Magnitudo (H&B, Di Bona) e Merge Correzioni (tutto vettoriale Pandas) -dfa_processed = apply_magnitudes_and_group(dfa_valid, dbcorr, use_stcorr_hb, use_stcorr_db, when_no_stcorr_hb, when_no_stcorr_db) - -# === START BRIDGE (Per mantenere vivo il costruttore JSON a fine file) === -components_N = {} -components_E = {} -for _, row in dfa_processed.iterrows(): - k = row['components_key'] + '_' + met - # Ricostruiamo in modo posticcio le tuple aspettate dal JSON writer - dummy_data = [[row['ml_hb'], row['ml_db']], [row['minamp'], row['maxamp']], row['epi_dist_km'], - 0, row['hypo_dist'], [row['s_hutton'], row['s_dibona']], - row['time_minamp'], row['time_maxamp'], 0, row['lat'], row['lon'], row['elev'], row['epi_azimuth']] - if row['orientation'] == 'N': components_N[k] = dummy_data - if row['orientation'] == 'E': components_E[k] = dummy_data -# === END BRIDGE === - - -# Hutton and Boore -mean_hb_ml_sta, min_dist, log['stationmagnitudes'] = create_sets_vectorized( - dfa_processed, met, mindist, maxdist, delta_peaks, 0, when_no_stcorr_hb, use_stcorr_hb, mag_mean_type, amp_mean_type -) - -# Di Bona -mean_db_ml_sta, min_dist, log['stationmagnitudes'] = create_sets_vectorized( - dfa_processed, met, mindist, maxdist, delta_peaks, 1, when_no_stcorr_db, use_stcorr_db, mag_mean_type, amp_mean_type -) - -#if log_out: -# log_out.write("create_sets: the execution time is: "+str(execution_time)+"\n") -if len(mean_hb_ml_sta) == 0: - msg_sts = 'critical' - msg_sum='HuttonBoore List is empty' - msg_ext = 'All the stations missing due to only one channel is present, or out of minmax distance' - if log_out: - log_out.write(msg_sts+":"+msg_sum+" --> "+msg_ext+"\n") - mlhb = False -else: - #meanmag_ml = list(list(zip(*meanmag_ml_sta))[1]) - mean_ml = list(list(zip(*mean_hb_ml_sta))[1]) - mean_ml_sta = np.asarray(list(list(zip(*mean_hb_ml_sta))[0]), dtype=object) - ma_mlh,ma_stdh,ma_ns_s_h,ma_nsh,cond_hb,outliers_hb,weights_hb,wh_hb_fail = calculate_event_ml(mean_ml,mean_ml_sta,outliers_max_it,outliers_red_stop,outliers_nstd,outliers_cutoff,hm_cutoff) - if ma_mlh: - mlhb = True - if wh_hb_fail: - if log_out: - log_out.write("Hutton&Boore: Weighted Huber Mean failed, Outliers Removal used instead\n") - msg_sts='warning' - msg_sum="Hutton&Boore: Weighted Huber Mean failed, Outliers Removal used instead" - msg_ext="" - else: - msg_sts='ok' - msg_sum="" - msg_ext="" - else: - mlhb = False - if log_out: - log_out.write("Hutton&Boore: Both Weighted Huber Mean and Outliers Removal failed\n") - msg_sts='critical' - msg_sum="Hutton&Boore: Both Weighted Huber Mean and Outliers Removal failed" - msg_ext="" -logm['hb']['status'] = msg_sts -logm['hb']['summary'] = msg_sum -logm['hb']['extended'] = msg_ext - -if len(mean_db_ml_sta) == 0: - msg_sts = 'critical' - msg_sum='Dibona List is empty' - msg_ext = 'All the stations missing due to only one channel is present, or out of minmax distance' - if log_out: - log_out.write(msg_sts+":"+msg_sum+" --> "+msg_ext+"\n") - mldb = False + try: + dbcorr = pd.read_csv(args.dbona_corr, sep=';') + except Exception as e: + if log_out: log_out.write(f"File {args.dbona_corr} not found: {e}\n") + sys.exit() else: - #meanmag_ml = list(list(zip(*meanmag_ml_sta))[1]) - mean_ml = list(list(zip(*mean_db_ml_sta))[1]) - mean_ml_sta = np.asarray(list(list(zip(*mean_db_ml_sta))[0]), dtype=object) - ma_mld,ma_stdd,ma_ns_s_d,ma_nsd,cond_db,outliers_db,weights_db,wh_db_fail = calculate_event_ml(mean_ml,mean_ml_sta,outliers_max_it,outliers_red_stop,outliers_nstd,outliers_cutoff,hm_cutoff) - if ma_mld: - mldb = True - if wh_db_fail: - if log_out: - log_out.write("Di Bona: Weighted Huber Mean failed, Outliers Removal used instead\n") - msg_st ="warning" - msg_sum="Di Bona: Weighted Huber Mean failed, Outliers Removal used instead" - msg_ext="" - else: - msg_sts ="ok" - msg_sum="" - msg_ext="" - else: - mldb = False - if log_out: - log_out.write("Di Bona: Both Weighted Huber Mean and Outliers Removal failed\n") - msg_sts='critical' - msg_sum="Di Bona: Both Weighted Huber Mean and Outliers Removal failed" - msg_ext="" - -logm['db']['status'] = msg_sts -logm['db']['summary'] = msg_sum -logm['db']['extended'] = msg_ext + dbcorr = False + +unit = 1000 if args.in_file_format.lower() == 'csv' else 1 + +# --- LOGICA VETTORIALIZZATA --- +dfa_valid, met = process_amplitudes_vectorized(dfa, origin, unit) +dfa_processed = apply_magnitudes_and_group(dfa_valid, dbcorr, use_stcorr_hb, use_stcorr_db, when_no_stcorr_hb, + when_no_stcorr_db) + +mean_hb_ml_sta, _, _ = create_sets_vectorized(dfa_processed, met, mindist, maxdist, delta_peaks, 0, use_stcorr_hb, + mag_mean_type, amp_mean_type) +mean_db_ml_sta, _, _ = create_sets_vectorized(dfa_processed, met, mindist, maxdist, delta_peaks, 1, use_stcorr_db, + mag_mean_type, amp_mean_type) + +# Gestione calcoli statistici per HB +ma_mlh, ma_stdh, ma_ns_s_h, ma_nsh = False, False, 0, 0 +cond_hb = "Failed" +weights_hb, outliers_hb = [], [] +mlhb = False + +if len(mean_hb_ml_sta) > 0: + mean_ml = [x[1] for x in mean_hb_ml_sta] + mean_ml_sta = np.asarray([x[0] for x in mean_hb_ml_sta], dtype=object) + ma_mlh, ma_stdh, ma_ns_s_h, ma_nsh, cond_hb, outliers_hb, weights_hb, wh_hb_fail = calculate_event_ml(mean_ml, + mean_ml_sta, + outliers_max_it, + outliers_red_stop, + outliers_nstd, + outliers_cutoff, + hm_cutoff) + if ma_mlh: + mlhb = True + logm['hb']['status'] = 'warning' if wh_hb_fail else 'ok' + else: + logm['hb']['status'] = 'critical' + +# Gestione calcoli statistici per DB +ma_mld, ma_stdd, ma_ns_s_d, ma_nsd = False, False, 0, 0 +cond_db = "Failed" +weights_db, outliers_db = [], [] +mldb = False + +if len(mean_db_ml_sta) > 0: + mean_ml = [x[1] for x in mean_db_ml_sta] + mean_ml_sta = np.asarray([x[0] for x in mean_db_ml_sta], dtype=object) + ma_mld, ma_stdd, ma_ns_s_d, ma_nsd, cond_db, outliers_db, weights_db, wh_db_fail = calculate_event_ml(mean_ml, + mean_ml_sta, + outliers_max_it, + outliers_red_stop, + outliers_nstd, + outliers_cutoff, + hm_cutoff) + if ma_mld: + mldb = True + logm['db']['status'] = 'warning' if wh_db_fail else 'ok' + else: + logm['db']['status'] = 'critical' resp["log"]['magnitude'] = logm -resp["log"]['stationmagnitudes'] = log['stationmagnitudes'] if not mlhb and not mldb: - if log_out: - log_out.write("Neither Hutton&Boore nor Di Bona ML could be calculate\n") - sys.stdout.write(json_pyml_response(resp)) - sys.exit() -#else: -# if not mlhb: -# if log_out: -# log_out.write("Hutton&Boore ML was impossible to calculate\n") -# sys.stdout.write(json_pyml_response(resp)) -# elif not mldb: -# if log_out: -# log_out.write("Di Bona ML was impossible to calculate\n") -# sys.stdout.write(json_pyml_response(resp)) -#mm_mld,mm_stdd,mm_ns_s_d,mm_nsd,cond = calculate_event_ml(meanmag_ml_sta,outliers_max_it,outliers_red_stop) - -###################################################### -# Writing magnitudes.csv file # -###################################################### -#if magnitudes_out: -# magnitudes_out=open(magnitudes_out,'w') -#else: -# magnitudes_out=sys.stdout - -if mag_mean_type == 'meanmag': - amp_mean_type='False' + if log_out: log_out.write("Neither Hutton&Boore nor Di Bona ML could be calculated\n") + sys.stdout.write(json_pyml_response(resp)) + sys.exit() if magnitudes_out: - magnitudes_out.write("EventID;ML_HB;Std_HB;TOTSTA_HB;USEDSTA_HB;ML_DB;Std_DB;TOTSTA_DB;USEDSTA_DB;ampmethod;amp_mean_type;mag_mean_type;loopexitcondition\n") - magnitudes_out.write(';'.join((str(eventid),str(ma_mlh),str(ma_stdh),str(ma_ns_s_h),str(ma_nsh),str(ma_mld),str(ma_stdd),str(ma_ns_s_d),str(ma_nsd),met,amp_mean_type,mag_mean_type,cond_hb+'-'+cond_db,'\n'))) + amp_mean_type_str = amp_mean_type if mag_mean_type == 'meanamp' else 'False' + magnitudes_out.write( + "EventID;ML_HB;Std_HB;TOTSTA_HB;USEDSTA_HB;ML_DB;Std_DB;TOTSTA_DB;USEDSTA_DB;ampmethod;amp_mean_type;mag_mean_type;loopexitcondition\n") + magnitudes_out.write( + f"{eventid};{ma_mlh};{ma_stdh};{ma_ns_s_h};{ma_nsh};{ma_mld};{ma_stdd};{ma_ns_s_d};{ma_nsd};{met};{amp_mean_type_str};{mag_mean_type};{cond_hb}-{cond_db}\n") -# JSON WRITE +# Aggiornamento JSON per l'intero evento jmags = copy.deepcopy(jmagnitudes) -empty_jemag = {} if ma_mlh: - jhb = copy.deepcopy(jemag) - jhb['ml'] = ma_mlh - jhb['std'] = ma_stdh - jhb['totsta'] = ma_ns_s_h - jhb['usedsta'] = ma_nsh - jhb['quality'] = char_quality(ma_ns_s_h,ma_nsh,mindist,ma_mlh,ma_stdh) -else: - jhb = copy.deepcopy(empty_jemag) -jmags["hb"].update(jhb) # push oggetto "magnitudo" HB in oggetto magnitudes + jhb = copy.deepcopy(jemag) + jhb.update({'ml': ma_mlh, 'std': ma_stdh, 'totsta': ma_ns_s_h, 'usedsta': ma_nsh, + 'quality': char_quality(ma_ns_s_h, mindist, ma_mlh, ma_stdh)}) + jmags["hb"].update(jhb) if ma_mld: - jhb = copy.deepcopy(jemag) - jhb['ml'] = ma_mld - jhb['std'] = ma_stdd - jhb['totsta'] = ma_ns_s_d - jhb['usedsta'] = ma_nsd - jhb['quality'] = char_quality(ma_ns_s_d,ma_nsd,mindist,ma_mld,ma_stdd) -else: - jhb = copy.deepcopy(empty_jemag) -jmags["db"].update(jhb) # push oggetto "magnitudo" HB in oggetto magnitudes -jmags["ampmethod"] = met -jmags["magmethod"] = mag_mean_type -jmags["loopexitcondition"] = cond_hb+'-'+cond_db + jdb = copy.deepcopy(jemag) + jdb.update({'ml': ma_mld, 'std': ma_stdd, 'totsta': ma_ns_s_d, 'usedsta': ma_nsd, + 'quality': char_quality(ma_ns_s_d, mindist, ma_mld, ma_stdd)}) + jmags["db"].update(jdb) +jmags.update({"ampmethod": met, "magmethod": mag_mean_type, "loopexitcondition": f"{cond_hb}-{cond_db}"}) resp["magnitudes"].update(jmags) -channels_dictionary = {} -for x, y, wx, wy in zip(mean_hb_ml_sta, mean_db_ml_sta, weights_hb, weights_db): - sth,mh = map(str,x) - std,md = map(str,y) - whb = str(wx) - wdb = str(wy) - if components_N[sth] and components_E[sth]: - nwr,swr,lwr,chwr,mwr = sth.split('_') - ch_N_rewrite = nwr + "_" + swr + "_" + lwr + "_" + chwr + "N" - ch_E_rewrite = nwr + "_" + swr + "_" + lwr + "_" + chwr + "E" - ch_rewrite = nwr + "_" + swr + "_" + lwr + "_" + chwr + "_" + met - if magnitudes_out: - magnitudes_out.write(' '.join(('MLCHA',ch_N_rewrite,str(components_N[sth][0][0]),str(whb),ch_N_rewrite,str(components_N[sth][0][1]),str(wdb),'\n'))) - magnitudes_out.write(' '.join(("MLCHA",ch_E_rewrite,str(components_E[sth][0][0]),str(whb),ch_E_rewrite,str(components_E[sth][0][1]),str(wdb),'\n'))) - channels_dictionary[ch_rewrite] = [[components_N[sth][0][0],whb,components_N[sth][0][1],wdb],[components_E[sth][0][0],whb,components_E[sth][0][1],wdb]] -if not hm_cutoff or wh_hb_fail or wh_db_fail: - # This loop is needed for sth and mh also in json but only if magnitudes_out is true goes to the text out ... nevertheless, outliers list might be empty - if magnitudes_out: - if len(outliers_hb) > 0: - for x in outliers_hb[0]: - sth,mh = map(str,list(x)) - magnitudes_out.write(' '.join(('OUTL_HB',sth,mh,'\n'))) - if len(outliers_db) > 0: - for y in outliers_db[0]: - std,md = map(str,list(y)) - magnitudes_out.write(' '.join(('OUTL_DB',std,md,'\n'))) - - -for key in components_N: - try: - components_N[key] - except: - components_N[key] = False - try: - components_E[key] - except: - components_E[key] = False - try: - channels_dictionary[key] - except: - channels_dictionary[key] = False - - n,s,l,c,m = key.split('_') - jstmag = copy.deepcopy(jstationmagnitude) - #sys.stderr.write("PeriodE: "+str(components_E[key][8])+"\n") - #sys.stderr.write("PeriodN: "+str(components_N[key][8])+"\n") - #sys.stderr.write("jstmag['period']:" + str(jstmag["period"] )+"\n") - if components_N[key]: - #logm = copy.deepcopy(jlogmessage) - #logmch = copy.deepcopy(jlogmessagech) - jstmag["net"] = n - jstmag["sta"] = s - jstmag["cha"] = c + 'N' - jstmag["loc"] = "--" if l == 'None' else l - jstmag["amp1"] = components_N[key][1][0] - jstmag["time1"] = components_N[key][6] - jstmag["amp2"] = components_N[key][1][1] - jstmag["time2"] = components_N[key][7] - if components_N[key][8]: - jstmag["period"] = components_N[key][8] - jstmag["ep_distance_km"] = components_N[key][2] - jstmag["ep_distance_delta"] = components_N[key][3] - jstmag["orig_distance"] = components_N[key][4] - if components_N[key][9]: - jstmag["lat"] = components_N[key][9] - if components_N[key][10]: - jstmag["lon"] = components_N[key][10] - if components_N[key][11]: - jstmag["elev"] = components_N[key][11] - if components_N[key][12]: - jstmag["azimuth"] = components_N[key][12] - if channels_dictionary[key]: - jstmag["hb"] = {"ml": channels_dictionary[key][0][0], "w": float(channels_dictionary[key][0][1])} - jstmag["db"] = {"ml": channels_dictionary[key][0][2], "w": float(channels_dictionary[key][0][3])} - else: - jstmag["hb"] = {} - jstmag["db"] = {} - #logmch["net"] = jstmag["net"] - #logmch["sta"] = jstmag["sta"] - #logmch["loc"] = jstmag["loc"] - #logmch["cha"] = jstmag["cha"] - #logmch['status'] = 'ok' - #logmch['level'] = 'channel' - #logmch['info'] = {"summary": n+' '+s+' '+jstmag["loc"]+' '+c+'E', "extended": ''} - if components_N[key][9] and components_N[key][10] and components_N[key][11]: - resp["stationmagnitudes"].append(jstmag) - #resp["log"].append(logmch) - jstmag = copy.deepcopy(jstationmagnitude) - if components_E[key]: - #logm = copy.deepcopy(jlogmessage) - #logmch = copy.deepcopy(jlogmessagech) - jstmag["net"] = n - jstmag["sta"] = s - jstmag["cha"] = c + 'E' - jstmag["loc"] = "--" if l == 'None' else l - jstmag["amp1"] = components_E[key][1][0] - jstmag["time1"] = components_E[key][6] - jstmag["amp2"] = components_E[key][1][1] - jstmag["time2"] = components_E[key][7] - if components_E[key][8]: - jstmag["period"] = components_E[key][8] - jstmag["ep_distance_km"] = components_E[key][2] - jstmag["ep_distance_delta"] = components_E[key][3] - jstmag["orig_distance"] = components_E[key][4] - if components_E[key][9]: - jstmag["lat"] = components_E[key][9] - if components_E[key][10]: - jstmag["lon"] = components_E[key][10] - if components_E[key][11]: - jstmag["elev"] = components_E[key][11] - if components_E[key][12]: - jstmag["azimuth"] = components_E[key][12] - if channels_dictionary[key]: - jstmag["hb"] = {"ml": channels_dictionary[key][1][0], "w": float(channels_dictionary[key][1][1])} - jstmag["db"] = {"ml": channels_dictionary[key][1][2], "w": float(channels_dictionary[key][1][3])} - else: - jstmag["hb"] = {} - jstmag["db"] = {} - - #logmch["net"] = jstmag["net"] - #logmch["sta"] = jstmag["sta"] - #logmch["loc"] = jstmag["loc"] - #logmch["cha"] = jstmag["cha"] - #logmch['status'] = 'ok' - #logmch['level'] = 'channel' - #logmch['info'] = {"summary": n+' '+s+' '+jstmag["loc"]+' '+c+'N', "extended": ''} - if components_E[key][9] and components_E[key][10] and components_E[key][11]: - resp["stationmagnitudes"].append(jstmag) - #resp["log"].append(logmch) - #resp["stationmagnitudes"].append(jstmag) - +# --- COSTRUZIONE OUTPUT STAZIONI DA DATAFRAME AGGREGATO --- +df_stations = dfa_processed[dfa_processed['orientation'].isin(['N', 'E'])].pivot(index='components_key', + columns='orientation') +df_stations = df_stations.dropna(subset=[('amp_calc', 'N'), ('amp_calc', 'E')]) + +w_hb_dict = dict(zip([x[0] for x in mean_hb_ml_sta], weights_hb)) if mlhb else {} +w_db_dict = dict(zip([x[0] for x in mean_db_ml_sta], weights_db)) if mldb else {} + +if not df_stations.empty: + for comp_key, row in df_stations.iterrows(): + st_key = f"{comp_key}_{met}" + # Casting a stringa forzato per evitare il warning di PyCharm su Hashable split + n_net, n_sta, n_loc, n_cha = str(comp_key).split('_') + + whb = w_hb_dict.get(st_key, 0.0) + wdb = w_db_dict.get(st_key, 0.0) + + for ori in ['N', 'E']: + jstmag: dict = copy.deepcopy(jstationmagnitude) + jstmag.update({ + "net": n_net, "sta": n_sta, "cha": f"{n_cha}{ori}", + "loc": "--" if n_loc == 'None' else n_loc, + "amp1": row.loc[('minamp', ori)], "time1": row.loc[('time_minamp', ori)], + "amp2": row.loc[('maxamp', ori)], "time2": row.loc[('time_maxamp', ori)], + "ep_distance_km": row.loc[('epi_dist_km', ori)], "orig_distance": row.loc[('hypo_dist', ori)], + "lat": row.loc[('lat', ori)], "lon": row.loc[('lon', ori)], "elev": row.loc[('elev', ori)], + "azimuth": row.loc[('epi_azimuth', ori)], + "hb": {"ml": row.loc[('ml_hb', ori)] if not pd.isna(row.loc[('ml_hb', ori)]) else None,"w": float(whb)}, + "db": {"ml": row.loc[('ml_db', ori)] if not pd.isna(row.loc[('ml_db', ori)]) else None, "w": float(wdb)} + }) + if 'period' in row.index.levels[0] and pd.notna(row[('period', ori)]): + jstmag["period"] = row[('period', ori)] + + resp["stationmagnitudes"].append(jstmag) + + if magnitudes_out: + magnitudes_out.write( + f"MLCHA {comp_key}_{ori} {jstmag['hb']['ml']} {whb} {comp_key}_{ori} {jstmag['db']['ml']} {wdb}\n") + if args.out_format == 'json': - sys.stdout.write(json_pyml_response(resp)) + sys.stdout.write(json_pyml_response(resp)) -# Now closing all output files if magnitudes_out: - magnitudes_out.close() -#main_end_time = time.perf_counter() -#main_execution_time = main_end_time - main_start_time + magnitudes_out.close() if log_out: - #log_out.write("MAIN: the execution time is: "+str(main_execution_time)+"\n") - log_out.close() -sys.exit() + log_out.close() +sys.exit() \ No newline at end of file From a750e0e0a5f09af5b412a4082ea639d0eabd7463 Mon Sep 17 00:00:00 2001 From: Raffaele Di Stefano Date: Wed, 23 Sep 2026 19:12:16 +0200 Subject: [PATCH 06/12] last two warnings removed --- pyml.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/pyml.py b/pyml.py index aea2b3e..d665458 100644 --- a/pyml.py +++ b/pyml.py @@ -375,23 +375,23 @@ def char_quality(nu, d, m, r): return ''.join((q1, q2)) -def standard_pyml_load(infile_val, conf_file_val, log_out_val): +def standard_pyml_load(infile_val, conf_file_val): if os.path.exists(conf_file_val) and os.path.getsize(conf_file_val) > 0: paramfile = conf_file_val else: sys.stderr.write(f"Config file {conf_file_val} not existing or empty\n\n") sys.exit(2) - confObj = cp.ConfigParser() - confObj.read(paramfile) + conf_obj = cp.ConfigParser() + conf_obj.read(paramfile) df_amp = pd.read_csv(infile_val, sep=';', index_col=False) df_amp.columns = df_amp.columns.str.lower() - pre_params = get_config_dictionary(confObj, 'preconditions') + pre_params = get_config_dictionary(conf_obj, 'preconditions') theo_p_val = eval(pre_params['theoretical_p']) theo_s_val = eval(pre_params['theoretical_s']) - sta_params = get_config_dictionary(confObj, 'station_magnitude') + sta_params = get_config_dictionary(conf_obj, 'station_magnitude') mag_mean_type_val = eval(sta_params['mag_mean_type']) amp_mean_type_val = eval(sta_params['amp_mean_type']) if mag_mean_type_val == 'meanamp' else None delta_peaks_val = float(sta_params['delta_peaks']) @@ -400,7 +400,7 @@ def standard_pyml_load(infile_val, conf_file_val, log_out_val): when_no_stcorr_hb_val = eval(sta_params['when_no_stcorr_hb']) when_no_stcorr_db_val = eval(sta_params['when_no_stcorr_db']) - ev_params = get_config_dictionary(confObj, 'event_magnitude') + ev_params = get_config_dictionary(conf_obj, 'event_magnitude') mindist_val = float(ev_params['mindist']) maxdist_val = float(ev_params['maxdist']) hm_cutoff_val = eval(ev_params['hm_cutoff']) @@ -531,8 +531,7 @@ def json_pyml_response(r): conf_file = args.conf eventid = args.eventid - dfa, theo_p, theo_s, delta_peaks, use_stcorr_hb, use_stcorr_db, when_no_stcorr_hb, when_no_stcorr_db, mindist, maxdist, hm_cutoff, outliers_max_it, outliers_red_stop, outliers_nstd, outliers_cutoff, mag_mean_type, amp_mean_type = standard_pyml_load( - infile, conf_file, log_out) + dfa, theo_p, theo_s, delta_peaks, use_stcorr_hb, use_stcorr_db, when_no_stcorr_hb, when_no_stcorr_db, mindist, maxdist, hm_cutoff, outliers_max_it, outliers_red_stop, outliers_nstd, outliers_cutoff, mag_mean_type, amp_mean_type = standard_pyml_load(infile, conf_file) origin = False if dfa.empty: gresp['status'] = "422" From 9ce19560e2a99a9d6860efa00213a9a212a7d633 Mon Sep 17 00:00:00 2001 From: Nicola Guglielmi Date: Thu, 19 Nov 2026 10:46:34 +0100 Subject: [PATCH 07/12] Fix zero azimuths and station metadata (REI #684) --- README.md | 21 ++++++ pyml.py | 40 ++++++----- tests/fixtures/cptp.json | 66 +++++++++++++++++ tests/test_station_metadata.py | 126 +++++++++++++++++++++++++++++++++ 4 files changed, 234 insertions(+), 19 deletions(-) create mode 100644 tests/fixtures/cptp.json create mode 100644 tests/test_station_metadata.py diff --git a/README.md b/README.md index a23d795..50c3303 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,27 @@ To run the container, use the command below; the `-v` option is used to "mount" docker run --rm -v $(pwd)/examples/input:/opt/data ingv/pyml --in_file_name /opt/data/eventid_28745631.json --in_file_format json --out_format json ``` +## Station metadata + +Station azimuths are measured from the station toward the event. A bearing of +`0.0` degrees is valid and is returned as a JSON number on both horizontal +components. An azimuth or angular distance that was not calculated is returned +as JSON `null`. Zero latitude, longitude and elevation are valid metadata; +stations with missing coordinates or elevation are omitted from the +`stationmagnitudes` list. + +## Tests + +With the Python dependencies listed in `Dockerfile` installed, run: + +```sh +python3 -m unittest discover -s tests -v +``` + +The tests run the complete CLI in temporary directories. They cover the CPTP +zero-azimuth regression reported in REI issue #684, nonzero bearings, sea-level +and zero-coordinate stations, missing metadata, and unchanged magnitude results. + ## Contribute Thanks to your contributions! diff --git a/pyml.py b/pyml.py index 6e64194..f281841 100644 --- a/pyml.py +++ b/pyml.py @@ -645,7 +645,7 @@ def json_response_structure(): "ep_distance_km": null, "ep_distance_delta": null, "orig_distance": null, - "azimuth": null, + "azimuth": None, "hb": { "ml": null, "w": null @@ -940,20 +940,22 @@ def json_pyml_response(r): components_key='_'.join((str(net),str(sta),str(loc),str(cha[0:2]))) cmp_keys.add(components_key) + # A precomputed distance does not imply that an azimuth was calculated. + epi_azimuth = None try: hypo_dist = row['ipodistance(km)'] epi_dist_km = row['epidistance(km)'] - epi_dist_deg = False + epi_dist_deg = None except: #calcolo le distanze evla=float(origin['lat']) evlo=float(origin['lon']) evdp=float(origin['depth']) - if not stla or not stlo: + if stla is False or stlo is False: epi_dist_km = False epi_dist_deg = False hypo_dist = False - epi_azimuth = False + epi_azimuth = None else: geo_dict = geod.Inverse(evla,evlo,stla,stlo) #distmeters = geo_dict['s12'] @@ -995,12 +997,14 @@ def json_pyml_response(r): else: ml[1] = False + # Preserve missing elevation separately from the valid sea-level value 0. + station_elevation_m = None if stel is False else stel*km if cha[2] == 'N': - components_N[components_key_met]=[ml,[minamp,maxamp],epi_dist_km,epi_dist_deg,hypo_dist,[s_hutton,s_dibona],time_minamp,time_maxamp,period,stla,stlo,stel*km,epi_azimuth] + components_N[components_key_met]=[ml,[minamp,maxamp],epi_dist_km,epi_dist_deg,hypo_dist,[s_hutton,s_dibona],time_minamp,time_maxamp,period,stla,stlo,station_elevation_m,epi_azimuth] elif cha[2] == 'E': - components_E[components_key_met]=[ml,[minamp,maxamp],epi_dist_km,epi_dist_deg,hypo_dist,[s_hutton,s_dibona],time_minamp,time_maxamp,period,stla,stlo,stel*km,epi_azimuth] + components_E[components_key_met]=[ml,[minamp,maxamp],epi_dist_km,epi_dist_deg,hypo_dist,[s_hutton,s_dibona],time_minamp,time_maxamp,period,stla,stlo,station_elevation_m,epi_azimuth] elif cha[2] == 'Z': - components_Z[components_key_met]=[ml,[minamp,maxamp],epi_dist_km,epi_dist_deg,hypo_dist,[s_hutton,s_dibona],time_minamp,time_maxamp,period,stla,stlo,stel*km,epi_azimuth] + components_Z[components_key_met]=[ml,[minamp,maxamp],epi_dist_km,epi_dist_deg,hypo_dist,[s_hutton,s_dibona],time_minamp,time_maxamp,period,stla,stlo,station_elevation_m,epi_azimuth] else: if log_out: log_out.write(' '.join(("Component not recognized for ",str(net),str(sta),str(loc),str(cha),"\n"))) @@ -1207,14 +1211,13 @@ def json_pyml_response(r): jstmag["ep_distance_km"] = components_N[key][2] jstmag["ep_distance_delta"] = components_N[key][3] jstmag["orig_distance"] = components_N[key][4] - if components_N[key][9]: + if components_N[key][9] is not False: jstmag["lat"] = components_N[key][9] - if components_N[key][10]: + if components_N[key][10] is not False: jstmag["lon"] = components_N[key][10] - if components_N[key][11]: + if components_N[key][11] is not None: jstmag["elev"] = components_N[key][11] - if components_N[key][12]: - jstmag["azimuth"] = components_N[key][12] + jstmag["azimuth"] = components_N[key][12] if channels_dictionary[key]: jstmag["hb"] = {"ml": channels_dictionary[key][0][0], "w": float(channels_dictionary[key][0][1])} jstmag["db"] = {"ml": channels_dictionary[key][0][2], "w": float(channels_dictionary[key][0][3])} @@ -1228,7 +1231,7 @@ def json_pyml_response(r): #logmch['status'] = 'ok' #logmch['level'] = 'channel' #logmch['info'] = {"summary": n+' '+s+' '+jstmag["loc"]+' '+c+'E', "extended": ''} - if components_N[key][9] and components_N[key][10] and components_N[key][11]: + if all(value is not False and value is not None for value in components_N[key][9:12]): resp["stationmagnitudes"].append(jstmag) #resp["log"].append(logmch) jstmag = copy.deepcopy(jstationmagnitude) @@ -1248,14 +1251,13 @@ def json_pyml_response(r): jstmag["ep_distance_km"] = components_E[key][2] jstmag["ep_distance_delta"] = components_E[key][3] jstmag["orig_distance"] = components_E[key][4] - if components_E[key][9]: + if components_E[key][9] is not False: jstmag["lat"] = components_E[key][9] - if components_E[key][10]: + if components_E[key][10] is not False: jstmag["lon"] = components_E[key][10] - if components_E[key][11]: + if components_E[key][11] is not None: jstmag["elev"] = components_E[key][11] - if components_E[key][12]: - jstmag["azimuth"] = components_E[key][12] + jstmag["azimuth"] = components_E[key][12] if channels_dictionary[key]: jstmag["hb"] = {"ml": channels_dictionary[key][1][0], "w": float(channels_dictionary[key][1][1])} jstmag["db"] = {"ml": channels_dictionary[key][1][2], "w": float(channels_dictionary[key][1][3])} @@ -1270,7 +1272,7 @@ def json_pyml_response(r): #logmch['status'] = 'ok' #logmch['level'] = 'channel' #logmch['info'] = {"summary": n+' '+s+' '+jstmag["loc"]+' '+c+'N', "extended": ''} - if components_E[key][9] and components_E[key][10] and components_E[key][11]: + if all(value is not False and value is not None for value in components_E[key][9:12]): resp["stationmagnitudes"].append(jstmag) #resp["log"].append(logmch) #resp["stationmagnitudes"].append(jstmag) diff --git a/tests/fixtures/cptp.json b/tests/fixtures/cptp.json new file mode 100644 index 0000000..6f528c4 --- /dev/null +++ b/tests/fixtures/cptp.json @@ -0,0 +1,66 @@ +{ + "data": { + "pyml_conf": { + "preconditions": { + "theoretical_p": true, + "theoretical_s": true, + "delta_corner": 5, + "max_lowcorner": 15 + }, + "station_magnitude": { + "mag_mean_type": "meanamp", + "amp_mean_type": "geo", + "delta_peaks": 1.0, + "use_stcorr_hb": true, + "use_stcorr_db": true, + "when_no_stcorr_hb": true, + "when_no_stcorr_db": true + }, + "event_magnitude": { + "mindist": 10, + "maxdist": 600, + "hm_cutoff": [ + 0.3, + 1.0 + ], + "outliers_max_it": 10, + "outliers_red_stop": 0.05, + "outliers_nstd": 1, + "outliers_cutoff": 0.1 + } + }, + "origin": { + "lat": 38.737, + "lon": 14.1625, + "depth": 11.03 + }, + "amplitudes": [ + { + "net": "IV", + "sta": "CPTP", + "cha": "HHN", + "loc": "--", + "amp1": 0.7755380868911743, + "time1": "2026-10-30T05:35:43.910Z", + "amp2": -0.925866961479187, + "time2": "2026-10-30T05:35:44.630Z", + "lat": 37.4075, + "lon": 14.1625, + "elev": 561 + }, + { + "net": "IV", + "sta": "CPTP", + "cha": "HHE", + "loc": "--", + "amp1": 1.0061566829681396, + "time1": "2026-10-30T05:35:45.700Z", + "amp2": -0.9390428066253662, + "time2": "2026-10-30T05:35:45.910Z", + "lat": 37.4075, + "lon": 14.1625, + "elev": 561 + } + ] + } +} diff --git a/tests/test_station_metadata.py b/tests/test_station_metadata.py new file mode 100644 index 0000000..e25916d --- /dev/null +++ b/tests/test_station_metadata.py @@ -0,0 +1,126 @@ +import copy +import json +from pathlib import Path +import subprocess +import sys +import tempfile +import unittest + + +PROJECT = Path(__file__).resolve().parents[1] + + +class StationMetadataTest(unittest.TestCase): + def setUp(self): + self.request = json.loads( + (PROJECT / "tests" / "fixtures" / "cptp.json").read_text() + ) + + def run_pyml(self): + with tempfile.TemporaryDirectory() as directory: + input_path = Path(directory) / "input.json" + input_path.write_text(json.dumps(self.request)) + result = subprocess.run( + [ + sys.executable, str(PROJECT / "pyml.py"), + "--in_file_name", str(input_path), + "--in_file_format", "json", "--out_format", "json", + "--dbona_corr", str( + PROJECT / "dbona_magnitudes_stations_corrections_extended_mq.csv" + ), + ], + cwd=directory, capture_output=True, text=True, timeout=30, + ) + self.assertEqual(result.returncode, 0, result.stderr) + return json.loads(result.stdout) + + def both_components(self, response): + stations = response["stationmagnitudes"] + self.assertEqual([station["cha"] for station in stations], ["HHN", "HHE"]) + return stations + + def test_zero_azimuth_is_a_number_on_both_components(self): + response = self.run_pyml() + for station in self.both_components(response): + self.assertIsInstance(station["azimuth"], (int, float)) + self.assertNotIsInstance(station["azimuth"], bool) + self.assertEqual(station["azimuth"], 0.0) + + def test_cptp_magnitudes_distances_and_readings_are_preserved(self): + response = self.run_pyml() + self.assertAlmostEqual(response["magnitudes"]["hb"]["ml"], 3.238701070990148, places=12) + self.assertAlmostEqual(response["magnitudes"]["db"]["ml"], 3.326183568153756, places=12) + for station, amplitude in zip( + self.both_components(response), self.request["data"]["amplitudes"] + ): + self.assertAlmostEqual(station["ep_distance_km"], 147.57165046575133, places=10) + self.assertAlmostEqual(station["orig_distance"], 148.02615749314674, places=10) + for field in ("net", "sta", "cha", "loc", "time1", "time2"): + self.assertEqual(station[field], amplitude[field], field) + for field in ("amp1", "amp2", "lat", "lon", "elev"): + self.assertAlmostEqual(station[field], amplitude[field], places=12, msg=field) + self.assertEqual(response["log"]["magnitude"]["hb"]["summary"], "") + + def test_small_longitude_changes_preserve_nonzero_bearings(self): + for longitude, expected in ( + (14.16250001, 3.3760522604080265e-7), + (14.16249999, 359.9999996623948), + ): + with self.subTest(longitude=longitude): + self.request["data"]["origin"]["lon"] = longitude + for station in self.both_components(self.run_pyml()): + self.assertAlmostEqual(station["azimuth"], expected, places=10) + + def test_southward_bearing_keeps_the_station_to_event_convention(self): + self.request["data"]["origin"]["lat"] = 36.737 + for station in self.both_components(self.run_pyml()): + self.assertEqual(station["azimuth"], 180.0) + + def test_sea_level_station_is_retained(self): + for amplitude in self.request["data"]["amplitudes"]: + amplitude["elev"] = 0.0 + for station in self.both_components(self.run_pyml()): + self.assertEqual(station["elev"], 0.0) + self.assertEqual(station["azimuth"], 0.0) + + def test_zero_latitude_and_longitude_are_valid_coordinates(self): + for latitude, longitude in ((0.0, 14.1625), (37.4075, 0.0), (0.0, 0.0)): + with self.subTest(latitude=latitude, longitude=longitude): + self.request["data"]["origin"].update(lat=latitude + 1.3295, lon=longitude) + for amplitude in self.request["data"]["amplitudes"]: + amplitude.update(lat=latitude, lon=longitude) + for station in self.both_components(self.run_pyml()): + self.assertEqual(station["lat"], latitude) + self.assertEqual(station["lon"], longitude) + self.assertEqual(station["azimuth"], 0.0) + + def test_negative_elevation_is_preserved(self): + for amplitude in self.request["data"]["amplitudes"]: + amplitude["elev"] = -20.0 + for station in self.both_components(self.run_pyml()): + self.assertEqual(station["elev"], -20.0) + + def test_missing_coordinates_are_not_replaced_with_zero(self): + original = copy.deepcopy(self.request) + for field in ("lat", "lon", "elev"): + for missing in (None, "absent"): + with self.subTest(field=field, missing=missing): + self.request = copy.deepcopy(original) + for amplitude in self.request["data"]["amplitudes"]: + if missing == "absent": + amplitude.pop(field) + else: + amplitude[field] = None + self.assertEqual(self.run_pyml()["stationmagnitudes"], []) + + def test_uncomputed_azimuth_is_json_null(self): + for amplitude in self.request["data"]["amplitudes"]: + amplitude["ipodistance(km)"] = 148.02615749314674 + amplitude["epidistance(km)"] = 147.57165046575133 + for station in self.both_components(self.run_pyml()): + self.assertIsNone(station["azimuth"]) + self.assertIsNone(station["ep_distance_delta"]) + + +if __name__ == "__main__": + unittest.main() From 159de39555c660323978ab4b9f8314751554308c Mon Sep 17 00:00:00 2001 From: Valentino Lauciani Date: Thu, 19 Nov 2026 14:58:27 +0100 Subject: [PATCH 08/12] ci: mount repo folder so publiccode parser finds the logo --- .github/workflows/docker-image.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index 9558c22..336615b 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -25,7 +25,7 @@ jobs: - name: Run publiccode parser run: | - docker run -i --rm italia/publiccode-parser-go /dev/stdin < publiccode.yml + docker run -i --rm -v $(pwd):/publiccode_folder italia/publiccode-parser-go /publiccode_folder/publiccode.yml build_and_push_to_registry: name: Build and Push Docker image to Docker Hub From e630ada3f0a9e40f23d6de0212c45ae9b8a9528c Mon Sep 17 00:00:00 2001 From: Valentino Lauciani Date: Thu, 19 Nov 2026 15:12:12 +0100 Subject: [PATCH 09/12] build: move base image to debian bookworm and pin numpy and pandas --- Dockerfile | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/Dockerfile b/Dockerfile index affd567..8f53d03 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,11 @@ -FROM debian:bullseye-slim +FROM debian:bookworm-slim LABEL maintainer="Raffaele Di Stefano " ENV DEBIAN_FRONTEND=noninteractive +# Bookworm marks the system Python as externally managed (PEP 668) and pip +# refuses to install into it. This image is single-purpose, so installing +# system-wide is fine and avoids changing how entrypoint.sh calls python3. +ENV PIP_BREAK_SYSTEM_PACKAGES=1 # Installing all needed applications RUN apt-get clean \ @@ -20,14 +24,19 @@ RUN apt-get clean \ # Adding python3 libraries RUN python3 -m pip install --upgrade pip -RUN python3 -m pip install numpy -RUN python3 -m pip install obspy -RUN python3 -m pip install pyrocko -RUN python3 -m pip install plotly -#RUN python3 -m pip install sklearn -RUN python3 -m pip install scipy -RUN python3 -m pip install pandas -RUN python3 -m pip install geographiclib +# One resolver run, so the pins hold across all packages: installed one by +# one, a later package silently upgrades numpy past what pyrocko accepts. +# numpy<2 is required by pyrocko on Python <= 3.11; pandas<3 because +# pyml.py assigns float results into columns that pandas 3 no longer +# upcasts (LossySetitemError). Both match what bullseye was resolving to. +RUN python3 -m pip install \ + "numpy<2" \ + obspy \ + pyrocko \ + plotly \ + scipy \ + "pandas<3" \ + geographiclib COPY ./dbona_magnitudes_stations_corrections_extended_mq.csv /opt/dbona_magnitudes_stations_corrections_extended_mq.csv COPY ./pyml.py /opt/pyml.py From da9eaab582ad2dc22a7accb7968ff5368d0bba7b Mon Sep 17 00:00:00 2001 From: Raffaele Di Stefano Date: Thu, 19 Nov 2026 19:59:14 +0100 Subject: [PATCH 10/12] git ignore updated --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 9f11b75..2483976 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .idea/ +__pycache__/ From b4949525853e83354c4ebc461acc9e193bfcfdd5 Mon Sep 17 00:00:00 2001 From: Raffaele Di Stefano Date: Thu, 19 Nov 2026 19:59:55 +0100 Subject: [PATCH 11/12] Refactoring based on errors in branch 9 and GPT/Codex Astra molto alto suggestions --- pyml.py | 1339 +++++++++++++++++++++++++------------------------------ 1 file changed, 616 insertions(+), 723 deletions(-) diff --git a/pyml.py b/pyml.py index 0403c90..cf3efcc 100644 --- a/pyml.py +++ b/pyml.py @@ -1,743 +1,636 @@ -# Authors: -# Raffaele Di Stefano (raffaele.distefano@ingv.it), -# Barbara Castello (barbara.castello@ingv.it) -# Coding of present version powered by Gemini 3.1 Pro -# Aknowledgements: Valentino Lauciani (valentino.lauciani@ingv.it) and Andrea Bono (andrea.bono@ingv.it) - adaptation to the monitoring system -# Licence: CC BY-SA 4.0 (https://creativecommons.org/licenses/by-sa/4.0/) - -# --- Libreria Standard Python --- +# Authors of the original INGV pyml: Raffaele Di Stefano, Barbara Castello. +# Acknowledgements: Valentino Lauciani, Andrea Bono. +# License: CC BY-SA 4.0, https://creativecommons.org/licenses/by-sa/4.0/ +"""Local magnitude calculator; JSON core with legacy CLI adapters. + +Input amplitudes: millimetres (JSON), metres (legacy CSV). +Depth/distances: kilometres; elevation: metres; coordinates: WGS84 degrees. +Missing numeric values are NaN internally and null in JSON, never zero. +""" +from __future__ import annotations + import argparse -import copy -import decimal +import ast +import configparser import json -import os +import math import socket import sys -import logging -from datetime import datetime -import configparser as cp -from math import log2 +from dataclasses import dataclass +from datetime import datetime, timezone +from pathlib import Path -# --- Librerie Scientifiche e di Analisi Dati --- import numpy as np import pandas as pd -from scipy.stats import median_abs_deviation - -# --- Librerie Geodetiche e Sismologiche --- from geographiclib.geodesic import Geodesic -from obspy.geodetics.base import gps2dist_azimuth - - -class MyParser(argparse.ArgumentParser): - def error(self, message): - sys.stderr.write(f'error: {message}\n') - self.print_help() - sys.exit(2) - - -def parse_arguments(): - parser = MyParser() - parser.add_argument('--in_file_name', default=None, help='Name (and path) of input amplitudes file') - parser.add_argument('--in_file_format', default='json', - help='options are json (data and config in one single file) or csv (pyamp format): this latter needs also the --conf argument') - parser.add_argument('--conf', default=None, - help='A file containing sections and related parameters (used only with --csv)') - parser.add_argument('--out_format', default='json', help='options are json or txt') - parser.add_argument('--eventid', default='0', - help='Unique identifier of the event (only used with --csv_in and --conf)') - parser.add_argument('--dbona_corr', default='dbcor.csv', help='Input file with DiBona Stations corrections') - parser.add_argument('--clipped_info', help='Input file with information on clipped channels') - if len(sys.argv) == 1: - parser.print_help() - sys.exit(1) - return parser.parse_args() - - -def get_config_dictionary(cfg, section): - dict1 = {} - options = cfg.options(section) - for option in options: - try: - dict1[option] = cfg.get(section, option) - if dict1[option] == -1: - sys.stderr.write(f"skip: {option}") - except (cp.NoOptionError, cp.NoSectionError) as ex: - sys.stderr.write(f"Exception on {option}! {ex}\n") - dict1[option] = None - return dict1 - - -def process_amplitudes_vectorized(df_amp, ev_origin, unit_val): - """ - Pulisce i dati, gestisce le distanze (sia precalcolate CSV che da calcolare via Obspy/JSON) - ed estrae il metodo operativo in logica Pandas. - """ - df_amp['net'] = df_amp.get('net', pd.Series(dtype=object)).fillna(False) - df_amp['sta'] = df_amp.get('sta', pd.Series(dtype=object)).fillna(False) - df_amp['loc'] = df_amp.get('loc', pd.Series(dtype=object)).fillna('--') - df_amp['cha'] = df_amp.get('cha', pd.Series(dtype=object)).fillna(False) - - method_type = str(df_amp['method'].iloc[0]) if 'method' in df_amp.columns else 'ingv' - - minamp_col = 'minamp(m)' if 'minamp(m)' in df_amp.columns else 'amp1' - maxamp_col = 'maxamp(m)' if 'maxamp(m)' in df_amp.columns else 'amp2' - time_min_col = 'minamptime' if 'minamptime' in df_amp.columns else 'time1' - time_max_col = 'maxamptime' if 'maxamptime' in df_amp.columns else 'time2' - - df_amp['minamp'] = df_amp[minamp_col].astype(np.float64) * unit_val - df_amp['maxamp'] = df_amp[maxamp_col].astype(np.float64) * unit_val - df_amp['amp_calc'] = np.abs(df_amp['maxamp'] - df_amp['minamp']) / 2.0 - - df_amp['time_minamp'] = df_amp[time_min_col] - df_amp['time_maxamp'] = df_amp[time_max_col] - - valid_mask = (df_amp['net'] != False) & (df_amp['sta'] != False) & (df_amp['cha'] != False) - df_valid = df_amp[valid_mask].copy() - - # Missing metadata stays distinct from valid zero coordinates and elevation. - for field in ('lat', 'lon', 'elev'): - df_valid[field] = pd.to_numeric( - df_valid.get(field, pd.Series(index=df_valid.index, dtype=float)), errors='coerce' - ) - - if 'ipodistance(km)' in df_valid.columns and 'epidistance(km)' in df_valid.columns: - df_valid['epi_dist_km'] = df_valid['epidistance(km)'].astype(np.float64) - df_valid['hypo_dist'] = df_valid['ipodistance(km)'].astype(np.float64) - # Precomputed distances do not imply that a bearing was calculated. - df_valid['epi_azimuth'] = None - df_valid['epi_dist_deg'] = None - else: - if ev_origin and 'lat' in ev_origin and 'lon' in ev_origin: - valid_coord_mask = df_valid['lat'].notna() & df_valid['lon'].notna() - df_valid = df_valid[valid_coord_mask].copy() - - evla = float(ev_origin['lat']) - evlo = float(ev_origin['lon']) - evdp = float(ev_origin['depth']) - - def calc_dist_az(stla, stlo): - distmeters, azi, _ = gps2dist_azimuth(stla, stlo, evla, evlo) - distdegrees = Geodesic.WGS84.Inverse(evla, evlo, stla, stlo)['a12'] - return distmeters / 1000.0, azi, distdegrees - - vec_dist_az = np.vectorize(calc_dist_az, otypes=[np.float64, np.float64, np.float64]) - df_valid['epi_dist_km'], df_valid['epi_azimuth'], df_valid['epi_dist_deg'] = vec_dist_az( - df_valid['lat'].to_numpy(), - df_valid['lon'].to_numpy() - ) - - df_valid['stel_km'] = df_valid['elev'].fillna(0.0) / 1000.0 - df_valid['hypo_dist'] = np.sqrt( - np.square(df_valid['epi_dist_km']) + np.square(evdp + df_valid['stel_km']) - ) - else: - sys.stderr.write("Dati di origine mancanti per il calcolo delle distanze.\n") - sys.exit(1) - - return df_valid, method_type - - -def calc_huttonboore_vec(a, d, s, uc): - s_arr = np.where(uc, s, 0.0) - with np.errstate(divide='ignore', invalid='ignore'): - mag_val = np.where(a > 0, np.log10(a) + 1.110 * np.log10(d / 100.) + 0.00189 * (d - 100.) + 3.0 + s_arr, np.nan) - return mag_val - - -def calc_dibona_vec(a, d, s, uc): - s_arr = np.where(uc, s, 0.0) - with np.errstate(divide='ignore', invalid='ignore'): - mag_val = np.where(a > 0, np.log10(a) + 1.667 * np.log10(d / 100.) + 0.001736 * (d - 100.) + 3.0 + s_arr, - np.nan) - return mag_val - - -def apply_magnitudes_and_group(df_valid, db_corr, use_stcorr_h, use_stcorr_d, when_no_stcorr_h, when_no_stcorr_d): - """ - Calcola ML in vettoriale e prepara i dati per l'aggregazione di stazione. - """ - if db_corr is not False and not db_corr.empty: - df_valid = pd.merge(df_valid, db_corr[['sta', 'c']], on='sta', how='left') - df_valid = df_valid.rename(columns={'c': 's_dibona'}) - df_valid['s_dibona'] = df_valid['s_dibona'].fillna(0.0) - else: - df_valid['s_dibona'] = False - - df_valid['s_hutton'] = False - - calc_hb_mask = (df_valid['s_hutton'] != False) | ((df_valid['s_hutton'] == False) & when_no_stcorr_h) - calc_db_mask = (df_valid['s_dibona'] != False) | ((df_valid['s_dibona'] == False) & when_no_stcorr_d) - - df_valid['ml_hb'] = np.nan - df_valid.loc[calc_hb_mask, 'ml_hb'] = calc_huttonboore_vec( - df_valid.loc[calc_hb_mask, 'amp_calc'].to_numpy(), - df_valid.loc[calc_hb_mask, 'hypo_dist'].to_numpy(), - df_valid.loc[calc_hb_mask, 's_hutton'].to_numpy(), - use_stcorr_h - ) - - df_valid['ml_db'] = np.nan - df_valid.loc[calc_db_mask, 'ml_db'] = calc_dibona_vec( - df_valid.loc[calc_db_mask, 'amp_calc'].to_numpy(), - df_valid.loc[calc_db_mask, 'hypo_dist'].to_numpy(), - df_valid.loc[calc_db_mask, 's_dibona'].to_numpy(), - use_stcorr_d - ) - - df_valid['cha_prefix'] = df_valid['cha'].str[0:2] - df_valid['components_key'] = df_valid['net'] + '_' + df_valid['sta'] + '_' + df_valid['loc'] + '_' + df_valid[ - 'cha_prefix'] - df_valid['orientation'] = df_valid['cha'].str[2] - - return df_valid - - -def create_sets_vectorized(dfa_proc, mtd, mid, mad, dp, mty, stc, mmt, amt): - """ - Usa Pandas per allineare canali N ed E, filtrare vettorialmente e calcolare la ML di stazione. - """ - df_ne = dfa_proc[dfa_proc['orientation'].isin(['N', 'E'])].copy() - - if df_ne.empty: - return [], 99999.0, [] - - df_p = df_ne.pivot(index='components_key', columns='orientation') - df_p = df_p.dropna(subset=[('amp_calc', 'N'), ('amp_calc', 'E')]) - - if df_p.empty: - return [], 99999.0, [] - - mean_hypo = (df_p[('hypo_dist', 'N')] + df_p[('hypo_dist', 'E')]) / 2.0 - mean_epi = (df_p[('epi_dist_km', 'N')] + df_p[('epi_dist_km', 'E')]) / 2.0 - - valid_mask = (mean_hypo >= mid) & (mean_hypo <= mad) - - if mtd == 'free': - t_max_n = pd.to_numeric(df_p[('time_maxamp', 'N')], errors='coerce') - t_min_n = pd.to_numeric(df_p[('time_minamp', 'N')], errors='coerce') - t_max_e = pd.to_numeric(df_p[('time_maxamp', 'E')], errors='coerce') - t_min_e = pd.to_numeric(df_p[('time_minamp', 'E')], errors='coerce') - - dp_n = (t_max_n - t_min_n).abs() - dp_e = (t_max_e - t_min_e).abs() - - valid_mask &= (dp_n <= dp) & (dp_e <= dp) - - df_v = df_p[valid_mask].copy() - if df_v.empty: - return [], 99999.0, [] - - mty_str = 'ml_hb' if mty == 0 else 'ml_db' - s_str = 's_hutton' if mty == 0 else 's_dibona' - calc_func = calc_huttonboore_vec if mty == 0 else calc_dibona_vec - - sta_ml = None - - if mmt == 'meanmag': - sta_ml = (df_v[(mty_str, 'N')] + df_v[(mty_str, 'E')]) / 2.0 - elif mmt == 'meanamp': - amp_n, amp_e = df_v[('amp_calc', 'N')], df_v[('amp_calc', 'E')] - mean_amp = (amp_n + amp_e) / 2.0 if amt == 'ari' else np.sqrt(amp_n * amp_e) - - corr_n = pd.to_numeric(df_v[(s_str, 'N')], errors='coerce').fillna(0) - corr_e = pd.to_numeric(df_v[(s_str, 'E')], errors='coerce').fillna(0) - mean_corr = (corr_n + corr_e) / 2.0 - - sta_ml = calc_func(mean_amp.to_numpy(), mean_hypo[valid_mask].to_numpy(), mean_corr.to_numpy(), stc) +from scipy.stats import median_abs_deviation - valid_keys = df_v.index.astype(str) + '_' + mtd - ml_set = [[k, m] for k, m in zip(valid_keys, sta_ml) if pd.notna(m)] +VERSION = "1.0.0" +KEY = ["net", "sta", "loc", "band_inst", "method"] +LAWS = {"hb": ("HuttonBoore", 1.110, 0.00189), + "db": ("DiBona", 1.667, 0.001736)} - midi = mean_epi[valid_mask].min() if not mean_epi[valid_mask].empty else 99999.0 - logsms = [] - return ml_set, midi, logsms +def is_bool(value): + return isinstance(value, (bool, np.bool_)) -def wstd(v, wm, wf): - eps2 = 5.0e-6 - if len(v) <= 1: - return False - index_not_zero = np.where(wf > eps2)[0] - wf_nz = wf[index_not_zero] - v_nz = v[index_not_zero] - wsd = np.sum(wf_nz * np.square((v_nz - wm))) - fac = ((len(wf_nz) - 1) / len(wf_nz)) * np.sum(wf_nz) - wsd = wsd / max(fac, eps2) - return np.sqrt(max(wsd, eps2)) +def numeric(series): + """Reject boolean sentinels without rejecting the numbers 0 or 1.""" + clean = series.mask(series.map(is_bool)) + values = pd.to_numeric(clean, errors="coerce").astype("float64") + return values.where(np.isfinite(values)) -def whuber(v, w_mean, ruse, zero_val): - res = abs(v - w_mean) - with np.errstate(divide='ignore'): - w = np.where(res <= ruse, 1.0, np.where(res > zero_val, 0.0, ruse / res)) +def finite_number(value, name): + if is_bool(value) or value is None: + raise ValueError(f"{name}: expected a finite number") try: - w_mean = np.sum(v * w) / np.sum(w) - except ZeroDivisionError as err: - sys.stderr.write(f"Error calculating the Huber Mean: {err}\n") - w_mean = False - - w_std = wstd(v, w_mean, w) if w_mean else False - w_std = 0.0 if not w_std else w_std - return w_mean, w_std, w - - -def rm_outliers(v, v_w, v_flag, v_mean, v_std, times_std, co, skip): - res = abs(v - v_mean) - cut_limit = times_std * v_std if (times_std * v_std) > co else co - not_outlier = res < cut_limit - yes_outlier = res >= cut_limit - skip.append(list(zip(v_flag[yes_outlier], v[yes_outlier]))) - v_w[yes_outlier] = 0 - v_w[not_outlier] = 1 - - if len(v[not_outlier]) > 0: - v_std = median_abs_deviation(v[not_outlier]) - v_mean = np.median(v[not_outlier]) - else: - v_std = False - v_mean = False - return v_mean, v_std, v_w, skip - - -def calculate_event_ml(magnitudes, magnitudes_sta, it_max, var_stop, max_dev, out_cutoff, hm_cutoff_val): - v = np.array(magnitudes) - s = magnitudes_sta - ruse = hm_cutoff_val - xmd = np.median(v) - xmd_std = median_abs_deviation(v) - vlen_start = len(v) - n = 1 - finished = False - - if hm_cutoff_val: - ruse, zero_val = [hm_cutoff_val, 9999.0] if isinstance(hm_cutoff_val, float) else hm_cutoff_val - else: - zero_val = 9999.0 - - weights = np.ones(len(v)) - removed = [] - whuber_fail = False - typemean = 'unknown' - whystop = 'unknown' - - while not finished: - amd = xmd - if hm_cutoff_val: - whuber_fail = False - try: - xmd, xmd_std, weights = whuber(v, xmd, ruse, zero_val) - typemean = 'whuber' - except (ZeroDivisionError, ValueError) as err: - logging.warning(f"Calcolo della media di Huber fallito. Fallback in rm_outliers. Errore: {err}") - whuber_fail = True - - if not hm_cutoff_val or whuber_fail: - xmd, xmd_std, weights, removed = rm_outliers(v, weights, s, xmd, xmd_std, max_dev, out_cutoff, removed) - typemean = 'rowhfl' if whuber_fail else 'rmoutl' - - xmd_var = abs(amd - xmd) - if xmd_var <= var_stop: - finished = True - whystop = f"{typemean}_varstop={xmd_var}_n={n}" - elif n == it_max: - finished = True - whystop = f"{typemean}_maxit={n}_xmdvar={xmd_var}" - n += 1 - - vlen_stop = round(np.sum(weights), 2) - return xmd, xmd_std, vlen_start, vlen_stop, whystop, removed, weights, whuber_fail - - -def char_quality(nu, d, m, r): - q1type = 'course' - q1, q2 = 'D', 'D' - if q1type == 'basili': - if (log2(nu * 2) - 2) >= m: - q1 = 'A' - elif (log2(nu * 2) - 1) >= m: - q1 = 'B' - elif log2(nu * 2) >= m: - q1 = 'C' - elif log2(nu * 2) < m: - q1 = 'D' + result = float(value) + except (TypeError, ValueError) as exc: + raise ValueError(f"{name}: expected a finite number") from exc + if not math.isfinite(result): + raise ValueError(f"{name}: expected a finite number") + return result + + +@dataclass(frozen=True) +class Config: + mag_mean_type: str = "meanamp" + amp_mean_type: str = "geo" + delta_peaks: float = 1.0 + use_stcorr_hb: bool = True + use_stcorr_db: bool = True + when_no_stcorr_hb: bool = True + when_no_stcorr_db: bool = True + mindist: float = 10.0 + maxdist: float = 600.0 + hm_cutoff: object = (0.3, 1.0) + outliers_max_it: int = 10 + outliers_red_stop: float = 0.05 + outliers_nstd: float = 1.0 + outliers_cutoff: float = 0.1 + delta_corner: float = 5.0 + max_lowcorner: float = 15.0 + + @classmethod + def parse(cls, document): + if not isinstance(document, dict): + raise ValueError("pyml_conf must be an object") + sections = [] + for name in ("preconditions", "station_magnitude", "event_magnitude"): + section = document.get(name) + if not isinstance(section, dict): + raise ValueError(f"Missing or invalid configuration section: {name}") + sections.append(section) + values = {} + for section in sections: + values.update({k: v for k, v in section.items() + if k in cls.__dataclass_fields__}) + for name in ("use_stcorr_hb", "use_stcorr_db", + "when_no_stcorr_hb", "when_no_stcorr_db"): + if name in values and not isinstance(values[name], bool): + raise ValueError(f"{name} must be a boolean") + for name in ("delta_peaks", "mindist", "maxdist", "outliers_red_stop", + "outliers_nstd", "outliers_cutoff", "delta_corner", + "max_lowcorner"): + if name in values: + values[name] = finite_number(values[name], name) + if "outliers_max_it" in values: + n = finite_number(values["outliers_max_it"], "outliers_max_it") + if n < 1 or not n.is_integer(): + raise ValueError("outliers_max_it must be a positive integer") + values["outliers_max_it"] = int(n) + cutoff = values.get("hm_cutoff", (0.3, 1.0)) + if cutoff is False or cutoff is None: + values["hm_cutoff"] = None else: - q1 = 'E' - elif q1type == 'course': - if d <= 50. and nu * 2 > (m * 10.0 + 1): - q1 = 'A' - elif d <= 100.0 and nu * 2 > (m * 7.5 + 1): - q1 = 'B' - elif d <= 150.0 and nu * 2 > (m * 5.0 + 1): - q1 = 'C' - else: - q1 = 'D' - - if r <= 0.2: - q2 = 'A' - elif 0.2 < r <= 0.3: - q2 = 'B' - elif 0.3 < r <= 0.4: - q2 = 'C' - elif r > 0.4: - q2 = 'D' - return ''.join((q1, q2)) - - -def standard_pyml_load(infile_val, conf_file_val): - if os.path.exists(conf_file_val) and os.path.getsize(conf_file_val) > 0: - paramfile = conf_file_val + parts = cutoff if isinstance(cutoff, (list, tuple)) else (cutoff, 9999.0) + if len(parts) != 2: + raise ValueError("hm_cutoff must be a number or [corner, limit]") + corner, limit = (finite_number(v, "hm_cutoff") for v in parts) + if not 0 < corner <= limit: + raise ValueError("hm_cutoff requires 0 < corner <= limit") + values["hm_cutoff"] = (corner, limit) + cfg = cls(**values) + if cfg.mag_mean_type not in ("meanamp", "meanmag"): + raise ValueError("mag_mean_type must be meanamp or meanmag") + if cfg.mag_mean_type == "meanamp" and cfg.amp_mean_type not in ("ari", "geo"): + raise ValueError("amp_mean_type must be ari or geo") + if not 0 <= cfg.mindist <= cfg.maxdist or cfg.maxdist <= 0: + raise ValueError("Invalid distance interval") + if min(cfg.delta_peaks, cfg.outliers_red_stop, cfg.outliers_nstd, + cfg.outliers_cutoff, cfg.delta_corner, cfg.max_lowcorner) < 0: + raise ValueError("Thresholds must be nonnegative") + return cfg + + +def station_log(row, status, summary, extended="", channels=None): + return {**{k: row.get(k, "") for k in KEY[:4]}, + "status": status, "summary": summary, "extended": extended, + "channels": [] if channels is None else channels} + + +def clean_json(value): + if isinstance(value, dict): + return {k: clean_json(v) for k, v in value.items()} + if isinstance(value, (list, tuple, np.ndarray)): + return [clean_json(v) for v in value] + if value is None or value is pd.NA or value is pd.NaT: + return None + if isinstance(value, (float, np.floating)): + return float(value) if np.isfinite(value) else None + if isinstance(value, np.integer): + return int(value) + if isinstance(value, (datetime, pd.Timestamp)): + return value.isoformat() + if isinstance(value, np.bool_): + return bool(value) + return value + + +def dumps(value): + return json.dumps(clean_json(value), allow_nan=False) + + +def channel_corrections(frame, corrections): + """Resolve rules on unique station/channel pairs, never duplicate readings. + + Accepted selectors have exactly three positions (letters/digits/*). + !(...) negates a selector. More literal positions take precedence. + Equal-priority matches with different corrections are an input error. + """ + empty = pd.Series(np.nan, index=frame.index, dtype=float) + if corrections is None or len(corrections) == 0: + return empty + table = pd.DataFrame(corrections, dtype=object).copy() + if not {"sta", "c"}.issubset(table.columns): + raise ValueError("Corrections require sta and c") + table["c"] = numeric(table["c"]) + if table["c"].isna().any(): + raise ValueError("Corrections contain missing/invalid c") + if not table["sta"].map(lambda x: isinstance(x, str) and bool(x)).all(): + raise ValueError("Corrections contain invalid station identifiers") + table["ch"] = table.get("ch", pd.Series("***", index=table.index)).fillna("***") + pairs = frame[["sta", "cha"]].drop_duplicates().reset_index(drop=True) + pairs["pair"] = pairs.index + candidates = pairs.merge(table[["sta", "c", "ch"]], on="sta", how="inner") + selected = [] + for pattern, group in candidates.groupby("ch", sort=False): + if not isinstance(pattern, str): + raise ValueError("Invalid correction selector") + negate = pattern.startswith("!(") and pattern.endswith(")") + body = pattern[2:-1] if negate else pattern + if len(body) != 3 or any(not (c.isalnum() or c == "*") for c in body): + raise ValueError(f"Unsupported correction selector: {pattern}") + mask = np.ones(len(group), dtype=bool) + for position, char in enumerate(body): + if char != "*": + mask &= group["cha"].str[position].eq(char).to_numpy() + if negate: + mask = ~mask + match = group.loc[mask].copy() + match["priority"] = sum(c != "*" for c in body) + selected.append(match) + if not selected: + return empty + hits = pd.concat(selected, ignore_index=True) + if hits.empty: + return empty + best = hits.groupby("pair")["priority"].transform("max") + hits = hits[hits["priority"].eq(best)] + if hits.groupby("pair")["c"].nunique().gt(1).any(): + raise ValueError("Ambiguous station/channel corrections at equal priority") + values = hits.drop_duplicates("pair").set_index("pair")["c"] + pairs["correction"] = pairs["pair"].map(values) + result = frame[["sta", "cha"]].merge( + pairs[["sta", "cha", "correction"]], on=["sta", "cha"], + how="left", sort=False, validate="many_to_one") + return pd.Series(result["correction"].to_numpy(dtype=float), index=frame.index) + + +def elapsed_seconds(first, second): + a, b = numeric(first), numeric(second) + seconds = (b - a).abs() + # Numeric times use seconds; ISO timestamps use timedeltas in seconds. + mask = a.isna() & b.isna() + ta = pd.to_datetime(first.where(mask & first.map(lambda x: isinstance(x, str))), + errors="coerce", utc=True, format="mixed") + tb = pd.to_datetime(second.where(mask & second.map(lambda x: isinstance(x, str))), + errors="coerce", utc=True, format="mixed") + return seconds.where(~mask, (tb - ta).dt.total_seconds().abs()) + + +def prepare(amplitudes, origin, cfg, corrections, clips, unit, logs): + if not isinstance(amplitudes, list) or not all(isinstance(a, dict) for a in amplitudes): + raise ValueError("amplitudes must be an array of objects") + raw = pd.DataFrame(amplitudes, dtype=object) + f = pd.DataFrame(index=raw.index) + + def column(name, alternate=None): + value = raw.get(name, pd.Series(None, index=raw.index, dtype=object)) + if alternate in raw: + value = value.where(value.notna(), raw[alternate]) + return value + + for name in ("net", "sta", "cha"): + f[name] = column(name) + f["loc"] = column("loc").map( + lambda v: "--" if v is None or v is False or v == "" or pd.isna(v) else v) + f["method"] = column("method").map( + lambda v: "ingv" if v is None or v is False or v == "" or pd.isna(v) else v) + valid = pd.Series(True, index=f.index) + for name in ("net", "sta", "loc", "method"): + valid &= f[name].map(lambda v: isinstance(v, str) and bool(v) and "_" not in v) + valid &= f["cha"].map(lambda v: isinstance(v, str) and len(v) == 3) + f["band_inst"] = f["cha"].where(valid, "").str[:2] + for row in f.loc[~valid].to_dict("records"): + logs.append(station_log(row, "critical", "Invalid channel identifier")) + for name, alternate in (("amp1", "minamp(m)"), ("amp2", "maxamp(m)"), + ("lat", None), ("lon", None), ("elev", None), + ("period", None), ("loco", None), ("hico", None), + ("epi", "epidistance(km)"), ("hypo", "ipodistance(km)")): + f[name] = numeric(column(name, alternate)) + f[["amp1", "amp2"]] *= unit + for name, alternate in (("time1", "minamptime"), ("time2", "maxamptime")): + times = column(name, alternate) + f[name] = times.mask(times.map(is_bool), None) + f = f.loc[valid].copy() + f["orientation"] = f["cha"].str[2] + f["reason"] = "" + if clips is not None and len(clips): + clip = pd.DataFrame(clips) + fields = ["net", "sta", "cha"] + if not set(fields).issubset(clip.columns): + raise ValueError("Clipping information requires net, sta, cha") + if "loc" in clip: + clip["loc"] = clip["loc"].fillna("--").replace("", "--") + fields.append("loc") + excluded = pd.MultiIndex.from_frame(f[fields]).isin( + pd.MultiIndex.from_frame(clip[fields].drop_duplicates())) + f.loc[excluded, "reason"] = "Component skipped due to clipping" + bad_band = f["loco"].notna() & f["hico"].notna() & ( + f["loco"].ge(cfg.max_lowcorner) | (f["hico"] - f["loco"]).lt(cfg.delta_corner)) + f.loc[bad_band & f["reason"].eq(""), "reason"] = "Component skipped due to filter corners" + for row in f.loc[f["reason"].ne("")].to_dict("records"): + logs.append(station_log(row, "warning", row["reason"], + f"Channel {row['cha']}")) + f = f.loc[f["reason"].eq("")].copy() + duplicate = f.duplicated(KEY + ["orientation"], keep="last") + for row in f.loc[duplicate].to_dict("records"): + logs.append(station_log(row, "warning", "Duplicate channel: last accepted reading used", + row["cha"])) + f = f.loc[~duplicate].reset_index(drop=True) + if f["method"].nunique() > 1: + raise ValueError("Mixed amplitude methods require separate requests") + f["amp"] = (f["amp2"] / 2 - f["amp1"] / 2).abs() + f["amp"] = f["amp"].where(np.isfinite(f["amp"]) & f["amp"].gt(0)) + f["azimuth"], f["delta"] = np.nan, np.nan + coords = f["lat"].between(-90, 90) & f["lon"].between(-180, 180) + f.loc[~coords, ["lat", "lon"]] = np.nan + precomputed = f["epi"].notna() | f["hypo"].notna() + valid_pre = f["epi"].ge(0) & f["hypo"].gt(0) + f.loc[precomputed & ~valid_pre, ["epi", "hypo"]] = np.nan + need = ~precomputed & coords & f["elev"].notna() + if need.any(): + if not isinstance(origin, dict): + raise ValueError("Missing origin for distance calculation") + lat = finite_number(origin.get("lat"), "origin.lat") + lon = finite_number(origin.get("lon"), "origin.lon") + depth = finite_number(origin.get("depth"), "origin.depth") + if not (-90 <= lat <= 90 and -180 <= lon <= 180): + raise ValueError("Origin coordinates out of range") + unique = f.loc[need, ["lat", "lon"]].drop_duplicates().copy() + # One inverse per coordinate pair, shared by N/E and channel families. + distances = [Geodesic.WGS84.Inverse(a, b, lat, lon) + for a, b in unique.itertuples(index=False, name=None)] + unique["epi"] = [d["s12"] / 1000 for d in distances] + unique["azimuth"] = [d["azi1"] % 360 for d in distances] + unique["delta"] = [d["a12"] for d in distances] + mapped = f.loc[need, ["lat", "lon"]].merge( + unique, on=["lat", "lon"], how="left", sort=False, validate="many_to_one") + f.loc[need, ["epi", "azimuth", "delta"]] = mapped[["epi", "azimuth", "delta"]].to_numpy() + f.loc[need, "hypo"] = np.hypot(f.loc[need, "epi"], + depth + f.loc[need, "elev"] / 1000) + f["seconds"] = elapsed_seconds(f["time1"], f["time2"]) + f["corr_db"] = channel_corrections(f, corrections) + f["corr_hb"] = np.nan + for law in LAWS: + use = getattr(cfg, "use_stcorr_" + law) + allow = getattr(cfg, "when_no_stcorr_" + law) + ok = ~pd.Series(use, index=f.index) | f["corr_" + law].notna() | allow + correction = f["corr_" + law].fillna(0).to_numpy() if use else 0.0 + f["ml_" + law] = np.where(ok, magnitude(f["amp"], f["hypo"], correction, law), np.nan) + return f + + +def magnitude(amplitude, distance, correction, law): + _, alpha, beta = LAWS[law] + a, d = np.asarray(amplitude, dtype=float), np.asarray(distance, dtype=float) + with np.errstate(divide="ignore", invalid="ignore", over="ignore"): + result = np.log10(a) + alpha * np.log10(d / 100) + beta * (d - 100) + 3 + correction + return np.where((a > 0) & (d > 0) & np.isfinite(result), result, np.nan) + + +def quality(count, distance, ml, std): + first = "D" + for limit, factor, letter in ((50, 10, "A"), (100, 7.5, "B"), (150, 5, "C")): + if distance <= limit and count * 2 > ml * factor + 1: + first = letter + break + second = "A" if std <= .2 else "B" if std <= .3 else "C" if std <= .4 else "D" + return first + second + + +def estimate(values, cfg): + """Historical Huber/MAD approach, with finite and zero-weight checks.""" + v = np.asarray(values, dtype=float) + if v.size == 0 or not np.isfinite(v).all(): + raise ValueError("Estimator requires a nonempty finite vector") + center = float(np.median(v)) + spread = float(median_abs_deviation(v)) + weights = np.ones(v.size) + removed, fallback = [], False + for iteration in range(1, cfg.outliers_max_it + 1): + previous = center + residual = np.abs(v - center) + mode = "rmoutl" + use_huber = cfg.hm_cutoff is not None + if use_huber: + corner, limit = cfg.hm_cutoff + weights = np.ones(v.size) + np.divide(corner, residual, out=weights, where=residual > corner) + weights[residual > limit] = 0 + total = weights.sum() + if total > 0 and np.isfinite(total): + center = float(np.dot(v, weights) / total) + active = weights > 5e-6 + count = int(active.sum()) + if v.size == 1: + spread = 0.0 + elif count: + denominator = (count - 1) / count * weights[active].sum() + variance = np.sum(weights[active] * (v[active] - center) ** 2) + spread = math.sqrt(max(variance / max(denominator, 5e-6), 5e-6)) + else: + use_huber = False + mode = "whuber" + else: + use_huber = False + if not use_huber: + fallback = True + center, spread = previous, float(median_abs_deviation(v)) + if not use_huber: + cutoff = max(cfg.outliers_nstd * spread, cfg.outliers_cutoff) + active = residual <= 0 if cutoff == 0 else residual < cutoff + weights = active.astype(float) + removed.append(np.flatnonzero(~active)) + if not active.any(): + return None, weights, "Failed", removed, fallback + center = float(np.median(v[active])) + spread = float(median_abs_deviation(v[active])) + mode = "rowhfl" if fallback else "rmoutl" + change = abs(previous - center) + if change <= cfg.outliers_red_stop: + reason = f"{mode}_varstop={change}_n={iteration}" + break + reason = f"{mode}_maxit={iteration}_xmdvar={change}" + return (center, spread), weights, reason, removed, fallback + + +def calculate(request, corrections=None, *, clips=None, unit=1.0): + """Return (response, legacy text details), without file access or exits.""" + if not isinstance(request, dict) or not isinstance(request.get("data"), dict): + raise ValueError("Request requires a data object") + data = request["data"] + cfg = Config.parse(data.get("pyml_conf")) + logs = [] + f = prepare(data.get("amplitudes", []), data.get("origin"), cfg, + corrections, clips, unit, logs) + response = {"random_string": "github/ingv/pyml", "hostname": socket.gethostname(), + "magnitudes": {}, "stationmagnitudes": [], + "log": {"magnitude": {}, "stationmagnitudes": logs}} + if f.empty: + keys = pd.MultiIndex.from_arrays([[] for _ in KEY], names=KEY) else: - sys.stderr.write(f"Config file {conf_file_val} not existing or empty\n\n") - sys.exit(2) - - conf_obj = cp.ConfigParser() - conf_obj.read(paramfile) - df_amp = pd.read_csv(infile_val, sep=';', index_col=False) - df_amp.columns = df_amp.columns.str.lower() - - pre_params = get_config_dictionary(conf_obj, 'preconditions') - theo_p_val = eval(pre_params['theoretical_p']) - theo_s_val = eval(pre_params['theoretical_s']) - - sta_params = get_config_dictionary(conf_obj, 'station_magnitude') - mag_mean_type_val = eval(sta_params['mag_mean_type']) - amp_mean_type_val = eval(sta_params['amp_mean_type']) if mag_mean_type_val == 'meanamp' else None - delta_peaks_val = float(sta_params['delta_peaks']) - use_stcorr_hb_val = eval(sta_params['use_stcorr_hb']) - use_stcorr_db_val = eval(sta_params['use_stcorr_db']) - when_no_stcorr_hb_val = eval(sta_params['when_no_stcorr_hb']) - when_no_stcorr_db_val = eval(sta_params['when_no_stcorr_db']) - - ev_params = get_config_dictionary(conf_obj, 'event_magnitude') - mindist_val = float(ev_params['mindist']) - maxdist_val = float(ev_params['maxdist']) - hm_cutoff_val = eval(ev_params['hm_cutoff']) - outliers_max_it_val = int(ev_params['outliers_max_it']) - outliers_red_stop_val = float(ev_params['outliers_red_stop']) - outliers_nstd_val = float(ev_params['outliers_nstd']) - outliers_cutoff_val = float(ev_params['outliers_cutoff']) - - return df_amp, theo_p_val, theo_s_val, delta_peaks_val, use_stcorr_hb_val, use_stcorr_db_val, when_no_stcorr_hb_val, when_no_stcorr_db_val, mindist_val, maxdist_val, hm_cutoff_val, outliers_max_it_val, outliers_red_stop_val, outliers_nstd_val, outliers_cutoff_val, mag_mean_type_val, amp_mean_type_val - - -def json_pyml_load(json_input): - cfg = json_input.get('data', {}).get('pyml_conf', False) - orig = json_input.get('data', {}).get('origin', False) - amps = json_input.get('data', {}).get('amplitudes', []) - df_amp = pd.DataFrame(amps) - return df_amp, cfg, orig - - -class DataEncoder(json.JSONEncoder): - def default(self, o): - if isinstance(o, decimal.Decimal): - return float(o) - if isinstance(o, datetime): - return o.isoformat() - return json.JSONEncoder.default(self, o) - - -def json_response_structure(): - null = '' - generic = { - "type": null, "title": null, "status": null, "detail": null, - "instance": null, "version": null, "request_submitted": null, - "errors": {"inputfile": [], "arguments": []} - } - response = { - "random_string": null, "hostname": {}, "magnitudes": {}, - "stationmagnitudes": [], "log": {} - } - log_dict = {"magnitude": {}, "stationmagnitudes": []} - log_magnitude = { - "hb": {"status": null, "summary": null, "extended": null}, - "db": {"status": null, "summary": null, "extended": null} - } - log_stationmagnitude = { - "net": null, "sta": null, "loc": null, "band_inst": null, - "status": null, "summary": null, "extended": null, "channels": [] - } - log_stationmagnitude_channel = { - "orientation": null, "status": null, "summary": null, "extended": null - } - magnitudes = { - "hb": {}, "db": {}, "ampmethod": null, "magmethod": null, "loopexitcondition": null - } - emag = { - "ml": null, "std": null, "totsta": null, "usedsta": null, "quality": '--' - } - stationmagnitude = { - "net": null, "sta": null, "cha": null, "loc": null, - "amp1": null, "time1": null, "amp2": null, "time2": null, - "ep_distance_km": null, "ep_distance_delta": null, - "orig_distance": null, "azimuth": None, - "hb": {"ml": null, "w": null}, - "db": {"ml": null, "w": null} - } - return response, generic, log_dict, log_magnitude, log_stationmagnitude, log_stationmagnitude_channel, magnitudes, stationmagnitude, emag - - -def json_pyml_response(r): - return json.dumps(r, cls=DataEncoder) - - -############################################################### -###### Main starts here ########## -pyml_version = "1.0.0" -args = parse_arguments() -geod = Geodesic.WGS84 - -# Inizializzazioni sicure per il Linter e per la robustezza -dfa = pd.DataFrame() -origin = False -config = None -eventid = 0 -magnitudes_out = False -log_out = False -exit_condition = False -met = 'ingv' -use_stcorr_hb = None -use_stcorr_db = None -when_no_stcorr_hb = None -when_no_stcorr_db = None -mindist = None -maxdist = None -delta_peaks = None -mag_mean_type = None -amp_mean_type = None -outliers_max_it = None -outliers_red_stop = None -outliers_nstd = None -outliers_cutoff = None -hm_cutoff = None -theo_p = None -theo_s = None - -jresponse, jbadrequest, jlog, jlog_mag, jlog_stamag, jlog_stamag_cha, jmagnitudes, jstationmagnitude, jemag = json_response_structure() -resp = copy.deepcopy(jresponse) -log = copy.deepcopy(jlog) -logm = copy.deepcopy(jlog_mag) -gresp = copy.deepcopy(jbadrequest) -resp['random_string'] = 'github/ingv/pyml' -resp['hostname'] = socket.gethostname() - -if args.out_format.lower() == 'txt': - magnitudes_out = sys.stdout - log_out = sys.stderr -elif args.out_format.lower() == 'json': - magnitudes_out = False - log_out = False -else: - log_out = False - gresp['type'] = "https://tools.ietf.org/html/rfc4918#section-11.2" - gresp['status'] = "422" - gresp['errors']["arguments"].append(f"The given argument out format {args.out_format} is unknown") - exit_condition = True - -if args.in_file_format.lower() == 'csv': - infile = args.in_file_name - conf_file = args.conf - eventid = args.eventid - - dfa, theo_p, theo_s, delta_peaks, use_stcorr_hb, use_stcorr_db, when_no_stcorr_hb, when_no_stcorr_db, mindist, maxdist, hm_cutoff, outliers_max_it, outliers_red_stop, outliers_nstd, outliers_cutoff, mag_mean_type, amp_mean_type = standard_pyml_load(infile, conf_file) - origin = False - if dfa.empty: - gresp['status'] = "422" - gresp['errors']["inputfile"].append("The given input file was incomplete") - exit_condition = True - -elif args.in_file_format.lower() == 'json': - if os.path.exists(args.in_file_name): - try: - with open(args.in_file_name, 'r') as f: - json_in = json.load(f) - dfa, config, origin = json_pyml_load(json_in) - except json.JSONDecodeError: - dfa = pd.DataFrame() - - if dfa.empty or not config or not origin: - gresp['status'] = "422" - gresp['errors']["inputfile"].append("The given input file was incomplete") - exit_condition = True + keys = pd.MultiIndex.from_frame(f[KEY].drop_duplicates()) + groups = keys.to_frame(index=False) + components = {} + for ori in ("N", "E"): + components[ori] = f.loc[f["orientation"].eq(ori)].set_index(KEY).reindex(keys) + n, e = components["N"], components["E"] + have_n, have_e = n["amp"].notna(), e["amp"].notna() + both = have_n & have_e + hypo, epi = (n["hypo"] + e["hypo"]) / 2, (n["epi"] + e["epi"]) / 2 + coords_ok = hypo.notna() & epi.notna() + distance_ok = hypo.between(cfg.mindist, cfg.maxdist) + free = pd.Series(keys.get_level_values("method") == "free", index=keys) + time_ok = ~free | (n["seconds"].le(cfg.delta_peaks) & e["seconds"].le(cfg.delta_peaks)) + groups["label"] = (groups[KEY].agg("_".join, axis=1) if len(groups) + else pd.Series(dtype=str)) + stats, weights_by_law, eligibility_by_law, reasons, outliers = {}, {}, {}, {}, {} + for law, (title, _, _) in LAWS.items(): + use = getattr(cfg, "use_stcorr_" + law) + allow = getattr(cfg, "when_no_stcorr_" + law) + cn, ce = n["corr_" + law], e["corr_" + law] + corr_ok = (~pd.Series(use, index=keys)) | (cn.notna() & ce.notna()) | allow + if cfg.mag_mean_type == "meanmag": + station_ml = (n["ml_" + law] + e["ml_" + law]) / 2 + else: + if cfg.amp_mean_type == "ari": + amp = n["amp"] / 2 + e["amp"] / 2 + else: + amp = np.sqrt(n["amp"]) * np.sqrt(e["amp"]) + corr = (cn.fillna(0) + ce.fillna(0)) / 2 if use else 0.0 + station_ml = pd.Series(magnitude(amp, hypo, corr, law), index=keys) + eligible = both & coords_ok & distance_ok & time_ok & corr_ok & station_ml.notna() + reasons[law] = "Failed" + full_weights = np.zeros(len(keys)) + maglog = {"status": "critical", "summary": f"{title} List is empty", + "extended": "All the stations missing due to only one channel is present, or out of minmax distance"} + for i in np.flatnonzero(~eligible.to_numpy()): + row = groups.iloc[i].to_dict() + channels = [] + status = "warning" + if not both.iloc[i]: + status, summary, extended = "critical", "Station skipped due to missing channel", "ML " + title + for ori, present, comp in (("N", have_n.iloc[i], n), ("E", have_e.iloc[i], e)): + detail = "" + if not present and pd.notna(comp["cha"].iloc[i]): + detail = "minamp missing" if pd.isna(comp["amp1"].iloc[i]) else ( + "maxamp missing" if pd.isna(comp["amp2"].iloc[i]) else "invalid amplitude") + channels.append({"orientation": ori, "status": "ok" if present else "critical", + "summary": "" if present else "missing", "extended": detail}) + elif not coords_ok.iloc[i]: + status, summary, extended = "critical", "Station skipped due to stations coordinates missing", title + elif not distance_ok.iloc[i]: + summary = f"In ML {title} Station {row['label']} skipped due to ipodist" + extended = f"Distance is {hypo.iloc[i]} km" + elif not time_ok.iloc[i]: + summary = f"In ML {title} Station skipped due to time distance between min and max amp larger than {cfg.delta_peaks}" + seconds = n["seconds"].iloc[i] + extended = (f"Time distance is {seconds} s" if pd.notna(seconds) + else "Missing or invalid peak times") + else: + summary, extended = "Station skipped due to missing correction or invalid magnitude", title + logs.append(station_log(row, status, summary, extended, channels)) + selected = np.flatnonzero(eligible.to_numpy()) + outliers[law] = [] + if selected.size: + result, w, reason, removed, fallback = estimate(station_ml.iloc[selected], cfg) + full_weights[selected] = w + reasons[law] = reason + if removed: + outliers[law] = [(groups["label"].iloc[selected[j]], float(station_ml.iloc[selected[j]])) + for j in removed[0]] + if result is not None: + ml, std = result + used = selected[w > 5e-6] + dmin = float(epi.iloc[used].min()) if used.size else math.inf + stats[law] = {"ml": ml, "std": std, "totsta": int(selected.size), + "usedsta": round(float(w.sum()), 2), + "quality": quality(int(selected.size), dmin, ml, std)} + maglog = {"status": "warning" if fallback else "ok", + "summary": f"{title}: Weighted Huber Mean failed, Outliers Removal used instead" if fallback else "", + "extended": ""} + else: + maglog = {"status": "critical", "summary": f"{title}: magnitude estimation failed", + "extended": "No usable weighted observations"} + weights_by_law[law] = full_weights + eligibility_by_law[law] = eligible.to_numpy() + response["log"]["magnitude"][law] = maglog + if stats: + methods = f["method"].drop_duplicates().tolist() + response["magnitudes"] = {"hb": stats.get("hb", {}), "db": stats.get("db", {}), + "ampmethod": methods[0] if len(methods) == 1 else "mixed", + "magmethod": cfg.mag_mean_type, + "loopexitcondition": reasons["hb"] + "-" + reasons["db"]} + # Keep available unpaired channels in the response, as main did. + out = f.loc[f["orientation"].isin(["N", "E"])].copy() + positions = keys.get_indexer(pd.MultiIndex.from_frame(out[KEY])) + for law in LAWS: + out["w_" + law] = weights_by_law[law][positions] + out["eligible_" + law] = eligibility_by_law[law][positions] + out["order"] = positions * 2 + out["orientation"].eq("E").astype(int) + out = out.sort_values("order", kind="stable") + outliers["_channels"] = [] + for row in out.to_dict("records"): + entry = {k: row[k] for k in ("net", "sta", "loc", "cha", "amp1", "amp2", + "time1", "time2", "lat", "lon", "elev", "azimuth")} + entry.update(ep_distance_km=row["epi"], ep_distance_delta=row["delta"], + orig_distance=row["hypo"]) + for law in LAWS: + entry[law] = ({"ml": row["ml_" + law], "w": row["w_" + law]} + if row["eligible_" + law] and law in stats else {}) + if pd.notna(row["period"]): + entry["period"] = row["period"] + outliers["_channels"].append(entry) + if all(pd.notna(row[k]) for k in ("lat", "lon", "elev")): + response["stationmagnitudes"].append(entry) + # Private details are returned separately to the legacy text adapter. + return clean_json(response), outliers + + +def analyze(request, corrections=None, *, clips=None): + """Public library API: request dict -> response dict.""" + return calculate(request, corrections, clips=clips)[0] + + +def read_table(path): + if path is None: + return None + if Path(path).suffix.lower() == ".json": + with open(path, encoding="utf-8") as stream: + result = json.load(stream) + if not isinstance(result, list): + raise ValueError("Table JSON must be an array of records") + return pd.DataFrame(result, dtype=object) + return pd.read_csv(path, sep=";", dtype=object, keep_default_na=False, index_col=False) + + +def load_request(args): + if args.in_file_format == "json": + if args.in_file_name == "-": + return json.load(sys.stdin), 1.0 + with open(args.in_file_name, encoding="utf-8") as stream: + return json.load(stream), 1.0 + if not args.conf: + raise ValueError("--conf is required for CSV input") + cfg = configparser.ConfigParser() + if not cfg.read(args.conf, encoding="utf-8"): + raise ValueError("Configuration file not found") + settings = {} + for section in ("preconditions", "station_magnitude", "event_magnitude"): + settings[section] = {} + for name, value in cfg.items(section): + try: + settings[section][name] = ast.literal_eval(value) + except (ValueError, SyntaxError): + settings[section][name] = value + table = pd.read_csv(args.in_file_name, sep=";", dtype=object, keep_default_na=False, index_col=False) + table.columns = table.columns.str.lower() + return {"data": {"pyml_conf": settings, "amplitudes": table.to_dict("records")}}, 1000.0 + + +def render_text(response, outliers, cfg, eventid, stdout, stderr): + mags = response["magnitudes"] + if mags: + fields = ["EventID", "ML_HB", "Std_HB", "TOTSTA_HB", "USEDSTA_HB", + "ML_DB", "Std_DB", "TOTSTA_DB", "USEDSTA_DB", + "ampmethod", "amp_mean_type", "mag_mean_type", "loopexitcondition"] + print(";".join(fields), file=stdout) + row = [eventid] + for law in LAWS: + value = mags[law] + row.extend(value.get(k, False) for k in ("ml", "std", "totsta", "usedsta")) + row.extend([mags["ampmethod"], cfg.amp_mean_type if cfg.mag_mean_type == "meanamp" else "False", + cfg.mag_mean_type, mags["loopexitcondition"]]) + print(";".join(map(str, row)) + ";", file=stdout) + for channel in outliers.get("_channels", response["stationmagnitudes"]): + if not channel["hb"] and not channel["db"]: + continue + key = "_".join(channel[k] for k in ("net", "sta", "loc", "cha")) + values = [channel[law].get(k, False) for law in LAWS for k in ("ml", "w")] + print("MLCHA", key, values[0], values[1], key, values[2], values[3], file=stdout) + for law in LAWS: + for key, ml in outliers[law]: + print("OUTL_" + law.upper(), key, ml, file=stdout) else: - gresp['status'] = "422" - gresp['errors']["inputfile"].append("No input file found.") - exit_condition = True - - if not exit_condition: - try: - theo_p = config['preconditions']['theoretical_p'] - theo_s = config['preconditions']['theoretical_s'] - mag_mean_type = config['station_magnitude']['mag_mean_type'] - amp_mean_type = config['station_magnitude'].get('amp_mean_type', None) - delta_peaks = config['station_magnitude']['delta_peaks'] - use_stcorr_hb = config['station_magnitude']['use_stcorr_hb'] - use_stcorr_db = config['station_magnitude']['use_stcorr_db'] - when_no_stcorr_hb = config['station_magnitude']['when_no_stcorr_hb'] - when_no_stcorr_db = config['station_magnitude']['when_no_stcorr_db'] - mindist = config['event_magnitude']['mindist'] - maxdist = config['event_magnitude']['maxdist'] - hm_cutoff = config['event_magnitude']['hm_cutoff'] - outliers_max_it = config['event_magnitude']['outliers_max_it'] - outliers_red_stop = config['event_magnitude']['outliers_red_stop'] - outliers_nstd = config['event_magnitude']['outliers_nstd'] - outliers_cutoff = config['event_magnitude']['outliers_cutoff'] - except KeyError as k_err: - gresp['status'] = "422" - gresp['errors']["inputfile"].append(f"Missing config key: {k_err}") - exit_condition = True -else: - gresp['status'] = "422" - gresp['errors']["arguments"].append("The given input format is unknown") - sys.stdout.write(json_pyml_response(gresp)) - sys.exit() - -if exit_condition: - sys.stdout.write(json_pyml_response(gresp)) - sys.exit() - -if args.dbona_corr: + print(dumps(response), file=stdout) + for item in response["log"]["stationmagnitudes"]: + print(f"{item['status']}:{item['summary']} --> {item['extended']}", file=stderr) + + +def main(argv=None): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--in_file_name", required=True) + parser.add_argument("--in_file_format", type=str.lower, choices=("json", "csv"), default="json") + parser.add_argument("--out_format", type=str.lower, choices=("json", "txt"), default="json") + parser.add_argument("--conf") + parser.add_argument("--eventid", default="0") + parser.add_argument("--dbona_corr", default="dbcor.csv") + parser.add_argument("--clipped_info") + args = parser.parse_args(argv) try: - dbcorr = pd.read_csv(args.dbona_corr, sep=';') - except Exception as e: - if log_out: log_out.write(f"File {args.dbona_corr} not found: {e}\n") - sys.exit() -else: - dbcorr = False - -unit = 1000 if args.in_file_format.lower() == 'csv' else 1 - -# --- LOGICA VETTORIALIZZATA --- -dfa_valid, met = process_amplitudes_vectorized(dfa, origin, unit) -dfa_processed = apply_magnitudes_and_group(dfa_valid, dbcorr, use_stcorr_hb, use_stcorr_db, when_no_stcorr_hb, - when_no_stcorr_db) - -mean_hb_ml_sta, _, _ = create_sets_vectorized(dfa_processed, met, mindist, maxdist, delta_peaks, 0, use_stcorr_hb, - mag_mean_type, amp_mean_type) -mean_db_ml_sta, _, _ = create_sets_vectorized(dfa_processed, met, mindist, maxdist, delta_peaks, 1, use_stcorr_db, - mag_mean_type, amp_mean_type) - -# Gestione calcoli statistici per HB -ma_mlh, ma_stdh, ma_ns_s_h, ma_nsh = False, False, 0, 0 -cond_hb = "Failed" -weights_hb, outliers_hb = [], [] -mlhb = False - -if len(mean_hb_ml_sta) > 0: - mean_ml = [x[1] for x in mean_hb_ml_sta] - mean_ml_sta = np.asarray([x[0] for x in mean_hb_ml_sta], dtype=object) - ma_mlh, ma_stdh, ma_ns_s_h, ma_nsh, cond_hb, outliers_hb, weights_hb, wh_hb_fail = calculate_event_ml(mean_ml, - mean_ml_sta, - outliers_max_it, - outliers_red_stop, - outliers_nstd, - outliers_cutoff, - hm_cutoff) - if ma_mlh: - mlhb = True - logm['hb']['status'] = 'warning' if wh_hb_fail else 'ok' - else: - logm['hb']['status'] = 'critical' - -# Gestione calcoli statistici per DB -ma_mld, ma_stdd, ma_ns_s_d, ma_nsd = False, False, 0, 0 -cond_db = "Failed" -weights_db, outliers_db = [], [] -mldb = False - -if len(mean_db_ml_sta) > 0: - mean_ml = [x[1] for x in mean_db_ml_sta] - mean_ml_sta = np.asarray([x[0] for x in mean_db_ml_sta], dtype=object) - ma_mld, ma_stdd, ma_ns_s_d, ma_nsd, cond_db, outliers_db, weights_db, wh_db_fail = calculate_event_ml(mean_ml, - mean_ml_sta, - outliers_max_it, - outliers_red_stop, - outliers_nstd, - outliers_cutoff, - hm_cutoff) - if ma_mld: - mldb = True - logm['db']['status'] = 'warning' if wh_db_fail else 'ok' - else: - logm['db']['status'] = 'critical' - -resp["log"]['magnitude'] = logm - -if not mlhb and not mldb: - if log_out: log_out.write("Neither Hutton&Boore nor Di Bona ML could be calculated\n") - sys.stdout.write(json_pyml_response(resp)) - sys.exit() - -if magnitudes_out: - amp_mean_type_str = amp_mean_type if mag_mean_type == 'meanamp' else 'False' - magnitudes_out.write( - "EventID;ML_HB;Std_HB;TOTSTA_HB;USEDSTA_HB;ML_DB;Std_DB;TOTSTA_DB;USEDSTA_DB;ampmethod;amp_mean_type;mag_mean_type;loopexitcondition\n") - magnitudes_out.write( - f"{eventid};{ma_mlh};{ma_stdh};{ma_ns_s_h};{ma_nsh};{ma_mld};{ma_stdd};{ma_ns_s_d};{ma_nsd};{met};{amp_mean_type_str};{mag_mean_type};{cond_hb}-{cond_db}\n") - -# Aggiornamento JSON per l'intero evento -jmags = copy.deepcopy(jmagnitudes) -if ma_mlh: - jhb = copy.deepcopy(jemag) - jhb.update({'ml': ma_mlh, 'std': ma_stdh, 'totsta': ma_ns_s_h, 'usedsta': ma_nsh, - 'quality': char_quality(ma_ns_s_h, mindist, ma_mlh, ma_stdh)}) - jmags["hb"].update(jhb) -if ma_mld: - jdb = copy.deepcopy(jemag) - jdb.update({'ml': ma_mld, 'std': ma_stdd, 'totsta': ma_ns_s_d, 'usedsta': ma_nsd, - 'quality': char_quality(ma_ns_s_d, mindist, ma_mld, ma_stdd)}) - jmags["db"].update(jdb) -jmags.update({"ampmethod": met, "magmethod": mag_mean_type, "loopexitcondition": f"{cond_hb}-{cond_db}"}) -resp["magnitudes"].update(jmags) - -# --- COSTRUZIONE OUTPUT STAZIONI DA DATAFRAME AGGREGATO --- -df_stations = dfa_processed[dfa_processed['orientation'].isin(['N', 'E'])].pivot(index='components_key', - columns='orientation') -df_stations = df_stations.dropna(subset=[('amp_calc', 'N'), ('amp_calc', 'E')]) - -w_hb_dict = dict(zip([x[0] for x in mean_hb_ml_sta], weights_hb)) if mlhb else {} -w_db_dict = dict(zip([x[0] for x in mean_db_ml_sta], weights_db)) if mldb else {} - -if not df_stations.empty: - for comp_key, row in df_stations.iterrows(): - st_key = f"{comp_key}_{met}" - # Casting a stringa forzato per evitare il warning di PyCharm su Hashable split - n_net, n_sta, n_loc, n_cha = str(comp_key).split('_') - - whb = w_hb_dict.get(st_key, 0.0) - wdb = w_db_dict.get(st_key, 0.0) - - for ori in ['N', 'E']: - if any(pd.isna(row.loc[(field, ori)]) for field in ('lat', 'lon', 'elev')): - continue - jstmag: dict = copy.deepcopy(jstationmagnitude) - jstmag.update({ - "net": n_net, "sta": n_sta, "cha": f"{n_cha}{ori}", - "loc": "--" if n_loc == 'None' else n_loc, - "amp1": row.loc[('minamp', ori)], "time1": row.loc[('time_minamp', ori)], - "amp2": row.loc[('maxamp', ori)], "time2": row.loc[('time_maxamp', ori)], - "ep_distance_km": row.loc[('epi_dist_km', ori)], "orig_distance": row.loc[('hypo_dist', ori)], - "ep_distance_delta": None if pd.isna(row.loc[('epi_dist_deg', ori)]) else row.loc[('epi_dist_deg', ori)], - "lat": row.loc[('lat', ori)], "lon": row.loc[('lon', ori)], "elev": row.loc[('elev', ori)], - "azimuth": None if pd.isna(row.loc[('epi_azimuth', ori)]) else row.loc[('epi_azimuth', ori)], - "hb": {"ml": row.loc[('ml_hb', ori)] if not pd.isna(row.loc[('ml_hb', ori)]) else None,"w": float(whb)}, - "db": {"ml": row.loc[('ml_db', ori)] if not pd.isna(row.loc[('ml_db', ori)]) else None, "w": float(wdb)} - }) - if 'period' in row.index.levels[0] and pd.notna(row[('period', ori)]): - jstmag["period"] = row[('period', ori)] - - resp["stationmagnitudes"].append(jstmag) - - if magnitudes_out: - magnitudes_out.write( - f"MLCHA {comp_key}_{ori} {jstmag['hb']['ml']} {whb} {comp_key}_{ori} {jstmag['db']['ml']} {wdb}\n") - -if args.out_format == 'json': - sys.stdout.write(json_pyml_response(resp)) - -if magnitudes_out: - magnitudes_out.close() -if log_out: - log_out.close() -sys.exit() + request, unit = load_request(args) + corrections = read_table(args.dbona_corr) if args.dbona_corr else None + clips = read_table(args.clipped_info) + response, outliers = calculate(request, corrections, clips=clips, unit=unit) + if args.out_format == "json": + print(dumps(response)) + else: + render_text(response, outliers, Config.parse(request["data"]["pyml_conf"]), + args.eventid, sys.stdout, sys.stderr) + return 0 + except (OSError, ValueError, TypeError, KeyError, configparser.Error) as exc: + problem = {"type": "https://tools.ietf.org/html/rfc4918#section-11.2", + "title": "Unprocessable entity", "status": "422", "detail": str(exc), + "instance": socket.gethostname(), "version": VERSION, + "request_submitted": datetime.now(timezone.utc).isoformat(), + "errors": {"inputfile": [str(exc)], "arguments": []}} + print(dumps(problem), file=sys.stdout if args.out_format == "json" else sys.stderr) + return 2 + + +if __name__ == "__main__": + raise SystemExit(main()) From cd21d9ee47bdb1e22984d7c4a834c85faedfca33 Mon Sep 17 00:00:00 2001 From: Raffaele Di Stefano Date: Sat, 21 Nov 2026 11:30:50 +0100 Subject: [PATCH 12/12] gitignore updated --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 2483976..4690939 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea/ __pycache__/ +mytests/