1

I have a Netcdf4 file with three dimensions illustrating five global climate variables:

 3 dimensions:
    lon  Size:2160
        units: degrees_east
        long_name: longitude
        standard_name: longitude
    lat  Size:1080
        units: degrees_north
        long_name: latitude
        standard_name: latitude
    time  Size:12   *** is unlimited ***
        units: months since 1949-12-01 00:00:00Z
        long_name: time
        standard_name: time
        calendar: standard

I want to mask out all of the data in the Antarctic using a specified fill value :-99999 What is important is that I want to retain the size/dimensions of my file, so when I map it out, it still shows the Antarctic, except there is just no data filled in.

I have been working with my data in THE 'Climate4r' package (https://rdrr.io/github/SantanderMetGroup/loadeR/) but I am also open to applying the mask using 'nc' packages? For example, I can read in a variable using:

library(ncdf4)
nc<-nc_open("myfile.nc")
tasmax<-ncvar_get(nc,"tasmax")

My thinking is that I have to read in the lats and longs and then maybe replace all the values south of the most northern point of the Antarctic with my fill value? I tried to read in the lat longs however then I noticed another issue:

yvals<- test$xyCoords$y
xvals<- test$xyCoords$x
length(xvals)
[1] 1081
length(yvals)
[1] 1080

The length of the xvals is supposed to be 2160. I'm assuming I have read something in incorrectly here or somehow the longitudes are getting halved in the process? This is something I can figure out, but if anyone can tell me if I am on the right track for the masking/suggest a better approach I'd appreciate it.

ADDITIONAL EDITS

The loadR library has loads of dependencies and can be exceptionally tricky to get working in R, so I'm not going to include it here in my example. This can create an example nc file. I can probably create a global mask using a global dataset multiplied by zero, but what I need to do is to mask out the Antarctic values only?

library(tidync)
library(ncdf4)
library(ncdf4.helpers)
library(RNetCDF)
library(easyNCDF)

#create data
vap=array(1:100, c(12,1080,2160))

#create the list
Variable <- list(Data = vap)
xyCoords <- list(x = seq(-179.8333,180,length.out=2160), y = seq(-89.91667,89.91667,length.out=1080))
Dates <- list(start = seq(as.Date("2012-01-01"), as.Date("2012-12-31"), by="months"), end=seq(as.Date("2012-01-01"), as.Date("2012-12-31"), by="days"))
All <- list(Variable = Variable,Data=vap, xyCoords=xyCoords,Dates=Dates)

data<-aperm(All$Data)
#----------------
# Make dimensions
#----------------
xvals<- All$xyCoords$x
yvals<- All$xyCoords$y

nx <- length(xvals)
ny <- length(yvals)

xdim2 <- ncdim_def( 'lon', 'degrees_east',longname="longitude", xvals )
ydim2 <- ncdim_def( 'lat', 'degrees_north',longname="latitude", yvals )
tdim2 <- ncdim_def( 'time', 'months since 1949-12-01 00:00:00Z',longname="time", 0:11, unlim=TRUE )
#---------
# Make var
#---------
mv <- 1.00000002004088e+20     # missing value
var_hur <- ncvar_def( 'hur', '%', list(xdim2,ydim2,tdim2), mv )

#---------------------
# Make new output file
#---------------------
output_fname <- "test.nc"
ncid_hur <- nc_create( output_fname, list(var_hur))

#-------------------------------
# Put the data in the file
#-------------------------------
ncvar_put( ncid_hur, var_hur, data, start=c(1,1,1), count=c(nx,ny,12))

nc_close( ncid_hur)
matlabcat
  • 172
  • 2
  • 12
  • I think you put the library(loadR) and steps to the nc you're using so everyone's working with the same data. – Chris Sep 28 '22 at 13:01
  • @Chris I have included a sample file – matlabcat Sep 29 '22 at 10:11
  • Perhaps [HiClimR](https://github.com/hsbadr/HiClimR) pkg using `geoMask()` to establish 'Antartica' bounds, which seems to be used in context of 'include', whereas you want to 'exclude' via fill arg. Truthfully, I have difficulty enough just reading nc files and going to bookmark this as example of writing one. Now have to read the above to have any sense for this conversation. Well, likely throwing an anvil at an ant, but convenient to get bounds for Antartica. – Chris Sep 29 '22 at 12:20

0 Answers0