前言
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)
{
}
}