Exemple #1
0
            /*!
             * \brief   Constructor to load external csv-File as soil database.
             *
             * \param   soilDbFileStream   A file stream to read csv data from.
             *
             * \code{.unparsed}
             * 'create filestream for csv data file
             * Dim fs As System.IO.FileStream = New IO.FileStream("C:\DHI\MIKE-Basin_SoilData.csv", IO.FileMode.Open)
             * 'create new soilDb
             * Dim soilDb As atbApi.data.SoilDb = New atbApi.data.SoilDb(fs)
             * 'important: close fileStream after SoilDb is created
             * fs.Close()
             * 'create new soil and provide your own soilDb to constructor
             * Dim mySoil As atbApi.data.Soil = New atbApi.data.Soil("soilname_from_csv_data", soilDb)
             * \endcode
             */

            public SoilDb(Stream soilDbFileStream)
            {
                this.soilDbFileStream = soilDbFileStream;
                CsvReader csvReader = new CsvReader(soilDbFileStream);

                while (!csvReader.EndOfStream())
                {
                    IDictionary <String, String> fields;
                    fields = csvReader.readLine();

                    if (fields == null || !fields.ContainsKey("dataObjName") || String.IsNullOrEmpty(fields["dataObjName"]))
                    {
                        continue;
                    }
                    String name = fields["dataObjName"];

                    if (!soilData.ContainsKey(name))
                    {
                        soilData.Add(name, new Dictionary <Double, SoilValues>());
                    }
                    IDictionary <Double, SoilValues> soilValues = soilData[name];
                    SoilValues values = new SoilValues();
                    values.parseData(fields);
                    Double _iterator = Math.Round(Double.Parse(fields["_iterator.z"], CultureInfo.InvariantCulture), 2, MidpointRounding.AwayFromZero);
                    soilValues.Add(_iterator, values);
                    if (!maxDepth.ContainsKey(name))
                    {
                        maxDepth[name] = _iterator;
                    }
                    else
                    {
                        maxDepth[name] = Math.Max(maxDepth[name], _iterator);
                    }
                }
            }
Exemple #2
0
            private int loadCsv(Stream stream)
            {
                try
                {
                    _e = null;
                    CsvReader csvReader = new CsvReader(stream);

                    while (!csvReader.EndOfStream())
                    {
                        IDictionary <String, String> fields;
                        fields = csvReader.readLine();

                        if (fields == null || !fields.ContainsKey("dataObjName") || String.IsNullOrEmpty(fields["dataObjName"]))
                        {
                            continue;
                        }
                        if (String.IsNullOrEmpty(_name))
                        {
                            _name = fields["dataObjName"];
                        }

                        if (fields.ContainsKey("dataObjId") && String.IsNullOrEmpty(_dataObjId) && !String.IsNullOrEmpty(fields["dataObjId"]))
                        {
                            _dataObjId = fields["dataObjId"];
                        }
                        //avoid to mix multiple stations, but for "nearest" it is nessecary
                        //if (!_name.Equals(fields["dataObjName"])) continue;

                        ClimateValues values = new ClimateValues();
                        values.parseData(fields, _cultureInfo == null ? CultureInfo.InvariantCulture : _cultureInfo);

                        if (!fields.ContainsKey("_iterator.date"))
                        {
                            continue;
                        }
                        DateTime _iterator = DateTime.Parse(fields["_iterator.date"], _cultureInfo == null ? CultureInfo.InvariantCulture : _cultureInfo);
                        if (_step != TimeStep.day)
                        {
                            addValues(_iterator, convertTimeStep(values, Tools.AdjustTimeStep(_iterator, this.step), this.step, TimeStep.day));
                        }
                        else
                        {
                            addValues(_iterator, values);
                        }
                    }
                    return(climateData.Count);
                }
                catch (Exception e)
                {
                    _e = e;
                    return(0);
                }
            }
Exemple #3
0
            private int loadCsv(Stream stream)
            {
                try
                {
                    _e = null;
                    CsvReader csvReader = new CsvReader(stream);

                    while (!csvReader.EndOfStream())
                    {
                        IDictionary <String, String> fields;
                        fields = csvReader.readLine();

                        if (fields == null || (
                                (!fields.ContainsKey("dataObjName") || String.IsNullOrEmpty(fields["dataObjName"])) &&
                                (!fields.ContainsKey("_id") || String.IsNullOrEmpty(fields["_id"]))
                                ))
                        {
                            continue;
                        }
                        if (String.IsNullOrEmpty(_name))
                        {
                            _name = fields.ContainsKey("dataObjName") ? fields["dataObjName"] : fields["_id"];
                        }

                        if (fields.ContainsKey("dataObjId") && String.IsNullOrEmpty(_dataObjId) && !String.IsNullOrEmpty(fields["dataObjId"]))
                        {
                            _dataObjId = fields["dataObjId"];
                        }

                        RainPatternValues values = new RainPatternValues();
                        values.parseData(fields, _cultureInfo == null ? CultureInfo.InvariantCulture : _cultureInfo);

                        //month and sum are required
                        if (!fields.ContainsKey("month") || !fields.ContainsKey("sum"))
                        {
                            continue;
                        }

                        addValues(values.month, values.sum, values);
                    }
                    return(rainPatternData.Count);
                }
                catch (Exception e)
                {
                    _e = e;
                    return(0);
                }
            }
