1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| public bool TifToNC3DSingleVarByYear(string TifPath, string NcPath, int year) { bool flag = false; try { List<String> tifList = Directory.GetFiles(TifPath, "*.tif").ToList(); Dataset dataset = Gdal.Open(tifList[0], Access.GA_ReadOnly); int width = dataset.RasterXSize; int heigh = dataset.RasterYSize; int timeCount = tifList.Count; int ncid; int result = NetCDF.nc_create(NcPath, NetCDF.CreateMode.NC_NETCDF4, out ncid); int lonDimidp; result = NetCDF.nc_def_dim(ncid, "longitude", width, out lonDimidp); int latDimidp; result = NetCDF.nc_def_dim(ncid, "latitude", heigh, out latDimidp); result = NetCDF.nc_def_var(ncid, "longitude", NetCDF.NcType.NC_FLOAT, 1, new int[] { lonDimidp }, out int lonVaridp); result = NetCDF.nc_def_var(ncid, "latitude", NetCDF.NcType.NC_FLOAT, 1, new int[] { latDimidp }, out int latVaridp); float[] lon = new float[7200]; for (int i = 0; i < 7200; i++) { lon[i] = -180 + 0.05f * i; } float[] lat = new float[3600]; for (int i = 0; i < 3600; i++) { lat[i] = 90 - 0.05f * i; } NetCDF.nc_put_var_float(ncid, lonVaridp, lon); NetCDF.nc_put_att_text(ncid, lonVaridp, "units", "degrees_east".Length, "degrees_east"); NetCDF.nc_put_att_text(ncid, lonVaridp, "long_name", "longitude".Length, "longitude"); NetCDF.nc_put_var_float(ncid, latVaridp, lat); NetCDF.nc_put_att_text(ncid, latVaridp, "units", "degrees_north".Length, "degrees_north"); NetCDF.nc_put_att_text(ncid, latVaridp, "long_name", "latitude".Length, "latitude"); result = put_var_year("time", "snw", tifList, ref dataset, ncid, lonDimidp, latDimidp); result = NetCDF.nc_close(ncid); flag = true; } catch (Exception ex) { flag = false; } return flag; }
private int put_var_year(string dimname, string varname, List<string> tifList, ref Dataset dataset, int ncid, int lonDimidp, int latDimidp) { tifList = tifList.OrderBy(p => p).ToList(); int timeCount = tifList.Count; int[] time = new int[timeCount]; string timeStr = Path.GetFileName(tifList[0]).Split("_")[3]; DateTime firstDay = DateTime.ParseExact(timeStr, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture); int firstDaysInt = (firstDay - new DateTime(1900, 1, 1)).Days; for (int i = 0; i < timeCount; i++) { time[i] = i + firstDaysInt; } int result; int timeDimidp; result = NetCDF.nc_def_dim(ncid, dimname, timeCount, out timeDimidp); result = NetCDF.nc_def_var(ncid, dimname, NetCDF.NcType.NC_INT, 1, new int[] { timeDimidp }, out int timeVaridp); NetCDF.nc_put_var_int(ncid, timeVaridp, time); NetCDF.nc_put_att_text(ncid, timeVaridp, "units", "days since 1900-01-01 00:00:0.0".Length, "days since 1900-01-01 00:00:0.0"); NetCDF.nc_put_att_text(ncid, timeVaridp, "long_name", dimname.Length, dimname); NetCDF.nc_put_att_text(ncid, timeVaridp, "calendar", "gregorian".Length, "gregorian"); int varidp; int[] dimids = new int[3]; dimids[0] = timeDimidp; dimids[1] = latDimidp; dimids[2] = lonDimidp; result = NetCDF.nc_def_var(ncid, varname, NetCDF.NcType.NC_BYTE, 3, dimids, out varidp); result = NetCDF.nc_def_var_deflate(ncid, varidp, 0, 1, 5); result = NetCDF.nc_enddef(ncid); LongArray<byte> buffer = new LongArray<byte>(timeCount * 3600L * 7200L); for (int i = 0; i < timeCount; i++) { dataset = Gdal.OpenShared(tifList[i], Access.GA_ReadOnly); Band srcBand = dataset.GetRasterBand(1); int[,] writeBuffer = ReadBandToArray<int>(srcBand); for (int m = 0; m < 3600; m++) { for (int n = 0; n < 7200; n++) { if (writeBuffer[m, n] == 200) buffer[i*3600L*7200L+m*7200L+n] = 1; } } } result = NetCDF.nc_put_var_ubyte(ncid, varidp, buffer._getAddress(0)); buffer.Dispose(); return result; }
|