Beispiel #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)
Beispiel #2
0
 def read_profiles_02(self):
     """
     Read CAM profiles
     """
     sps, dmin, dmax = read_profiles_csv(CAM_DATA_PATH, upper_depth=0,
                                         lower_depth=1000, from_id="13",
                                         to_id="32")
Beispiel #3
0
 def test_read_profiles_03(self):
     """
     Read CAM profiles
     """
     lower_depth = 30
     upper_depth = 20
     sps, dmin, dmax = read_profiles_csv(CAM_DATA_PATH, upper_depth,
                                         lower_depth, from_id="13",
                                         to_id="32")
     assert dmin >= upper_depth
     assert dmax <= lower_depth
Beispiel #4
0
 def test_length_calc_01(self):
     """
     Test computing the lenght of profiles
     """
     # read data and compute distances
     sps, dmin, dmax = read_profiles_csv(CS_DATA_PATH)
     lengths, longest_key, shortest_key = get_profiles_length(sps)
     # check shortest and longest profiles
     assert longest_key == '003'
     assert shortest_key == '004'
     # check lenghts
     expected = np.array([103.454865, 101.369319])
     computed = np.array([lengths[key] for key in sorted(sps.keys())])
     np.testing.assert_allclose(computed, expected, rtol=2)
Beispiel #5
0
 def test_build_01(self):
     """
     """
     upper_depth = 0
     lower_depth = 1000
     from_id = '.*'
     to_id = '.*'
     #
     # read profiles
     sps, odmin, odmax = read_profiles_csv(self.in_path, upper_depth,
                                           lower_depth, from_id, to_id)
     #
     # build the complex surface
     build_complex_surface(self.in_path, self.max_sampl_dist, self.out_path,
                           upper_depth, lower_depth, from_id, to_id)
     #
     # read the output profiles
     sps, edmin, edmax = read_profiles_csv(self.out_path, upper_depth,
                                           lower_depth, from_id, to_id)
     #
     #
     self.assertEqual(odmin, edmin)
     assert lower_depth >= edmax
     assert upper_depth <= edmin
Beispiel #6
0
 def test_read_profiles_01(self):
     """
     Test reading a profile file
     """
     sps, dmin, dmax = read_profiles_csv(CS_DATA_PATH)
     # check the minimum and maximum depths computed
     assert dmin == 0
     assert dmax == 40.0
     expected_keys = ['003', '004']
     # check the keys
     self.assertListEqual(expected_keys, sorted(list(sps.keys())))
     # check the coordinates of the profile
     expected = np.array([[10., 45., 0.],
                          [10.2, 45.2, 10.],
                          [10.3, 45.3, 15.],
                          [10.5, 45.5, 25.],
                          [10.7, 45.7, 40.]])
     np.testing.assert_allclose(sps['003'], expected, rtol=2)
Beispiel #7
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)