public void Open() { Debug.Assert(_numOpenFiles == 0, "Another NetCDF file is still open!"); NetCDF.ResultCode result = NetCDF.nc_open(_fileDirectory, NetCDF.CreateMode.NC_NOWRITE, out _fileID); Debug.Assert(result == NetCDF.ResultCode.NC_NOERR, result.ToString()); result = NetCDF.nc_inq_nvars(_fileID, out _numVars); Debug.Assert(result == NetCDF.ResultCode.NC_NOERR, result.ToString()); _numOpenFiles++; }
/// <summary> /// Load a slice from the file. /// </summary> /// <param name="slice">Carries variable to load, dimensions in file and what to load.</param> /// <returns></returns> public override ScalarField LoadFieldSlice(SliceRange slice) { ScalarField field; Index offsets = new Index(slice.GetOffsets()); NetCDF.ResultCode ncState = NetCDF.ResultCode.NC_NOERR; //int[] sizeInFile = new int[offsets.Length]; int[] sizeInFile = slice.GetLengths(); // Probably has less dimensions. int[] sizeField = new int[offsets.Length]; int numDimsField = 0; //int currDimSlice = 0; for (int dim = 0; dim < offsets.Length; ++dim) { if (offsets[dim] != -1 && sizeInFile[dim] > 1) { sizeField[numDimsField++] = sizeInFile[dim]; } // Include whole dimension. else if (offsets[dim] == -1) { // Fill size. int sizeDim; ncState = NetCDF.nc_inq_dimlen(_fileID, slice.GetDimensionID(dim), out sizeDim); Debug.Assert(ncState == NetCDF.ResultCode.NC_NOERR); sizeInFile[dim] = sizeDim; // Set offset to one. offset = 0, size = size of dimension. offsets[dim] = 0; // Save size in size-vector for the scalar field. sizeField[numDimsField++] = sizeDim; } } //if (slice.IsTimeDependent()) // numDimsField++; // Generate size index for field class. Index fieldSize = new Index(numDimsField); Array.Copy(sizeField, fieldSize.Data, numDimsField); // When the field has several time slices, add a time dimension. //if (slice.IsTimeDependent()) // fieldSize[numDimsField - 1] = slice.GetNumTimeSlices(); // Change order of dimensions, so that fastest dimension is at the end. for (int dim = 0; dim < fieldSize.Length / 2; ++dim) { int tmp = fieldSize[dim]; fieldSize[dim] = fieldSize[fieldSize.Length - 1 - dim]; fieldSize[fieldSize.Length - 1 - dim] = tmp; } // Create a grid descriptor for the field. // TODO: Actually load this data. RectlinearGrid grid = new RectlinearGrid(fieldSize);//, new Vector(0.0f, fieldSize.Length), new Vector(0.1f, fieldSize.Length)); // Create scalar field instance and fill it with data. field = new ScalarField(grid); int sliceSize = grid.Size.Product();// / slice.GetNumTimeSlices(); // Get data. x64 dll fails in debug here... ncState = NetCDF.nc_get_vara_float(_fileID, (int)slice.GetVariable(), offsets.Data, sizeInFile, field.Data); Debug.Assert(ncState == NetCDF.ResultCode.NC_NOERR, ncState.ToString()); // Read in invalid value. float[] invalidval = new float[1]; ncState = NetCDF.nc_get_att_float(_fileID, (int)slice.GetVariable(), "_FillValue", invalidval); field.InvalidValue = invalidval[0]; return(field); }