src/MetadataParser.php
/**
     * Parse metadata from CSV file
     * 
     * Legacy: Both parameter and location metadata were stored in VQHA80/98 files.
     * 
     * As of 2021 only parameter metadata is stored in text files. Location metadata is stored
     * in CSV files.
     * 
     * @param string $raw SwissMetNet metadata string from CSV file
     * @return stdClass $metadata[array locations, array parameters]
     * @throws UnexpectedValueException
     */
Parse metadata from CSV file

Both parameter and location metadata were stored in VQHA80/98 files.

As of 2021 only parameter metadata is stored in text files. Location metadata is stored
in CSV files.

@param string $raw SwissMetNet metadata string from CSV file
@return stdClass $metadata[array locations, array parameters]
@throws UnexpectedValueException
Parse metadata from CSV file Both parameter and location metadata were stored in VQHA80 / 98 files . As of 2021 only parameter metadata is stored in text files . Location metadata is stored in CSV files . @param string $raw SwissMetNet metadata string from CSV file @return stdClass $metadata [ array locations array parameters ] @throws UnexpectedValueException
public static function parseFromCsvFile(string $raw)
    {
        $separator = ';';
        $metadata = new \stdClass();
        $metadata->locations = [];
        $metadata->parameters = [];
        if (substr($raw, 0, 10) != '"Station";' && substr($raw, 0, 11) != '"Stazione";') {
            return $metadata;
        }
        $lines = explode("\n", $raw);
        $headers = str_getcsv(array_shift($lines), $separator);
        foreach ($lines as $line) {
            if (empty(trim($line))) {
                break;
            }
            $fields = str_getcsv($line, $separator);
            $location = [];
            foreach ($fields as $index => $value) {
                switch (utf8_encode($headers[$index])) {
                    default:
                        throw new \UnexpectedValueException('Unknown header column: ' . utf8_encode($headers[$index]));
                        break;
                    case "Station":
                    case "Stazione":
                        $location['name'] = utf8_encode($value);
                        break;
                    case "Abbr.":
                    case "Abk.":
                    case "Abr.":
                        $location['id'] = $value;
                        break;
                    case "WIGOS-ID":
                        $location['wigos-id'] = $value ? $value : null;
                        break;
                    case "Station type":
                    case "Stationstyp":
                    case "Type de station":
                    case "Tipo di stazione":
                        $location['station-type'] = $value;
                        break;
                    case "Data Owner":
                    case "Eigentümer":
                    case "Propriétaire ":
                    case "Proprietario ":
                        $location['data-owner'] = $value;
                        break;
                    case "Data since":
                    case "Daten seit":
                    case "Données depuis":
                    case "Dati dal":
                        $location['data-since'] = $value;
                        break;
                    case "Station height m. a. sea level":
                    case "Stationshöhe m. ü. M.":
                    case "Altitude station m. s. mer":
                    case "Altitudine stazione m slm":
                        $location['alt'] = intval($value);
                        break;
                    case "Barometric altitude m. a. ground":
                    case "Barometerhöhe m. ü. Boden":
                    case "Altitude du baromètre m. s. sol":
                    case "Altitudine del barometro m da terra":
                        $location['alt-barometric'] = $value ? intval($value) : null;
                        break;
                    case "KoordinatenE":
                    case "CoordinatesE":
                    case "CoordonnéesE":
                    case "CoordinateE":
                        $location['chx'] = intval($value);
                        break;
                    case "CoordinatesN":
                    case "KoordinatenN":
                    case "CoordonnéesN":
                    case "CoordinateN":
                        $location['chy'] = intval($value);
                        break;
                    case "Latitude":
                    case "Breitengrad":
                    case "Latitudine":
                        $location['lat'] = floatval($value);
                        break;
                    case "Longitude":
                    case "Längengrad":
                    case "Longitudine ":
                        $location['lon'] = floatval($value);
                        break;
                    case "Canton":
                    case "Kanton":
                    case "Cantone":
                        $location['canton'] = $value;
                        break;
                    case "Measurements":
                    case "Messungen":
                    case "Mesures":
                    case "Misurazioni":
                        $location['measurements'] = $value;
                        break;
                    case "Link":
                    case "Lien":
                    case "Collegamento":
                        $location['link'] = $value;
                        break;
                }
            }
            $metadata->locations[] = (object) $location;
        }
        return $metadata;
    }
