|
2 | 2 | Test the functions that put vector data into GMT.
|
3 | 3 | """
|
4 | 4 | import itertools
|
| 5 | +from datetime import datetime |
5 | 6 |
|
6 | 7 | import numpy as np
|
7 | 8 | import numpy.testing as npt
|
@@ -90,6 +91,75 @@ def test_put_vector_mixed_dtypes():
|
90 | 91 | npt.assert_allclose(newy, y)
|
91 | 92 |
|
92 | 93 |
|
| 94 | +def test_put_vector_string_dtype(): |
| 95 | + """ |
| 96 | + Passing string type vectors to a dataset. |
| 97 | + """ |
| 98 | + # input string vectors: numbers, longitudes, latitudes, and datetimes |
| 99 | + vectors = np.array( |
| 100 | + [ |
| 101 | + ["10", "20.0", "-30.0", "3.5e1"], |
| 102 | + ["10W", "30.50E", "30:30W", "40:30:30.500E"], |
| 103 | + ["10N", "30.50S", "30:30N", "40:30:30.500S"], |
| 104 | + ["2021-02-03", "2021-02-03T04", "2021-02-03T04:05:06.700", "T04:50:06.700"], |
| 105 | + ] |
| 106 | + ) |
| 107 | + # output vectors in double or string type |
| 108 | + # Notes: |
| 109 | + # 1. longitudes and latitudes are stored in double in GMT |
| 110 | + # 2. The default output format for datetime is YYYY-mm-ddTHH:MM:SS |
| 111 | + expected_vectors = [ |
| 112 | + [10.0, 20.0, -30.0, 35], |
| 113 | + [-10, 30.5, -30.5, 40.508472], |
| 114 | + [10, -30.50, 30.5, -40.508472], |
| 115 | + [ |
| 116 | + "2021-02-03T00:00:00", |
| 117 | + "2021-02-03T04:00:00", |
| 118 | + "2021-02-03T04:05:06", |
| 119 | + f"{datetime.utcnow().strftime('%Y-%m-%d')}T04:50:06", |
| 120 | + ], |
| 121 | + ] |
| 122 | + |
| 123 | + # loop over all possible combinations of input types |
| 124 | + for i, j in itertools.combinations_with_replacement(range(4), r=2): |
| 125 | + with clib.Session() as lib: |
| 126 | + dataset = lib.create_data( |
| 127 | + family="GMT_IS_DATASET|GMT_VIA_VECTOR", |
| 128 | + geometry="GMT_IS_POINT", |
| 129 | + mode="GMT_CONTAINER_ONLY", |
| 130 | + dim=[2, 4, 1, 0], # columns, rows, layers, dtype |
| 131 | + ) |
| 132 | + lib.put_vector(dataset, column=lib["GMT_X"], vector=vectors[i]) |
| 133 | + lib.put_vector(dataset, column=lib["GMT_Y"], vector=vectors[j]) |
| 134 | + # Turns out wesn doesn't matter for Datasets |
| 135 | + wesn = [0] * 6 |
| 136 | + # Save the data to a file to see if it's being accessed correctly |
| 137 | + with GMTTempFile() as tmp_file: |
| 138 | + lib.write_data( |
| 139 | + "GMT_IS_VECTOR", |
| 140 | + "GMT_IS_POINT", |
| 141 | + "GMT_WRITE_SET", |
| 142 | + wesn, |
| 143 | + tmp_file.name, |
| 144 | + dataset, |
| 145 | + ) |
| 146 | + # Load the data |
| 147 | + output = np.genfromtxt( |
| 148 | + tmp_file.name, dtype=None, names=("x", "y"), encoding=None |
| 149 | + ) |
| 150 | + # check that the output is correct |
| 151 | + # Use npt.assert_allclose for numeric arrays |
| 152 | + # and npt.assert_array_equal for string arrays |
| 153 | + if i != 3: |
| 154 | + npt.assert_allclose(output["x"], expected_vectors[i]) |
| 155 | + else: |
| 156 | + npt.assert_array_equal(output["x"], expected_vectors[i]) |
| 157 | + if j != 3: |
| 158 | + npt.assert_allclose(output["y"], expected_vectors[j]) |
| 159 | + else: |
| 160 | + npt.assert_array_equal(output["y"], expected_vectors[j]) |
| 161 | + |
| 162 | + |
93 | 163 | def test_put_vector_invalid_dtype():
|
94 | 164 | """
|
95 | 165 | Check that it fails with an exception for invalid data types.
|
|
0 commit comments