Пример #1
0
 def test_write_profiles_edges(self):
     """
     Test writing edges
     """
     #
     # read data and compute distances
     sps, dmin, dmax = read_profiles_csv(CS_DATA_PATH)
     lengths, longest_key, shortest_key = get_profiles_length(sps)
     maximum_sampling_distance = 15
     num_sampl = np.ceil(lengths[longest_key] / maximum_sampling_distance)
     ssps = get_interpolated_profiles(sps, lengths, num_sampl)
     write_profiles_csv(ssps, self.test_dir)
     write_edges_csv(ssps, self.test_dir)
     #
     #
     tmp = []
     for fname in glob.glob(os.path.join(self.test_dir, 'cs*')):
         tmp.append(fname)
     self.assertEqual(len(tmp), 2)
     #
     #
     tmp = []
     for fname in glob.glob(os.path.join(self.test_dir, 'edge*')):
         tmp.append(fname)
     self.assertEqual(len(tmp), 7)
Пример #2
0
    def test_interpolation_cam(self):
        """
        Test profile interpolation: CAM | maximum sampling: 30 km
        """
        #
        # read data and compute distances
        sps, dmin, dmax = read_profiles_csv(CAM_DATA_PATH)
        lengths, longest_key, shortest_key = get_profiles_length(sps)
        maximum_sampling_distance = 30.
        num_sampl = np.ceil(lengths[longest_key] / maximum_sampling_distance)
        #
        # get interpolated profiles
        ssps = get_interpolated_profiles(sps, lengths, num_sampl)
        lll = []
        for key in sorted(ssps.keys()):
            odat = sps[key]
            dat = ssps[key]

            distances = distance(dat[0:-2, 0], dat[0:-2, 1], dat[0:-2, 2],
                                 dat[1:-1, 0], dat[1:-1, 1], dat[1:-1, 2])
            expected = lengths[key] / num_sampl * np.ones_like(distances)
            np.testing.assert_allclose(distances, expected, rtol=3)
            #
            # update the list with the number of points in each profile
            lll.append(len(dat[:, 0]))
            #
            # check that the interpolated profile starts from the same point
            # of the original one
            self.assertListEqual([odat[0, 0], odat[0, 1]],
                                 [dat[0, 0], dat[0, 1]])
            #
            # check that the depth of the profiles is always increasing
            computed = np.all(np.sign(dat[:-1, 2]-dat[1:, 2]) < 0)
            self.assertTrue(computed)
        #
        # check that all the profiles have all the same length
        dff = np.diff(np.array(lll))
        zeros = np.zeros_like(dff)
        np.testing.assert_allclose(dff, zeros, rtol=2)
def build_complex_surface(in_path,
                          max_sampl_dist,
                          out_path,
                          upper_depth=0,
                          lower_depth=1000,
                          from_id='.*',
                          to_id='.*'):
    """
    :param str in_path:
        Folder name. It contains files with the prefix 'cs_'
    :param str float max_sampl_dist:
        Sampling distance [km]
    :param str out_path:
        Folder name
    :param float upper_depth:
        The depth above which we cut the profiles
    :param float lower_depth:
        The depth below which we cut the profiles
    :param str from_id:
        The ID of the first profile to be considered
    :param str to_id:
        The ID of the last profile to be considered
    """

    # Check input and output folders
    if in_path == out_path:
        tmps = '\nError: the input folder cannot be also the output one\n'
        tmps += '    input : {0:s}\n'.format(in_path)
        tmps += '    output: {0:s}\n'.format(out_path)
        sys.exit()

    # Read the profiles
    sps, dmin, dmax = read_profiles_csv(in_path, float(upper_depth),
                                        float(lower_depth), from_id, to_id)

    # Check
    logging.info('Number of profiles: {:d}'.format(len(sps)))
    if len(sps) < 1:
        fmt = 'Did not find cross-sections in {:s}\n exiting'
        msg = fmt.format(os.path.abspath(in_path))
        logging.error(msg)
        sys.exit(0)

    # Compute length of profiles
    lengths, longest_key, shortest_key = get_profiles_length(sps)
    logging.info('Longest profile (id: {:s}): {:2f}'.format(
        longest_key, lengths[longest_key]))
    logging.info('Shortest profile (id: {:s}): {:2f}'.format(
        shortest_key, lengths[shortest_key]))
    logging.info('Depth min: {:.2f}'.format(dmin))
    logging.info('Depth max: {:.2f}'.format(dmax))

    # Info
    number_of_samples = numpy.ceil(lengths[longest_key] /
                                   float(max_sampl_dist))
    tmps = 'Number of subsegments for each profile: {:d}'
    logging.info(tmps.format(int(number_of_samples)))
    tmp = lengths[shortest_key] / number_of_samples
    logging.info('Shortest sampling [%s]: %.4f' % (shortest_key, tmp))
    tmp = lengths[longest_key] / number_of_samples
    logging.info('Longest sampling  [%s]: %.4f' % (longest_key, tmp))

    # Resampled profiles
    rsps = get_interpolated_profiles(sps, lengths, number_of_samples)

    # Store new profiles
    write_profiles_csv(rsps, out_path)

    # Store computed edges
    write_edges_csv(rsps, out_path)