Skip to content
Snippets Groups Projects

Mask coastal area in MPIOM

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Sebastian Milinski

    This NCL script creates a NetCDF file with a coast-following mask for the ocean. The user can choose a distance. Every ocean grid cell that is within this distance from the coast will get the value 1, everything else will be missing values.

    This script is using a number of nested loops and is therefore not very efficient. Because the script will most likely only run once to create a mask file, efficiency is not of major importance in this case.

    If you are only interested in a small area, use only this area as an input for the script to reduce computational cost and thus runtime.

    mask_coastal_region.ncl 1.77 KiB
    ;=================================================
    ; creates a netcdf file with a mask that is 1 within a certain distance off the coast and missing elsewhere
    ;=================================================
    load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
    load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
    load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
    ;=================================================
    begin
    ;=================================================
    ; open file and read in data
    ;=================================================
    ; input needs to be land sea mask with land=1 and ocean=0
    ; global file will take long to process, choose subregion if you don't need a global mask
      ifile 		= addfile("/work/bm0764/m300265/test/TP04_lsm_SETA.nc","r")
    	coast  	= ifile->var1
    	lat		= ifile->lat
    	lon		= ifile->lon
    
    printVarSummary(coast)
    
    ; choose output directory
    outdir = "/scratch/m/$USER/"
    
    ofile = addfile(outdir + "mask_box_TP04_1deg.nc" ,"c")  ; open output netCDF file
    ;=================================================
    ; calculations
    ;=================================================
    
    newcoast = coast
    newcoast(:,:,:) = newcoast@_FillValue
    
    ; select distance off coast (every ocean cell closer to coast than this value will be marked)
    range = 1
    
    dim=dimsizes(coast)
    imax = dim(1)-1
    jmax = dim(2)-1
    
    do i=0,imax
    	do j=0,jmax
    	if(coast(0,i,j).eq.0) then
    		clat=lat(i,j)
    		clon=lon(i,j)
    		distmat = new((/dim(1),dim(2)/),"float")
    
    			do ii=0,imax
    				do jj=0,jmax
    					if(coast(0,ii,jj).eq.1) then
    						distmat(ii,jj)=gc_latlon(clat,clon,lat(ii,jj),lon(ii,jj),2,2)
    					end if
    				end do
    			end do
    			mindist=min(distmat)
    			if(mindist.le.range) then
    				newcoast(0,i,j)=1
    			end if
    	end if
    	end do
    end do
    
    
    ofile->coast 	= newcoast
    ofile->lon		= lon
    ofile->lat		= lat
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment