@@ -263,7 +263,6 @@ def des_download(stations, dest_dir, start, end=None, param=None, overwrite=Fals
263263 etime = pd .Timestamp .now ().strftime ("%Y-%m-%d" )
264264 found = False
265265
266-
267266 try :
268267 tst_id = int (agency_id )
269268 except :
@@ -272,49 +271,67 @@ def des_download(stations, dest_dir, start, end=None, param=None, overwrite=Fals
272271 )
273272 failures .append ((station , param ))
274273 continue
275-
276-
277- rids = inventory_full .loc [(inventory_full ['station_id' ] == tst_id ) &
278- (inventory_full ['interval_name' ]!= 'Visit' ) &
279- (inventory_full ['analyte_name' ] == param ), ['result_id' ,'station_id' ,'station_name' ,
280- 'analyte_name' ,'program_id' ,
281- 'probe_depth' ,'unit_name' ,'equipment_name' ,
282- 'aggregate_name' ,'interval_name' ,'cdec_code' ,
283- 'start_date' ,'end_date' ] ] #
274+
275+ rids = inventory_full .loc [
276+ (inventory_full ["station_id" ] == tst_id )
277+ & (inventory_full ["interval_name" ] != "Visit" )
278+ & (inventory_full ["analyte_name" ] == param ),
279+ [
280+ "result_id" ,
281+ "station_id" ,
282+ "station_name" ,
283+ "analyte_name" ,
284+ "program_id" ,
285+ "probe_depth" ,
286+ "unit_name" ,
287+ "equipment_name" ,
288+ "aggregate_name" ,
289+ "interval_name" ,
290+ "cdec_code" ,
291+ "start_date" ,
292+ "end_date" ,
293+ ],
294+ ] #
284295 # This is a workaround at a time when DISE data is due to shift back ends and I (Eli) didn't want to do a ton
285296 # of work that would be superceded. Apparently the DISE backend station_id is not unique across EMP and Suisun
286297 # Marsh programs. This makes it kind of useless, but we will deal with that later.
287- cross_program_redundant_ids = [10 ,21 ,22 ,40 ,110 ,120 ]
298+ cross_program_redundant_ids = [10 , 21 , 22 , 40 , 110 , 120 ]
288299 if tst_id in cross_program_redundant_ids :
289300 # Think the CDEC code works for these cases
290301 row_station_id = row .station_id .upper ()
291- rids = rids .loc [rids [' cdec_code' ].str [0 :3 ] == row_station_id [0 :3 ]]
302+ rids = rids .loc [rids [" cdec_code" ].str [0 :3 ] == row_station_id [0 :3 ]]
292303
293- # Add subloc column that is translated to "air", "upper" or "lower".
304+ # Add subloc column that is translated to "air", "upper" or "lower".
294305 # If there is only a single subloc (depth=1) it is marked "default"
295- rids ["subloc" ] = rids ["probe_depth" ].apply (_depth_trans ) #"default" # dummy for transform
296- rids ["nrep" ] = rids .groupby (['station_id' ,'analyte_name' ])["subloc" ].transform ('count' )
297- rids ["nrepdepth" ] = rids .groupby (['station_id' ,'analyte_name' ,'subloc' ])["subloc" ].transform ('count' )
306+ rids ["subloc" ] = rids ["probe_depth" ].apply (
307+ _depth_trans
308+ ) # "default" # dummy for transform
309+ rids ["nrep" ] = rids .groupby (["station_id" , "analyte_name" ])["subloc" ].transform (
310+ "count"
311+ )
312+ rids ["nrepdepth" ] = rids .groupby (["station_id" , "analyte_name" , "subloc" ])[
313+ "subloc"
314+ ].transform ("count" )
298315
299-
300- if subloc in ('all' , 'default' ):
316+ if subloc in ("all" , "default" ):
301317 # Assume default requests makes sense if all the rid entries are the same
302318 # otherwise we need a direct hit
303- # todo: a better way to handle this would be to
319+ # todo: a better way to handle this would be to
304320 # link the subloc table and look and see if the station
305- # has sublocations defined and only use 'default' if it isn't defined.
321+ # has sublocations defined and only use 'default' if it isn't defined.
306322 # Here we hope the client application has done this.
307323 rids .loc [rids .nrep == rids .nrepdepth , "subloc" ] = "default"
308324 # if not is_unique(rids.subloc):
309325 # raise ValueError(f"Default location requested for a station with multiple sublocations for variable {param}")
310- elif subloc != ' all' :
311- rids = rids .loc [rids .subloc == subloc ,:]
326+ elif subloc != " all" :
327+ rids = rids .loc [rids .subloc == subloc , :]
312328
313-
314- if len (rids ) == 0 :
315- logger .debug (f"No Data for station { station } and param { paramname } , agency station id { agency_id } " )
316- failures .append ((station ,paramname ))
317- continue # next request
329+ if len (rids ) == 0 :
330+ logger .debug (
331+ f"No Data for station { station } and param { paramname } , agency station id { agency_id } "
332+ )
333+ failures .append ((station , paramname ))
334+ continue # next request
318335
319336 for ndx , rid in rids .iterrows ():
320337 rid_code = rid .result_id
@@ -356,14 +373,14 @@ def des_download(stations, dest_dir, start, end=None, param=None, overwrite=Fals
356373 if pd .isnull (fend ):
357374 yearname = f"{ fstart .year } _9999"
358375 else :
359- yearname = f"{ fstart .year } _{ fend .year } "
376+ yearname = f"{ fstart .year } _{ fend .year } "
360377
361378 sub = rid .subloc
362379
363380 if sub == "default" : # omit from name
364381 outfname = f"des_{ station } _{ agency_id } _{ paramname } _{ yearname } .csv"
365382 else :
366- outfname = f"des_{ station } @{ sub } _{ agency_id } _{ paramname } _{ yearname } .csv"
383+ outfname = f"des_{ station } @{ sub } _{ agency_id } _{ paramname } _{ yearname } .csv"
367384 outfname = outfname .lower ()
368385 outfname = outfname .lower ()
369386 path = os .path .join (dest_dir , outfname )
@@ -426,13 +443,18 @@ def download_des(dest_dir, start, end, param, stations, overwrite, stationfile):
426443
427444 stationfile = stationfile_or_stations (stationfile , stations )
428445 slookup = dstore_config .config_file ("station_dbase" )
429- vlookup = dstore_config .config_file ("variable_mappings" )
430- df = process_station_list (stationfile ,param = param ,station_lookup = slookup ,
431- agency_id_col = "agency_id" ,
432- param_lookup = vlookup ,
433- source = 'dwr_des' ,
434- subloc = 'all' )
435- des_download (df ,destdir ,stime ,etime ,overwrite = overwrite )
446+ vlookup = dstore_config .config_file ("variable_mappings" )
447+ df = process_station_list (
448+ stationfile ,
449+ param = param ,
450+ station_lookup = slookup ,
451+ agency_id_col = "agency_id" ,
452+ param_lookup = vlookup ,
453+ source = "dwr_des" ,
454+ subloc = "all" ,
455+ )
456+ des_download (df , destdir , stime , etime , overwrite = overwrite )
457+
436458
437459@click .command ()
438460@click .option (
0 commit comments