|
33 | 33 | } |
34 | 34 | """ Excel variable names """ |
35 | 35 |
|
| 36 | +DEF_VAR_CSV = {'lat': 'X', |
| 37 | + 'lon': 'Y', |
| 38 | + 'region_id': 'iso_n3', |
| 39 | + } |
| 40 | +""" CSV variable names """ |
| 41 | + |
36 | 42 | LOGGER = logging.getLogger(__name__) |
37 | 43 |
|
38 | 44 | def read_excel(centroids, file_name, var_names): |
@@ -111,6 +117,31 @@ def read_att_mat(centroids, cent, num_try, var_names): |
111 | 117 | except KeyError: |
112 | 118 | pass |
113 | 119 |
|
| 120 | +def read_csv(centroids, file_name, var_names): |
| 121 | + """ Read csv centroids representations. Currently only supports lat/lon |
| 122 | + and region_id. |
| 123 | + """ |
| 124 | + # TODO iterate over additional variables in var_names |
| 125 | + if var_names is None: |
| 126 | + var_names = DEF_VAR_CSV |
| 127 | + |
| 128 | + cent_pd = pd.read_csv(file_name) |
| 129 | + |
| 130 | + centroids.id = np.array(cent_pd.index) |
| 131 | + centroids.coord = GridPoints( |
| 132 | + np.array(cent_pd[[ |
| 133 | + var_names['lat'], |
| 134 | + var_names['lon'], |
| 135 | + ]]) |
| 136 | + ) |
| 137 | + centroids.region_id = np.array( |
| 138 | + cent_pd[[var_names['region_id']]] |
| 139 | + ) |
| 140 | + |
| 141 | + centroids.tag.file_name = file_name |
| 142 | + centroids.tag.description = 'Read from csv' |
| 143 | + |
114 | 144 | READ_SET = {'XLS': (DEF_VAR_EXCEL, read_excel), |
115 | | - 'MAT': (DEF_VAR_MAT, read_mat) |
116 | | - } |
| 145 | + 'MAT': (DEF_VAR_MAT, read_mat), |
| 146 | + 'CSV': (DEF_VAR_CSV, read_csv), |
| 147 | + } |
0 commit comments