ERA5数据(.nc格式)导出至TIFF格式(基于ArcEngine+C#)

ERA5数据 nc格式导出至TIFF格式(基于ArcEngine+C#)

前言

C# + AE ,ERA5数据(.nc格式)导出至TIFF格式
era5再分析气象数据,nc格式,分辨率:daily,0.25°
命名:yyyymmdd_后缀.tif

代码

private void button40_Click(object sender, EventArgs e)
                {
                    
                    string[] Findfiles;
                    Findfiles = Directory.GetFiles(@"E:\cai", "*wind.nc");
                    string name = "";
                    string outname = "";
                    int k = 0;
                    string type = "WIN";
        
                    for (int i = 0; i < Findfiles.Count(); i++)
                    {
                        string year = System.IO.Path.GetFileName(Findfiles[i]).Substring(0,4);//对应年份
        
                        IWorkspaceFactory wsf = new NetCDFWorkspaceFactory();
                        IWorkspace workspace;
                        workspace = wsf.OpenFromFile(Findfiles[i], 0);
                        INetCDFWorkspace nc = (INetCDFWorkspace)workspace;
                        IStringArray vararr = nc.GetVariables();
                        IMDRasterDatasetView mdrasview = new NetCDFRasterDatasetNameClass();
                        mdrasview.Variable = "i10fg";                      //sfr-sp;  spt-d2m;  sra-msdrswrf;  tmp-t2m;   wind-i10fg 
                        mdrasview.XDimension = "longitude";
                        mdrasview.YDimension = "latitude";
                        mdrasview.BandDimension = "time";
                        IMDWorkspace mdwksp = nc as IMDWorkspace;
                        IRasterDataset rasterdataset = mdwksp.CreateView("a", (IMDDatasetView)mdrasview) as IRasterDataset;
                        IRasterBandCollection bands = rasterdataset as IRasterBandCollection;
        
                        IRaster raster = rasterdataset.CreateDefaultRaster();
                        IRasterProps rasterProps = (IRasterProps)raster;
                        IPnt pBlockSize = new Pnt();
                        pBlockSize.SetCoords(rasterProps.Width, rasterProps.Height);
                        IPixelBlock pPixelBlock = raster.CreatePixelBlock(pBlockSize);
                        IPnt tlp = new Pnt();
                        tlp.SetCoords(0, 0);
                        //IRasterBandCollection pRasterBands = rasterdataset as IRasterBandCollection;
                        //int NumBand = pRasterBands.Count;
                        //string year = name.Split('_')[3];
                        //string mouth = System.IO.Path.GetFileNameWithoutExtension(Findfiles[i]).Split('_')[4];
                        //IRasterBand pRasterBand;
                        IRawPixels pRawRixels;
        
                        for (int j = 0; j < bands.Count; j++)  //波段数
                        {
                            pRawRixels = bands.Item(j) as IRawPixels;   //第j个波段
                            pRawRixels.Read(tlp, pPixelBlock);
                            if (year.Equals("2004") || year.Equals("2008") || year.Equals("2012"))  //闰年
                        {
                            if (j <= 30)
                            {
                                //一月
                                int daynum = j + 1;
                                string day = "";
                                if (daynum < 10)
                                {
                                    day = "0" + daynum.ToString();
                                }
                                else
                                {
                                    day = daynum.ToString();
                                }
                                //string day = (j + 1).ToString();
                                outname = year + "01" + day + "_"+type+".tif";
                                CreateRasterDataset_3(pPixelBlock, @"E:\cai", outname);
    
                            }    //1,3,5,7,8,10,12
                            else if (j <= 59)
                            {
                                //2月
                                int daynum = j - 30;
                                string day = "";
                                if (daynum < 10)
                                {
                                    day = "0" + daynum.ToString();
                                }
                                else
                                {
                                    day = daynum.ToString();
                                }
                                //string day = (j + 1).ToString();
                                outname = year + "02" + day + "_" + type + ".tif";
                                CreateRasterDataset_3(pPixelBlock, @"E:\cai", outname);
                            }
                            else if (j <= 90){}.......
                            }
                           else   //非闰年 365天
                        { 
                            if (j <= 30)
                            {
                                //一月
                                int daynum = j + 1;
                                string day = "";
                                if (daynum < 10)
                                {
                                    day = "0" + daynum.ToString();
                                }
                                else
                                {
                                    day = daynum.ToString();
                                }
                                //string day = (j + 1).ToString();
                                outname = year + "01" + day + "_" + type + ".tif";
                                CreateRasterDataset_3(pPixelBlock, @"E:\cai", outname);
    
                            }    //1,3,5,7,8,10,12
                            else if (j <= 58)
                            {
                                //2月
                                int daynum = j - 30;
                                string day = "";
                                if (daynum < 10)
                                {
                                    day = "0" + daynum.ToString();
                                }
                                else
                                {
                                    day = daynum.ToString();
                                }
                                //string day = (j + 1).ToString();
                                outname = year + "02" + day + "_" + type + ".tif";
                                CreateRasterDataset_3(pPixelBlock, @"E:\cai", outname);
                            }
                            else if (j <= 89){}......
                            }
                            }
                            }
                            
     public static void CreateRasterDataset_3(IPixelBlock pixelblock, string path, string fileName)
            {
                try
                {
                    IRasterWorkspace2 rasterWorkSpace;
                    IWorkspaceFactory workspaceFact = new RasterWorkspaceFactoryClass();
                    rasterWorkSpace = workspaceFact.OpenFromFile(path, 0) as IRasterWorkspace2;
    
    
    
                    ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
                    ISpatialReference spatialReference = spatialReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);//定义空间参考
                    IPoint OrigionalPoint = new PointClass();
                    OrigionalPoint.PutCoords(-1.0 / 8, -90 + (-1.0 / 8));
                    int width = 1440;
                    int height = 721;
                    double xcell = 1.0 / 4;
                    double ycell = 1.0 / 4;
    
                    IRasterDataset rasterDataset = rasterWorkSpace.CreateRasterDataset(fileName, "TIFF", OrigionalPoint, width, height, xcell, ycell, 1, rstPixelType.PT_DOUBLE, spatialReference, true);
    
                    IRasterBandCollection rasterBands = (IRasterBandCollection)rasterDataset;
                    IRasterBand rasterBand;
                    IRasterProps rasterProps;
                    rasterBand = rasterBands.Item(0);
                    rasterProps = (IRasterProps)rasterBand;
                    //Set NoData if necessary. For a multiband image, a NoData value needs to be set for each band.
                    rasterProps.NoDataValue = -3.40282346639e+038;
                    //Create a raster from the dataset.
                    IRaster raster = rasterDataset.CreateDefaultRaster();
    
                    System.Array pSafeArray = pixelblock.get_SafeArray(0) as System.Array;            //原像元数据
    
    
                    IPnt blocksize = new PntClass();
                    blocksize.SetCoords(width, height);
                    IPixelBlock3 pixelblock11 = raster.CreatePixelBlock(blocksize) as IPixelBlock3;
                    System.Array pixels;
                    pixels = (System.Array)pixelblock11.get_PixelData(0);
                    for (int y = 0; y < rasterProps.Height; y++)
                    {
                        for (int x = 0; x < rasterProps.Width; x++)
                        {
                            if (Math.Abs(Convert.ToDouble(pSafeArray.GetValue(x, y)) - (-3.40282346639e+038)) < 10e-5)
                            {
                                pixels.SetValue(-3.40282346639e+038, x, y);
                            }
                            else
                            {
                                pixels.SetValue(Convert.ToDouble(pSafeArray.GetValue(x, y)), x, y);
                            }
                        }
                    }
                    pixelblock11.set_PixelData(0, (System.Array)pixels);
    
                    IPnt upperLeft = new PntClass();
                    upperLeft.SetCoords(0, 0);
                    IRasterEdit rasterEdit = (IRasterEdit)raster;
                    rasterEdit.Write(upperLeft, (PixelBlock)pixelblock11);
                    rasterEdit.Refresh();
                    GC.Collect();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterEdit);              //Release rasterEdit explicitly. 
    
                }
                catch (Exception e)
                {
    
                }
            }
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cshgiser

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值