NCO cropping a netcdf file using dimension values rather than indices - netcdf

CDO can crop a netcdf file in terms of latitude and longitude as long as they are defined in a standard way, and I know that NCO can cut out a subset of a netcdf file along any dimension if you know the range of indices that you want, as stated in the answers to this related question:
Is there a way to crop a NETCDF file?
However, I was wondering if the ncks hyperslabber can work directly on the values of the dimension, rather than the index values?

Yes, using a decimal indicates the range of actual values (eg, latitudes) to extract over, while using integers indicates the range of indices corresponding to the values.
For instance, to extract across latitudes 30.0 - 40.0 degrees N:
ncks -d lat,30.,40. file.nc -O cropped_file.nc

Just to add to the answer above. If longitude is in "degrees east", i.e. it goes from 0 to 360 and not -180 to 180, you will need to modify the call.
So, the following will not crop the file from -30 to 40 degrees.
ncks -d lon,-30.,40. file.nc -O cropped_file.nc
But this will:
ncks -d lon,40.,-30. file.nc -O cropped_file.nc

Related

raster:: extract produces an empty list

I am trying to extract the values of pixels in a DSM(CHM) within digitized tree crowns.
first I set my working directory read in the shapefile and raster.
TreeCrowns <-shapefile("plot1sag_shape/plot1sag.shp")
CHM <- raster('272280split4.tif')
Then I try to extract the pixel values
pixel <- raster::extract(CHM, TreeCrowns, method= 'simple', weights=FALSE, fun=NULL)
But I get an empty list with all NULL values for every polygon. I have confirmed that the CHM and polygons are in the same location. What can I do to fix this?
Since your shapefile consists of polygon, the extract() function need to know how to summarise the pixel values across a polygon via the fun= argument. Since you provide fun=NULL, the function interpret as returning NULL values to summarise the pixel values.
Try fun=mean or fun=sum (and they mean different thing so see which one suits you).
That probably happens because the polygons and the raster do not overlap. Can you show(CHM) and TreeCrowns? Have you looked at
plot(CHM)
lines(TreeCrowns)
Or are your polygons very small relative to the raster cells? In that case try argument small=TRUE

How to change number of layers of a raster brick

When I load a tiff image in R as raster brick then I have the expected number of pixels (number rows and columns) when I check afterwards the details of the image. But I expect 44 layers. I think I can change the number of layers with the nlayers function. Unfortunately, this does not work. The number of layers remain at 4.
sample_brick = brick("file_example_TIFF_1MB.tiff", nlayers = 44)
Can someone help me how to adapt the number of layers?
Here you can find a example tiff image.
https://file-examples.com/wp-content/uploads/2017/10/file_example_TIFF_1MB.tiff
The brick function creates a RasterBrick that has all the layers that are in your file.
Your expectation is probably wrong.
You cannot add imaginary layers when creating an object from file.

Average variable values across coordinates within NetCDF before importing into R as Raster Brick?

