import { HttpClient , HttpErrorResponse } from '@angular/common/http' ; import { Injectable } from '@angular/core' ; import { BehaviorSubject , Observable , of } from 'rxjs' ; import { catchError } from 'rxjs/operators' ; /**
 * Service for getting station lists, handling station json
 */ @ Injectable ( ) export class StationService { error : any = null ; stationsJson$ : BehaviorSubject < any > = new BehaviorSubject < any > ( null ) ; constructor ( private httpClient ) { } param product getStations ( product ) { if ( product === null || ! product . contents [ 'download/stationlist.json' ] ) { this . stationsJson$ . next ( null ) ; return ; } const stations = product . contents [ 'download/stationlist.json' ] ; this . httpClient . get ( stations . url ) . pipe ( catchError ( this . handleError ( ) ) ) . subscribe ( response => { try { this . onStations ( response ) ; } catch ( e ) { this . error = e ; this . stationsJson$ . next ( null ) ; } } ) ; } param stations onStations ( stations ) { if ( stations ) { stations = this . translate ( stations ) ; this . stationsJson$ . next ( stations ) ; } else { this . stationsJson$ . next ( null ) ; } } param stations returns translate ( stations ) { const newStations = [ ] ; for ( let station of stations . features ) { station = this . translateNan ( station ) ; station = this . translateAmps ( station ) ; newStations . push ( station ) ; } stations . features = newStations ; return stations ; } param station returns translateAmps ( station ) { const channels = station . properties . channels ; const translateNames = { 'sa(0.3)' : 'psa03' , 'sa(1.0)' : 'psa10' , 'sa(3.0)' : 'psa30' } ; station . channels = channels . map ( channel => { const parsed = { name : channel . name , pga : { } , pgv : { } , psa03 : { } , psa10 : { } , psa30 : { } } ; channel . amplitudes . forEach ( amp => { const name = translateNames [ amp . name ] || amp . name ; parsed [ name ] = amp ; } ) ; return parsed ; } ) ; return station ; } param station returns translateNan ( station ) { for ( const item of Object . keys ( station . properties ) ) { if ( station . properties [ item ] === 'null' || station . properties [ item ] === 'nan' ) { station . properties [ item ] = null ; } } return station ; } returns private handleError ( ) { return ( error ) : Observable < any > => { this . error = error ; return of ( null ) ; } ; } }