load "$NCARG_ROOT/lib/ncarg/nclscripts/contrib/cd_string.ncl"
begin
; Read station information------------
fname = "./station_unstructure_index.txt" ;excute the $ncl read_write_station.ncl
stinfo = asciiread(fname,-1,"string")
stlon = stringtofloat(str_get_field(stinfo,1," ")) ; longitude values
stlat = stringtofloat(str_get_field(stinfo,2," ")) ; latitude values
stname = str_get_field(stinfo,4," ") ; station name
stindx = stringtoint(str_get_field(stinfo,5," ")) ; index in unstructure files
;print(stindx)
;print(stname)
nst=dimsizes(stname)
f = addfile("../fort.63.nc","r")
times=f->time
nt=dimsizes(times)
depth=f->depth ;
zeta=f->zeta ; elevator above grid, double zeta(time=72, node=182048);
format = "%Y-%N-%D %H:%M" ; replace format_string with the desired format string
;format = "%N/%D %H:%M:%S"
stime = cd_string(times, format)
;data_stn=new((/nst,nt/),"float")
data_stn=new((/nst,nt/),"string")
do i=0,nst-1
;data_stn(i,:)= tofloat(zeta(:,stindx(i)))*100 ;m to cm
do j=0,nt-1
data_stn(i,j)= sprintf("%4.0f",tofloat(zeta(j,stindx(i)))*100) ; m to cm
end do
end do
;---add "time" to names sequence---
head_str=new((/nst+1/),"string")
head_str(0)="Time"
do ih=1,nst
head_str(ih)=stname(ih-1)
end do
csv_filename = "Result_station2.csv" ; name of CSV file to create
dq = str_get_dq() ; double quote character
fields = dq + head_str + dq ; Pre/append quotes to field names
header = [/str_join(fields,",")/]
; station names is joined and separated by commas. ;
system("rm -rf " + csv_filename) ; Remove file in case it exists.
write_table(csv_filename, "w", header, "%s") ; Write header to CSV file.
;; Note: if you don't want spaces in CSV file, use the following format string.
;; format = "%d,%g,%g,%g,%s,%d"
format = "%s,%s" ;--Outputs 2 strings, separated by commas
do t=0,nt-1
;print("write time of " + t)
linet = [/stime(t),str_join(data_stn(:,t),",")/]
;; --join data to a long string and separated by commas
;; --and Outputs time and data long string in format
write_table(csv_filename, "a", linet, format) ; Write mixed data to CSV file.
end do
end