Is there a way to average values in a NetCDF file over a given list of lat/lon coordinates using nco before using R to convert it into a Raster Brick? This list of lat/lon coordinates represents a non-rectangular region.
Right now, I have an .nc file with monthly temperature data for a single year for the entire ocean. I have a list of lat/lon values that describe my study region. I would like to extract a single average value for temperature for each month across all coordinates in my lat/lon list. Thus far, I've been trying to perform this operation after the .nc file has been converted to a Raster Brick in R, but it requires too much memory to perform the operation.
Therefore, I'm wondering if there is a way I can do this in bash using nco instead.
Thanks!
I'm not sure I understand the intent of your question. Either of these commands should give a single temperature for the whole year for each point:
ncwa -a time in.nc out.nc
or
ncra in.nc out.nc
To extract the specific points on the list from the global ocean (and store them in a single file without using _FillValues for the other points) is more problematic, and would be most concise if you used ncremap to first convert your global ocean data from a (presumably) rectangular latitude-longitude format to an unstructured (i.e., 1D) array, then used ncks -X to extract the list of points into a single compact file. The User Guide gives more info on each step.
If the lat lon points are a rectangle region (i.e. lon1-lon2 and lat1-lat2), then you can extract them and then take the spatial average in this way with CDO:
cdo fldmean -sellonlatbox,lon1,lon2,lat1,lat2 in.nc out.nc
If the region in question is not a simple "box", then you will need to extract the nearest grid point into a file and then use ensemble averaging:
for lonlat in $lonlatlist ; do
lable=`echo $lonlat | sed 's/\//_/g' # replace / in output name
cdo remapbil,$lonlat in.nc point_${lonlat}.nc
done
cdo ensmean point_*.nc out.nc
where lonlat list is a list of array indexes in strings "lon=X/lat=Y" (or you could use an alternative imitation 2D array in bash (How to declare 2D array in bash)
This method uses bilinear interpolation to get the value for each given lat-lon (and they need to be regularly spaced to get a sensible answer, otherwise you are weighting the average more to one region where your points are more closely space)

Extracting grid cells of a netcdf file based values from another nc file with R

I want to extract the grid cells from a MERRA2 netcdf file which are flagged as sea only with R:
However parts of the land areas are within my selection due to the curved nature of the lower boundary of the land area. I want to select the area of the sea area which are closest to the land but there are areas of the land in my selection which I want to filter out.
I have another nc file which has the fractions of the land and sea areas from which I understand it is possible to extract the grid cells I need from:
So I have to define a mask that I can use to only select ocean points from my first file with a certain threshold since there are fractional coverage of land and ocean.
Please is there a way to do this with R or maybe in ArcGIS
you can do something using CDO perhaps. If you decide on a land fraction threshold "c" you want to use for your mask from your landsea mask file landsea.nc (it sounds from your description that c needs to be a very small positive real number) then you can either define a mask which is zero for sea and one for land
cdo lec,c landsea.nc mask.nc
This sets all points with values < c to 1, otherwise 0 (for land)
Or if you want to then set all land points to "missing" then you can
cdo setrtomiss,0.5,2 mask.nc mask_miss.nc
Now land points are set to "missing"
you can then scale the data file with the mask to either set the land points to zero
cdo mul merra2.nc mask.nc out1.nc
or you can set them to missing :
cdo mul merra2.nc mask_miss.nc out2.nc
Which you can then process.
Of course, here I am assuming that the landsea.nc mask file is on the same grid as your merra file. If they are not you will need to regrid the mask file first, which you can also do with CDO.

Not able to export the desired results in R

I have a square mesh of latitudes and longitudes for a geographical region. However I only know the latitude and longitude values of the 4 corners of this mesh. Using these I need to calculate the lat-long values at all the cross hairs. So, I separately crested a nested loop program in R for latitudes and longitudes.
tllong<-67.481961
sink("output_long.txt")
for (i in c(1:11447)) {
for (j in c(1:10335)) {
tllong<- 67.481961 + (j-1)*0.0030769
print(tllong)
}
}
sink()
The above program was for calculating longitudes. tllong is the value of longitude at top left corner of the mesh. 11447 are the number of latitudes and 10335 are the number of longitudes.
Similarly I created a program for calculating latitudes.
tllat<-36.348639
sink("output_lat_again.txt")
for (i in c(1:11447)) {
for (j in c(1:10335)) {
print(tllat)
}
tllat<- tllat - (i-1)*0.002508
}
sink()
tllat is the value of latitude of the top left corner mesh square.
So as you can see that the loop first calculate all the lat,long values for first row Then goes to second row, then third and so on. However when I get the exported text files for both the programs, I get a single column containing all the values. This is not much of a use for me. I tried to export the output results of R in xlsx format using sink("output_long.xlsx") but when I get the excel file (after 4-5 hours of constant long run of loop) I fail to open it. The error message shows either the file is corrupted or file is of different format. I have tried this 3-4 times but in vain.
So how do I export the results of these two programs in an excel file such that I do not get all the values in a single column but in an appropriate matrix form (i.e. the values of lat,long in each cell corresponds to the values of lat,long in the corresponding cross hair of the mesh).
Also, it would be nice if someone can tell me how to run these two programs together so that I can get the lat-long values in a single run in the same file.
Seems like you want to create 10335*11447=118304745 pairs of lat/lon values. It's a pretty big number. Is that correct? However, I will show the procedure applied to a smaller example. Try this:
#setting the values of parameters
tllong<-67.481961
tllat<-36.348639
deltalong<-0.0030769
deltalat<-0.002508
#small example: you can set the following to the real values
nlong<-10
nlat<-10
#create vectors of values without loops
lat<-seq(tllat,by=deltalat,length.out=nlat)
lon<-seq(tllong,by=deltalong,length.out=nlong)
#now we build every possible pair of lat/lon values
latlong<-expand.grid(lon=lon,lat=lat)
#we export it to a csv file
write.csv(latlong,"somefile.csv",row.names=FALSE,quote=FALSE)
At the end, the somefile.csv will be created. Keep in mind that, with your values, the created file will be very big.

Resources