string datafilepath = @"D:\CAMVS\CAMVS\bin\Debug\Data\20110804\NumbericFile\wrfout_d01_2011-08-24_18_00_00.nc";
// string datafilepath1 = @"D:\CAMVS\CAMVS\bin\Debug\Data\20110804\NumbericFile\wrfout_d01_2011-08-24_18_00_temp.nc";
string var = "T";
string varpath = "NETCDF:\"" + datafilepath + "\":" + var;
string varpath1 = "ss.tif";
Gdal.AllRegister();
OSGeo.OGR.Ogr.RegisterAll();
Dataset ds = Gdal.Open(varpath, Access.GA_ReadOnly);
//从shp数据中获取投影坐标信息
OSGeo.OGR.Driver poDriver = OSGeo.OGR.Ogr.GetDriverByName("ESRI Shapefile");
OSGeo.OGR.DataSource src = poDriver.Open(@"D:\lambert\country2.shp", 0);
OSGeo.OSR.SpatialReference sprfc = src.GetLayerByIndex(0).GetSpatialRef();
string wktstr = "";
sprfc.ExportToWkt(out wktstr);
string lambert = wktstr;
GeographicCoordinateSystem fromCS = GeographicCoordinateSystem.WGS84;
IProjectedCoordinateSystem toCS = CoordinateSystemWktReader.Parse(lambert) as IProjectedCoordinateSystem;
//计算左上角的空间地理坐标信息
double[] xy = new double[] { 110.399994, 39.900005 };
List<double[]> pts = new List<double[]>();
pts.Add(xy);
List<double[]> results = new List<double[]>();
PtsTransform.PtsToPts(fromCS, toCS, pts, out results);
double x = 0;
double y = 0;
x = results[0][0];
y = results[0][1];
double[] bound = ComputeBound(x, y, 387, 250, 36000);
x = bound[0];
y = bound[3];
double[] arg = new double[6] { x, 36000, 0.0, y, 0.0, 36000 };
// ds.SetGeoTransform(arg);
// ds.SetProjection(wktstr);
Band band = ds.GetRasterBand(1);
//数据的类型如果设置成float生成的图像,可能会导致在设置投影时出错,导致arcgis也不能够识别这个图像
double[] buffer = new double[band.XSize * band.YSize];
NumberFile.GetVarBandData(datafilepath, "T", 1, band.XSize, band.YSize, buffer);
string[] option = { "INTERLEAVE=PIXEL" };
Dataset todataset = Gdal.GetDriverByName("GTiff").Create(varpath1, band.XSize, band.YSize, 1, DataType.GDT_Float32, option);
//设置起始点坐标信息及栅格大小和投影坐标系
todataset.SetGeoTransform(arg);
todataset.SetProjection(wktstr);
todataset.GetRasterBand(1).WriteRaster(0, 0, band.XSize, band.YSize, buffer, band.XSize, band.YSize, 0, 0);