anchor.diff:  %% API Function Definitions
   %% ------------------------------------------------------------------
   
  -start_link(Token,StationIds,VarIds,TimeoutMins,Method) ->
  -  gen_server:start_link({local, ?SERVER}, ?MODULE, [Token,StationIds,VarIds,TimeoutMins,Method,[]], []).
  +start_link(Token,StationInfos,VarIds,TimeoutMins,Method) ->
  +  gen_server:start_link({local, ?SERVER}, ?MODULE, [Token,StationInfos,VarIds,TimeoutMins,calendar:local_time(),Method,[]], []).
   
   
   report_errors() ->
  
positive.code: -module(raws_ingest_server).
  -behaviour(gen_server).
  -define(SERVER, ?MODULE).
  
  %% ------------------------------------------------------------------
  %% API Function Exports
  %% ------------------------------------------------------------------
  
  -export([start_link/5]).
  -export([report_errors/0,clear_errors/0,update_now/0]).
  
  %% ------------------------------------------------------------------
  %% gen_server Function Exports
  
negative.code: %% Internal Function Definitions
  %% ------------------------------------------------------------------
  
  update_observations_now([Token,StationSel0,VarIds,_TimeoutMins,UpdateFrom,Method,_Errors]) ->
    TimeNow = calendar:universal_time(),
    StationSel = resolve_station_selector(StationSel0, VarIds, Token),
    case StationSel of
      {station_list, StationIds} ->
        {NewErrors,StationInfos,Obs} = safe_retrieve_observations(Method,Token,UpdateFrom,TimeNow,StationIds,VarIds),
        store_station_infos(StationInfos),
        store_observations(Obs),
        case NewErrors of
          [] ->
            % only mark database updated till TimeNow if there were no errors
            {[],TimeNow,StationSel};
          _NotEmpty ->
            % since there were errors, do not consider the time interval UpdateFrom to TimeNow as processed
            {NewErrors,UpdateFrom,StationSel}
        end;
      _ ->
        {[{error,StationSel,TimeNow,unresolved_stations,"Could not resolve stations from region."}],UpdateFrom,StationSel}
    end.
  
  
  -spec resolve_station_selector(station_selector(),[var_id()],string()) -> station_selector().
  resolve_station_selector({station_list, Lst}, _, _) ->
    {station_list, Lst};
  resolve_station_selector(Sel={region, {MinLat,MaxLat}, {MinLon,MaxLon}}, VarIds, Token) ->
    try
      StationInfos = mesowest_json_ingest:list_raws_in_bbox({MinLat,MaxLat},{MinLon,MaxLon},VarIds,Token),
      Ids = lists:map(fun (#raws_station{id=Id}) -> Id end, StationInfos),
      {station_list, Ids}
    catch Bdy:Exc ->
      error_logger:error_msg("Failed to resolve station selection ~p due to exception~n~p (bdy ~p)", [Sel,Bdy,Exc]),
      Sel
    end.
  
  -spec is_station_selector(any()) -> boolean().
  is_station_selector({station_list,Lst}) when is_list(Lst) ->
    true;
  is_station_selector({region,{MinLat,MaxLat},{MinLon,MaxLon}}) when is_number(MinLat) and is_number(MaxLat)
                                                                 and is_number(MinLon) and is_number(MaxLon) ->
    true;
  is_station_selector(_) ->
    false.
  
  
  store_station_infos(StInfos) ->
    mnesia:transaction(fun () -> lists:map(fun mnesia:write/1, StInfos) end).
  
  