Exemplo n.º 1
0
        public async Task <ActionResult> Edit([Bind(Include = "Id,ApplianceTypeId,Power")] Appliance appliance)
        {
            if (ModelState.IsValid)
            {
                Appliance app = null;
                using (var dblocal = new NpgsqlContext())
                {
                    app = dblocal.Appliances.Where(a => a.Id == appliance.Id).FirstOrDefault();
                    dblocal.Dispose();
                    GC.Collect();
                }

                string comment = $"Id: {appliance.Id} ApplianceTypeId: {appliance.ApplianceTypeId} Power: {appliance.Power.ToString()}";
                comment += $" -> ApplianceTypeId: {app.ApplianceTypeId} Power: {app.Power.ToString()}";
                SystemLog.New("ApplianceEdit", comment, null, false);

                db.Entry(appliance).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            IList <ApplianceType> appliancetypes = db.ApplianceTypes
                                                   .Where(a => true)
                                                   .ToList();

            appliancetypes = appliancetypes
                             .OrderBy(a => a.Name)
                             .ToList();
            ViewBag.ApplianceTypeId = new SelectList(appliancetypes, "Id", "Name", appliance.ApplianceTypeId);
            return(View(appliance));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> Edit([Bind(Include = "Id,Code,NameEN,NameKZ,NameRU")] PanelOrientation panelOrientation)
        {
            if (ModelState.IsValid)
            {
                PanelOrientation po = null;
                using (var dblocal = new NpgsqlContext())
                {
                    po = dblocal.PanelOrientations.Where(p => p.Id == panelOrientation.Id).FirstOrDefault();
                    dblocal.Dispose();
                    GC.Collect();
                }

                panelOrientation.Code = po.Code;

                string comment = $"Id: {po.Id} Code: {po.Code} NameEN: {po.NameEN} NameKZ: {po.NameKZ} NameRU: {po.NameRU}";
                comment += $" -> Code: {panelOrientation.Code} NameEN: {panelOrientation.NameEN} NameKZ: {panelOrientation.NameKZ} NameRU: {panelOrientation.NameRU}";
                SystemLog.New("PanelOrientationEdit", comment, null, false);

                db.Entry(panelOrientation).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(panelOrientation));
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            var blogPostFaker = new BlogPostFaker();

            NpgsqlContext context = new NpgsqlContext();

            IEnumerable <BlogPostDocument> blogPosts = new List <BlogPostDocument>();

            int blogsToGenerate = 100;

            TimeThis($"Generating {blogsToGenerate} Blogs", () =>
            {
                blogPosts = blogPostFaker.Generate(blogsToGenerate).Select(bp => new BlogPostDocument {
                    Document = bp
                });
            });

            TimeThis("Adding Blogs To Context", () =>
            {
                context.BlogPosts.AddRange(blogPosts);
            });

            TimeThis("Saving Changes", () =>
            {
                context.SaveChanges();
            });

            var count = context.BlogPosts.Count();

            Console.WriteLine($"There are now {count} blogs.");

            IEnumerable <BlogPostDocument> blogPostDocuments = new List <BlogPostDocument>();

            TimeThis($"Fetching {blogsToGenerate} blogs", () =>
            {
                blogPostDocuments = context.BlogPosts.Take(blogsToGenerate);
            });

            Console.WriteLine($"Here are {blogsToGenerate} blog authors and the number of comments");
            Console.WriteLine(JsonSerializer.Serialize(blogPostDocuments.Select(bp => new { Author = bp.Document.Author.Name, Comments = bp.Document.Comments.Count }), new JsonSerializerOptions {
                WriteIndented = true
            }));

            List <string> authors = new List <string>();

            TimeThis($"Querying for authors of posts who's name starts with 'A'", () =>
            {
                authors = context.BlogPosts
                          .Where(bp => bp.Document.Author.Name.StartsWith("A"))
                          .Select(bp => bp.Document.Author.Name)
                          .ToList();

                context.BlogPosts.Where(bp => bp.Document.Comments.Any(c => c.Content.Contains("great"))).ToList();
            });

            Console.WriteLine(JsonSerializer.Serialize(authors, new JsonSerializerOptions {
                WriteIndented = true
            }));
        }
Exemplo n.º 4
0
        public async Task <ActionResult> Edit([Bind(Include = "Id,Count,Power,Cost,Startup,Link,Customer,Investor,Executor,Name,CapacityFactor,SPPStatusId,SPPPurposeId,PanelOrientationId,Coordinates")] SPP sPP, HttpPostedFileBase Photo)
        {
            if (ModelState.IsValid)
            {
                SPP sPPdb = null;
                using (var dblocal = new NpgsqlContext())
                {
                    sPPdb = dblocal.SPPs.Find(sPP.Id);
                }

                sPP.Photo = sPPdb.Photo;
                if (Photo != null && Photo.ContentLength > 0)
                {
                    sPP.Photo = null;
                    MemoryStream target = new MemoryStream();
                    Photo.InputStream.CopyTo(target);
                    sPP.Photo = target.ToArray();
                }

                string comment = $"Id: {sPPdb.Id}";
                comment += $" Count: {sPPdb.Count.ToString()}";
                comment += $" Power: {sPPdb.Power.ToString()}";
                comment += $" Cost: {sPPdb.Cost}";
                comment += $" Startup: {sPPdb.Startup.ToString()}";
                comment += $" Link: {sPPdb.Link}";
                comment += $" Customer: {sPPdb.Customer}";
                comment += $" Investor: {sPPdb.Investor}";
                comment += $" Executor: {sPPdb.Executor}";
                comment += $" CapacityFactor: {sPPdb.CapacityFactor.ToString()}";
                comment += $" SPPStatusId: {sPPdb.SPPStatusId.ToString()}";
                comment += $" SPPPurposeId: {sPPdb.SPPPurposeId.ToString()}";
                comment += $" PanelOrientationId: {sPPdb.PanelOrientationId.ToString()}";
                comment += $" Coordinates: {sPPdb.Coordinates}";
                comment += $" -> Count: {sPP.Count.ToString()}";
                comment += $" Power: {sPP.Power.ToString()}";
                comment += $" Cost: {sPP.Cost}";
                comment += $" Startup: {sPP.Startup.ToString()}";
                comment += $" Link: {sPP.Link}";
                comment += $" Customer: {sPP.Customer}";
                comment += $" Investor: {sPP.Investor}";
                comment += $" Executor: {sPP.Executor}";
                comment += $" CapacityFactor: {sPP.CapacityFactor.ToString()}";
                comment += $" SPPStatusId: {sPP.SPPStatusId.ToString()}";
                comment += $" SPPPurposeId: {sPP.SPPPurposeId.ToString()}";
                comment += $" PanelOrientationId: {sPP.PanelOrientationId.ToString()}";
                comment += $" Coordinates: {sPP.Coordinates}";
                SystemLog.New("SPPEdit", comment, null, false);

                db.Entry(sPP).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.PanelOrientationId = new SelectList(db.PanelOrientations, "Id", "Name", sPP.PanelOrientationId);
            ViewBag.SPPPurposeId       = new SelectList(db.SPPPurposes, "Id", "Name", sPP.SPPPurposeId);
            ViewBag.SPPStatusId        = new SelectList(db.SPPStatus, "Id", "Name", sPP.SPPStatusId);
            return(View(sPP));
        }
Exemplo n.º 5
0
        public ActionResult EraseMeteoDataTypes(bool?post)
        {
            string   report = "";
            DateTime start  = DateTime.Now;

            using (var db = new NpgsqlContext())
            {
                db.Database.ExecuteSqlCommand("TRUNCATE TABLE \"MeteoDataTypes\" CASCADE");
            }

            TimeSpan time = DateTime.Now - start;

            report        += "<br/>Time: " + time.ToString() + "<br/>";
            ViewBag.Report = report;
            return(View());
        }
Exemplo n.º 6
0
        static public CurrentUserViewModel GetCurrentUser(string login)
        {
            NpgsqlContext        dbContext = new NpgsqlContext();
            CurrentUserViewModel userVM    = new CurrentUserViewModel();
            User user = new User();

            try
            {
                if (string.IsNullOrEmpty(login))
                {
                    throw new ValidateException("Не указан логин", "");
                }
                user = dbContext.Users.FirstOrDefault(x => x.Login.Equals(login));
                if (user == null)
                {
                    throw new ValidateException("Пользователь не найден", "");
                }
            }
            catch
            {
                userVM.FullName = "Пользоваель не найден";
                userVM.Id       = -1;
            }
            var config = new MapperConfiguration(cfg => cfg.CreateMap <User, CurrentUserViewModel>()
                                                 .ForMember(dest => dest.Department, opt => opt.Ignore()));
            var mapper = config.CreateMapper();

            userVM            = mapper.Map <User, CurrentUserViewModel>(user);
            userVM.Department = new DepartmentViewModel();
            if (user.Department != null)
            {
                userVM.Department.Id               = user.Department.Id;
                userVM.Department.CodeId           = user.Department.CodeId;
                userVM.Department.DepartmentCode   = user.Department.DepartmentCode;
                userVM.Department.DepartmentPrefix = user.Department.DepartmentPrefix;
                userVM.Department.Description      = user.Department.Description;
                userVM.Department.Title            = user.Department.Title;
            }
            return(userVM);
        }
Exemplo n.º 7
0
        public async Task <ActionResult> Edit([Bind(Include = "Id,LayersCode,DescriptionForUser,NameEN,NameKZ,NameRU,DescriptionEN,DescriptionKZ,DescriptionRU,AppointmentEN,AppointmentKZ,AppointmentRU,SourceEN,SourceKZ,SourceRU,AdditionalEN,AdditionalKZ,AdditionalRU,ResolutionEN,ResolutionKZ,ResolutionRU")] MapDescription mapDescription)
        {
            if (ModelState.IsValid)
            {
                MapDescription md = null;
                using (var dblocal = new NpgsqlContext())
                {
                    md = dblocal.MapDescriptions.Where(m => m.Id == mapDescription.Id).FirstOrDefault();
                    dblocal.Dispose();
                    GC.Collect();
                }

                mapDescription.LayersCode = md.LayersCode;

                db.Entry(mapDescription).State = EntityState.Modified;
                await db.SaveChangesAsync();

                string comment = $"Id: {md.Id} LayersCode: {md.LayersCode} DescriptionForUser: {md.DescriptionForUser}";
                comment += $" NameEN: {md.NameEN} NameKZ: {md.NameKZ} NameRU: {md.NameRU}";
                comment += $" DescriptionEN: {md.DescriptionEN} DescriptionKZ: {md.DescriptionKZ} DescriptionRU: {md.DescriptionRU}";
                comment += $" AppointmentEN: {md.AppointmentEN} AppointmentKZ: {md.AppointmentKZ} AppointmentRU: {md.AppointmentRU}";
                comment += $" SourceEN: {md.SourceEN} SourceKZ: {md.SourceKZ} SourceRU: {md.SourceRU}";
                comment += $" AdditionalEN: {md.AdditionalEN} AdditionalKZ: {md.AdditionalKZ} AdditionalRU: {md.AdditionalRU}";
                comment += $" ResolutionEN: {md.ResolutionEN} ResolutionKZ: {md.ResolutionKZ} ResolutionRU: {md.ResolutionRU}";
                comment  = $" -> Id: {mapDescription.Id} LayersCode: {mapDescription.LayersCode} DescriptionForUser: {mapDescription.DescriptionForUser}";
                comment += $" NameEN: {mapDescription.NameEN} NameKZ: {mapDescription.NameKZ} NameRU: {mapDescription.NameRU}";
                comment += $" DescriptionEN: {mapDescription.DescriptionEN} DescriptionKZ: {mapDescription.DescriptionKZ} DescriptionRU: {mapDescription.DescriptionRU}";
                comment += $" AppointmentEN: {mapDescription.AppointmentEN} AppointmentKZ: {mapDescription.AppointmentKZ} AppointmentRU: {mapDescription.AppointmentRU}";
                comment += $" SourceEN: {mapDescription.SourceEN} SourceKZ: {mapDescription.SourceKZ} SourceRU: {mapDescription.SourceRU}";
                comment += $" AdditionalEN: {mapDescription.AdditionalEN} AdditionalKZ: {mapDescription.AdditionalKZ} AdditionalRU: {mapDescription.AdditionalRU}";
                comment += $" ResolutionEN: {mapDescription.ResolutionEN} ResolutionKZ: {mapDescription.ResolutionKZ} ResolutionRU: {mapDescription.ResolutionRU}";

                SystemLog.New("MapDescriptionEdit", comment, null, false);

                return(RedirectToAction("Index"));
            }
            return(View(mapDescription));
        }
Exemplo n.º 8
0
        public async Task <ActionResult> Edit([Bind(Include = "Id,NameEN,NameKZ,NameRU")] ApplianceType applianceType)
        {
            if (ModelState.IsValid)
            {
                ApplianceType at = null;
                using (var dblocal = new NpgsqlContext())
                {
                    at = dblocal.ApplianceTypes.Where(a => a.Id == applianceType.Id).FirstOrDefault();
                    dblocal.Dispose();
                    GC.Collect();
                }

                string comment = $"Id: {at.Id} NameEN: {at.NameEN} NameKZ: {at.NameKZ} NameRU: {at.NameRU}";
                comment += $" -> NameEN: {applianceType.NameEN} NameKZ: {applianceType.NameKZ} NameRU: {applianceType.NameRU}";
                SystemLog.New("ApplianceTypeEdit", comment, null, false);

                db.Entry(applianceType).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(applianceType));
        }
        public async Task <ActionResult> Edit([Bind(Include = "Id,NameEN,NameKZ,NameRU,Efficiency,RatedOperatingTemperature,ThermalPowerFactor")] PVSystemMaterial pVSystemMaterial)
        {
            if (ModelState.IsValid)
            {
                PVSystemMaterial pvsm = null;
                using (var dblocal = new NpgsqlContext())
                {
                    pvsm = dblocal.PVSystemMaterials.Where(p => p.Id == pVSystemMaterial.Id).FirstOrDefault();
                    dblocal.Dispose();
                    GC.Collect();
                }

                db.Entry(pVSystemMaterial).State = EntityState.Modified;
                await db.SaveChangesAsync();

                string comment = $"Id: {pvsm.Id} NameEN: {pvsm.NameEN} NameKZ: {pvsm.NameKZ} NameRU: {pvsm.NameRU} Efficiency: {pvsm.Efficiency} RatedOperatingTemperature: {pvsm.RatedOperatingTemperature} ThermalPowerFactor: {pvsm.ThermalPowerFactor}";
                comment += $"  -> Id: {pVSystemMaterial.Id} NameEN: {pVSystemMaterial.NameEN} NameKZ: {pVSystemMaterial.NameKZ} NameRU: {pVSystemMaterial.NameRU} Efficiency: {pVSystemMaterial.Efficiency} RatedOperatingTemperature: {pVSystemMaterial.RatedOperatingTemperature} ThermalPowerFactor: {pVSystemMaterial.ThermalPowerFactor}";

                SystemLog.New("PVSystemMaterialEdit", comment, null, false);

                return(RedirectToAction("Index"));
            }
            return(View(pVSystemMaterial));
        }
Exemplo n.º 10
0
        public ActionResult Average(int?MeteoDataPeriodicityId, int?MeteoDataPeriodicityToId, int?MeteoDataSourceId, int?MeteoDataTypeId, string Action)
        {
            string   report = "";
            DateTime start  = DateTime.Now;
            int      count  = 0;

            //if(MeteoDataTypeId==null)
            if (Action == "Change")
            {
                Session["MeteoDataPeriodicityId"]      = MeteoDataPeriodicityId;
                Session["MeteoDataSourceId"]           = MeteoDataSourceId;
                Session["MeteoDataPeriodicityIdErase"] = false;
                return(RedirectToAction("Average"));
            }
            Session["MeteoDataPeriodicityIdErase"] = true;
            // Average
            if (MeteoDataTypeId != null && MeteoDataPeriodicityToId != null)
            {
                MeteoDataType meteodatatype = db.MeteoDataTypes
                                              .Where(m => m.Id == MeteoDataTypeId)
                                              .FirstOrDefault();
                MeteoDataPeriodicity meteodataperoidicity = db.MeteoDataPeriodicities
                                                            .Where(m => m.Id == meteodatatype.MeteoDataPeriodicityId)
                                                            .FirstOrDefault();
                MeteoDataPeriodicity meteodataperoidicityto = db.MeteoDataPeriodicities
                                                              .Where(m => m.Id == MeteoDataPeriodicityToId)
                                                              .FirstOrDefault();
                MeteoDataSource meteodatasource = db.MeteoDataSources
                                                  .Where(m => m.Id == meteodatatype.MeteoDataSourceId)
                                                  .FirstOrDefault();
                //----------------------------------------------------------------------------------------------------------------------------------------
                if (meteodataperoidicity.Code.Contains("Daily") && meteodataperoidicityto.Code.Contains("Daily") && meteodataperoidicityto.Code.Contains("average"))
                {
                    // add a new type of weather data if not exists
                    int ismeteodatatypeto = db.MeteoDataTypes
                                            .Where(m => m.MeteoDataPeriodicityId == meteodataperoidicityto.Id && m.MeteoDataSourceId == meteodatatype.MeteoDataSourceId)
                                            .Count();
                    if (ismeteodatatypeto == 0)
                    {
                        MeteoDataType meteodatatypetonew = new MeteoDataType()
                        {
                            AdditionalEN           = meteodatatype.AdditionalEN,
                            Code                   = meteodatatype.Code,
                            MeteoDataSourceId      = meteodatatype.MeteoDataSourceId,
                            NameEN                 = meteodatatype.NameEN,
                            NameKZ                 = meteodatatype.NameKZ,
                            NameRU                 = meteodatatype.NameRU,
                            MeteoDataPeriodicityId = meteodataperoidicityto.Id
                        };
                        db.MeteoDataTypes.Add(meteodatatypetonew);
                        db.SaveChanges();
                    }
                    MeteoDataType meteodatatypeto = db.MeteoDataTypes
                                                    .Where(m => m.MeteoDataPeriodicityId == MeteoDataPeriodicityToId && m.MeteoDataSourceId == meteodatasource.Id)
                                                    .FirstOrDefault();

                    string CurrentUserId = User.Identity.GetUserId();
                    string path          = "~/Upload/" + CurrentUserId;
                    bool   error         = false;
                    try
                    {
                        if (!Directory.Exists(Server.MapPath(path)))
                        {
                            DirectoryInfo di = Directory.CreateDirectory(Server.MapPath(path));
                        }
                    }
                    catch (Exception e)
                    {
                        report += "<br/>" + e.Message;
                        error   = true;
                    }
                    if (!error)
                    {
                        decimal longitude_min = 45 - 0.5m,
                                longitude_max = 95 + 0.5m,
                                latitude_min  = 39 - 0.5m,
                                latitude_max  = 56 + 0.5m;
                        string filenameout    = Path.Combine(Server.MapPath(path), "Average.txt");
                        using (StreamWriter sw = System.IO.File.AppendText(filenameout))
                        {
                            for (decimal latitude = latitude_min; latitude <= latitude_max; latitude++)
                            {
                                for (decimal longitude = longitude_min; longitude <= longitude_max; longitude++)
                                {
                                    using (var db_ = new NpgsqlContext())
                                    {
                                        List <MeteoData> meteodatas = db_.MeteoDatas
                                                                      .Where(m => m.Latitude == latitude && m.Longitude == longitude && m.MeteoDataTypeId == meteodatatype.Id)
                                                                      .ToList();
                                        for (int month = 1; month <= 12; month++)
                                        {
                                            for (int day = 1; day <= 31; day++)
                                            {
                                                List <MeteoData> meteodatasnew = meteodatas.
                                                                                 Where(m => m.Month == month && m.Day == day)
                                                                                 .ToList();
                                                if (meteodatasnew.Count() > 0)
                                                {
                                                    sw.WriteLine(meteodatatypeto.Id.ToString() + "\t" +
                                                                 "\t" +
                                                                 month.ToString() + "\t" +
                                                                 day.ToString() + "\t" +
                                                                 longitude.ToString().Replace(',', '.') + "\t" +
                                                                 latitude.ToString().Replace(',', '.') + "\t" +
                                                                 meteodatasnew.Average(m => m.Value).ToString().Replace(',', '.')
                                                                 );
                                                    count++;
                                                }
                                            }
                                        }
                                        db_.Dispose();
                                        GC.Collect();
                                    }
                                }
                            }
                        }
                        string query = "COPY \"MeteoData\" (\"MeteoDataTypeId\", \"Year\", \"Month\", \"Day\", \"Longitude\", \"Latitude\", \"Value\") FROM '" + filenameout + "' WITH NULL AS ''";
                        try
                        {
                            db.MeteoDatas.SqlQuery(query).SingleOrDefault();
                        }
                        catch (Exception ex)
                        {
                            if (ex.Message != "The data reader is incompatible with the specified 'AtlasSolar.Models.MeteoData'. A member of the type, 'Id', does not have a corresponding column in the data reader with the same name.")
                            {
                            }
                        }
                        try
                        {
                            System.IO.File.Delete(filenameout);
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
                //----------------------------------------------------------------------------------------------------------------------------------------
                if (meteodataperoidicity.Code.Contains("Daily") && meteodataperoidicityto.Code.Contains("Yearly") && meteodataperoidicityto.Code.Contains("average"))
                {
                    // add a new type of weather data if not exists
                    int ismeteodatatypeto = db.MeteoDataTypes
                                            .Where(m => m.MeteoDataPeriodicityId == meteodataperoidicityto.Id && m.MeteoDataSourceId == meteodatatype.MeteoDataSourceId && m.Code == meteodatatype.Code)
                                            .Count();
                    if (ismeteodatatypeto == 0)
                    {
                        MeteoDataType meteodatatypetonew = new MeteoDataType()
                        {
                            AdditionalEN           = meteodatatype.AdditionalEN,
                            Code                   = meteodatatype.Code,
                            MeteoDataSourceId      = meteodatatype.MeteoDataSourceId,
                            NameEN                 = meteodatatype.NameEN,
                            NameKZ                 = meteodatatype.NameKZ,
                            NameRU                 = meteodatatype.NameRU,
                            MeteoDataPeriodicityId = meteodataperoidicityto.Id
                        };
                        db.MeteoDataTypes.Add(meteodatatypetonew);
                        db.SaveChanges();
                    }
                    MeteoDataType meteodatatypeto = db.MeteoDataTypes
                                                    .Where(m => m.MeteoDataPeriodicityId == MeteoDataPeriodicityToId && m.MeteoDataSourceId == meteodatasource.Id)
                                                    .FirstOrDefault();

                    string CurrentUserId = User.Identity.GetUserId();
                    string path          = "~/Upload/" + CurrentUserId;
                    bool   error         = false;
                    try
                    {
                        if (!Directory.Exists(Server.MapPath(path)))
                        {
                            DirectoryInfo di = Directory.CreateDirectory(Server.MapPath(path));
                        }
                    }
                    catch (Exception e)
                    {
                        report += "<br/>" + e.Message;
                        error   = true;
                    }
                    if (!error)
                    {
                        decimal longitude_min = 45 - 0.5m,
                                longitude_max = 95 + 0.5m,
                                latitude_min  = 39 - 0.5m,
                                latitude_max  = 56 + 0.5m;
                        string filenameout    = Path.Combine(Server.MapPath(path), "Average.txt");
                        try
                        {
                            System.IO.File.Delete(filenameout);
                        }
                        catch (Exception ex)
                        {
                        }
                        using (StreamWriter sw = System.IO.File.AppendText(filenameout))
                        {
                            for (decimal latitude = latitude_min; latitude <= latitude_max; latitude++)
                            {
                                for (decimal longitude = longitude_min; longitude <= longitude_max; longitude++)
                                {
                                    using (var db_ = new NpgsqlContext())
                                    {
                                        List <MeteoData> meteodatas = db_.MeteoDatas
                                                                      .Where(m => m.Latitude == latitude && m.Longitude == longitude && m.MeteoDataTypeId == meteodatatype.Id)
                                                                      .ToList();
                                        List <MeteoData> meteodatasnew = meteodatas.ToList();
                                        if (meteodatasnew.Count() > 0)
                                        {
                                            sw.WriteLine(meteodatatypeto.Id.ToString() + "\t" +
                                                         "\t" +
                                                         "\t" +
                                                         "\t" +
                                                         longitude.ToString().Replace(',', '.') + "\t" +
                                                         latitude.ToString().Replace(',', '.') + "\t" +
                                                         meteodatasnew.Average(m => m.Value).ToString().Replace(',', '.')
                                                         );
                                            count++;
                                        }
                                        db_.Dispose();
                                        GC.Collect();
                                    }
                                }
                            }
                        }
                        string query = "COPY \"MeteoData\" (\"MeteoDataTypeId\", \"Year\", \"Month\", \"Day\", \"Longitude\", \"Latitude\", \"Value\") FROM '" + filenameout + "' WITH NULL AS ''";
                        try
                        {
                            db.MeteoDatas.SqlQuery(query).SingleOrDefault();
                        }
                        catch (Exception ex)
                        {
                            if (ex.Message != "The data reader is incompatible with the specified 'AtlasSolar.Models.MeteoData'. A member of the type, 'Id', does not have a corresponding column in the data reader with the same name.")
                            {
                            }
                        }
                        try
                        {
                            System.IO.File.Delete(filenameout);
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
                //----------------------------------------------------------------------------------------------------------------------------------------
                TimeSpan time = DateTime.Now - start;
                report        += "<br/>Time: " + time.ToString() + "<br/>Count: " + count.ToString();
                ViewBag.Report = report;
            }
            else
            {
                ViewBag.Report = "Select Periodicity and Source!";
            }
            return(View());
        }
Exemplo n.º 11
0
        public ActionResult CreateLayerData(int?MeteoDataPeriodicityId, int?MeteoDataSourceId, int?MeteoDataTypeId, string Action)
        {
            string   report = "";
            DateTime start  = DateTime.Now;
            int      count  = 0;

            if (Action == "Change")
            {
                Session["MeteoDataPeriodicityId"]      = MeteoDataPeriodicityId;
                Session["MeteoDataSourceId"]           = MeteoDataSourceId;
                Session["MeteoDataPeriodicityIdErase"] = false;
                return(RedirectToAction("CreateLayerData"));
            }
            Session["MeteoDataPeriodicityIdErase"] = true;
            // Average
            if (MeteoDataTypeId != null)
            {
                MeteoDataType meteodatatype = db.MeteoDataTypes
                                              .Where(m => m.Id == MeteoDataTypeId)
                                              .FirstOrDefault();
                MeteoDataPeriodicity meteodataperoidicity = db.MeteoDataPeriodicities
                                                            .Where(m => m.Id == meteodatatype.MeteoDataPeriodicityId)
                                                            .FirstOrDefault();
                MeteoDataSource meteodatasource = db.MeteoDataSources
                                                  .Where(m => m.Id == meteodatatype.MeteoDataSourceId)
                                                  .FirstOrDefault();
                //----------------------------------------------------------------------------------------------------------------------------------------
                if (meteodataperoidicity.Code.Contains("Monthly average"))
                {
                    string CurrentUserId = User.Identity.GetUserId();
                    string path          = "~/Upload/" + CurrentUserId;
                    bool   error         = false;
                    try
                    {
                        if (!Directory.Exists(Server.MapPath(path)))
                        {
                            DirectoryInfo di = Directory.CreateDirectory(Server.MapPath(path));
                        }
                    }
                    catch (Exception e)
                    {
                        report += "<br/>" + e.Message;
                        error   = true;
                    }
                    if (!error)
                    {
                        decimal longitude_min = 45 - 0.5m,
                                longitude_max = 95 + 0.5m,
                                latitude_min  = 39 - 0.5m,
                                latitude_max  = 56 + 0.5m;

                        longitude_min = Properties.Settings.Default.NASASSELongitudeMin;
                        longitude_max = Properties.Settings.Default.NASASSELongitudeMax;
                        latitude_min  = Properties.Settings.Default.NASASSELatitudeMin;
                        latitude_max  = Properties.Settings.Default.NASASSELatitudeMax;
                        decimal step = Properties.Settings.Default.NASASSECoordinatesStep;

                        string filenameout = Path.Combine(Server.MapPath(path), $"{meteodatatype.Code} {meteodatatype.AdditionalEN}.csv");
                        System.IO.File.Delete(filenameout);
                        using (StreamWriter sw = System.IO.File.AppendText(filenameout))
                        {
                            sw.WriteLine("latitude" + ";" +
                                         "longitude" + ";" +
                                         "sum month 1" + ";" +
                                         "sum month 2" + ";" +
                                         "sum month 3" + ";" +
                                         "sum month 4" + ";" +
                                         "sum month 5" + ";" +
                                         "sum month 6" + ";" +
                                         "sum month 7" + ";" +
                                         "sum month 8" + ";" +
                                         "sum month 9" + ";" +
                                         "sum month 10" + ";" +
                                         "sum month 11" + ";" +
                                         "sum month 12" + ";" +
                                         "average" + ";" +
                                         "sum year" + ";" +
                                         "average month 1" + ";" +
                                         "average month 2" + ";" +
                                         "average month 3" + ";" +
                                         "average month 4" + ";" +
                                         "average month 5" + ";" +
                                         "average month 6" + ";" +
                                         "average month 7" + ";" +
                                         "average month 8" + ";" +
                                         "average month 9" + ";" +
                                         "average month 10" + ";" +
                                         "average month 11" + ";" +
                                         "average month 12"
                                         );
                            for (decimal latitude = latitude_min; latitude <= latitude_max; latitude += step)
                            {
                                for (decimal longitude = longitude_min; longitude <= longitude_max; longitude += step)
                                {
                                    using (var db_ = new NpgsqlContext())
                                    {
                                        List <MeteoData> meteodatas = db_.MeteoDatas
                                                                      .Where(m => m.Latitude == latitude && m.Longitude == longitude && m.MeteoDataTypeId == meteodatatype.Id)
                                                                      .ToList();
                                        if (meteodatas.Count() > 0)
                                        {
                                            decimal?[] msperday = new decimal?[12];
                                            msperday[0]  = meteodatas.Where(m => m.Month == 1).Select(m => m.Value).Sum();
                                            msperday[1]  = meteodatas.Where(m => m.Month == 2).Select(m => m.Value).Sum();
                                            msperday[2]  = meteodatas.Where(m => m.Month == 3).Select(m => m.Value).Sum();
                                            msperday[3]  = meteodatas.Where(m => m.Month == 4).Select(m => m.Value).Sum();
                                            msperday[4]  = meteodatas.Where(m => m.Month == 5).Select(m => m.Value).Sum();
                                            msperday[5]  = meteodatas.Where(m => m.Month == 6).Select(m => m.Value).Sum();
                                            msperday[6]  = meteodatas.Where(m => m.Month == 7).Select(m => m.Value).Sum();
                                            msperday[7]  = meteodatas.Where(m => m.Month == 8).Select(m => m.Value).Sum();
                                            msperday[8]  = meteodatas.Where(m => m.Month == 9).Select(m => m.Value).Sum();
                                            msperday[9]  = meteodatas.Where(m => m.Month == 10).Select(m => m.Value).Sum();
                                            msperday[10] = meteodatas.Where(m => m.Month == 11).Select(m => m.Value).Sum();
                                            msperday[11] = meteodatas.Where(m => m.Month == 12).Select(m => m.Value).Sum();
                                            decimal?[] days = new decimal?[12];
                                            days[0]  = 31;
                                            days[1]  = 622 / 22;
                                            days[2]  = 31;
                                            days[3]  = 30;
                                            days[4]  = 31;
                                            days[5]  = 30;
                                            days[6]  = 31;
                                            days[7]  = 31;
                                            days[8]  = 30;
                                            days[9]  = 31;
                                            days[10] = 30;
                                            days[11] = 31;
                                            decimal?average   = 0,
                                                   sum        = 0,
                                                   days_count = 0;
                                            for (int i = 0; i < 12; i++)
                                            {
                                                sum        += days[i] * msperday[i];
                                                days_count += days[i];
                                            }
                                            average = sum / days_count;
                                            sw.WriteLine(latitude.ToString() + ";" +
                                                         longitude.ToString() + ";" +
                                                         (msperday[0] * days[0]).ToString() + ";" +
                                                         (msperday[1] * days[1]).ToString() + ";" +
                                                         (msperday[2] * days[2]).ToString() + ";" +
                                                         (msperday[3] * days[3]).ToString() + ";" +
                                                         (msperday[4] * days[4]).ToString() + ";" +
                                                         (msperday[5] * days[5]).ToString() + ";" +
                                                         (msperday[6] * days[6]).ToString() + ";" +
                                                         (msperday[7] * days[7]).ToString() + ";" +
                                                         (msperday[8] * days[8]).ToString() + ";" +
                                                         (msperday[9] * days[9]).ToString() + ";" +
                                                         (msperday[10] * days[10]).ToString() + ";" +
                                                         (msperday[11] * days[11]).ToString() + ";" +
                                                         average.ToString() + ";" +
                                                         sum.ToString() + ";" +
                                                         msperday[0].ToString() + ";" +
                                                         msperday[1].ToString() + ";" +
                                                         msperday[2].ToString() + ";" +
                                                         msperday[3].ToString() + ";" +
                                                         msperday[4].ToString() + ";" +
                                                         msperday[5].ToString() + ";" +
                                                         msperday[6].ToString() + ";" +
                                                         msperday[7].ToString() + ";" +
                                                         msperday[8].ToString() + ";" +
                                                         msperday[9].ToString() + ";" +
                                                         msperday[10].ToString() + ";" +
                                                         msperday[11].ToString()
                                                         );
                                            count++;
                                        }

                                        db_.Dispose();
                                        GC.Collect();
                                    }
                                }
                            }
                        }
                    }
                }
                //----------------------------------------------------------------------------------------------------------------------
                TimeSpan time = DateTime.Now - start;
                report        += "<br/>Time: " + time.ToString() + "<br/>Count: " + count.ToString();
                ViewBag.Report = report;
            }
            else
            {
                ViewBag.Report = "Select Periodicity and Source!";
            }
            return(View());
        }
Exemplo n.º 12
0
        // getting a list of monthly average data NASA (1983-2005)
        public ActionResult ParseNASA1983Monthly(bool?post)
        {
            string   report = "";
            DateTime start  = DateTime.Now;

            List <MeteoDataType> meteodatatypes = new List <MeteoDataType>();

            using (var db = new NpgsqlContext())
            {
                meteodatatypes = db.MeteoDataTypes
                                 .Where(m => m.MeteoDataSource.Code == "NASA (1983-2005)" && m.MeteoDataPeriodicity.Code == "Monthly average")
                                 .ToList();
                db.Dispose();
                GC.Collect();
            }

            decimal longitude_min = 45 - 0.5m,
                    longitude_max = 95 + 0.5m,
                    latitude_min  = 39 - 0.5m,
                    latitude_max  = 56 + 0.5m;

            int count = 0;

            for (decimal latitude = latitude_min; latitude <= latitude_max; latitude++)
            {
                for (decimal longitude = longitude_min; longitude <= longitude_max; longitude++)
                {
                    string url = "https://eosweb.larc.nasa.gov/cgi-bin/sse/grid.cgi?&num=225129&lat=" +
                                 latitude.ToString().Replace(",", ".") +
                                 "&hgt=100&submit=Submit&veg=17&sitelev=&[email protected]&p=grid_id&step=2&lon=" +
                                 longitude.ToString().Replace(",", ".");
                    HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
                    html.LoadHtml(new WebClient().DownloadString(url));

                    HtmlAgilityPack.HtmlNode root = html.DocumentNode;

                    string slat = "",
                           slon = "";
                    foreach (HtmlAgilityPack.HtmlNode td in root.Descendants("td"))
                    {
                        if (td.InnerText.IndexOf("Center") >= 0)
                        {
                            IEnumerable <HtmlAgilityPack.HtmlNode> bs = td.Descendants("b");
                            slat = bs.First().InnerText;
                            slon = bs.Last().InnerText;
                            break;
                        }
                    }

                    string current_table_name = "";
                    using (var db = new NpgsqlContext())
                    {
                        db.Configuration.AutoDetectChangesEnabled = false;
                        foreach (HtmlAgilityPack.HtmlNode node in root.Descendants())
                        {
                            if (node.Name == "a")
                            {
                                if (node.GetAttributeValue("name", "") != "")
                                {
                                    current_table_name = HttpUtility.HtmlDecode(node.GetAttributeValue("name", "").Trim());
                                }
                            }
                            if ((current_table_name != "") && (node.Name == "div"))
                            {
                                int current_tr_i = 0;
                                foreach (HtmlAgilityPack.HtmlNode tr in node.Descendants("tr"))
                                {
                                    current_tr_i++;
                                    if (current_tr_i > 1)
                                    {
                                        int    current_td_i = 0;
                                        string row_name     = "";
                                        foreach (HtmlAgilityPack.HtmlNode td in tr.Descendants("td"))
                                        {
                                            current_td_i++;
                                            if (current_td_i == 1)
                                            {
                                                row_name = HttpUtility.HtmlDecode(td.InnerText.Trim());
                                            }
                                            else
                                            {
                                                decimal out_decimal     = 0;
                                                int     MeteoDataTypeId = meteodatatypes
                                                                          .Where(m => m.Code == current_table_name && m.AdditionalEN == row_name)
                                                                          .FirstOrDefault()
                                                                          .Id;
                                                string V = td.InnerText.Replace("*", "").Trim();
                                                db.MeteoDatas.Add(new MeteoData()
                                                {
                                                    Latitude        = Convert.ToDecimal(slat.Replace(".", ",")),
                                                    Longitude       = Convert.ToDecimal(slon.Replace(".", ",")),
                                                    Year            = null,
                                                    Month           = current_td_i - 1,
                                                    Day             = null,
                                                    MeteoDataTypeId = MeteoDataTypeId,
                                                    Value           = decimal.TryParse(V.Replace(".", ","), out out_decimal) ? Convert.ToDecimal(V.Replace(".", ",")) : (decimal?)null
                                                });

                                                count++;
                                            }
                                        }
                                    }
                                }
                                current_table_name = "";
                            }
                        }
                        db.SaveChanges();
                        db.Dispose();
                        GC.Collect();
                    }
                }
            }

            TimeSpan time = DateTime.Now - start;

            report        += "<br/>Time: " + time.ToString() + "<br/>Count: " + count.ToString() + "<br/>";
            ViewBag.Report = report;
            return(View());
        }
Exemplo n.º 13
0
        // getting a list of daily data NASA
        public ActionResult ParseNASADaily(bool?post)
        {
            string   report = "";
            DateTime start  = DateTime.Now;

            decimal longitude_min = 45 - 0.25m,
                    longitude_max = 95 + 0.25m,
                    latitude_min  = 39 - 0.25m,
                    latitude_max  = 56 + 0.25m;

            List <MeteoDataType> meteodatatypes = new List <MeteoDataType>();

            using (var db = new NpgsqlContext())
            {
                meteodatatypes = db.MeteoDataTypes
                                 .Where(m => true)
                                 .Include(m => m.MeteoDataSource)
                                 .Include(m => m.MeteoDataPeriodicity)
                                 .Where(m => m.MeteoDataSource.Code == "NASA" && m.MeteoDataPeriodicity.Code == "Daily")
                                 .ToList();
                db.Dispose();
                GC.Collect();
            }
            List <int> meteodatatypeids = meteodatatypes
                                          .Select(m => m.Id)
                                          .ToList();

            int count = 0;

            for (decimal latitude = latitude_min; latitude <= latitude_max; latitude += 0.5M)
            {
                for (decimal longitude = longitude_min; longitude <= longitude_max; longitude += 0.5M)
                {
                    bool go = false;
                    if (!go)
                    {
                        using (var db = new NpgsqlContext())
                        {
                            if (db.MeteoDatas.Where(m => m.Longitude == longitude && m.Latitude == latitude && m.Year == 1982 && meteodatatypeids.Contains(m.MeteoDataTypeId)).FirstOrDefault() != null)
                            {
                                db.Dispose();
                                GC.Collect();
                                continue;
                            }
                            go = true;
                            db.Dispose();
                            GC.Collect();
                        }
                    }

                    string file_url = "https://power.larc.nasa.gov/cgi-bin/hirestimeser.cgi?email=hirestimeser%40larc.nasa.gov&step=1&lat=" +
                                      latitude.ToString().Replace(",", ".") +
                                      "&lon=" +
                                      longitude.ToString().Replace(",", ".") +
                                      "&ms=1&ds=1&ys=1982&me=1&de=31&ye=1982&p=MHswv_dwn&p=MHlwv_dwn&p=MHtoa_dwn&p=MHclr_sky&p=MHPS&p=MHT2M&p=MHT2MN&p=MHT2MX&p=MHQV2M&p=MHRH2M&p=MHDFP2M&p=MHTS&p=MHWS10M&p=MHPRECTOT&submit=Submit";

                    string file = "";
                    bool   ok   = false;
                    while (!ok)
                    {
                        try
                        {
                            file = (new WebClient()).DownloadString(file_url);
                            ok   = true;
                        }
                        catch (Exception ex)
                        {
                        }
                    }

                    string[]      lines   = file.Split('\n');
                    List <string> columns = new List <string>();
                    using (var db = new NpgsqlContext())
                    {
                        db.Configuration.AutoDetectChangesEnabled = false;
                        foreach (string line in lines)
                        {
                            if (line.Contains("Completed"))
                            {
                                break;
                            }

                            if (line.Length == 0)
                            {
                                continue;
                            }

                            string s = line;
                            while (s.Contains("  "))
                            {
                                s = s.Replace("  ", " ");
                            }
                            string[] linecolumns = s.Split(' ');

                            if ((columns.Count > 0) && (linecolumns.Length == 17))
                            {
                                for (int c = 3; c < columns.Count; c++)
                                {
                                    decimal out_decimal = 0;
                                    try
                                    {
                                        db.MeteoDatas.Add(new MeteoData()
                                        {
                                            Year            = Convert.ToInt32(linecolumns[0]),
                                            Month           = Convert.ToInt32(linecolumns[1]),
                                            Day             = Convert.ToInt32(linecolumns[2]),
                                            Longitude       = longitude,
                                            Latitude        = latitude,
                                            MeteoDataTypeId = meteodatatypes.Where(m => m.Code == columns[c]).FirstOrDefault().Id,
                                            Value           = decimal.TryParse(linecolumns[c].Replace(".", ","), out out_decimal) ? Convert.ToDecimal(linecolumns[c].Replace(".", ",")) : (decimal?)null
                                        });
                                        count++;
                                    }
                                    catch (Exception ex)
                                    {
                                    }
                                }
                            }

                            if (line.Contains("YEAR MO DY"))
                            {
                                foreach (string linecolumn in linecolumns)
                                {
                                    columns.Add(linecolumn);
                                }
                            }
                        }
                        db.SaveChanges();
                        db.Dispose();
                        GC.Collect();
                    }
                }
            }

            TimeSpan time = DateTime.Now - start;

            report        += "<br/>Time: " + time.ToString() + "<br/>Count: " + count.ToString() + "<br/>";
            ViewBag.Report = report;
            return(View());
        }
Exemplo n.º 14
0
        // getting a list of daily data NASA (1983-2005)
        public ActionResult ParseNASA1983Daily(bool?post)
        {
            string   report = "";
            DateTime start  = DateTime.Now;
            int      count  = 0;

            decimal longitude_min = 45 - 0.5m,
                    longitude_max = 95 + 0.5m,
                    latitude_min  = 39 - 0.5m,
                    latitude_max  = 56 + 0.5m;

            List <MeteoDataType> meteodatatypes = new List <MeteoDataType>();

            using (var db = new NpgsqlContext())
            {
                meteodatatypes = db.MeteoDataTypes
                                 .Where(m => m.MeteoDataSource.Code == "NASA (1983-2005)" && m.MeteoDataPeriodicity.Code == "Daily")
                                 .ToList();
                db.Dispose();
                GC.Collect();
            }
            List <int> meteodatatypeids = meteodatatypes
                                          .Select(m => m.Id)
                                          .ToList();

            string CurrentUserId = User.Identity.GetUserId();
            string path          = "~/Upload/" + CurrentUserId;

            bool error = false;

            try
            {
                if (!Directory.Exists(Server.MapPath(path)))
                {
                    DirectoryInfo di = Directory.CreateDirectory(Server.MapPath(path));
                }
            }
            catch (Exception e)
            {
                report += "<br/>" + e.Message;
                error   = true;
            }

            if (!error)
            {
                bool go = false;
                for (decimal latitude = latitude_min; latitude <= latitude_max; latitude++)
                {
                    for (decimal longitude = longitude_min; longitude <= longitude_max; longitude++)
                    {
                        string filenameout = Path.Combine(Server.MapPath(path), $"From NASA (1983-2005) {latitude.ToString()} - {longitude.ToString()}.txt");
                        using (StreamWriter sw = System.IO.File.AppendText(filenameout))
                        {
                            if (!go)
                            {
                                using (var db = new NpgsqlContext())
                                {
                                    if (db.MeteoDatas.Where(m => m.Longitude == longitude && m.Latitude == latitude && meteodatatypeids.Contains(m.MeteoDataTypeId)).FirstOrDefault() != null)
                                    {
                                        db.Dispose();
                                        GC.Collect();
                                        continue;
                                    }
                                    go = true;
                                    db.Dispose();
                                    GC.Collect();
                                }
                            }

                            string url = "https://eosweb.larc.nasa.gov/cgi-bin/sse/daily.cgi?email=skip%40larc.nasa.gov&step=1&lat=" +
                                         latitude.ToString().Replace(",", ".") +
                                         "&lon=" +
                                         longitude.ToString().Replace(",", ".") +
                                         "&sitelev=&ms=1&ds=1&ys=1983&me=12&de=31&ye=2005&p=swv_dwn&p=avg_kt&p=clr_sky&p=clr_dif&p=clr_dnr&p=clr_kt&p=lwv_dwn&p=toa_dwn&p=PS&p=TSKIN&p=T10M&p=T10MN&p=T10MX&p=Q10M&p=RH10M&p=DFP10M&submit=Submit&plot=swv_dwn";
                            HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
                            html.LoadHtml(new WebClient().DownloadString(url));

                            HtmlAgilityPack.HtmlNode root = html.DocumentNode;

                            string slat = "",
                                   slon = "";
                            foreach (HtmlAgilityPack.HtmlNode td in root.Descendants("td"))
                            {
                                if (td.InnerText.IndexOf("Center") >= 0)
                                {
                                    IEnumerable <HtmlAgilityPack.HtmlNode> bs = td.Descendants("b");
                                    slat = bs.First().InnerText;
                                    slon = bs.Last().InnerText;
                                    break;
                                }
                            }

                            string file_url = "https://eosweb.larc.nasa.gov";
                            foreach (HtmlAgilityPack.HtmlNode node in root.Descendants())
                            {
                                if ((node.Name == "a") && (node.InnerText == "Download a text file"))
                                {
                                    file_url += node.GetAttributeValue("href", "");
                                    break;
                                }
                            }

                            string        file    = (new WebClient()).DownloadString(file_url);
                            string[]      lines   = file.Split('\n');
                            List <string> columns = new List <string>();
                            foreach (string line in lines)
                            {
                                if (line.Length == 0)
                                {
                                    continue;
                                }

                                string s = line;
                                while (s.Contains("  "))
                                {
                                    s = s.Replace("  ", " ");
                                }
                                string[] linecolumns = s.Split(' ');

                                if (columns.Count > 0)
                                {
                                    for (int c = 3; c < columns.Count; c++)
                                    {
                                        sw.WriteLine(meteodatatypes.Where(m => m.Code == columns[c]).FirstOrDefault().Id.ToString() + "\t" +
                                                     linecolumns[0] + "\t" +
                                                     linecolumns[1] + "\t" +
                                                     linecolumns[2] + "\t" +
                                                     "\t" +
                                                     slon + "\t" +
                                                     slat + "\t" +
                                                     ((linecolumns[c] == "-") ? "" : linecolumns[c])
                                                     );
                                        count++;
                                    }
                                }

                                if (line.Contains("YEAR MO DY"))
                                {
                                    foreach (string linecolumn in linecolumns)
                                    {
                                        columns.Add(linecolumn);
                                    }
                                }
                            }
                        }
                    }
                }

                for (decimal latitude = latitude_min; latitude <= latitude_max; latitude++)
                {
                    for (decimal longitude = longitude_min; longitude <= longitude_max; longitude++)
                    {
                        string filenameout = Path.Combine(Server.MapPath(path), $"From NASA (1983-2005) {latitude.ToString()} - {longitude.ToString()}.txt");
                        using (var db = new NpgsqlContext())
                        {
                            string query = "COPY \"MeteoData\" (\"MeteoDataTypeId\", \"Year\", \"Month\", \"Day\", \"Hour\", \"Longitude\", \"Latitude\", \"Value\") FROM '" + filenameout + "' WITH NULL AS ''";
                            try
                            {
                                db.MeteoDatas.SqlQuery(query).SingleOrDefault();
                            }
                            catch (Exception ex)
                            {
                                if (ex.Message != "The data reader is incompatible with the specified 'AtlasSolar.Models.MeteoData'. A member of the type, 'Id', does not have a corresponding column in the data reader with the same name.")
                                {
                                    int re = 5;
                                }
                            }
                            db.Dispose();
                            GC.Collect();
                        }
                    }
                }
            }

            TimeSpan time = DateTime.Now - start;

            report        += "<br/>Time: " + time.ToString() + "<br/>Count: " + count.ToString() + "<br/>";
            ViewBag.Report = report;
            return(View());
        }
Exemplo n.º 15
0
        // getting a list of data types
        public ActionResult ParseMeteoDataTypes(bool?post)
        {
            string   report = "";
            DateTime start  = DateTime.Now;
            int      count  = 0;

            string url = "";

            HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
            HtmlAgilityPack.HtmlNode     root = html.DocumentNode;

            // monthly average types NASA (1983-2005)
            decimal longitude_min = 45 - 0.5m,
                    longitude_max = 45 - 0.5m,
                    latitude_min  = 39 - 0.5m,
                    latitude_max  = 56 + 0.5m;

            for (decimal latitude = latitude_min; latitude <= latitude_max; latitude++)
            {
                for (decimal longitude = longitude_min; longitude <= longitude_max; longitude++)
                {
                    url = "https://eosweb.larc.nasa.gov/cgi-bin/sse/grid.cgi?&num=225129&lat=" +
                          latitude.ToString().Replace(",", ".") +
                          "&hgt=100&submit=Submit&veg=17&sitelev=&[email protected]&p=grid_id&step=2&lon=" +
                          longitude.ToString().Replace(",", ".");
                    html = new HtmlAgilityPack.HtmlDocument();
                    html.LoadHtml(new WebClient().DownloadString(url));
                    root = html.DocumentNode;

                    string code         = "";
                    string name         = "";
                    int    current_tr_i = 0;
                    using (var db = new NpgsqlContext())
                    {
                        List <MeteoDataType>        meteodatatypes         = db.MeteoDataTypes.ToList();
                        List <MeteoDataPeriodicity> meteodataperiodicities = db.MeteoDataPeriodicities.ToList();
                        List <MeteoDataSource>      meteodatasources       = db.MeteoDataSources.ToList();

                        foreach (HtmlAgilityPack.HtmlNode node in root.Descendants())
                        {
                            if (node.Name == "a")
                            {
                                if (node.GetAttributeValue("name", "") != "")
                                {
                                    code = node.GetAttributeValue("name", "");
                                }
                            }
                            if ((code != "") && (name == "") && (node.Name == "b"))
                            {
                                name = HttpUtility.HtmlDecode(node.InnerText.Trim());
                            }

                            if ((code != "") && (name != ""))
                            {
                                if (node.ParentNode.Name == "table")
                                {
                                    current_tr_i = 0;
                                    foreach (HtmlAgilityPack.HtmlNode tr in node.ParentNode.Descendants("tr"))
                                    {
                                        current_tr_i++;
                                        if (current_tr_i > 1)
                                        {
                                            string additional             = "";
                                            int    MeteoDataSourceId      = meteodatasources.Where(m => m.Code == "NASA (1983-2005)").FirstOrDefault().Id;
                                            int    MeteoDataPeriodicityId = meteodataperiodicities.Where(m => m.Code == "Monthly average").FirstOrDefault().Id;
                                            additional = HttpUtility.HtmlDecode(tr.Descendants("td").FirstOrDefault().InnerText.Trim());
                                            int is_yet = meteodatatypes
                                                         .Where(m => m.MeteoDataSourceId == MeteoDataSourceId && m.MeteoDataPeriodicityId == MeteoDataPeriodicityId && m.Code == code && m.AdditionalEN == additional)
                                                         .Count();
                                            if (is_yet > 0)
                                            {
                                                continue;
                                            }
                                            else
                                            {
                                                db.MeteoDataTypes.Add(new MeteoDataType()
                                                {
                                                    MeteoDataSourceId      = MeteoDataSourceId,
                                                    MeteoDataPeriodicityId = MeteoDataPeriodicityId,
                                                    Code         = code,
                                                    NameEN       = name,
                                                    AdditionalEN = additional
                                                });
                                                count++;
                                            }
                                        }
                                    }
                                    code = "";
                                    name = "";
                                }
                            }
                        }
                        db.SaveChanges();
                        db.Dispose();
                        GC.Collect();
                    }
                }
            }

            // daily types NASA (1983-2005)
            url = "https://eosweb.larc.nasa.gov/cgi-bin/sse/daily.cgi?email=skip%40larc.nasa.gov&step=1&lat=" +
                  "38.5".ToString().Replace(",", ".") +
                  "&lon=" +
                  "44.5".ToString().Replace(",", ".") +
                  "&sitelev=&ms=1&ds=1&ys=1983&me=12&de=31&ye=2005&p=swv_dwn&p=avg_kt&p=clr_sky&p=clr_dif&p=clr_dnr&p=clr_kt&p=lwv_dwn&p=toa_dwn&p=PS&p=TSKIN&p=T10M&p=T10MN&p=T10MX&p=Q10M&p=RH10M&p=DFP10M&submit=Submit&plot=swv_dwn";
            html = new HtmlAgilityPack.HtmlDocument();
            html.LoadHtml(new WebClient().DownloadString(url));
            root = html.DocumentNode;

            string file_url = "https://eosweb.larc.nasa.gov";

            foreach (HtmlAgilityPack.HtmlNode node in root.Descendants())
            {
                if ((node.Name == "a") && (node.InnerText == "Download a text file"))
                {
                    file_url += node.GetAttributeValue("href", "");
                    break;
                }
            }
            string file = (new WebClient()).DownloadString(file_url);

            string[] lines = file.Split('\n');
            using (var db = new NpgsqlContext())
            {
                List <MeteoDataType>        meteodatatypes         = db.MeteoDataTypes.ToList();
                List <MeteoDataPeriodicity> meteodataperiodicities = db.MeteoDataPeriodicities.ToList();
                List <MeteoDataSource>      meteodatasources       = db.MeteoDataSources.ToList();
                bool begin = false;
                foreach (string line in lines)
                {
                    if (string.IsNullOrEmpty(line))
                    {
                        break;
                    }
                    if (begin)
                    {
                        string linen = line;
                        while (linen.Contains("   "))
                        {
                            linen = linen.Replace("   ", "  ");
                        }
                        string[] linecolumns            = linen.Split(new string[] { "  " }, StringSplitOptions.None);
                        int      MeteoDataSourceId      = meteodatasources.Where(m => m.Code == "NASA (1983-2005)").FirstOrDefault().Id;
                        int      MeteoDataPeriodicityId = meteodataperiodicities.Where(m => m.Code == "Daily").FirstOrDefault().Id;
                        int      is_yet = meteodatatypes
                                          .Where(m => m.MeteoDataSourceId == MeteoDataSourceId && m.MeteoDataPeriodicityId == MeteoDataPeriodicityId && m.Code == linecolumns[0])
                                          .Count();
                        if (is_yet > 0)
                        {
                            continue;
                        }
                        else
                        {
                            db.MeteoDataTypes.Add(new MeteoDataType()
                            {
                                MeteoDataSourceId      = MeteoDataSourceId,
                                MeteoDataPeriodicityId = MeteoDataPeriodicityId,
                                Code   = linecolumns[0],
                                NameEN = linecolumns[1]
                            });
                            count++;
                        }
                    }
                    if (line.Contains("Parameter(s):"))
                    {
                        begin = true;
                    }
                }
                db.SaveChanges();
                db.Dispose();
                GC.Collect();
            }

            // daily types NASA
            file_url = "https://power.larc.nasa.gov/cgi-bin/hirestimeser.cgi?email=hirestimeser%40larc.nasa.gov&step=1&lat=38&lon=44&ms=1&ds=1&ys=2016&me=1&de=2&ye=2016&p=MHswv_dwn&p=MHlwv_dwn&p=MHtoa_dwn&p=MHclr_sky&p=MHPS&p=MHT2M&p=MHT2MN&p=MHT2MX&p=MHQV2M&p=MHRH2M&p=MHDFP2M&p=MHTS&p=MHWS10M&p=MHPRECTOT&submit=Submit";
            file     = (new WebClient()).DownloadString(file_url);
            lines    = file.Split('\n');
            using (var db = new NpgsqlContext())
            {
                List <MeteoDataType>        meteodatatypes         = db.MeteoDataTypes.ToList();
                List <MeteoDataPeriodicity> meteodataperiodicities = db.MeteoDataPeriodicities.ToList();
                List <MeteoDataSource>      meteodatasources       = db.MeteoDataSources.ToList();
                bool begin = false;
                foreach (string line in lines)
                {
                    if (line.Contains("YEAR MO DY"))
                    {
                        break;
                    }
                    if (begin)
                    {
                        string linen = line;
                        while (linen.Contains("   "))
                        {
                            linen = linen.Replace("   ", "  ");
                        }
                        string[] linecolumns            = linen.Split(new string[] { "  " }, StringSplitOptions.None);
                        int      MeteoDataSourceId      = meteodatasources.Where(m => m.Code == "NASA").FirstOrDefault().Id;
                        int      MeteoDataPeriodicityId = meteodataperiodicities.Where(m => m.Code == "Daily").FirstOrDefault().Id;
                        int      is_yet = meteodatatypes
                                          .Where(m => m.MeteoDataSourceId == MeteoDataSourceId && m.MeteoDataPeriodicityId == MeteoDataPeriodicityId && m.Code == linecolumns[0])
                                          .Count();
                        if (is_yet > 0)
                        {
                            continue;
                        }
                        else
                        {
                            db.MeteoDataTypes.Add(new MeteoDataType()
                            {
                                MeteoDataSourceId      = MeteoDataSourceId,
                                MeteoDataPeriodicityId = MeteoDataPeriodicityId,
                                Code   = linecolumns[0],
                                NameEN = linecolumns[1]
                            });
                            count++;
                        }
                    }
                    if (line.Contains("Parameter(s):"))
                    {
                        begin = true;
                    }
                }
                db.SaveChanges();
                db.Dispose();
                GC.Collect();
            }

            TimeSpan time = DateTime.Now - start;

            report        += "<br/>Time: " + time.ToString() + "<br/>Count: " + count.ToString() + "<br/>";
            ViewBag.Report = report;
            return(View());
        }
        public async Task <ActionResult> Edit([Bind(Include = "Id,MeteoDataSourceId,MeteoDataPeriodicityId,Code,GroupEN,GroupKZ,GroupRU,NameEN,NameKZ,NameRU,AdditionalEN,AdditionalKZ,AdditionalRU,DescriptionEN,DescriptionKZ,DescriptionRU")] MeteoDataType meteoDataType)
        {
            if (ModelState.IsValid)
            {
                MeteoDataType mdt = null;
                using (var dblocal = new NpgsqlContext())
                {
                    mdt = dblocal.MeteoDataTypes.Where(m => m.Id == meteoDataType.Id).FirstOrDefault();
                    dblocal.Dispose();
                    GC.Collect();
                }

                meteoDataType.Code         = mdt.Code;
                meteoDataType.AdditionalEN = mdt.AdditionalEN;
                meteoDataType.GroupEN      = mdt.GroupEN;

                db.Entry(meteoDataType).State = EntityState.Modified;
                await db.SaveChangesAsync();


                string comment = $"Id: {mdt.Id} MeteoDataSourceId: {mdt.MeteoDataSourceId} MeteoDataPeriodicityId: {mdt.MeteoDataPeriodicityId} Code: {mdt.Code}";
                if (!string.IsNullOrEmpty(mdt.GroupEN))
                {
                    comment += $" GroupEN: {mdt.GroupEN} GroupKZ: {mdt.GroupKZ} GroupRU: {mdt.GroupRU}";
                }
                comment += $" NameEN: {mdt.NameEN} NameKZ: {mdt.NameKZ} NameRU: {mdt.NameRU}";
                if (!string.IsNullOrEmpty(mdt.AdditionalEN))
                {
                    comment += $" AdditionalEN: {mdt.AdditionalEN}";
                }
                if (!string.IsNullOrEmpty(mdt.AdditionalKZ))
                {
                    comment += $" AdditionalKZ: {mdt.AdditionalKZ}";
                }
                if (!string.IsNullOrEmpty(mdt.AdditionalRU))
                {
                    comment += $" AdditionalRU: {mdt.AdditionalRU}";
                }
                if (!string.IsNullOrEmpty(mdt.DescriptionEN))
                {
                    comment += $" DescriptionEN: {mdt.DescriptionEN} DescriptionKZ: {mdt.DescriptionKZ} DescriptionRU: {mdt.DescriptionRU}";
                }
                comment += $" -> MeteoDataSourceId: {meteoDataType.MeteoDataSourceId} MeteoDataPeriodicityId: {meteoDataType.MeteoDataPeriodicityId} Code: {meteoDataType.Code}";
                if (!string.IsNullOrEmpty(meteoDataType.GroupEN))
                {
                    comment += $" GroupEN: {meteoDataType.GroupEN} GroupKZ: {meteoDataType.GroupKZ} GroupRU: {meteoDataType.GroupRU}";
                }
                comment += $" NameEN: {meteoDataType.NameEN} NameKZ: {meteoDataType.NameKZ} NameRU: {meteoDataType.NameRU}";
                if (!string.IsNullOrEmpty(meteoDataType.AdditionalEN))
                {
                    comment += $" AdditionalEN: {meteoDataType.AdditionalEN}";
                }
                if (!string.IsNullOrEmpty(meteoDataType.AdditionalKZ))
                {
                    comment += $" AdditionalKZ: {meteoDataType.AdditionalKZ}";
                }
                if (!string.IsNullOrEmpty(meteoDataType.AdditionalRU))
                {
                    comment += $" AdditionalRU: {meteoDataType.AdditionalRU}";
                }
                if (!string.IsNullOrEmpty(meteoDataType.DescriptionEN))
                {
                    comment += $" DescriptionEN: {meteoDataType.DescriptionEN} DescriptionKZ: {meteoDataType.DescriptionKZ} DescriptionRU: {meteoDataType.DescriptionRU}";
                }

                SystemLog.New("MeteoDataTypeEdit", comment, null, false);

                return(RedirectToAction("Index"));
            }

            ViewBag.MeteoDataSourceId = new SelectList(db.MeteoDataSources
                                                       .ToList()
                                                       .OrderBy(m => m.Name), "Id", "Name", meteoDataType.MeteoDataSourceId);
            ViewBag.MeteoDataPeriodicityId = new SelectList(db.MeteoDataPeriodicities
                                                            .ToList()
                                                            .OrderBy(m => m.Name), "Id", "Name", meteoDataType.MeteoDataPeriodicityId);
            return(View(meteoDataType));
        }
        public ActionResult Upload(IEnumerable <HttpPostedFileBase> Files)
        {
            string   report = "";
            DateTime start  = DateTime.Now;
            int      count  = 0;

            string CurrentUserId = User.Identity.GetUserId();
            string path          = "~/Upload/" + CurrentUserId;

            bool error = false;

            try
            {
                if (!Directory.Exists(Server.MapPath(path)))
                {
                    DirectoryInfo di = Directory.CreateDirectory(Server.MapPath(path));
                }
            }
            catch (Exception e)
            {
                report += "<br/>" + e.Message;
                error   = true;
            }

            if (!error)
            {
                foreach (HttpPostedFileBase file in Files)
                {
                    string path_filename = Path.Combine(Server.MapPath(path), Path.GetFileName(file.FileName));
                    file.SaveAs(path_filename);
                }

                string       batfilename = Path.ChangeExtension(Path.Combine(Server.MapPath(path), DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss")), ".bat");
                StreamWriter bat         = new StreamWriter(batfilename);
                foreach (HttpPostedFileBase file in Files)
                {
                    string filename = Path.GetFileNameWithoutExtension(file.FileName);
                    bat.WriteLine("ncdump -f c " + filename + ".nc > " + filename + ".txt");
                }
                bat.Close();
                Process proc = new System.Diagnostics.Process();
                proc.StartInfo.FileName         = batfilename;
                proc.StartInfo.Arguments        = "/c start /wait " + Path.GetFileName(batfilename);
                proc.StartInfo.WorkingDirectory = Path.GetDirectoryName(batfilename);
                proc.Start();
                proc.WaitForExit();
                // loading data from files
                foreach (HttpPostedFileBase file in Files)
                {
                    string filenameout = Path.Combine(Server.MapPath(path), Path.GetFileName(Path.ChangeExtension(Path.GetFileNameWithoutExtension(file.FileName) + "_out", "txt")));

                    List <decimal>  lats  = new List <decimal>();
                    List <decimal>  lons  = new List <decimal>();
                    List <DateTime> times = new List <DateTime>();
                    string[]        lines = System.IO.File.ReadAllLines(Path.ChangeExtension(Path.Combine(Server.MapPath(path), Path.GetFileName(file.FileName)), "txt"));

                    bool variables         = false,
                         global_attributes = false,
                         data = false;

                    List <string> meteodatatypecodes   = new List <string>();
                    List <string> meteodatatypenameENs = new List <string>();

                    string meteodataperiodicitycode = "";
                    string meteodatasourcecode      = "SARAH-E";
                    int    meteodatatypeid          = -1;
                    using (var db = new NpgsqlContext())
                    {
                        db.Configuration.AutoDetectChangesEnabled = false;
                        db.Configuration.ValidateOnSaveEnabled    = false;

                        List <MeteoDataType> meteodatatypes = db.MeteoDataTypes.ToList();

                        int meteodatasourceid      = -1;
                        int meteodataperiodicityid = -1;
                        int meteodatatypescount    = -1;

                        using (StreamWriter sw = System.IO.File.AppendText(filenameout))
                        {
                            foreach (string line in lines)
                            {
                                if (variables)
                                {
                                    if (line.Contains("time, lat, lon"))
                                    {
                                        meteodatatypecodes.Add(line.Split(' ')[1].Split('(')[0].Trim());
                                        meteodatatypenameENs.Add("");
                                    }
                                    if (line.Contains("long_name"))
                                    {
                                        for (int i = 0; i < meteodatatypecodes.Count; i++)
                                        {
                                            if (line.Contains(meteodatatypecodes[i]))
                                            {
                                                string newname = line.Split('"')[1];
                                                newname = newname.First().ToString().ToUpper() + newname.Substring(1);
                                                meteodatatypenameENs[i] = newname;
                                                break;
                                            }
                                        }
                                    }
                                    for (int i = 0; i < meteodatatypecodes.Count; i++)
                                    {
                                        if (line.Contains(meteodatatypecodes[i] + ":units") && meteodatatypecodes[i] != "")
                                        {
                                            if (line.Contains(meteodatatypecodes[i]))
                                            {
                                                meteodatatypenameENs[i] += " " + line.Split('"')[1];
                                                break;
                                            }
                                        }
                                    }
                                }
                                if (global_attributes)
                                {
                                    if (line.Contains(":time_coverage_resolution"))
                                    {
                                        meteodataperiodicitycode = line.Split('"')[1];
                                        switch (line.Split('"')[1])
                                        {
                                        case "P1M":
                                            meteodataperiodicitycode = "Monthly";
                                            break;

                                        case "P1D":
                                            meteodataperiodicitycode = "Daily";
                                            break;

                                        case "PT1H":
                                            meteodataperiodicitycode = "Hourly";
                                            break;

                                        default:
                                            meteodataperiodicitycode = "";
                                            break;
                                        }
                                    }
                                }
                                if (data)
                                {
                                    if (meteodatasourceid < 0)
                                    {
                                        meteodatasourceid = db.MeteoDataSources
                                                            .Where(m => m.Code == meteodatasourcecode)
                                                            .FirstOrDefault()
                                                            .Id;
                                        meteodataperiodicityid = db.MeteoDataPeriodicities
                                                                 .Where(m => m.Code == meteodataperiodicitycode)
                                                                 .FirstOrDefault()
                                                                 .Id;
                                        for (int i = 0; i < meteodatatypecodes.Count; i++)
                                        {
                                            string meteodatatypecode = meteodatatypecodes[i];
                                            meteodatatypescount = db.MeteoDataTypes
                                                                  .Where(m => m.Code == meteodatatypecode && m.MeteoDataSourceId == meteodatasourceid && m.MeteoDataPeriodicityId == meteodataperiodicityid)
                                                                  .Count();
                                            if (meteodatatypescount == 0)
                                            {
                                                db.MeteoDataTypes.Add(new MeteoDataType()
                                                {
                                                    MeteoDataSourceId      = meteodatasourceid,
                                                    MeteoDataPeriodicityId = meteodataperiodicityid,
                                                    Code   = meteodatatypecodes[i],
                                                    NameEN = meteodatatypenameENs[i]
                                                });
                                                db.SaveChanges();
                                            }
                                        }
                                        meteodatatypes = db.MeteoDataTypes.ToList();
                                    }

                                    if (line.Contains("lon"))
                                    {
                                        if (line.Contains("lon ="))
                                        {
                                            lons.Add(Convert.ToDecimal(line.Replace("lon =", "").Split(',')[0].Trim().Replace('.', ',')));
                                        }
                                        else
                                        {
                                            if (line.Contains(","))
                                            {
                                                lons.Add(Convert.ToDecimal(line.Split(',')[0].Trim().Replace('.', ',')));
                                            }
                                            if (line.Contains(";"))
                                            {
                                                lons.Add(Convert.ToDecimal(line.Split(';')[0].Trim().Replace('.', ',')));
                                            }
                                        }
                                    }
                                    if (line.Contains("lat"))
                                    {
                                        if (line.Contains("lat ="))
                                        {
                                            lats.Add(Convert.ToDecimal(line.Replace("lat =", "").Split(',')[0].Trim().Replace('.', ',')));
                                        }
                                        else
                                        {
                                            if (line.Contains(","))
                                            {
                                                lats.Add(Convert.ToDecimal(line.Split(',')[0].Trim().Replace('.', ',')));
                                            }
                                            if (line.Contains(";"))
                                            {
                                                lats.Add(Convert.ToDecimal(line.Split(';')[0].Trim().Replace('.', ',')));
                                            }
                                        }
                                    }
                                    if (line.Contains("time"))
                                    {
                                        if (line.Contains("time ="))
                                        {
                                            if (line.Contains(","))
                                            {
                                                times.Add(new DateTime(1983, 1, 1).AddHours(Convert.ToInt32(line.Replace("time =", "").Split(',')[0].Trim())));
                                            }
                                            if (line.Contains(";"))
                                            {
                                                times.Add(new DateTime(1983, 1, 1).AddHours(Convert.ToInt32(line.Replace("time =", "").Split(';')[0].Trim())));
                                            }
                                        }
                                        else
                                        {
                                            if (line.Contains(","))
                                            {
                                                times.Add(new DateTime(1983, 1, 1).AddHours(Convert.ToInt32(line.Split(',')[0].Trim())));
                                            }
                                            if (line.Contains(";"))
                                            {
                                                times.Add(new DateTime(1983, 1, 1).AddHours(Convert.ToInt32(line.Split(';')[0].Trim())));
                                            }
                                        }
                                    }
                                    for (int i = 0; i < meteodatatypecodes.Count; i++)
                                    {
                                        if (line.Contains(meteodatatypecodes[i]) && !line.Contains("="))
                                        {
                                            meteodatatypeid = meteodatatypes
                                                              .Where(m => m.Code == meteodatatypecodes[i] && m.MeteoDataSourceId == meteodatasourceid && m.MeteoDataPeriodicityId == meteodataperiodicityid)
                                                              .FirstOrDefault()
                                                              .Id;

                                            string p = line.Split('(')[1];
                                            p = p.Substring(0, p.Length - 1);
                                            string[] pi = p.Split(',');
                                            string   v  = line.Trim().Split(' ')[0];
                                            v = v.Substring(0, v.Length - 1);
                                            decimal value = Convert.ToDecimal(v);

                                            sw.WriteLine(meteodatatypeid.ToString() + "\t" +
                                                         times[Convert.ToInt32(pi[0])].Year.ToString() + "\t" +
                                                         times[Convert.ToInt32(pi[0])].Month.ToString() + "\t" +
                                                         (meteodataperiodicitycode == "Daily" || meteodataperiodicitycode == "Hourly" ? (int?)times[Convert.ToInt32(pi[0])].Day : null).ToString() + "\t" +
                                                         (meteodataperiodicitycode == "Hourly" ? (int?)times[Convert.ToInt32(pi[0])].Hour : null).ToString() + "\t" +
                                                         lons[Convert.ToInt32(pi[2])].ToString().Replace(',', '.') + "\t" +
                                                         lats[Convert.ToInt32(pi[1])].ToString().Replace(',', '.') + "\t" +
                                                         value.ToString().Replace(',', '.')
                                                         );
                                            count++;
                                        }
                                    }
                                }
                                if (line.Contains("variables"))
                                {
                                    variables         = true;
                                    global_attributes = false;
                                    data = false;
                                }
                                if (line.Contains("global attributes"))
                                {
                                    variables         = false;
                                    global_attributes = true;
                                    data = false;
                                }
                                if (line == "data:")
                                {
                                    variables         = false;
                                    global_attributes = false;
                                    data = true;
                                }
                            }
                        }
                        string query = "COPY \"MeteoData\" (\"MeteoDataTypeId\", \"Year\", \"Month\", \"Day\", \"Hour\", \"Longitude\", \"Latitude\", \"Value\") FROM '" + filenameout + "' WITH NULL AS ''";
                        try
                        {
                            db.MeteoDatas.SqlQuery(query).SingleOrDefault();
                        }
                        catch (Exception ex)
                        {
                            if (ex.Message != "The data reader is incompatible with the specified 'AtlasSolar.Models.MeteoData'. A member of the type, 'Id', does not have a corresponding column in the data reader with the same name.")
                            {
                                int re = 5;
                            }
                        }
                        db.Dispose();
                        GC.Collect();
                    }
                }
                // file deletion
                try
                {
                    System.IO.File.Delete(batfilename);
                }
                catch (Exception ex) { }
                foreach (HttpPostedFileBase file in Files)
                {
                    string path_filename = Path.Combine(Server.MapPath(path), Path.GetFileName(file.FileName));
                    string filenameout   = Path.Combine(Server.MapPath(path), Path.GetFileName(Path.ChangeExtension(Path.GetFileNameWithoutExtension(file.FileName) + "_out", "txt")));
                    try
                    {
                        System.IO.File.Delete(path_filename);
                    }
                    catch (Exception ex) { }
                    try
                    {
                        System.IO.File.Delete(Path.ChangeExtension(path_filename, "txt"));
                    }
                    catch (Exception ex) { }
                    try
                    {
                        System.IO.File.Delete(filenameout);
                    }
                    catch (Exception ex) { }
                }
            }

            TimeSpan time = DateTime.Now - start;

            report        += "<br/>Time: " + time.ToString() + "<br/>Count: " + count.ToString();
            ViewBag.Report = report;
            return(View());
        }