Hot answers tagged

14

I would recommend looking into rioxarray for your dataset. You can open your dataset like so: import rioxarray import xarray 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: xds.rio.write_crs("epsg:4326", inplace=True) Then, you should be able to create a geotiff ...


11

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. The ...


10

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 ...


9

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 ...


9

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: http://nbviewer.jupyter.org/gist/rsignell-usgs/4113653 But it's way easier using Xarray: http://nbviewer.jupyter.org/gist/rsignell-usgs/e032db75e748cf5922d38d8be9e0ecef import xarray as xr fname =...


9

Here is an example of using rioxarray to mask out data with a shapefile: https://corteva.github.io/rioxarray/stable/examples/clip_geom.html import geopandas import rioxarray import xarray from shapely.geometry import mapping MSWEP_monthly2 = xarray.open_dataarray('D:\G3P\DATA\Models\MSWEP\MSWEP_monthly.nc4') MSWEP_monthly2.rio.set_spatial_dims(x_dim="lon", ...


7

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. For example: import netCDF4 url = 'http://thredds.met.no/thredds/dodsC/arome25/...


7

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 ...


7

Use this to get access to all bands lradin <- brick("lrad_ITPCAS-CMFD_V0106_B-01_197912.nc") Use ?subset or lradin[[1]] 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 ...


7

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 ...


6

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 ...


6

GDAL supports NetCDF: http://www.gdal.org/frmt_netcdf.html gdal_translate input.cdf output.tif


6

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 ...


6

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 ...


6

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) UPDATE: You can find the nearest neighbor using a KDTree. See: https://stackoverflow.com/questions/10818546/finding-index-of-nearest-point-in-numpy-arrays-...


6

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 ...


5

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:\\...


5

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 ...


5

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, "[.]")...


5

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. ...


5

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 ...


4

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.


4

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 ...


4

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 ...


4

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. library(raster) library(ncdf4) prec <- getData("...


4

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. :) Easy fix: When I was looking for an answer I saw that populating the "row dimension " in ArcGIS with the dimensions of the netcdf ...


4

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 ...


4

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....


4

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 ...


4

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" library(raster) ## treat these not as rasters, but as arrays of values ## though raster() is extremely helpful in simplifying the ...


Only top voted, non community-wiki answers of a minimum length are eligible