Exemple #4
0
            /*!
             * \brief   Constructor to load external csv-File as plant database.
             *
             * \param   plantDbFileStream   A file stream to read csv data from.
             *
             * \code{.unparsed}
             * 'create filestream for csv data file
             * Dim fs As System.IO.FileStream = New IO.FileStream("C:\DHI\MIKE-Basin_PlantData.csv", IO.FileMode.Open)
             * 'create new plantDb
             * Dim plantDb As atbApi.data.PlantDb = New atbApi.data.PlantDb(fs)
             * 'important: close fileStream after PlantDb is created
             * fs.Close()
             * 'create new plant and provide your own plantDb to constructor
             * Dim myPlant As atbApi.data.Plant = New atbApi.data.Plant("CROPWAT_80_Crop_data_CITRUS_70p_ca_bare", plantDb)
             * \endcode
             */

            public PlantDb(Stream plantDbFileStream, CultureInfo cultureInfo = null)
            {
                this.plantDbFileStream = plantDbFileStream;
                CsvReader csvReader = new CsvReader(plantDbFileStream);

                while (!csvReader.EndOfStream())
                {
                    IDictionary <String, String> fields;
                    fields = csvReader.readLine();
                    //Debug.WriteLine(fields["dataObjName"] + " day: " + fields["_iterator.day"]);

                    if (fields == null || !fields.ContainsKey("dataObjName") || String.IsNullOrEmpty(fields["dataObjName"]))
                    {
                        continue;
                    }
                    String name = fields["dataObjName"];

                    if (!plantData.ContainsKey(name))
                    {
                        plantData.Add(name, new Dictionary <int, PlantValues>());
                        plantStagesLength.Add(name, 0);
                        initialEnd.Add(name, 0);
                    }
                    IDictionary <int, PlantValues> plantValues = plantData[name];
                    PlantValues values = new PlantValues();
                    values.parseData(fields, cultureInfo != null ? cultureInfo : CultureInfo.InvariantCulture);
                    int _iterator = Int32.Parse(fields["_iterator.day"], cultureInfo != null ? cultureInfo : CultureInfo.InvariantCulture);
                    plantValues.Add(_iterator, values);
                    plantStagesLength[name] = Math.Max(plantStagesLength[name], _iterator);
                    if (values.stage.Equals(PlantStage.initial))
                    {
                        if (!initialEnd.ContainsKey(name))
                        {
                            initialEnd[name] = 0;
                        }
                        initialEnd[name] = Math.Max(initialEnd[name], _iterator);
                    }
                    if (values.stage.Equals(PlantStage.development))
                    {
                        if (!developmentEnd.ContainsKey(name))
                        {
                            developmentEnd[name] = 0;
                        }
                        developmentEnd[name] = Math.Max(developmentEnd[name], _iterator);
                    }
                }
            }
Exemple #5
0
            private static async Task <int> InitializeInstance(String tag = null)
            {
                climateIds.Clear();
                climateInstances.Clear();
                CsvReader csvReader = new CsvReader(await WebApiRequest.LoadClimateIdsFromATBWebService(tag));

                while (!csvReader.EndOfStream())
                {
                    IDictionary <String, String> fields;
                    fields = csvReader.readLine();

                    if (String.IsNullOrEmpty(fields["name"]) || String.IsNullOrEmpty(fields["_id"]))
                    {
                        continue;
                    }
                    climateIds.Add(fields["name"], fields["_id"]);
                }
                return(climateIds.Count);
            }
Exemple #6
0
            private int loadCsv(Stream stream)
            {
                try
                {
                    _e = null;
                    CsvReader csvReader = new CsvReader(stream);

                    while (!csvReader.EndOfStream())
                    {
                        IDictionary <String, String> fields;
                        fields = csvReader.readLine();

                        if (fields == null || !fields.ContainsKey("dataObjName") || String.IsNullOrEmpty(fields["dataObjName"]))
                        {
                            continue;
                        }
                        if (String.IsNullOrEmpty(_name))
                        {
                            _name = fields["dataObjName"];
                        }

                        if (fields.ContainsKey("dataObjId") && String.IsNullOrEmpty(_dataObjId) && !String.IsNullOrEmpty(fields["dataObjId"]))
                        {
                            _dataObjId = fields["dataObjId"];
                        }

                        CropSequenceValues values = new CropSequenceValues();
                        //catch parse exception and continue reading file
                        try
                        {
                            values.parseData(fields, _cultureInfo == null ? CultureInfo.InvariantCulture : _cultureInfo, pdb: plantDb, sdb: soilDb, cdb: climateDb, rpdb: rainPatternDb);
                        }
                        catch (Exception e)
                        {
                            _e = e;
                            continue;
                        }

                        bool fieldMissing = false;
                        foreach (String requiredField in requiredFields)
                        {
                            if (!fields.ContainsKey(requiredField))
                            {
                                fieldMissing = true;
                                break;
                            }
                        }
                        if (fieldMissing)
                        {
                            continue;
                        }

                        addValues(values);
                    }
                    return(cropSequenceData.Count);
                }
                catch (Exception e)
                {
                    _e = e;
                    return(0);
                }
            }