All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.hlfsousa.ncml.examples.ucararray.SampleModelWrapper Maven / Gradle / Ivy

package io.github.hlfsousa.ncml.examples.ucararray;

/*-
 * #%L
 * ncml-io
 * %%
 * Copyright (C) 2020 - 2021 Henrique L. F. de Sousa
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 2.1 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import io.github.hlfsousa.ncml.io.ConvertUtils;
import io.github.hlfsousa.ncml.io.RuntimeConfiguration;
import io.github.hlfsousa.ncml.io.wrapper.NetcdfWrapper;
import java.io.IOException;
import java.util.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import ucar.ma2.*;
import ucar.nc2.Dimension;
import ucar.nc2.Group;
import ucar.nc2.Variable;


public class SampleModelWrapper extends NetcdfWrapper implements SampleModel {

    private class TemperatureMapWrapper implements TemperatureMapVariable {

        private final Variable variable;
        private Float value;
        
        public TemperatureMapWrapper(Variable variable) {
            this.variable = variable;
        }

        @Override
        public Float getValue() {
            if (value == null) {
            	Array ncArray;
            	DataType dataType = variable.getDataType();
            	if (dataType.isNumeric()) {
                    ncArray = getNumericArray(variable);
                } else {
                    try {
                        ncArray = variable.read();
                    } catch (IOException e) {
                        throw new IllegalStateException("Unable to read variable value", e);
                    }
                }
                value = convertUtils.toJavaObject(ncArray, Float.class);
            }
            return value;
        }

        public void setValue(Float value) {
            throw new UnsupportedOperationException();
        }

        public List getDimensions() {
            return Collections.emptyList();
        }

        public void setDimensions(List dimensions) {
            throw new UnsupportedOperationException("This is a scalar variable");
        }

        @Override
        public String getLongName() {
            return Optional.ofNullable(variable.findAttribute("long_name"))
                    .map(longName -> longName.getStringValue())
                    .orElse(null);
        }

        @Override
        public void setLongName(String longName) {
            throw new UnsupportedOperationException();
        }

        @Override
        public String getUnits() {
            return Optional.ofNullable(variable.findAttribute("units"))
                    .map(units -> units.getStringValue())
                    .orElse(null);
        }

        @Override
        public void setUnits(String units) {
            throw new UnsupportedOperationException();
        }

    }


    public SampleModelWrapper(Group group, RuntimeConfiguration runtimeConfiguration) {
        super(group, runtimeConfiguration);
    }

    private ConvertUtils convertUtils = ConvertUtils.getInstance();
    private Map variableCache = new HashMap();
    
    // additionalFields >>
    // << additionalFields

    public Map> getTemperatureMap() {
        return (Map>) variableCache.computeIfAbsent("temperatureMap:temp_.*", varName -> {
            Pattern regex = Pattern.compile(varName.substring(varName.indexOf(':') + 1));
            Map> value = new LinkedHashMap<>();
            for (Variable variable : group.getVariables()) {
                Matcher matcher = regex.matcher(variable.getShortName());
                if (matcher.matches()) {
                    value.put(variable.getName(), new TemperatureMapWrapper(variable));
                }
            }
            return value;
        });
    }
    
    public void setTemperatureMap(Map> temperatureMap) {
        throw new UnsupportedOperationException();
    }

    @Override
    public String getTitle() {
        return Optional.ofNullable(group.findAttribute("title"))
                .map(title -> title.getStringValue())
                .orElse(null);
    }

    @Override
    public void setTitle(String title) {
        throw new UnsupportedOperationException();
    }

    private Map groupCache = new HashMap();
    
    @SuppressWarnings("unchecked")
    public Map getGroupMap() {
        return (Map) groupCache.computeIfAbsent("groupMap:group\\d+", key -> {
            Map groupMap = new LinkedHashMap<>();
            Pattern regex = Pattern.compile("group\\d+");
            for (Group child : group.getGroups()) {
                Matcher matcher = regex.matcher(child.getShortName());
                if (matcher.matches()) {
                    groupMap.put(child.getShortName(), new GroupMapWrapper(child, runtimeConfiguration));
                }
            }
            return groupMap;
        });
    }
    
    public void setGroupMap(Map groupMap) {
        throw new UnsupportedOperationException();
    }

    // additionalMethods >>
    // << additionalMethods

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy