<commit_before>//
//  WeatherLayer.swift
//  CAVOK
//
//  Created by <NAME> on 08.03.16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation

class WeatherLayer {

    private let mapView: WhirlyGlobeViewController
    
    private let presentation: ObservationPresentation
    
    private var config: WeatherConfig?
    
    var loader: MaplyQuadImageFrameLoader? = nil
    
    var fetcher: WeatherTileFetcher? = nil
    
    private var frameChanger: FrameChanger? = nil
    
    init(mapView: WhirlyGlobeViewController, presentation: ObservationPresentation, region: WeatherRegion?) {
        self.mapView = mapView
        self.presentation = presentation
        
        if let region = region {
            reposition(region: region)
        }
    
    }
    
    deinit {
        clean()
    }
    
    func reposition(region: WeatherRegion) {
        config = WeatherConfig(region: region)
    }
    
    func load(groups: ObservationGroups, at coordinate: MaplyCoordinate?, loaded: @escaping (Int, UIColor) -> Void) {
        guard let selected = groups.selectedFrame else {
            return
        }
        
        clean()
        
        // generate heatmaps in inverse order
        let frames = groups.frames.enumerated().map { index, obs in
            return HeatMap(index: index, observations: obs, config: config!, presentation: self.presentation)
        }
        
        frames.reversed().forEach { frame in
            let selected = frame.index == selected
            frame.process(priority: selected).done {
                if let coordinate = coordinate {
                    loaded(frame.index, frame.color(for: coordinate))
                }
            }.catch { error in
                print("Failed to generate heatmap \(frame.index) because of \(error)")
            }
        }
        
        
        
        DispatchQueue.main.async {
            self.loader = self.initLoader(frames: frames)
        }
    }
    
    func go(frame: Int) -> [Observation] {
        if let frameChanger = self.frameChanger {
            frameChanger.go(frame)
        }
        
        if let fetcher = fetcher, frame < fetcher.frames.count {
            let tileSource = fetcher.frames[frame]
            return tileSource.observations
        } else {
            return []
        }
    }
    
    func clean() {<commit_msg>Fix crash when reloading observations.<commit_after>//
//  WeatherLayer.swift
//  CAVOK
//
//  Created by <NAME> on 08.03.16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation

class WeatherLayer {

    private let mapView: WhirlyGlobeViewController
    
    private let presentation: ObservationPresentation
    
    private var config: WeatherConfig?
    
    var loader: MaplyQuadImageFrameLoader? = nil
    
    var fetcher: WeatherTileFetcher? = nil
    
    private var frameChanger: FrameChanger? = nil
    
    init(mapView: WhirlyGlobeViewController, presentation: ObservationPresentation, region: WeatherRegion?) {
        self.mapView = mapView
        self.presentation = presentation
        
        if let region = region {
            reposition(region: region)
        }
    
    }
    
    deinit {
        clean()
    }
    
    func reposition(region: WeatherRegion) {
        config = WeatherConfig(region: region)
    }
    
    func load(groups: ObservationGroups, at coordinate: MaplyCoordinate?, loaded: @escaping (Int, UIColor) -> Void) {
        guard let selected = groups.selectedFrame else {
            return
        }
        
        clean()
        
        // generate heatmaps in inverse order
        let frames = groups.frames.enumerated().map { index, obs in
            return HeatMap(index: index, observations: obs, config: config!, presentation: self.presentation)
        }
        
        frames.reversed().forEach { frame in
            let selected = frame.index == selected
            frame.process(priority: selected).done {
                if let coordinate = coordinate {
                    loaded(frame.index, frame.color(for: coordinate))
                }
            }.catch { error in
                print("Failed to generate heatmap \(frame.index) because of \(error)")
            }
        }
        
        
        
        DispatchQueue.main.async {
            self.loader = self.initLoader(frames: frames)
        }
    }
    
    func go(frame: Int) -> [Observation] {
        if let frameChanger = self.frameChanger {
            frameChanger.go(frame)
        }
        
        if let fetcher = fetcher, frame < fetcher.frames.count {
            let tileSource = fetcher.frames[frame]
            // when reloading data the observations might get deleted
            return tileSource.observations.filter({!$0.isInvalidated})
        } else {
            return []
        }
    }
    
    func clean() {
{'fraction_non_alphanumeric': 0.08669809387169503, 'fraction_numerical': 0.00430416068866571, 'mean_word_length': 1.7901658090337336, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6068716', 'n_tokens_mistral': 1329, 'n_tokens_neox': 1272, 'n_words': 334}