/// <summary> /// Makes a snow raster in bgd format from the downloaded SNODAS data file /// </summary> /// <param name="snodasDatFile">the SNODAS unzipped data file in .dat format</param> /// <param name="bgdFile">the output bgd data file</param> /// <returns>The IRaster object</returns> public IRaster MakeSnowRaster(string snodasDatFile, string bgdFile) { //for masked case rows and columns: int numCols = 6935; int numRows = 3351; //for masked case lat and long bounding box: Extent maskedExtent = new Extent(-124.7337, 24.9504, -66.9421, 52.8754); int numCells = numCols * numRows; //read the SNODAS .dat binary file into an array Int16[] binaryData = BinaryFileHelper.ReadBinaryArray(snodasDatFile, numCells); IRaster ras = Raster.Create(bgdFile, "bgd", numCols, numRows, 1, typeof(Int16), new string[0]); //set the properties of our raster: bounds, projection, NoDataValue ras.Bounds = new RasterBounds(numRows, numCols, maskedExtent); ras.ProjectionString = "+proj=longlat +datum=WGS84 +no_defs"; ras.NoDataValue = -9999; //set the raster's values for (int row = 0; row < numRows; row++) { for (int col = 0; col < numCols; col++) { ras.Value[row, col] = binaryData[row * numCols + col]; } } ras.Save(); return(ras); }
public static Int16[] ReadBinaryArray(string fileName, int numCells) { int numBytes = numCells * 2; //reading the file. The data is Big-Endian using (Stream stream = new FileStream(fileName, FileMode.Open)) { byte[] rawData = BinaryFileHelper.ReadFully(stream, numBytes); //we need to swap the byte order in the raw data Swap(rawData); Int16[] myArray = new Int16[numCells]; Buffer.BlockCopy(rawData, 0, myArray, 0, numBytes); return(myArray); } }