@@ -250,23 +250,32 @@ def read_station_in(fpath):
250250 Returns
251251 -------
252252 Result : DataFrame
253- DataFrame with hierarchical index (id ,subloc) and columns x,y,z,name
253+ DataFrame with hierarchical index (station_id ,subloc) and columns x,y,z,name
254254
255255 """
256256
257+ rows = []
257258 with open (fpath , "r" ) as f :
258- request = f .readline ()
259- n_entry = f .readline ()
260- stations = pd .read_csv (
261- f ,
262- sep = r"\s+" ,
263- header = None ,
264- names = ["index" , "x" , "y" , "z" , "excl" , "id" , "subloc" , "name" ],
265- usecols = ["x" , "y" , "z" , "id" , "subloc" , "name" ],
266- index_col = ["id" , "subloc" ],
267- na_values = "-" ,
268- keep_default_na = True ,
269- )
259+ f .readline () # request line
260+ f .readline () # n_entry line
261+ for line in f :
262+ line = line .strip ()
263+ if not line :
264+ continue
265+ if "!" not in line :
266+ continue
267+ left , right = line .split ("!" , 1 )
268+ parts_left = left .split ()
269+ # parts_left: index, x, y, z
270+ x , y , z = float (parts_left [1 ]), float (parts_left [2 ]), float (parts_left [3 ])
271+ # right: station_id subloc name
272+ parts_right = right .strip ().split (None , 2 )
273+ station_id = parts_right [0 ]
274+ subloc = parts_right [1 ] if len (parts_right ) > 1 else "default"
275+ name = parts_right [2 ].strip ("'\" " ) if len (parts_right ) > 2 else ""
276+ rows .append ((station_id , subloc , x , y , z , name ))
277+ stations = pd .DataFrame (rows , columns = ["station_id" , "subloc" , "x" , "y" , "z" , "name" ])
278+ stations = stations .set_index (["station_id" , "subloc" ])
270279 return stations
271280
272281
@@ -303,7 +312,7 @@ def write_station_in(fpath, station_in, request=None):
303312 # Then the specific requests, here written to a string buffer
304313 buffer2 = dfmerged .to_csv (
305314 None ,
306- columns = ["x" , "y" , "z" , "excl" , "id " , "subloc" , "name" ],
315+ columns = ["x" , "y" , "z" , "excl" , "station_id " , "subloc" , "name" ],
307316 index_label = "id" ,
308317 sep = " " ,
309318 float_format = "%.2f" ,
@@ -342,8 +351,8 @@ def read_station_subloc(fpath):
342351 DataFrame with hierarchical index (id,subloc) and data column z
343352
344353 """
345-
346- df = pd .read_csv (fpath , sep = "," , header = 0 , index_col = ["id " , "subloc" ], comment = "#" )
354+
355+ df = pd .read_csv (fpath , sep = "," , header = 0 , index_col = ["station_id " , "subloc" ], comment = "#" )
347356 df ["z" ] = df .z
348357 return df [["z" ]]
349358
@@ -375,7 +384,7 @@ def read_station_dbase(fpath):
375384 db = pd .read_csv (
376385 fpath , sep = "," , comment = "#" , header = 0 , index_col = "station_id" , dtype = {"agency_id" : str }
377386 )
378- db .index .name = "id "
387+ db .index .name = "station_id "
379388
380389 db ["agency_id" ] = db ["agency_id" ].str .replace ("'" , "" , regex = True )
381390
@@ -405,12 +414,13 @@ def merge_station_subloc(station_dbase, station_subloc, default_z):
405414 DataFrame that links the information.
406415
407416 """
408-
417+ print (station_dbase .head ())
418+ print (station_subloc .head ())
409419 merged = station_dbase .reset_index ().merge (
410- station_subloc .reset_index (), left_on = "id " , right_on = "id " , how = "left"
420+ station_subloc .reset_index (), left_on = "station_id " , right_on = "station_id " , how = "left"
411421 )
412422 merged .fillna ({"subloc" : "default" , "z" : default_z }, inplace = True )
413- merged .set_index (["id " , "subloc" ], inplace = True )
423+ merged = merged .set_index (["station_id " , "subloc" ])
414424
415425 return merged
416426
@@ -834,8 +844,11 @@ def convert_db_station_in(
834844 sublocdb = None ,
835845 station_request = "all" ,
836846 default = - 0.5 ,
847+ mask_col = None ,
837848):
838849 stations_utm = read_station_dbase (stationdb )
850+ if mask_col is not None :
851+ stations_utm = stations_utm [stations_utm [mask_col ] == 1 ]
839852 ssubloc = read_station_subloc (sublocdb )
840853 stations_in = merge_station_subloc (stations_utm , ssubloc , default_z = - 0.5 )
841854 write_station_in (outfile , stations_in , request = station_request )
@@ -866,7 +879,12 @@ def convert_db_station_in(
866879 help = "z coordinate used when there is no listing for station id (z coordinate, not subloc from surface)" ,
867880)
868881@click .option ("--out" , default = "station.in" , help = "station.in formatted file" )
869- def convert_station_cli (station_db , subloc_db , request , default_zcor , out ):
882+ @click .option (
883+ "--mask-col" ,
884+ default = None ,
885+ help = "Integer column in station dbase used to select stations (1=include)" ,
886+ )
887+ def convert_station_cli (station_db , subloc_db , request , default_zcor , out , mask_col ):
870888 """Create station.in file from station database (stations_utm.csv) and station subloc listing station_subloc.csv"""
871889 stationdb = station_db
872890 sublocdb = subloc_db
@@ -877,7 +895,7 @@ def convert_station_cli(station_db, subloc_db, request, default_zcor, out):
877895 if sublocdb is None :
878896 sublocdb = config_file ("sublocations" )
879897
880- convert_db_station_in (outfile , stationdb , sublocdb , request , default )
898+ convert_db_station_in (outfile , stationdb , sublocdb , request , default , mask_col = mask_col )
881899
882900
883901if __name__ == "__main__" :
0 commit comments