public static function parseFromCsvFile ( string $ raw ) { $ separator = ';' ; $ metadata = new \ stdClass ( ) ; $ metadata -> locations = [ ] ; $ metadata -> parameters = [ ] ; if ( substr ( $ raw , 0 , 10 ) != '"Station";' && substr ( $ raw , 0 , 11 ) != '"Stazione";' ) { return $ metadata ; } $ lines = explode ( " \n " , $ raw ) ; $ headers = str_getcsv ( array_shift ( $ lines ) , $ separator ) ; foreach ( $ lines as $ line ) { if ( empty ( trim ( $ line ) ) ) { break ; } $ fields = str_getcsv ( $ line , $ separator ) ; $ location = [ ] ; foreach ( $ fields as $ index => $ value ) { switch ( utf8_encode ( $ headers [ $ index ] ) ) { default : throw new \ UnexpectedValueException ( 'Unknown header column: ' . utf8_encode ( $ headers [ $ index ] ) ) ; break ; case " Station " : case " Stazione " : $ location [ 'name' ] = utf8_encode ( $ value ) ; break ; case " Abbr. " : case " Abk. " : case " Abr. " : $ location [ 'id' ] = $ value ; break ; case " WIGOS-ID " : $ location [ 'wigos-id' ] = $ value ? $ value : null ; break ; case " Station type " : case " Stationstyp " : case " Type de station " : case " Tipo di stazione " : $ location [ 'station-type' ] = $ value ; break ; case " Data Owner " : case " Eigentümer" : case " Propriétaire " : case " Proprietario  " : $ location [ 'data-owner' ] = $ value ; break ; case " Data since " : case " Daten seit " : case " Données depuis" : case " Dati dal " : $ location [ 'data-since' ] = $ value ; break ; case " Station height m. a. sea level " : case " Stationshöhe m. ü. M.": case " Altitude station m. s. mer " : case " Altitudine stazione m slm " : $ location [ 'alt' ] = intval ( $ value ) ; break ; case " Barometric altitude m. a. ground " : case " Barometerhöhe m. ü. Boden": case " Altitude du baromètre m. s. sol" : case " Altitudine del barometro m da terra " : $ location [ 'alt-barometric' ] = $ value ? intval ( $ value ) : null ; break ; case " KoordinatenE " : case " CoordinatesE " : case " CoordonnéesE" : case " CoordinateE " : $ location [ 'chx' ] = intval ( $ value ) ; break ; case " CoordinatesN " : case " KoordinatenN " : case " CoordonnéesN" : case " CoordinateN " : $ location [ 'chy' ] = intval ( $ value ) ; break ; case " Latitude " : case " Breitengrad " : case " Latitudine " : $ location [ 'lat' ] = floatval ( $ value ) ; break ; case " Longitude " : case " Längengrad" : case " Longitudine  " : $ location [ 'lon' ] = floatval ( $ value ) ; break ; case " Canton " : case " Kanton " : case " Cantone " : $ location [ 'canton' ] = $ value ; break ; case " Measurements " : case " Messungen " : case " Mesures " : case " Misurazioni " : $ location [ 'measurements' ] = $ value ; break ; case " Link " : case " Lien " : case " Collegamento " : $ location [ 'link' ] = $ value ; break ; } } $ metadata -> locations [ ] = ( object ) $ location ; } return $ metadata ; }
Parse metadata from CSV file
Parse metadata from CSV file
// Probably not the CSV we're looking for // Unknown header