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

Read ASCII data with mixed fields

NCL example script to read ASCII data containing mixed/merged fields.

 

NCL read ASCII data with mixed fields

Example script:

;-----------------------------------------------------------------
;  DKRZ NCL example:   read_ASCII_split_columns.ncl
;
;  Input data:  ID      Date     LAT    LON   PW
;              BLAC 20150101120036.75 -97.25 48.00
;              BREC 20150101120036.41 -97.69 46.30
;              BURB 20150101120036.63 -96.81 49.80
;              DQUA 20150101120034.11 -94.29 45.00
;              ............
;-----------------------------------------------------------------
begin
;-- read complete input file
  data = asciiread("pw.dat",-1,"string")
  data := data(1::)                            ;-- get rid of the header
  
  delim = " "                                  ;-- delimiter sign
  
  id    = str_get_field(data,1,delim)          ;-- retrieve station id
  mix   = str_get_field(data,2,delim)          ;-- retrieve mixed field contains date, lat and lon without delimiter!
  lon   = tofloat(str_get_field(data,3,delim)) ;-- retrieve longitudes
  var   = tofloat(str_get_field(data,4,delim)) ;-- retrieve data
  
  year  = toint(str_get_cols(mix,0,3))         ;-- retrieve year from mixed field
  mon   = toint(str_get_cols(mix,4,5))         ;-- retrieve months from mixed field
  day   = toint(str_get_cols(mix,6,7))         ;-- retrieve days from mixed field
  hour  = toint(str_get_cols(mix,8,9))         ;-- retrieve hours from mixed field
  minu  = toint(str_get_cols(mix,10,11))       ;-- retrieve minutes from mixed field

  lat  = tofloat(str_get_cols(mix,12,15))      ;-- retrieve latitude values from mixed field
  lat@units = "degrees_north"                  ;-- set lat units for map plot routine
  lon@units = "degrees_east"                   ;-- set lon units for map plot routine

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

;-- set contour plot resources
  res                 =  True
  res@gsnFrame        =  False                ;-- don't draw the plot yet
  
  res@mpMinLatF       =  min(lat)
  res@mpMaxLatF       =  max(lat)
  res@mpMinLonF       =  min(lon)-1.
  res@mpMaxLonF       =  max(lon)+1.
  
  res@sfXArray        =  lon                  ;-- tell NCL to use 1D longitudes for x-axis
  res@sfYArray        =  lat                  ;-- tell NCL to use 1D latitudes for y-axis

;-- create the contour plot
  plot = gsn_csm_contour_map(wks,var,res)

;-- draw marker at station location
  mkres               = True
  mkres@gsMarkerIndex = 17                    ;-- filled circle
  mkres@gsMarkerSizeF = 0.05                  ;-- bigger markers

  gsn_polymarker(wks,plot,lon,lat,mkres)      ;-- draw the markers

;-- draw the frame
  frame(wks)
  
end

Document Actions