I would recommend looking into rioxarray for your dataset.
You can open your dataset like so:
xds = xarray.open_dataset('D:\Weather data\et_01012018.nc')
If your CRS is not discovered, you should be able to add it like so:
Then, you should be able to create a geotiff ...
Xarray is (intentionally) ignorant of coordinate systems, so it has no special handling for cyclic coordinates such as longitude. Because your longitude array has only increasing values, xarray interprets selections like slice(40, -80) in the same way that x[i:j] works if x is a NumPy array and i > j >= 0, and thus returns an empty selection.
I figured out the answer some time ago but I hope this will help other people that are dipping their toes into the NetCDF file format.
I found that the easiest way to create a new NetCDF file containing several GeoTIFF files is using GDAL and then using NCO and CDO tools (Provided by unidata) to add the appropriate metadata.
First you convert the GeoTIFF´s ...
To get the appropriate cell coordinates from your latitude and longitude you need to know the coverage of the netCDF file (for example, the coordinate of the upper left cell and the size of the cells in the x and y directions). @Luke 's solution below works for a srtaight x/y raster with no cell rotation. If you don't have that, you could look at the affine ...
So you have 3D data (lon,lat,time) in a netcdf file and you want to extract a time-series as a specific location in Python, right?
Here's one way using netCDF4:
But it's way easier using Xarray:
import xarray as xr
Here is an example of using rioxarray to mask out data with a shapefile:
from shapely.geometry import mapping
MSWEP_monthly2 = xarray.open_dataarray('D:\G3P\DATA\Models\MSWEP\MSWEP_monthly.nc4')
The Norwegian Met office has a THREDDS server at http://thredds.met.no/thredds/
so if you see the forecast you are trying to access there, you can extract just the subset you want from the OPeNDAP URL, which NetCDF4-Python treats like a local netcdf file.
url = 'http://thredds.met.no/thredds/dodsC/arome25/...
The metadata of the netcdf file includes projection information, which can be identified by QGIS as a custom CRS:
+proj=lcc +lat_1=50 +lat_2=50 +lat_0=50 +lon_0=-107 +x_0=5632642.22547 +y_0=4612545.65137 +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
but there is no extent given in projection units, only latlon for the corners.
If you run gdalinfo ...
Use this to get access to all bands
lradin <- brick("lrad_ITPCAS-CMFD_V0106_B-01_197912.nc")
Use ?subset or lradin[] to get specific band/s.
Use ?getZ to find out the specific date for each band. Use extract(lradin, cbind(lon, lat)) to get time series at specific point/s.
Feel free to ask other focussed questions like this as needed.
There are ...
The QGIS raster import for netcdf uses the GDAL drivers which assume the data follows the CF conventions, and the multidimensional arrays within the data in (...Y,X) order per this:
Dimension The NetCDF driver assume that data follows the CF-1
convention from UNIDATA The dimensions inside the NetCDF file use the
following rules: (Z,Y,X). If there are more ...
You can shift the data into the correct position using these GDAL commands:
gdal_translate -a_srs EPSG:4326 -a_ullr 0 0 360 -90 NETCDF:"sic_average_nclimate.nc":SIC_Change change360.tif
gdalwarp -t_srs WGS84 change360.tif change180.tif -wo SOURCE_EXTRA=1000 --config CENTER_LONG 0
(with a little help by Frank Warmerdam: How to reproject raster from 0 360 ...
I'd suggest trying NCO again, as it's well suited for this type of operation, and can be done with one command. The tool to concatenate several files along the record dimension (e.g. time) is ncrcat. For example, to combine all the files for May 2018:
ncrcat gdaps_anal_201805????.nc gdaps_anal_201805.nc
The resulting file gdaps_anal_201805.nc should ...
This documentation from xarray outlines quite simply the solution to the problem. xarray allows you to interpolate in multiple dimensions and specify another Dataset's x and y dimensions as the output dimensions. So in this case it is done with
# interpolation based on http://xarray.pydata.org/en/stable/interpolation.html
# interpolation can't be done ...
You can use xarray.where. See: http://xarray.pydata.org/en/stable/indexing.html
xds = xarray.open_dataset("TT_199501_daymean.nc")
xds.where((xds.lon==5.8252) & (xds.lat==46.9359), drop=True)
You can find the nearest neighbor using a KDTree. See: https://stackoverflow.com/questions/10818546/finding-index-of-nearest-point-in-numpy-arrays-...
As stated in the linked thread, QGIS (3.2+) can open the NetCDF file as a mesh layer.
(1) Open the file using Layer > Add Layer > Add Mesh Layer.
(2) By default, the first variable (Geopotential_level:250) will be plotted. Below is a short note to To view the variable you need.
Open the Layer Properties (by right-clicking on the Dec-Levels layer on ...
The dataset is getting dereferenced when you return only the band from your function.
The solution is to return the dataset from your function.
From the GDAL Python Gotchas page:
Python crashes if you use an object after deleting an object it has a relationship with
Consider this example:
from osgeo import gdal
dataset = gdal.Open('C:\\...
NetCDF is incredibly general and writing slow code is easy. I routinely deal with tens of thousands of NetCDF files in R, using some combination of packages raster, ncdf, ncdf4, RNetCDF, or rgdal. The key is to leverage the cell index tools in raster so that the "cell-in-polygon" test occurs only once, then you can apply the extraction across all files. Many ...
These data are not in a netcdf format but, rather a space delimited ASCII format. This data is a bit difficult to deal with because of the lack of headers and any type of unique station identifier.
Returns a vector of file names on disk and pulls associate years from file name.
f <- list.files(getwd())
y <- as.numeric(unlist(lapply(strsplit(f, "[.]")...
One way is to create a boolean mask for the dataset coordinates using the extent you specified and then using the .where() method on the dataset.
Here is one example using a tutorial dataset that comes with xarray.
First, load the dataset (passing the decode_times=False argument because, at least in my case, it raises an error otherwise) and inspect it.
I've solved my issue with the help of @Kazuhito.
Firstly, you need to install the NCO (netCDF Operator). The issue is that NetCDF has sometimes different order of variables and QGIS is not able to recognize and match right variables (so it is reading time as longitude).
The NCO command as follows should change variables to the right order for QGIS:
ncpdq -a ...
I think the issue here might be the way the data is imported not the projection.
If you use the Multidimension Tools tool box in ArcGIS 10.0, you need to enter the dimensions of your netcdf variable as the "Row Dimension" option in order to plot the value at each coordinate.
A big question is whether you are going to read the entire raster from the file into memory before processing it, or whether the file is so large that you will process it incrementally, or process some subset of the overall file.
If you will load it all into memory, then you will be doing mostly sequential access, and the fastest format will be a tossup ...
Yes, the NetCDF CF Metadata Conventions version 1.6 specifies how to store point and station time series data in chapter 9 "Discrete Sampling Geometries".
Since your data has the same sample times for all stations, I agree with Rich that you can base your netCDF structure on the example in section H.2.1 "Orthogonal multidimensional array representation of ...
I know this is an old question, but if anyone is still looking for an answer on how to do this in R the following code that will make a netCDF with 2 variables (prec, tmax), and 3 dimensions (long, lat, time). Pretty straight forward to change this suit your number of variables or time dimension etc.
prec <- getData("...
I have found the answer to my own question. If this works for anybody else who is having the same problem, maybe they can vote up the question or the answer, since I cannot get any points for answering my own question. :)
When I was looking for an answer I saw that populating the "row dimension " in ArcGIS with the dimensions of the netcdf ...
If you have moved on to the netCDF Java API it might be interesting for you to try Python as well. I have worked with Java for a while but find Python for number crunching and data analysis more convenient.
Geoserver should be able to serve your postGIS data as NetCDF. You can then use Python or Java to generate a Netcdf file that contains the difference in ...
It might be easier to see what is going on when you create a RasterBrick (multiple layers), either representing depth, or time. The number of layers should make it clear which is is. the z-values too.
You could start with
b <- brick("data.nc", varname="nitrat")
and go from there.
The error in hour ncdf code is because you mix up the ncdf package (open....
I guess that my answer from your other StackOverflow question did not lead you in the right direction?
Here is a more detailed answer that may nudge you in the right direction.
First, we need to know the projection of your data and the extent to be able to project to the Long/Lat grid correctly. Unfortunately, we do not have the PROJ4 CRS or the extent, so ...
This is not really a raster in longitude latitude, it's just arrays of values (including longitude and latitude). You can deal with these explicitly like this:
f <- "F18-SSUSI_EDR-NIGHT-DISK_DD.20150107_SN.26920-00_DF.NC"
## treat these not as rasters, but as arrays of values
## though raster() is extremely helpful in simplifying the ...