@@ -115,7 +115,15 @@ def from_dict(cls, data: typing.Dict) -> 'ESMCatalogModel':
115
115
cat ._df = df
116
116
return cat
117
117
118
- def save (self , name : str , * , directory : str = None , catalog_type : str = 'dict' ) -> None :
118
+ def save (
119
+ self ,
120
+ name : str ,
121
+ * ,
122
+ directory : str = None ,
123
+ catalog_type : str = 'dict' ,
124
+ to_csv_kwargs : dict = None ,
125
+ json_dump_kwargs : dict = None ,
126
+ ) -> None :
119
127
"""
120
128
Save the catalog to a file.
121
129
@@ -128,6 +136,10 @@ def save(self, name: str, *, directory: str = None, catalog_type: str = 'dict')
128
136
catalog_type: str
129
137
The type of catalog to save. Whether to save the catalog table as a dictionary
130
138
in the JSON file or as a separate CSV file. Valid options are 'dict' and 'file'.
139
+ to_csv_kwargs : dict, optional
140
+ Additional keyword arguments passed through to the :py:meth:`~pandas.DataFrame.to_csv` method.
141
+ json_dump_kwargs : dict, optional
142
+ Additional keyword arguments passed through to the :py:func:`~json.dump` function.
131
143
132
144
Notes
133
145
-----
@@ -140,7 +152,7 @@ def save(self, name: str, *, directory: str = None, catalog_type: str = 'dict')
140
152
raise ValueError (
141
153
f'catalog_type must be either "dict" or "file". Received catalog_type={ catalog_type } '
142
154
)
143
- csv_file_name = pathlib .Path (f'{ name } .csv.gz ' )
155
+ csv_file_name = pathlib .Path (f'{ name } .csv' )
144
156
json_file_name = pathlib .Path (f'{ name } .json' )
145
157
if directory :
146
158
directory = pathlib .Path (directory )
@@ -154,13 +166,20 @@ def save(self, name: str, *, directory: str = None, catalog_type: str = 'dict')
154
166
data ['id' ] = name
155
167
156
168
if catalog_type == 'file' :
169
+ csv_kwargs = {'index' : False }
170
+ csv_kwargs .update (to_csv_kwargs or {})
171
+ compression = csv_kwargs .get ('compression' )
172
+ extensions = {'gzip' : '.gz' , 'bz2' : '.bz2' , 'zip' : '.zip' , 'xz' : '.xz' , None : '' }
173
+ csv_file_name = f'{ csv_file_name } { extensions [compression ]} '
157
174
data ['catalog_file' ] = str (csv_file_name )
158
- self .df .to_csv (csv_file_name , compression = 'gzip' , index = False )
175
+ self .df .to_csv (csv_file_name , ** csv_kwargs )
159
176
else :
160
177
data ['catalog_dict' ] = self .df .to_dict (orient = 'records' )
161
178
162
179
with open (json_file_name , 'w' ) as outfile :
163
- json .dump (data , outfile , indent = 2 )
180
+ json_kwargs = {'indent' : 2 }
181
+ json_kwargs .update (json_dump_kwargs or {})
182
+ json .dump (data , outfile , ** json_kwargs )
164
183
165
184
print (f'Successfully wrote ESM collection json file to: { json_file_name } ' )
166
185
0 commit comments