Skip to content

Commit b279b56

Browse files
committed
implement a simple csv centroids reader
1 parent 7e01145 commit b279b56

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

climada/hazard/centroids/base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121

2222
FILE_EXT = {'.mat': 'MAT',
2323
'.xls': 'XLS',
24-
'.xlsx': 'XLS'
25-
}
24+
'.xlsx': 'XLS',
25+
'.csv': 'CSV',
26+
}
27+
2628
""" Supported files format to read from """
2729

2830
class Centroids(object):

climada/hazard/centroids/source.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
}
3434
""" Excel variable names """
3535

36+
DEF_VAR_CSV = {'lat': 'X',
37+
'lon': 'Y',
38+
'region_id': 'iso_n3',
39+
}
40+
""" CSV variable names """
41+
3642
LOGGER = logging.getLogger(__name__)
3743

3844
def read_excel(centroids, file_name, var_names):
@@ -111,6 +117,31 @@ def read_att_mat(centroids, cent, num_try, var_names):
111117
except KeyError:
112118
pass
113119

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+
114144
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

Comments
 (0)