You are here: Home / News & Events / Workshops and Trainings / NCL Workshops / Masking ocean
Info
Our docs have moved to https://docs.dkrz.de This portal will be shutdown as soon the last content is migrated.

Masking ocean

Create a contour plot and mask the ocean using a mask file which contains 0 and 1.

Verschiedene Masking Methoden mithilfe der mask und where Funktion

 

NCL script masking using mask and where

 

Masking Skript

begin
;-- read the data
  data = asciiread("ASCII_val_no_delim.txt",-1,"string")

;-- get the number of lines in input file
  rdims = toint(systemfunc("cat ASCII_val_no_delim.txt|wc -l"))
  print("Number of rows in input file:     "+rdims)

;-- retrieve the number of columns in the input lines  
  nc = str_fields_count(data(0)," ")
  print("Number of columns in input file:  "+nc)
  
;-- check if mask data is concatenated without any delimiter (-> nc = 1)
  if( nc .eq. 1) then
     cdims = dimsizes(tochar(data(0)))
     print("Number of extracted rows:    "+rdims)
     print("Number of extracted columns: "+cdims)
     
;-- create latitude and longitude values lat and lon
     lat =  fspan(-90,90,rdims)
     lon =  fspan(-180,180,cdims)
     
;-- assign mask array
     a = new((/rdims,cdims/),"integer")
     a!0         = "lat"
     a!1         = "lon"
     a&lat       =  lat
     a&lon       =  lon
     a&lat@units = "degrees_north"
     a&lon@units = "degrees_east"
     a@_FillValue = -9999
     
     do j = 0,rdims-1                                   ;-- loop through rows
        do i = 0,cdims-1                                ;-- loop through columns
           a(j,i) = toshort(str_get_cols(data(j),i,i))
        end do
     end do
     a = a(::-1,:)                                      ;-- reverse the mask data, because it is 
                                                        ;-- read from north to south (-> degrees_south
                                                        ;-- and NCL needs degrees_north)
  end if
  
;-- create dummy data
  var            =  random_uniform(-10,10,(/rdims,cdims/))
  var!0          = "lat"
  var!1          = "lon"
  var&lat        =  lat
  var&lon        =  lon
  var&lat@units  = "degrees_north"
  var&lon@units  = "degrees_east"
  var@_FillValue = -99999.9
  
;-- mask dummy data with ASCII data field using mask function
  var1 =  var
  var1 =  mask(var,a,0)

;-- mask dummy data with ASCII data field using where function
  var2 = var
  var2 = where(a .eq. 0, var, var@_FillValue)
  
;-- create the graphic output
  wks = gsn_open_wks("png","plot_masking_test")
  
  res                   =  True
  res@gsnDraw           =  False
  res@gsnFrame          =  False
  res@gsnAddCyclic      =  False
  res@cnFillOn          =  True
  res@cnLineLabelsOn    =  False
  res@cnLinesOn         =  False
  res@cnFillPalette     = "GMT_hot"
  res@mpOutlineOn       =  False
  
  res@tiMainString = "Using mask function"
  plot1 = gsn_csm_contour_map(wks,var1,res)
  
  res@tiMainString = "Using where function"
  plot2 = gsn_csm_contour_map(wks,var2,res)

  gsn_panel(wks,(/plot1,plot2/),(/2,1/),False)
  
end

Mask Datei  ASCII_val_no_delim.txt

0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000001111111110000000111000000111100011111111110000001111111111111000000000000000000000000000
0000000000000000000000000000000001111111111000000111000001111000011100001111000001111111111111000000000000000000000000000
0000000000000000000000000000000001110000111100000111000011110000011100000111100000000000011111000000000000000000000000000
0000000000000000000000000000000001110000011110000111000111100000011100000011110000000000111110000000000000000000000000000
0000000000000000000000000000000001110000011110000111001111000000011100000111100000000001111100000000000000000000000000000
0000000000000000000000000000000001110000001111000111011110000000011100001111000000000011111000000000000000000000000000000
0000000000000000000000000000000001110000001111000111111100000000011111111100000000000111110000000000000000000000000000000
0000000000000000000000000000000001110000001111000111011110000000011100011110000000001111100000000000000000000000000000000
0000000000000000000000000000000001110000011110000111001111000000011100001111000000011111000000000000000000000000000000000
0000000000000000000000000000000001110000011110000111000111100000011100001111000000111110000000000000000000000000000000000
0000000000000000000000000000000001110000111100000111000011110000011100000111100001111100000000000000000000000000000000000
0000000000000000000000000000000001111111111000000111000001111000011100000111100001111111111111000000000000000000000000000
0000000000000000000000000000000001111111110000000111000000111100011100000011110001111111111111000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

NCL script masking ocean

Masking Skript

begin
;-- read the data and define variable reference var
  f   =  addfile("$NCL_TUT/data/tas_Amon_MPI-ESM-LR_historical_r1i1p1_2005.nc","r")
  var =  f->tas(0,:,:)

;-- read the land /sea mask data
  m   =  addfile("$NCL_TUT/data/sftlf_fx_MPI-ESM-LR_historical_r0i0p0.nc","r")
  lsm =  m->sftlf(:,:)/100                         ;-- values between 0 and 1

;-- create the mased variable land_only
  land_only  =  var                                     ;-- copy var with meta data
  land_only  =  mask(var,lsm,1)                         ;-- return the values of var where lsm1=1
  
;-- define the workstation (plot output type and name)
  wks = gsn_open_wks("ps","plot_masking")

;-- set resources
  res                       =  True
  res@gsnMaximize           =  True

  res@lbLabelStride         =  2
  res@lbBoxMinorExtentF     =  0.15                      ;-- decrease the height of the labelbar

  res@cnFillOn              =  True                      ;-- turn on contour fill
  res@cnLinesOn             =  False                     ;-- turn off contour lines
  res@cnLineLabelsOn        =  False                     ;-- turn off line labels
  res@cnLevelSelectionMode  = "ManualLevels"             ;-- set contour levels manually
  res@cnMinLevelValF        =  250.                      ;-- minimum contour level
  res@cnMaxLevelValF        =  310.                      ;-- maximum contour level
  res@cnLevelSpacingF       =  1                         ;-- contour level spacing

  res@tiMainString          = "Masking ocean"            ;-- title string
  res@tiMainFontHeightF     =  0.02

;-- draw the contour map
  plot = gsn_csm_contour_map(wks, land_only, res)

end
Daten:

tas_Amon_MPI-ESM-LR_historical_r1i1p1_2005.nc

sftlf_fx_MPI-ESM-LR_historical_r0i0p0.nc

 

Document Actions