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

NCL Basics II - IO

Kurze Beispiel NCL Skripte zum Lesen uind Schreiben von Daten.

 

Lesen einer netCDF Datei:

begin

  f   = addfile("ECHAM5_OM_A1B_t_20010101.nc","r")  ;-- open data file
  var = f->t                                        ;-- set variable
  
  printVarSummary(var)                              ;-- print variable info
  
end

 

Lese ASCII Daten:

begin

  data1 = asciiread("World_population.asc",-1,"float")
  print(data1)
  
  data2 = asciiread("World_population.asc",(/17,2/),"float")
  print(data2)

end

 

Lese CSV Daten:

begin
  diri = "../data/"
  fili = "Test_6h.csv"
  
;-- read in file as array of strings so we can parse each line
  delim  = ";"

  data   = asciiread(diri+fili, -1, "string")
  scount = str_fields_count(data(0),delim)

;-- read 6h values
  val = tofloat(str_split_csv(data,delim,0))
  print("Val: " + val)

;-- x-axis: 5 timesteps, interval 6h
  x = ispan(0,(scount*6)-1,6)

;-- open a workstation
  wks = gsn_open_wks("png","plot_csv")

;-- set resources
  res                    =  True

  res@tiMainString       = "NCL Doc Examples: Read CSV data (delimiter = ;)"

  res@trYMinF            =   0.0
  res@trYMaxF            =  12.0
  res@trXMinF            =   0
  res@trXMaxF            =  18

  res@xyLineThicknessF   =   5
  res@xyDashPattern      =  0                    ;-- make lines all solid
  res@xyLineColors       = (/"blue", "red", "green", "black", "orange"/)

;-- create the plot
  plot = gsn_csm_xy(wks, x, val, res)

end

 

Schreiben einer netCDF Datei (einfach):

begin
  outfile = "t_in_Celsius_1.nc"
  
  if (isfilepresent(outfile)) then
     system("rm -rf "+outfile)         ;-- make sure that file does not exist
  end if
  
  fin  = addfile("ECHAM5_OM_A1B_t_20010101.nc","r")  ;-- open data file
  fout = addfile(outfile,"c")          ;-- create new file (netcdf 3)

  filedimdef(fout,"time",-1,True)      ;-- make time and UNLIMITED dimension

  tK           =  fin->t               ;-- get variable
  tC           =  tK                   ;-- copy variable and its related dimensions
                                       ;   and attributes
  tC           =  tK - 273.15          ;-- convert from Kelvin to Celsius
  tC@units     = "Celsius"             ;-- define new units

  fout->tC     =  tC                   ;-- write variable to new netCDF file
  
end

 

Schreiben einer netCDF Datei (ausführlich):

begin

  outfile = "t_in_Celsius_2.nc"
  
  if (isfilepresent(outfile)) then
    system("rm -rf "+outfile)          ;-- make sure that file does not exist
  end if

;-- open data file
  fin  = addfile("ECHAM5_OM_A1B_t_20010101.nc","r")  ;-- open data file

;-- get variable t and its dimensions and dimension sizes
  tK    =  fin->t                      ;-- get variable
  
  time  =  fin->time                   ;-- get dimension time
  lev   =  fin->lev                    ;-- get dimension lev
  lat   =  fin->lat                    ;-- get dimension lat
  lon   =  fin->lon                    ;-- get dimension lon
  
  ntim  =  dimsizes(time)              ;-- get dimension sizes of time
  nlev  =  dimsizes(lev)               ;-- get dimension sizes of lev                  
  nlat  =  dimsizes(lat)               ;-- get dimension sizes of lat
  nlon  =  dimsizes(lon)               ;-- get dimension sizes of lon

;-- convert variable t: Kelvin to Celsius
  tC           =  tK                   ;-- copy variable and its dimensions and attributes
  tC           =  tK - 273.15          ;-- convert from Kelvin to Celsius
  tC@units     = "Celsius"             ;-- define new units

;-- create new netCDF file
  fout = addfile(outfile,"c")

;-- begin output file settings
  setfileoption(fout,"DefineMode",True) ;-- explicitly declare file definition mode

;-- create global attributes of the file
  fAtt                  =  True        ;-- assign file attributes
  fAtt@title            = "NCL Efficient Approach to netCDF Creation"  
  fAtt@source_file      = "ECHAM5_OM_A1B_t_20010101.nc"
  fAtt@Conventions      = "CF"   
  fAtt@creation_date    =  systemfunc ("date")        
  fAtt@history          =  "NCL script: ex_netcdf_2.ncl" 
  fAtt@comment          = "Convert variable t: Kelvin to Celsius"       
  fileattdef(fout,fAtt)                ;-- copy file attributes    

;-- predefine the coordinate variables and their dimensionality
  dimNames = (/"time", "lev", "lat", "lon"/)  
  dimSizes = (/ -1   ,  nlev, nlat,  nlon /) 
  dimUnlim = (/ True , False, False, False/)   
  filedimdef(fout,dimNames,dimSizes,dimUnlim)

;-- predefine the the dimensionality of the variables to be written out
  filevardef(fout, "time" ,typeof(time),getvardims(time)) 
  filevardef(fout, "lev"  ,typeof(lev), getvardims(lev))                           
  filevardef(fout, "lat"  ,typeof(lat), getvardims(lat))                          
  filevardef(fout, "lon"  ,typeof(lon), getvardims(lon))                          
  filevardef(fout, "tC"   ,typeof(tK),  getvardims(tK))

;-- copy attributes associated with each variable to the file
  filevarattdef(fout,"time" ,time)       ;-- copy time attributes
  filevarattdef(fout,"lev"  ,lev)        ;-- copy lev attributes
  filevarattdef(fout,"lat"  ,lat)        ;-- copy lat attributes
  filevarattdef(fout,"lon"  ,lon)        ;-- copy lon attributes
  filevarattdef(fout,"tC",   tC)         ;-- copy tC attributes

;-- explicitly exit file definition mode (not required)
  setfileoption(fout,"DefineMode",False)

;-- output only the data values since the dimensionality and such have been predefined.
;-- The "(/", "/)" syntax tells NCL to only output the data values to the predefined 
;-- locations on the file.
  fout->time   =  (/time/)               ;-- write time to new netCDF file
  fout->lev    =  (/lev/)                ;-- write lev to new netCDF file
  fout->lat    =  (/lat/)                ;-- write lat to new netCDF file
  fout->lon    =  (/lon/)                ;-- write lon to new netCDF file
  fout->tC     =  (/tC/)                 ;-- write variable to new netCDF file
  
end

 

 

Document Actions