#!/usr/bin/env python # Purpose: To convert ENI TL stroke data to flash data # # Usage: stroke_to_flash.py # Example: stroke_to_flash.py 007.ascii 007_flash.ascii import sys print ('Re-formatting strokes into flashes') print ('') # Check the arguments being passed to this script nargs = len(sys.argv) if nargs < 3: print ('Usage: stroke_to_flash.py infile outfile') quit() in_file = sys.argv[1] out_file = sys.argv[2] # Open the Total Lightning data file f = open(in_file, 'r') # Initialize lists mon=[] day=[] year=[] hh=[] mm=[] ss=[] lat=[] lon=[] amp=[] stype=[] nstrokes=[] mon_flash=[] day_flash=[] year_flash=[] hh_flash=[] mm_flash=[] ss_flash=[] lat_flash=[] lon_flash=[] amp_flash=[] stype_flash=[] nstrokes_flash=[] # Now, Read each line from ltg ascii file for line in f: split_line = line.split(",") mon.append(split_line[0]) day.append(split_line[1]) year.append(split_line[2]) hh.append(int(split_line[3])) mm.append(int(split_line[4])) ss.append(int(split_line[5])) lat.append(float(split_line[6])) lon.append(float(split_line[7])) amp.append(int(split_line[8])) stype.append(int(split_line[10])) nstrokes.append(int(split_line[11])) n = len(mon) i = 0 index = 0. for ii in range(n): while (i < n): # if nstrokes = 1, then stroke is a flash if nstrokes[i] == 1: mon_flash.append(mon[i]) day_flash.append(day[i]) year_flash.append(year[i]) hh_flash.append(hh[i]) mm_flash.append(mm[i]) ss_flash.append(ss[i]) lat_flash.append(lat[i]) lon_flash.append(lon[i]) amp_flash.append(amp[i]) stype_flash.append(stype[i]) nstrokes_flash.append(1) i = i + 1 index = index + 1. # if nstrokes > 1, then get all strokes/pulses for a single flash if i < n: if nstrokes[i] > 1: temp_mon = mon[i:i+nstrokes[i]] temp_day = day[i:i+nstrokes[i]] temp_year = year[i:i+nstrokes[i]] temp_hh = hh[i:i+nstrokes[i]] temp_mm = mm[i:i+nstrokes[i]] temp_ss = ss[i:i+nstrokes[i]] temp_lat = lat[i:i+nstrokes[i]] temp_lon = lon[i:i+nstrokes[i]] temp_amp = amp[i:i+nstrokes[i]] temp_stype = stype[i:i+nstrokes[i]] temp_nstrokes = nstrokes[i:i+nstrokes[i]] count_q = len(temp_mon) if min(temp_nstrokes) != max(temp_nstrokes): stroke_index = [item for item in range(len(temp_nstrokes)) if temp_nstrokes[item] == temp_nstrokes[0]] temp_mon = temp_mon[0:len(stroke_index)] temp_day = temp_day[0:len(stroke_index)] temp_year = temp_year[0:len(stroke_index)] temp_hh = temp_hh[0:len(stroke_index)] temp_mm = temp_mm[0:len(stroke_index)] temp_ss = temp_ss[0:len(stroke_index)] temp_lat = temp_lat[0:len(stroke_index)] temp_lon = temp_lon[0:len(stroke_index)] temp_amp = temp_amp[0:len(stroke_index)] temp_stype = temp_stype[0:len(stroke_index)] temp_nstrokes = temp_nstrokes[0:len(stroke_index)] count_q = len(stroke_index) # Determind if the flash is CG or IC # CG Flash contains CG strokes and IC pulses # IC Flash contains only IC pulses cg = temp_stype.count(0) # CG Flash # Use time and location of stroke with peak amp if cg > 0: abs_temp_amp = [] for j in temp_amp: abs_temp_amp.append(abs(j)) peak_amp = max(abs_temp_amp) peak_amp_indx = abs_temp_amp.index(peak_amp) mon_flash.append(temp_mon[peak_amp_indx]) day_flash.append(temp_day[peak_amp_indx]) year_flash.append(temp_year[peak_amp_indx]) hh_flash.append(temp_hh[peak_amp_indx]) mm_flash.append(temp_mm[peak_amp_indx]) ss_flash.append(temp_ss[peak_amp_indx]) lat_flash.append(temp_lat[peak_amp_indx]) lon_flash.append(temp_lon[peak_amp_indx]) amp_flash.append(temp_amp[peak_amp_indx]) stype_flash.append(0) # IC Flash # Use average time and location if cg == 0: temp_time = [] for k in range(len(temp_hh)): time = temp_hh[k] + (temp_mm[k]/60.) + (temp_ss[k]/3600.) temp_time.append(time) if min(temp_time) < 4 and max(temp_time) > 20: print ('here!') print (len(temp_time)) print (temp_time) temp_time2=[] for kk in range(len(temp_time)): if temp_time[kk] < 4: temp_time2.append(temp_time[kk]+24) dayp1=temp_day[kk] monp1=temp_mon[kk] yearp1=temp_year[kk] else: temp_time2.append(temp_time[kk]) daym1=temp_day[kk] monm1=temp_mon[kk] yearm1=temp_year[kk] mean_time = sum(temp_time2) / float(len(temp_time2)) if mean_time >= 24.0: mean_time = mean_time - 24.0 if mean_time > 20: mon_flash.append(monm1) day_flash.append(daym1) year_flash.append(yearm1) else: mon_flash.append(monp1) day_flash.append(dayp1) year_flash.append(yearp1) else: mean_time = sum(temp_time) / float(len(temp_time)) mon_flash.append(temp_mon[0]) day_flash.append(temp_day[0]) year_flash.append(temp_year[0]) mean_hh = int(mean_time) mean_mm = int((mean_time-int(mean_time))*60) mean_ss = int(((mean_time-int(mean_time))*60 - int((mean_time-int(mean_time))*60))*60) mean_lat = sum(temp_lat) / float(len(temp_lat)) mean_lon = sum(temp_lon) / float(len(temp_lon)) hh_flash.append(mean_hh) mm_flash.append(mean_mm) ss_flash.append(mean_ss) lat_flash.append(mean_lat) lon_flash.append(mean_lon) amp_flash.append(temp_amp[0]) stype_flash.append(1) nstrokes_flash.append(count_q) i = i + count_q index = index + 1. #print len(mon_flash),len(day_flash),len(year_flash),len(hh_flash),\ # len(mm_flash),len(ss_flash),len(lat_flash),len(lon_flash),\ # len(amp_flash),len(stype_flash),len(nstrokes_flash) #quit() ############################################################## # Put flashes in chronological order time_flash = [] for k in range(len(hh_flash)): time = hh_flash[k] + (mm_flash[k]/60.) + (ss_flash[k]/3600.) time_flash.append(time) if min(hh_flash) < 4 and max(hh_flash) > 20: print ('Flash times cross 00Z') time_flash2 = [] for k in range(len(time_flash)): if time_flash[k] < 4: time_flash2.append(time_flash[k]+24) else: time_flash2.append(time_flash[k]) time_flash = time_flash2 index2 = sorted(range(len(time_flash)),key=lambda x:time_flash[x]) mon_sorted = [] day_sorted = [] year_sorted = [] time_sorted = [] hh_sorted = [] mm_sorted = [] ss_sorted = [] lat_sorted = [] lon_sorted = [] amp_sorted = [] stype_sorted = [] nstrokes_sorted = [] for k in index2: time_sorted.append(time_flash[k]) mon_sorted.append(mon_flash[k]) day_sorted.append(day_flash[k]) year_sorted.append(year_flash[k]) hh_sorted.append(hh_flash[k]) mm_sorted.append(mm_flash[k]) ss_sorted.append(ss_flash[k]) lat_sorted.append(lat_flash[k]) lon_sorted.append(lon_flash[k]) amp_sorted.append(amp_flash[k]) stype_sorted.append(stype_flash[k]) nstrokes_sorted.append(nstrokes_flash[k]) mon_flash = mon_sorted day_flash = day_sorted year_flash = year_sorted hh_flash = hh_sorted mm_flash = mm_sorted ss_flash = ss_sorted lat_flash = lat_sorted lon_flash = lon_sorted amp_flash = amp_sorted stype_flash = stype_sorted nstrokes_flash = nstrokes_sorted ############################################################## # Open a new file for writing a = open(out_file, 'w') for x in range(len(mon_flash)): mon = mon_flash[x] day = day_flash[x] year = year_flash[x] hh = hh_flash[x] mm = mm_flash[x] ss = ss_flash[x] lat = lat_flash[x] lon = lon_flash[x] amp = amp_flash[x] stype = stype_flash[x] nstrokes = nstrokes_flash[x] str_hh = str(hh) if hh < 10: str_hh = "0" + str_hh str_mm = str(mm) if mm < 10: str_mm = "0" + str_mm str_ss = str(ss) if ss < 10: str_ss = "0" + str_ss a.write('%2s %2s %2s %2s %2s %2s%8.3f%9.3f%8.1f kA %1s %3i\n' % \ (mon,day,year,str_hh,str_mm,str_ss,lat,lon,amp,stype,nstrokes))