Example #1
0
def main():
    """
    """
    file_base = '/store/msrad/radar/pyrad_products/rad4alp_hydro_PHA/'
    time_dir_list = ['2017-06-29']
    trt_cell_id = '2017062913000174'

    datatype_list = ['dBZc', 'ZDRc', 'RhoHVc', 'KDPc', 'TEMP', 'hydro']
    dataset_list = [
        'reflectivity', 'ZDRc', 'RhoHVc', 'KDPc', 'temperature', 'hydroclass'
    ]

    print("====== Plot time-hist started: %s" %
          datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"))
    atexit.register(_print_end_msg, "====== Plot time-hist finished: ")

    for time_dir in time_dir_list:
        for i, datatype in enumerate(datatype_list):
            dataset = dataset_list[i]
            file_path = file_base + time_dir + '/' + dataset + '_trt_traj/HISTOGRAM/'
            flist = glob.glob(file_path + '*_' + trt_cell_id +
                              '_histogram_*_' + datatype + '.csv')

            if not flist:
                warn('No histogram files found in ' + file_path +
                     ' for TRT cell ' + trt_cell_id)
                continue

            tbin_edges, bin_edges, data_ma = read_histogram_ts(flist, datatype)

            basepath_out = os.path.dirname(flist[0])
            fname = (basepath_out + '/' + trt_cell_id + '_trt_HISTOGRAM_' +
                     datatype + '.png')
            field_name = get_fieldname_pyart(datatype)
            field_dict = get_metadata(field_name)
            titl = 'TRT cell ' + trt_cell_id + '\n' + get_field_name(
                field_dict, field_name)

            _plot_time_range(tbin_edges,
                             bin_edges,
                             data_ma,
                             'frequency_of_occurrence', [fname],
                             titl=titl,
                             ylabel=get_colobar_label(field_dict, field_name),
                             vmin=0.,
                             vmax=np.max(data_ma),
                             figsize=[10, 8],
                             dpi=72)

            print("----- plot to '%s'" % fname)
Example #2
0
def main():
    """
    """
    # parse the arguments
    parser = argparse.ArgumentParser(
        description='Entry to Pyrad processing framework')

    # positional arguments
    parser.add_argument(
        'proc_cfgfile', type=str, help='name of main configuration file')
    parser.add_argument(
        'starttime', type=str,
        help=('starting time of the data to be processed. ' +
              'Format ''YYYYMMDDhhmmss'''))
    parser.add_argument(
        'endtime', type=str,
        help='end time of the data to be processed. Format ''YYYYMMDDhhmmss''')

    # keyword arguments
    parser.add_argument(
        '--cfgpath', type=str,
        default=os.path.expanduser('~')+'/pyrad/config/processing/',
        help='configuration file path')

    parser.add_argument(
        '--storepath', type=str,
        default='/store/msrad/radar/pyrad_products/rad4alp_birds_PHA/',
        help='Base data storing path')

    parser.add_argument(
        '--hres', type=int, default=200, help='Height resolution [m]')

    args = parser.parse_args()

    print("====== PYRAD data processing started: %s" %
          datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"))
    atexit.register(_print_end_msg,
                    "====== PYRAD data processing finished: ")

    print('config path: '+args.cfgpath)
    print('config file: '+args.proc_cfgfile)
    print('start time: '+args.starttime)
    print('end time: '+args.endtime)

    proc_starttime = datetime.datetime.strptime(
        args.starttime, '%Y%m%d%H%M%S')
    proc_endtime = datetime.datetime.strptime(
        args.endtime, '%Y%m%d%H%M%S')
    cfgfile_proc = args.cfgpath+args.proc_cfgfile

    pyrad_main(cfgfile_proc, starttime=proc_starttime, endtime=proc_endtime)

    # Plot time-height
    file_base = args.storepath
    hres = args.hres

    datatype_list = [
        'dBZc', 'eta_h', 'bird_density', 'WIND_SPEED', 'WIND_DIRECTION',
        'wind_vel_h_u', 'wind_vel_h_v', 'wind_vel_v']

    startdate = proc_starttime.replace(hour=0, minute=0, second=0, microsecond=0)
    enddate = proc_endtime.replace(hour=0, minute=0, second=0, microsecond=0)
    ndays = int((enddate-startdate).days)+1
    for datatype in datatype_list:
        flist = []
        for i in range(ndays):
            time_dir = (
                proc_starttime+datetime.timedelta(days=i)).strftime('%Y-%m-%d')

            filepath = (
                file_base+time_dir+'/VAD/PROFILE_WIND/' +
                '*_wind_profile_VAD_WIND_hres'+str(hres)+'.csv')
            labels = [
                'u_wind', 'std_u_wind', 'np_u_wind',
                'v_wind', 'std_v_wind', 'np_v_wind',
                'w_wind', 'std_w_wind', 'np_w_wind',
                'mag_h_wind', 'dir_h_wind']
            label_nr = 0
            if datatype == 'dBZc':
                filepath = (
                    file_base+time_dir+'/velFilter/PROFILE_dBZc/' +
                    '*_rhi_profile_*_dBZc_hres'+str(hres)+'.csv')
                labels = [
                    '50.0-percentile', '25.0-percentile', '75.0-percentile']

                # dBZ mean data
                # filepath = (
                #     file_base+time_dir+'/velFilter/PROFILE_dBZc_mean/' +
                #     '*_rhi_profile_*_dBZc_hres'+str(hres)+'.csv')
                # labels = [
                #     'Mean', 'Min', 'Max']

                # dBZ linear mean data
                # filepath = (
                #     file_base+time_dir+'/velFilter/PROFILE_dBZc_linear_mean/' +
                #     '*_rhi_profile_*_dBZc_hres'+str(hres)+'.csv')
                # labels = [
                #     'Mean', 'Min', 'Max']

                # dBZ before filtering with fitted velocity
                # filepath = (
                #     file_base+time_dir+'/echoFilter/PROFILE_dBZc/' +
                #     '*_rhi_profile_*_dBZc_hres'+str(hres)+'.csv')
                # labels = [
                #     '50.0-percentile', '25.0-percentile', '75.0-percentile']
                #
                # dBZ before filtering with fitted velocity. Linear mean
                # filepath = (
                #     file_base+time_dir+'/echoFilter/PROFILE_dBZc_linear_mean/' +
                #     '*_rhi_profile_*_dBZc_hres'+str(hres)+'.csv')
                # labels = [
                #     'Mean', 'Min', 'Max']
            elif datatype == 'eta_h':
                filepath = (
                    file_base+time_dir+'/vol_refl/PROFILE/' +
                    '*_rhi_profile_*_eta_h_hres'+str(hres)+'.csv')
                labels = [
                    '50.0-percentile', '25.0-percentile', '75.0-percentile']

                # mean data
                # filepath = (
                #     file_base+time_dir+'/vol_refl/PROFILE_mean/' +
                #     '*_rhi_profile_*_eta_h_hres'+str(hres)+'.csv')
                # labels = [
                #     'Mean', 'Min', 'Max']
            elif datatype == 'bird_density':
                filepath = (
                    file_base+time_dir+'/bird_density/PROFILE/' +
                    '*_rhi_profile_*_bird_density_hres'+str(hres)+'.csv')
                labels = [
                    '50.0-percentile', '25.0-percentile', '75.0-percentile']

                # mean data
                # filepath = (
                #     file_base+time_dir+'/bird_density/PROFILE_mean/' +
                #     '*_rhi_profile_*_bird_density_hres'+str(hres)+'.csv')
                # labels = [
                #     'Mean', 'Min', 'Max']
            elif datatype == 'WIND_SPEED':
                label_nr = 9
            elif datatype == 'WIND_DIRECTION':
                label_nr = 10
            elif datatype == 'wind_vel_h_u':
                label_nr = 0
            elif datatype == 'wind_vel_h_v':
                label_nr = 3
            elif datatype == 'wind_vel_v':
                label_nr = 6

            flist_aux = glob.glob(filepath)
            if not flist_aux:
                warn('No profile files found in '+filepath)
                continue
            flist.extend(flist_aux)

        if not flist:
            warn('No profile files found')
            continue
        flist.sort()

        field_name = get_fieldname_pyart(datatype)
        field_dict = get_metadata(field_name)
        titl = 'bird retrieval '+args.starttime+'\n'+get_field_name(
            field_dict, field_name)

        tbin_edges, hbin_edges, np_ma, data_ma, t_start = read_profile_ts(
            flist, labels, hres=hres, label_nr=label_nr)

        basepath_out = os.path.dirname(flist[0])
        fname = (
            basepath_out+'/'+args.starttime+'_TIME_HEIGHT_' +
            datatype+'_hres'+str(hres)+'.png')

        vmin = vmax = None
        _plot_time_range(
            tbin_edges, hbin_edges/1000., data_ma, field_name, [fname],
            titl=titl, figsize=[10, 8], vmin=vmin, vmax=vmax, dpi=72)

        print("----- plot to '%s'" % fname)

        # Plot number of points
        field_dict = get_metadata('number_of_samples')
        titl = 'bird retrieval '+args.starttime+'\n'+get_field_name(
            field_dict, 'number_of_samples')

        fname = (
            basepath_out+'/'+args.starttime+'_TIME_HEIGHT_' +
            datatype+'nsamples_hres'+str(hres)+'.png')

        vmin = vmax = None
        _plot_time_range(
            tbin_edges, hbin_edges/1000., np_ma, 'number_of_samples', [fname],
            titl=titl, figsize=[10, 8], vmin=vmin, vmax=vmax, dpi=72)

        print("----- plot to '%s'" % fname)
Example #3
0
def main():
    """
    """

    # parse the arguments
    parser = argparse.ArgumentParser(
        description='Entry to Pyrad processing framework')

    # positional arguments
    parser.add_argument('proc_cfgfile',
                        type=str,
                        help='name of main configuration file')

    parser.add_argument('days',
                        nargs='+',
                        type=str,
                        help='Dates to process. Format YYYY-MM-DD')

    # keyword arguments
    parser.add_argument('--trtbase',
                        type=str,
                        default='/store/msrad/radar/trt/',
                        help='name of folder containing the TRT cell data')

    parser.add_argument(
        '--radarbase',
        type=str,
        default='/store/msrad/radar/pyrad_products/rad4alp_hydro_PHA/',
        help='name of folder containing the radar data')

    parser.add_argument('--cfgpath',
                        type=str,
                        default=os.path.expanduser('~') +
                        '/pyrad/config/processing/',
                        help='configuration file path')

    parser.add_argument(
        '--datatypes',
        type=str,
        default='hydro,KDPc,dBZc,RhoHVc,TEMP,ZDRc',
        help='Name of the polarimetric moments to process. Coma separated')

    parser.add_argument(
        '--datasets',
        type=str,
        default='hydroclass,KDPc,reflectivity,RhoHVc,temperature,ZDRc',
        help='Name of the directory containing the datasets')

    parser.add_argument('--hres',
                        type=float,
                        default=250.,
                        help='Height resolution')

    args = parser.parse_args()

    print("====== PYRAD TRT data processing started: %s" %
          datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"))
    atexit.register(_print_end_msg,
                    "====== PYRAD TRT data processing finished: ")

    print('config path: ' + args.cfgpath)
    print('config file: ' + args.proc_cfgfile)
    print('trt path: ' + args.trtbase)
    print('radar data path: ' + args.radarbase)

    cfgfile_proc = args.cfgpath + args.proc_cfgfile
    trajtype = 'trt'

    time_dir_list = args.days
    datatype_list = args.datatypes.split(',')
    dataset_list = args.datasets.split(',')

    if np.size(datatype_list) != np.size(dataset_list):
        warn(
            str(np.size(datatype_list)) + ' datatypes but ' +
            str(np.size(dataset_list)) +
            ' dataset directories. Their number must be equal')
        return

    # Find all TRT files in directory
    trt_list = []
    for time_dir in time_dir_list:
        trt_list.extend(
            glob.glob(args.trtbase + time_dir + '/TRTC_cell_plots/All/*.trt'))
        trt_list.extend(
            glob.glob(args.trtbase + time_dir + '/TRTC_cell_plots/Some/*.trt'))

    # Pyrad data processing
    trt_cell_id_list = []
    trt_file_list = []
    for fname in trt_list:
        print('processing TRT cell file ' + fname)
        try:
            infostr = os.path.basename(fname).split('.')[0]
            pyrad_main(cfgfile_proc,
                       trajfile=fname,
                       infostr=infostr,
                       trajtype=trajtype)
            trt_cell_id_list.append(infostr)
            trt_file_list.append(fname)
        except ValueError:
            print(ValueError)

    # plot time series and get altitude of graupel column
    if 'hydro' in datatype_list:
        cell_ID_list = np.asarray([], dtype=int)
        time_list = np.asarray([], dtype=datetime.datetime)
        lon_list = np.asarray([], dtype=float)
        lat_list = np.asarray([], dtype=float)
        area_list = np.asarray([], dtype=float)
        rank_list = np.asarray([], dtype=float)
        rm_hmin_list = np.ma.asarray([], dtype=float)
        rm_hmax_list = np.ma.asarray([], dtype=float)

    for i, trt_cell_id in enumerate(trt_cell_id_list):
        print('\n\nPost-processing cell: ' + trt_cell_id)
        dt_str = trt_cell_id[0:12]
        dt_cell = datetime.datetime.strptime(dt_str, "%Y%m%d%H%M")
        time_dir = dt_cell.strftime("%Y-%m-%d")
        for j, datatype in enumerate(datatype_list):
            dataset = dataset_list[j]
            file_base2 = args.radarbase + time_dir + '/' + dataset + '_trt_traj/'

            field_name = get_fieldname_pyart(datatype)
            field_dict = get_metadata(field_name)
            titl = 'TRT cell ' + trt_cell_id + '\n' + get_field_name(
                field_dict, field_name)

            # plot time-height
            flist = glob.glob(file_base2 + 'PROFILE/*_' + trt_cell_id +
                              '_rhi_profile_*_' + datatype + '_hres' +
                              str(int(args.hres)) + '.csv')
            if not flist:
                warn('No profile files found in ' + file_base2 +
                     'PROFILE/ for TRT cell ' + trt_cell_id +
                     ' with resolution ' + str(args.hres))
            else:
                labels = [
                    '50.0-percentile', '25.0-percentile', '75.0-percentile'
                ]
                if datatype == 'RhoHVc':
                    labels = [
                        '80.0-percentile', '65.0-percentile', '95.0-percentile'
                    ]
                elif datatype == 'hydro':
                    labels = [
                        'Mode', '2nd most common', '3rd most common',
                        '% points mode', '% points 2nd most common',
                        '% points 3rd most common'
                    ]
                elif datatype == 'entropy' or 'prop' in datatype:
                    labels = ['Mean', 'Min', 'Max']

                tbin_edges, hbin_edges, _, data_ma, start_time = (
                    read_profile_ts(flist, labels, hres=args.hres))

                basepath_out = os.path.dirname(flist[0])
                fname = (basepath_out + '/' + trt_cell_id +
                         '_trt_TIME_HEIGHT_' + datatype + '_hres' +
                         str(args.hres) + '.png')

                vmin = vmax = None
                if datatype == 'RhoHVc':
                    vmin = 0.95
                    vmax = 1.00

                xlabel = ('time (s from ' +
                          start_time.strftime("%Y-%m-%d %H:%M:%S") + ')')
                _plot_time_range(tbin_edges,
                                 hbin_edges,
                                 data_ma,
                                 field_name, [fname],
                                 titl=titl,
                                 xlabel=xlabel,
                                 ylabel='height (m MSL)',
                                 figsize=[10, 8],
                                 vmin=vmin,
                                 vmax=vmax,
                                 dpi=72)

                print("----- plot to '%s'" % fname)

                # Get min and max altitude of graupel/hail area
                if datatype == 'hydro':
                    (traj_ID, yyyymmddHHMM, lon, lat, _, _, _, area, _, _, _,
                     RANKr, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
                     _) = read_trt_traj_data(trt_file_list[i])

                    hmin, hmax = get_graupel_column(tbin_edges, hbin_edges,
                                                    data_ma, start_time,
                                                    yyyymmddHHMM)

                    cell_ID_list = np.append(cell_ID_list, traj_ID)
                    time_list = np.append(time_list, yyyymmddHHMM)
                    lon_list = np.append(lon_list, lon)
                    lat_list = np.append(lat_list, lat)
                    area_list = np.append(area_list, area)
                    rank_list = np.append(rank_list, RANKr)
                    rm_hmin_list = np.ma.append(rm_hmin_list, hmin)
                    rm_hmax_list = np.ma.append(rm_hmax_list, hmax)

            # plot time-hist
            flist = glob.glob(file_base2 + 'HISTOGRAM/*_' + trt_cell_id +
                              '_histogram_*_' + datatype + '.csv')

            if not flist:
                warn('No histogram files found in ' + file_base2 +
                     'HISTOGRAM/ for TRT cell ' + trt_cell_id)
            else:
                tbin_edges, bin_edges, data_ma, start_time = read_histogram_ts(
                    flist, datatype)

                basepath_out = os.path.dirname(flist[0])
                fname = (basepath_out + '/' + trt_cell_id + '_trt_HISTOGRAM_' +
                         datatype + '.png')

                data_ma[data_ma == 0.] = np.ma.masked
                xlabel = ('time (s from ' +
                          start_time.strftime("%Y-%m-%d %H:%M:%S") + ')')
                _plot_time_range(tbin_edges,
                                 bin_edges,
                                 data_ma,
                                 'frequency_of_occurrence', [fname],
                                 titl=titl,
                                 xlabel=xlabel,
                                 ylabel=get_colobar_label(
                                     field_dict, field_name),
                                 vmin=0.,
                                 vmax=np.max(data_ma),
                                 figsize=[10, 8],
                                 dpi=72)

                print("----- plot to '%s'" % fname)

            # plot quantiles
            flist = glob.glob(file_base2 + 'QUANTILES/*_' + trt_cell_id +
                              '_quantiles_*_' + datatype + '.csv')

            if not flist:
                warn('No quantiles files found in ' + file_base2 +
                     'QUANTILES/ for TRT cell ' + trt_cell_id)
                continue

            tbin_edges, qbin_edges, data_ma, start_time = read_quantiles_ts(
                flist, step=5., qmin=0., qmax=100.)

            basepath_out = os.path.dirname(flist[0])
            fname = (basepath_out + '/' + trt_cell_id + '_trt_QUANTILES_' +
                     datatype + '.png')

            vmin = vmax = None
            if datatype == 'RhoHVc':
                vmin = 0.95
                vmax = 1.00
            xlabel = ('time (s from ' +
                      start_time.strftime("%Y-%m-%d %H:%M:%S") + ')')
            _plot_time_range(tbin_edges,
                             qbin_edges,
                             data_ma,
                             field_name, [fname],
                             titl=titl,
                             xlabel=xlabel,
                             ylabel='Quantile',
                             vmin=vmin,
                             vmax=vmax,
                             figsize=[10, 8],
                             dpi=72)

            print("----- plot to '%s'" % fname)

    if 'hydro' in datatype_list:
        fname = args.trtbase + 'cell_rimmed_particles_column.csv'
        write_trt_cell_lightning(cell_ID_list, time_list, lon_list, lat_list,
                                 area_list, rank_list, rm_hmin_list,
                                 rm_hmax_list, fname)

        print("----- written to '%s'" % fname)
Example #4
0
def main():
    """
    """
    file_base = '/store/msrad/radar/pyrad_products/rad4alp_hydro_PHA/'
    time_dir_list = ['2017-06-29']
    trt_cell_id = '2017062913000174'
    hres = 250

    datatype_list = ['dBZc', 'ZDRc', 'RhoHVc', 'KDPc', 'TEMP', 'hydro']
    dataset_list = [
        'reflectivity', 'ZDRc', 'RhoHVc', 'KDPc', 'temperature', 'hydroclass'
    ]

    print("====== Plot time-height started: %s" %
          datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"))
    atexit.register(_print_end_msg, "====== Plot time-height finished: ")

    for time_dir in time_dir_list:
        for i, datatype in enumerate(datatype_list):

            labels = ['50.0-percentile', '25.0-percentile', '75.0-percentile']
            if datatype == 'RhoHVc':
                labels = [
                    '80.0-percentile', '65.0-percentile', '95.0-percentile'
                ]
            elif datatype == 'hydro':
                labels = [
                    'Mode', '2nd most common', '3rd most common',
                    '% points mode', '% points 2nd most common',
                    '% points 3rd most common'
                ]

            dataset = dataset_list[i]
            file_path = file_base + time_dir + '/' + dataset + '_trt_traj/PROFILE/'
            flist = glob.glob(file_path + '*_' + trt_cell_id +
                              '_rhi_profile_*_' + datatype + '_hres' +
                              str(hres) + '.csv')

            if not flist:
                warn('No profile files found in ' + file_path +
                     ' for TRT cell ' + trt_cell_id + ' with resolution ' +
                     str(hres))
                continue

            tbin_edges, hbin_edges, data_ma = read_profile_ts(flist,
                                                              labels,
                                                              hres=hres)

            basepath_out = os.path.dirname(flist[0])
            fname = (basepath_out + '/' + trt_cell_id + '_trt_TIME_HEIGHT_' +
                     datatype + '_hres' + str(hres) + '.png')
            field_name = get_fieldname_pyart(datatype)
            field_dict = get_metadata(field_name)
            titl = 'TRT cell ' + trt_cell_id + '\n' + get_field_name(
                field_dict, field_name)

            vmin = vmax = None
            if datatype == 'RhoHVc':
                vmin = 0.95
                vmax = 1.00
            _plot_time_range(tbin_edges,
                             hbin_edges,
                             data_ma,
                             field_name, [fname],
                             titl=titl,
                             figsize=[10, 8],
                             vmin=vmin,
                             vmax=vmax,
                             dpi=72)

            print("----- plot to '%s'" % fname)
def main():
    """
    """
    # basepath = '/data/pyrad_products/rad4alp_hydro_PHA/'
    basepath = '/store/msrad/radar/pyrad_products/rad4alp_hydro_PHA/'
    day_vec = [
        datetime.datetime(2017, 6, 29),
        datetime.datetime(2017, 6, 30),
        datetime.datetime(2017, 7, 10),
        datetime.datetime(2017, 7, 14),
        datetime.datetime(2017, 7, 18),
        datetime.datetime(2017, 7, 19),
        datetime.datetime(2017, 7, 30),
        datetime.datetime(2017, 8, 1)
    ]

    #    day_vec = [
    #        datetime.datetime(2017, 7, 14)]

    basename = 'Santis_data_entropy_CGpn'
    filt_type = 'keep_all'
    nsources_min = 10

    if 'entropy' in basename:
        pol_vals_labels = [
            'hydro', 'entropy', 'propAG', 'propCR', 'propIH', 'propLR',
            'propMH', 'propRN', 'propRP', 'propVI', 'propWS'
        ]

        datatype_vec = [
            'hydro', 'entropy', 'propAG', 'propCR', 'propIH', 'propLR',
            'propMH', 'propRN', 'propRP', 'propVI', 'propWS'
        ]

        step_list = [None, 0.1, 1., 1., 1., 1., 1., 1., 1., 1., 1.]
    else:
        pol_vals_labels = [
            'hydro [-]', 'KDPc [deg/Km]', 'dBZc [dBZ]', 'RhoHVc [-]',
            'TEMP [deg C]', 'ZDRc [dB]'
        ]

        datatype_vec = ['hydro', 'KDPc', 'dBZc', 'RhoHVc', 'TEMP', 'ZDRc']

        step_list = [None, 0.05, 0.5, 0.001, 1., 0.1]

    for label in pol_vals_labels:
        if 'hydro' in label:
            hydro_label = label
            break

    print("====== Lightning post-processing started: %s" %
          datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"))
    atexit.register(_print_end_msg,
                    "====== Lightning post-processing finished: ")

    # read all the data to analyze
    flashnr, time_data, time_in_flash, lat, lon, alt, dBm, pol_vals_dict = (
        read_data(basepath,
                  day_vec,
                  basename=basename,
                  pol_vals_labels=pol_vals_labels))

    #    flashnr, time_data, time_in_flash, lat, lon, alt, dBm, pol_vals_dict = (
    #        read_data_two_sources(
    #            basepath, day_vec, basename1='Santis_data_entropy',
    #            basename2='Santis_data_entropy_CGt', basename_out='Santis_data_entropy_no_CG',
    #            keep_common=False,
    #            pol_vals_labels=pol_vals_labels))

    # Get indices of data to keep
    if filt_type == 'keep_all':
        ind, data_ID, subtitl = get_indices_all_data(flashnr,
                                                     nsources_min=nsources_min)
    elif filt_type == 'keep_solid':
        ind, data_ID, subtitl = get_indices_solid_phase(
            flashnr, pol_vals_dict[hydro_label], nsources_min=nsources_min)
    elif filt_type == 'keep_liquid':
        ind, data_ID, subtitl = get_indices_liquid_phase(
            flashnr, pol_vals_dict[hydro_label], nsources_min=nsources_min)
    elif filt_type == 'keep_liquid_origin':
        ind, data_ID, subtitl = get_indices_liquid_phase_origin(
            flashnr, pol_vals_dict[hydro_label], nsources_min=nsources_min)
    else:
        warn('Unknown filter type ' + filt_type)
        return

    flashnr_filt = flashnr[ind]
    time_data_filt = time_data[ind]
    time_in_flash_filt = time_in_flash[ind]
    lat_filt = lat[ind]
    lon_filt = lon[ind]
    alt_filt = alt[ind]
    dBm_filt = dBm[ind]
    pol_vals_dict_filt = deepcopy(pol_vals_dict)
    for key in pol_vals_dict.keys():
        pol_vals_dict_filt[key] = pol_vals_dict[key][ind]


#    # write the filtered data in a file
#    vals_list = []
#    for label in pol_vals_labels:
#        vals_list.append(pol_vals_dict_filt[label])
#
#    fname = basepath+basename'_'+data_ID+'.csv'
#    write_ts_lightning(
#        flashnr_filt, time_data_filt, time_in_flash_filt, lat_filt, lon_filt,
#        alt_filt, dBm_filt, vals_list, fname, pol_vals_labels)
#    print('written to '+fname)

# get flashes origin of filtered data
    flashnr_first, ind_first = np.unique(flashnr_filt, return_index=True)
    time_data_first = time_data_filt[ind_first]
    time_in_flash_first = time_in_flash_filt[ind_first]
    lat_first = lat_filt[ind_first]
    lon_first = lon_filt[ind_first]
    alt_first = alt_filt[ind_first]
    dBm_first = dBm_filt[ind_first]
    pol_vals_dict_first = deepcopy(pol_vals_dict_filt)
    for key in pol_vals_dict_filt.keys():
        pol_vals_dict_first[key] = pol_vals_dict_filt[key][ind_first]

    # get duration and area of flash
    duration_filt = np.ma.masked_all(flashnr_first.size)
    area_filt = np.ma.masked_all(flashnr_first.size)

    chy_filt, chx_filt, _ = wgs84_to_swissCH1903(lon_filt,
                                                 lat_filt,
                                                 alt_filt,
                                                 no_altitude_transform=True)

    for i, flash_ID in enumerate(flashnr_first):
        time_data_flash = time_data_filt[flashnr_filt == flash_ID]
        duration_filt[i] = (
            1e3 * (time_data_flash[-1] - time_data_flash[0]).total_seconds())

        chy_flash = chy_filt[flashnr_filt == flash_ID]
        chx_flash = chx_filt[flashnr_filt == flash_ID]

        points_flash = shapely.geometry.MultiPoint(
            list(zip(chy_flash, chx_flash)))
        area_filt[i] = points_flash.minimum_rotated_rectangle.area * 1e-6

    print('N flashes: ' + str(flashnr_first.size))
    print('N sources: ' + str(flashnr_filt.size))

    # Analyse the data

    # create histograms of hydrometeor proportions
    if 'propAG' in pol_vals_dict_filt:
        bins_centers = np.arange(0, 10, 1)
        bins_edges = np.arange(-0.5, 10.5, 1)

        # Create histogram of number of differnt hydrometeors types in each
        # radar range gate. All sources
        nhydros_hist = hist_nhydros_gate(pol_vals_dict_filt, percent_min=10.)

        fname = (basepath + data_ID +
                 '_allsources_ts_trajlightning_nhydro.png')
        plot_histogram2(bins_centers,
                        nhydros_hist, [fname],
                        labelx='Number of hydrometeors in radar range gate',
                        labely='occurrence',
                        titl='Trajectory Histogram All Sources' + subtitl)

        print("----- plot to '%s'" % fname)

        # store histogram
        fname = (basepath + data_ID +
                 '_allsources_ts_trajlightning_nhydro.csv')
        write_histogram(bins_edges, nhydros_hist, fname)
        print('Written ' + fname)

        # Create histogram of number of different hydrometeors types in each
        # radar range gate. First source
        nhydros_hist = hist_nhydros_gate(pol_vals_dict_first, percent_min=10.)

        fname = (basepath + data_ID +
                 '_firstsource_ts_trajlightning_nhydro.png')
        plot_histogram2(bins_centers,
                        nhydros_hist, [fname],
                        labelx='Number of hydrometeors in radar range gate',
                        labely='occurrence',
                        titl='Trajectory Histogram First Sources' + subtitl)

        print("----- plot to '%s'" % fname)

        # store histogram
        fname = (basepath + data_ID +
                 '_firstsource_ts_trajlightning_nhydro.csv')
        write_histogram(bins_edges, nhydros_hist, fname)
        print('Written ' + fname)

        return

        # Create histograms of dominant hydrometeors all sources
        hydro_hist2 = hist_dominant_hydrometeors(pol_vals_dict_filt,
                                                 percent_min=10.)

        fname_hist = basepath + data_ID + '_allsources_ts_trajlightning_hydro_dominant.png'
        fname_hist = _plot_time_range(bins_edges,
                                      bins_edges,
                                      hydro_hist2,
                                      None, [fname_hist],
                                      titl='Trajectory Histogram All Sources' +
                                      subtitl,
                                      xlabel='Dominant hydrometeor',
                                      ylabel='2nd most dominant hydrometeor',
                                      vmin=0,
                                      clabel='Occurrence',
                                      figsize=[10, 8],
                                      dpi=72)
        print('Plotted ' + ' '.join(fname_hist))

        # Create histogram of dominant hydrometeors first sources
        hydro_hist2 = hist_dominant_hydrometeors(pol_vals_dict_first,
                                                 percent_min=10.)

        fname_hist = basepath + data_ID + '_firstsource_ts_trajlightning_hydro_dominant.png'
        fname_hist = _plot_time_range(
            bins_edges,
            bins_edges,
            hydro_hist2,
            None, [fname_hist],
            titl='Trajectory Histogram First Sources' + subtitl,
            xlabel='Dominant hydrometeor',
            ylabel='2nd most dominant hydrometeor',
            vmin=0,
            clabel='Occurrence',
            figsize=[10, 8],
            dpi=72)
        print('Plotted ' + ' '.join(fname_hist))

        # create histogram of percentage of dominant hydrometeor all sources
        hydro_hist = hist_hydrometeor_mixtures(pol_vals_dict_filt)

        fname = (basepath + data_ID +
                 '_allsources_ts_trajlightning_hydro_prop.png')
        plot_histogram2(bins_centers,
                        hydro_hist, [fname],
                        labelx='radar echo classification (-)',
                        labely='percentage',
                        titl='Trajectory Histogram All Sources' + subtitl)

        print("----- plot to '%s'" % fname)

        # store histogram
        fname = (basepath + data_ID +
                 '_allsources_ts_trajlightning_hydro_prop.csv')
        write_histogram(bins_edges, hydro_hist, fname)
        print('Written ' + fname)

        # create histogram of percentage of dominant hydrometeor first sources
        hydro_hist = hist_hydrometeor_mixtures(pol_vals_dict_first)

        fname = (basepath + data_ID +
                 '_firstsource_ts_trajlightning_hydro_prop.png')
        plot_histogram2(bins_centers,
                        hydro_hist, [fname],
                        labelx='radar echo classification (-)',
                        labely='percentage',
                        titl='Trajectory Histogram First Sources' + subtitl)

        print("----- plot to '%s'" % fname)

        # store histogram
        fname = (basepath + data_ID +
                 '_firstsource_ts_trajlightning_hydro_prop.csv')
        write_histogram(bins_edges, hydro_hist, fname)
        print('Written ' + fname)

    for i, key in enumerate(pol_vals_labels):
        step = step_list[i]
        datatype = datatype_vec[i]

        field_name = get_fieldname_pyart(datatype)
        field_dict = get_metadata(field_name)

        labelx = get_colobar_label(field_dict, field_name)

        vals = pol_vals_dict_filt[key]
        bins, values = compute_histogram(vals, field_name, step=step)

        print(datatype + ' min: ' + str(vals.min()))
        print(datatype + ' max: ' + str(vals.max()))

        # Plot all sources histogram
        fname_first_source = (basepath + data_ID +
                              '_allsources_ts_trajlightning_' + datatype +
                              '.png')
        plot_histogram(bins,
                       values, [fname_first_source],
                       labelx=labelx,
                       titl='Trajectory Histogram All Sources' + subtitl)

        print("----- plot to '%s'" % fname_first_source)

        # store histogram
        fname_first_source = (basepath + data_ID +
                              '_allsources_ts_trajlightning_' + datatype +
                              '.csv')
        hist_values, _ = np.histogram(values, bins=bins)
        write_histogram(bins, hist_values, fname_first_source)
        print('Written ' + fname_first_source)

        # First sources
        vals = pol_vals_dict_first[key]
        bins, values = compute_histogram(vals, field_name, step=step)

        # Plot first source histogram
        fname_first_source = (basepath + data_ID +
                              '_firstsource_ts_trajlightning_' + datatype +
                              '.png')
        plot_histogram(bins,
                       values, [fname_first_source],
                       labelx=labelx,
                       titl='Trajectory Histogram First Source' + subtitl)

        print("----- plot to '%s'" % fname_first_source)

        # store histogram
        fname_first_source = (basepath + data_ID +
                              '_firstsource_ts_trajlightning_' + datatype +
                              '.csv')

        hist_values_first, _ = np.histogram(values, bins=bins)
        write_histogram(bins, hist_values_first, fname_first_source)
        print('Written ' + fname_first_source)

    # Get histograms of sources altitude and power

    # define histogram bin edges
    bin_edges_alt = np.arange(-50., 14150., 100.)
    bin_edges_dBm = np.arange(-17., 47., 1.)
    bin_edges_time = np.arange(0, 25, 1)
    bin_edges_area = np.arange(0., 2100., 100.)
    bin_edges_duration = np.arange(0., 1100., 100.)

    # Plot histogram of LMA flash area
    _, area_filt_values = compute_histogram(area_filt,
                                            None,
                                            bin_edges=bin_edges_area)
    fname_hist = basepath + data_ID + '_Santis_hist_area.png'
    fname_hist = plot_histogram(bin_edges_area,
                                area_filt_values, [fname_hist],
                                labelx='Area [km2]',
                                titl='Flash area' + subtitl)
    print('Plotted ' + ' '.join(fname_hist))

    fname_hist = basepath + data_ID + '_Santis_hist_area.csv'
    hist_area, _ = np.histogram(area_filt_values, bins=bin_edges_area)
    fname_hist = write_histogram(bin_edges_area, hist_area, fname_hist)
    print('Written ' + fname_hist)

    # Plot histogram of LMA flash duration
    _, duration_filt_values = compute_histogram(duration_filt,
                                                None,
                                                bin_edges=bin_edges_duration)
    fname_hist = basepath + data_ID + '_Santis_hist_duration.png'
    fname_hist = plot_histogram(bin_edges_duration,
                                duration_filt_values, [fname_hist],
                                labelx='Duration [ms]',
                                titl='Flash duration' + subtitl)
    print('Plotted ' + ' '.join(fname_hist))

    fname_hist = basepath + data_ID + '_Santis_hist_duration.csv'
    hist_duration, _ = np.histogram(duration_filt_values,
                                    bins=bin_edges_duration)
    fname_hist = write_histogram(bin_edges_duration, hist_duration, fname_hist)
    print('Written ' + fname_hist)

    # Plot histogram time of occurrence
    time_hour_first = occurrence_time(time_data_first)

    fname_hist = basepath + data_ID + '_Santis_hist_time.png'
    fname_hist = plot_histogram(bin_edges_time,
                                time_hour_first, [fname_hist],
                                labelx='Hour [UTC]',
                                titl='Flash occurrence time' + subtitl)
    print('Plotted ' + ' '.join(fname_hist))

    fname_hist = basepath + data_ID + '_Santis_hist_time.csv'
    hist_time, _ = np.histogram(time_hour_first, bins=bin_edges_time)
    fname_hist = write_histogram(bin_edges_time, hist_time, fname_hist)
    print('Written ' + fname_hist)

    # Plot histogram altitude all sources
    _, alt_filt_values = compute_histogram(alt_filt,
                                           None,
                                           bin_edges=bin_edges_alt)
    fname_hist = basepath + data_ID + '_Santis_hist_alt.png'
    fname_hist = plot_histogram(bin_edges_alt,
                                alt_filt_values, [fname_hist],
                                labelx='Altitude [m MSL]',
                                titl='Flash sources altitude' + subtitl)
    print('Plotted ' + ' '.join(fname_hist))

    fname_hist = basepath + data_ID + '_Santis_hist_alt.csv'
    hist_alt, _ = np.histogram(alt_filt_values, bins=bin_edges_alt)
    fname_hist = write_histogram(bin_edges_alt, hist_alt, fname_hist)
    print('Written ' + fname_hist)

    # Plot histogram altitude first sources
    _, alt_first_values = compute_histogram(alt_first,
                                            None,
                                            bin_edges=bin_edges_alt)
    fname_hist = basepath + data_ID + '_Santis_hist_alt_first_source.png'
    fname_hist = plot_histogram(bin_edges_alt,
                                alt_first_values, [fname_hist],
                                labelx='Altitude [m MSL]',
                                titl='Flash first source altitude' + subtitl)
    print('Plotted ' + ' '.join(fname_hist))

    fname_hist = basepath + data_ID + '_Santis_hist_alt_first_source.csv'
    hist_alt_fist, _ = np.histogram(alt_first_values, bins=bin_edges_alt)
    fname_hist = write_histogram(bin_edges_alt, hist_alt_fist, fname_hist)
    print('Written ' + fname_hist)

    # Plot histogram power all sources
    _, dBm_filt_values = compute_histogram(dBm_filt,
                                           None,
                                           bin_edges=bin_edges_dBm)
    fname_hist = basepath + data_ID + '_Santis_hist_dBm.png'
    fname_hist = plot_histogram(bin_edges_dBm,
                                dBm_filt_values, [fname_hist],
                                labelx='Power [dBm]',
                                titl='Flash sources power' + subtitl)
    print('Plotted ' + ' '.join(fname_hist))

    fname_hist = basepath + data_ID + '_Santis_hist_dBm.csv'
    hist_dBm, _ = np.histogram(dBm_filt_values, bins=bin_edges_dBm)
    fname_hist = write_histogram(bin_edges_dBm, hist_dBm, fname_hist)
    print('Written ' + fname_hist)

    # Plot histogram power first sources
    _, dBm_first_values = compute_histogram(dBm_first,
                                            None,
                                            bin_edges=bin_edges_dBm)
    fname_hist = basepath + data_ID + '_Santis_hist_dBm_first_source.png'
    fname_hist = plot_histogram(bin_edges_dBm,
                                dBm_first_values, [fname_hist],
                                labelx='Power [dBm]',
                                titl='Flash first source power' + subtitl)
    print('Plotted ' + ' '.join(fname_hist))

    fname_hist = basepath + data_ID + '_Santis_hist_dBm_first_source.csv'
    hist_dBm_first, _ = np.histogram(dBm_first_values, bins=bin_edges_dBm)
    fname_hist = write_histogram(bin_edges_dBm, hist_dBm_first, fname_hist)
    print('Written ' + fname_hist)

    # Plot 2D histogram all sources
    H, _, _ = np.histogram2d(alt_filt_values,
                             dBm_filt_values,
                             bins=[bin_edges_alt, bin_edges_dBm])

    # set 0 values to blank
    H = np.ma.asarray(H)
    H[H == 0] = np.ma.masked

    fname_hist = basepath + data_ID + '_Santis_2Dhist_alt_dBm.png'
    fname_hist = _plot_time_range(bin_edges_alt,
                                  bin_edges_dBm,
                                  H,
                                  None, [fname_hist],
                                  titl='LMA sources Altitude-Power histogram' +
                                  subtitl,
                                  xlabel='Altitude [m MSL]',
                                  ylabel='Power [dBm]',
                                  clabel='Occurrence',
                                  vmin=0,
                                  vmax=None,
                                  figsize=[10, 8],
                                  dpi=72)
    print('Plotted ' + ' '.join(fname_hist))

    # Plot 2D histogram first sources
    H, _, _ = np.histogram2d(alt_first_values,
                             dBm_first_values,
                             bins=[bin_edges_alt, bin_edges_dBm])

    # set 0 values to blank
    H = np.ma.asarray(H)
    H[H == 0] = np.ma.masked

    fname_hist = basepath + data_ID + '_Santis_2Dhist_alt_dBm_first_source.png'
    fname_hist = _plot_time_range(
        bin_edges_alt,
        bin_edges_dBm,
        H,
        None, [fname_hist],
        titl='LMA first sources Altitude-Power histogram' + subtitl,
        xlabel='Altitude [m MSL]',
        ylabel='Power [dBm]',
        clabel='Occurrence',
        vmin=0,
        vmax=None,
        figsize=[10, 8],
        dpi=72)
    print('Plotted ' + ' '.join(fname_hist))

    # plot position all sources
    figfname = basepath + data_ID + '_Santis_LMA_sources_pos_max_height_on_top.png'
    figfname = plot_pos(lat_filt,
                        lon_filt,
                        alt_filt, [figfname],
                        sort_altitude='Highest_on_top',
                        cb_label='Source height [m MSL]',
                        titl='Flash sources position. Highest on top' +
                        subtitl)
    print('Plotted ' + ' '.join(figfname))

    figfname = basepath + data_ID + '_Santis_LMA_sources_pos_min_height_on_top.png'
    figfname = plot_pos(lat_filt,
                        lon_filt,
                        alt_filt, [figfname],
                        sort_altitude='Lowest_on_top',
                        cb_label='Source height [m MSL]',
                        titl='Flash sources position. Lowest on top' + subtitl)
    print('Plotted ' + ' '.join(figfname))

    # plot position first source
    figfname = (basepath + data_ID +
                '_Santis_LMA_first_source_pos_max_height_on_top.png')
    figfname = plot_pos(lat_first,
                        lon_first,
                        alt_first, [figfname],
                        sort_altitude='Highest_on_top',
                        cb_label='Source height [m MSL]',
                        titl='First flash source position. Highest on top' +
                        subtitl)
    print('Plotted ' + ' '.join(figfname))

    figfname = (basepath + data_ID +
                '_Santis_LMA_first_source_pos_min_height_on_top.png')
    figfname = plot_pos(lat_first,
                        lon_first,
                        alt_first, [figfname],
                        sort_altitude='Lowest_on_top',
                        cb_label='Source height [m MSL]',
                        titl='First flash source position. Lowest on top' +
                        subtitl)
    print('Plotted ' + ' '.join(figfname))
Example #6
0
def main():
    """
    """
    # parse the arguments
    parser = argparse.ArgumentParser(
        description='Entry to Pyrad processing framework')

    # positional arguments
    parser.add_argument(
        'days', nargs='+', type=str,
        help='Dates to process. Format YYYY-MM-DD')

    # keyword arguments
    parser.add_argument(
        '--nsources_min', type=int, default=10,
        help='Minimum number of sources to consider the LMA flash valid')

    parser.add_argument(
        '--trtbase', type=str,
        default='/store/msrad/radar/trt/',
        help='name of folder containing the TRT cell data')

    parser.add_argument(
        '--flashpath', type=str,
        default='/store/msrad/lightning/LMA/Santis/',
        help='name of the folder containing the Santis LMA data')

    args = parser.parse_args()

    print("====== LMA data TRT processing started: %s" %
          datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"))
    atexit.register(_print_end_msg,
                    "====== LMA data TRT processing finished: ")

    print('trt path: '+args.trtbase)

    time_dir_list = args.days

    # Get bins altitude
    alt_min = 0.
    alt_max = 14000.
    step = 100.
    bin_edges = np.linspace(
        alt_min-step/2., alt_max+step/2, num=int((alt_max-alt_min)/step)+2)

    trt_list = []
    for time_dir in time_dir_list:
        trt_list.extend(glob.glob(
            args.trtbase+time_dir+'/TRTC_cell_plots/All/*.trt'))
        trt_list.extend(glob.glob(
            args.trtbase+time_dir+'/TRTC_cell_plots/Some/*.trt'))

    cell_ID_list = np.ma.asarray([], dtype=int)
    time_list = np.ma.asarray([], dtype=datetime.datetime)
    lon_list = np.ma.asarray([], dtype=float)
    lat_list = np.ma.asarray([], dtype=float)
    flash_density_list = np.ma.asarray([], dtype=float)
    sources_density_list = np.ma.asarray([], dtype=float)
    rank_flash_density_list = np.ma.asarray([], dtype=float)
    area_list = np.ma.asarray([], dtype=float)
    nflash_list = np.ma.asarray([], dtype=int)
    nsources_list = np.ma.asarray([], dtype=int)
    for trt_fname in trt_list:
        print('processing TRT cell file '+trt_fname)
        trtpath = os.path.dirname(trt_fname)+'/'

        # reading TRT cell file
        (traj_ID, yyyymmddHHMM, lon_trt, lat_trt, _, _, _, area, _, _, _,
         RANKr, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, cell_contour) = (
             read_trt_traj_data(trt_fname))

        # reading lightning file
        infostr = os.path.basename(trt_fname).split('.')[0]
        dt_str = infostr[0:12]
        dt = datetime.datetime.strptime(dt_str, '%Y%m%d%H%M')

        flashnr, time_data, _, lat, lon, alt, _ = read_lightning(
            args.flashpath+dt.strftime("%y%m%d")+'.txt')

        if flashnr is None:
            continue

        # Filter data with less than nsources_min sources
        unique_flashnr = np.unique(flashnr, return_index=False)

        ind = []
        for flash in unique_flashnr:
            ind_flash = np.where(flashnr == flash)[0]
            if ind_flash.size < args.nsources_min:
                continue
            ind.extend(ind_flash)

        if np.size(ind) == 0:
            continue

        flashnr = flashnr[ind]
        time_data = time_data[ind]
        lat = lat[ind]
        lon = lon[ind]
        alt = alt[ind]

        # Get first sources data
        _, unique_ind = np.unique(flashnr, return_index=True)
        lat_first = lat[unique_ind]
        lon_first = lon[unique_ind]
        alt_first = alt[unique_ind]
        # dBm_first = dBm[unique_ind]
        time_first = time_data[unique_ind]

        # Find cell period
        cell_dt_s = np.empty(yyyymmddHHMM.size, dtype=float)
        for i, cell_time in enumerate(yyyymmddHHMM):
            cell_dt_s[i] = (cell_time-yyyymmddHHMM[0]).total_seconds()
        t_res = np.mean(cell_dt_s[1:]-cell_dt_s[:-1])

        # analyze first sources
        print('\n\n--- Processing first sources ----')
        nflashes = np.zeros(yyyymmddHHMM.size, dtype=int)
        for i, cell_time in enumerate(yyyymmddHHMM):
            cell_time_str = cell_time.strftime("%Y%m%d%H%M%S")

            # Find flashes within time step of cell
            tstart_cell_step = cell_time-datetime.timedelta(seconds=t_res)
            inds = np.where(np.logical_and(
                time_first > tstart_cell_step, time_first <= cell_time))[0]
            if inds.size == 0:
                warn('No flashes within time step')
                fname_hist = (
                    trtpath+cell_time_str+'_'+infostr +
                    '_hist_alt_first_source.csv')
                fname_hist = write_histogram(
                    bin_edges, np.zeros(bin_edges.size-1, dtype=int),
                    fname_hist, step=step)
                print('----- save to '+fname_hist)
                continue

            lat_cell = lat_first[inds]
            lon_cell = lon_first[inds]
            alt_cell = alt_first[inds]

            # Find flashes within cell contour
            inds, is_roi = belongs_roi_indices(
                lat_cell, lon_cell, cell_contour[i])
            if is_roi == 'None':
                warn('No flashes within cell contour')
                fname_hist = (
                    trtpath+cell_time_str+'_'+infostr +
                    '_hist_alt_first_source.csv')
                fname_hist = write_histogram(
                    bin_edges, np.zeros(bin_edges.size-1, dtype=int),
                    fname_hist, step=step)
                print('----- save to '+fname_hist)
                continue
            elif is_roi == 'All':
                inds = inds[0]

            lat_cell = lat_cell[inds]
            lon_cell = lon_cell[inds]
            alt_cell = alt_cell[inds]

            # compute number of flashes
            nflashes[i] = lat_cell.size

            # Plot altitude histogram
            fname_hist = (
                trtpath+cell_time_str+'_'+infostr +
                '_hist_alt_first_source.png')
            titl = (
                cell_time_str+' '+infostr +
                ' TRT cell. Flash first source altitude')
            fname_hist = plot_histogram(
                bin_edges, alt_cell, [fname_hist], labelx='Altitude [m MSL]',
                titl=titl)
            print('Plotted '+' '.join(fname_hist))

            fname_hist = (
                trtpath+cell_time_str+'_'+infostr +
                '_hist_alt_first_source.csv')
            hist, bin_edges = np.histogram(alt_cell, bins=bin_edges)
            fname_hist = write_histogram(
                bin_edges, hist, fname_hist, step=step)
            print('----- save to '+fname_hist)

            # plot position first source
            figfname = (
                trtpath+cell_time_str+'_'+infostr +
                '_first_source_pos_max_height_on_top.png')
            titl = (
                cell_time_str+' '+infostr +
                ' first flash source position. Highest on top')
            figfname = plot_pos(
                lat_cell, lon_cell, alt_cell, [figfname],
                sort_altitude='Highest_on_top',
                cb_label='Source height [m MSL]', titl=titl)
            print('Plotted '+' '.join(figfname))

        # plot time series lightning density
        figfname = (
            trtpath+cell_time_str+'_'+infostr+'_dens_first_sources.png')
        titl = infostr+' First sources density'
        figfname = plot_timeseries(
            yyyymmddHHMM, [nflashes/area], [figfname], labelx='Time [UTC]',
            labely='Flash dens [Flashes/Km2]', labels=['Flash density'],
            title=titl, period=0, timeformat=None, colors=None,
            linestyles=None, markers=None, ymin=None, ymax=None, dpi=72)
        print('Plotted '+' '.join(figfname))

        # plot time series lightning
        figfname = (
            trtpath+cell_time_str+'_'+infostr+'_N_first_sources.png')
        titl = infostr+' Number of first sources in cell'
        figfname = plot_timeseries(
            yyyymmddHHMM, [nflashes], [figfname], labelx='Time [UTC]',
            labely='N flashes', labels=['N flashes'], title=titl,
            period=0, timeformat=None, colors=None, linestyles=None,
            markers=None, ymin=None, ymax=None, dpi=72)
        print('Plotted '+' '.join(figfname))

        # plot time-hist_height
        flist = glob.glob(
            trtpath+'*_'+infostr+'_hist_alt_first_source.csv')

        if not flist:
            warn('No histogram files found in '+trtpath +
                 ' for TRT cell '+infostr)
        else:
            tbin_edges, bin_edges, data_ma, _ = read_histogram_ts(
                flist, 'flash_altitude')

            vmax = np.max(data_ma)
            if vmax == 0.:
                warn('Unable to plot histogram. No valid data')
            else:
                data_ma[data_ma == 0.] = np.ma.masked
                fname_hist = (
                    trtpath+infostr+'_trt_HISTOGRAM_alt_first_source.png')
                titl = ('TRT cell '+infostr+'\n' +
                        'Altitude of first flash source')
                _plot_time_range(
                    tbin_edges, bin_edges, data_ma, 'frequency_of_occurrence',
                    [fname_hist], titl=titl,
                    ylabel='Altitude [m MSL]',
                    vmin=0., vmax=vmax, figsize=[10, 8], dpi=72)

                print("----- plot to '%s'" % fname_hist)

        # Append flash data
        cell_ID_list = np.ma.append(cell_ID_list, traj_ID)
        time_list = np.ma.append(time_list, yyyymmddHHMM)
        lon_list = np.ma.append(lon_list, lon_trt)
        lat_list = np.ma.append(lat_list, lat_trt)
        flash_density_list = np.ma.append(flash_density_list, nflashes/area)
        rank_flash_density_list = np.ma.append(
            rank_flash_density_list, RANKr)
        area_list = np.ma.append(area_list, area)
        nflash_list = np.ma.append(nflash_list, nflashes)

        # analyze all flashes
        print('\n\n--- Processing all sources ----')
        nflashes = np.zeros(yyyymmddHHMM.size, dtype=int)
        for i, cell_time in enumerate(yyyymmddHHMM):
            cell_time_str = cell_time.strftime("%Y%m%d%H%M%S")

            # Find flashes within time step of cell
            tstart_cell_step = cell_time-datetime.timedelta(seconds=t_res)
            inds = np.where(np.logical_and(
                time_data > tstart_cell_step, time_data <= cell_time))[0]
            if inds.size == 0:
                warn('No flashes within time step')
                fname_hist = (
                    trtpath+cell_time_str+'_'+infostr +
                    '_hist_alt_all_sources.csv')
                fname_hist = write_histogram(
                    bin_edges, np.zeros(bin_edges.size-1, dtype=int),
                    fname_hist, step=step)
                print('----- save to '+fname_hist)
                continue

            lat_cell = lat[inds]
            lon_cell = lon[inds]
            alt_cell = alt[inds]

            # Find flashes within cell contour
            inds, is_roi = belongs_roi_indices(
                lat_cell, lon_cell, cell_contour[i])
            if is_roi == 'None':
                warn('No flashes within cell contour')
                fname_hist = (
                    trtpath+cell_time_str+'_'+infostr +
                    '_hist_alt_all_sources.csv')
                fname_hist = write_histogram(
                    bin_edges, np.zeros(bin_edges.size-1, dtype=int),
                    fname_hist, step=step)
                print('----- save to '+fname_hist)
                continue
            elif is_roi == 'All':
                inds = inds[0]

            lat_cell = lat_cell[inds]
            lon_cell = lon_cell[inds]
            alt_cell = alt_cell[inds]

            # compute number of flashes
            nflashes[i] = lat_cell.size

            # Plot altitude histogram
            fname_hist = (
                trtpath+cell_time_str+'_'+infostr +
                '_hist_alt_all_sources.png')
            titl = (
                cell_time_str+' '+infostr +
                ' TRT cell. Flash all sources altitude')
            fname_hist = plot_histogram(
                bin_edges, alt_cell, [fname_hist], labelx='Altitude [m MSL]',
                titl=titl)
            print('Plotted '+' '.join(fname_hist))

            fname_hist = (
                trtpath+cell_time_str+'_'+infostr +
                '_hist_alt_all_sources.csv')
            hist, bin_edges = np.histogram(alt_cell, bins=bin_edges)
            fname_hist = write_histogram(
                bin_edges, hist, fname_hist, step=step)
            print('----- save to '+fname_hist)

            # plot position first source
            figfname = (
                trtpath+cell_time_str+'_'+infostr +
                '_all_sources_pos_max_height_on_top.png')
            titl = (
                cell_time_str+' '+infostr +
                ' all flash sources position. Highest on top')
            figfname = plot_pos(
                lat_cell, lon_cell, alt_cell, [figfname],
                sort_altitude='Highest_on_top',
                cb_label='Source height [m MSL]', titl=titl)
            print('Plotted '+' '.join(figfname))

        # plot time series lightning
        figfname = (
            trtpath+cell_time_str+'_'+infostr+'_N_all_sources.png')
        titl = infostr+' Number of sources in cell'
        figfname = plot_timeseries(
            yyyymmddHHMM, [nflashes], [figfname], labelx='Time [UTC]',
            labely='N flashes', labels=['N flashes'], title=titl,
            period=0, timeformat=None, colors=None, linestyles=None,
            markers=None, ymin=None, ymax=None, dpi=72)
        print('Plotted '+' '.join(figfname))

        # plot time series lightning density
        figfname = (
            trtpath+cell_time_str+'_'+infostr+'_dens_all_sources.png')
        titl = infostr+' Sources density'
        figfname = plot_timeseries(
            yyyymmddHHMM, [nflashes/area], [figfname], labelx='Time [UTC]',
            labely='Source dens [Sources/Km2]', labels=['Source density'],
            title=titl, period=0, timeformat=None, colors=None,
            linestyles=None, markers=None, ymin=None, ymax=None, dpi=72)
        print('Plotted '+' '.join(figfname))

        # plot time-hist_height
        flist = glob.glob(
            trtpath+'*_'+infostr+'_hist_alt_all_sources.csv')

        if not flist:
            warn('No histogram files found in '+trtpath +
                 ' for TRT cell '+infostr)
        else:
            tbin_edges, bin_edges, data_ma, _ = read_histogram_ts(
                flist, 'flash_altitude')

            vmax = np.max(data_ma)
            if vmax == 0.:
                warn('Unable to plot histogram. No valid data')
            else:
                data_ma[data_ma == 0.] = np.ma.masked
                fname_hist = (
                    trtpath+'/'+infostr+'_trt_HISTOGRAM_alt_all_source.png')
                titl = (
                    'TRT cell '+infostr+'\n'+'Altitude of all flash sources')
                _plot_time_range(
                    tbin_edges, bin_edges, data_ma, 'frequency_of_occurrence',
                    [fname_hist], titl=titl,
                    ylabel='Altitude [m MSL]',
                    vmin=0., vmax=np.max(data_ma), figsize=[10, 8], dpi=72)

                print("----- plot to '%s'" % fname_hist)

        # Append sources data
        sources_density_list = np.ma.append(sources_density_list, nflashes/area)
        nsources_list = np.ma.append(nsources_list, nflashes)

    fname = args.trtbase+'cell_LMA_flashes.csv'
    write_trt_cell_lightning(
        cell_ID_list, time_list, lon_list, lat_list, area_list,
        rank_flash_density_list, nflash_list, flash_density_list, fname)

    fname = args.trtbase+'cell_LMA_sources.csv'
    write_trt_cell_lightning(
        cell_ID_list, time_list, lon_list, lat_list, area_list,
        rank_flash_density_list, nsources_list, sources_density_list, fname)