;---------------------------------------------------------------------- ; write_csv_o3.ncl ; ; Concepts illustrated: ; - Writing a CSV file with a header using write_table ;---------------------------------------------------------------------- ; This example reads three 4D arrays off a NetCDF file and writes ; the contents to a CSV file with a header that contains the ; long_name and units of each field. ;---------------------------------------------------------------------- begin ;---NetCDF file to read in. ; filename = "wrfout_d01_2010-07-11_20:00:00.nc" ; filename = "wrfout_d02_2007-06-02_01:00:00_radm2_ab3.nc" ; filename = "dailymax_2007-06-01.nc" ; filename = "dailyave_2010-06-15.nc" filename = "o3.month.nc" fin = addfile(filename,"r") ;---Pick three 4D arrays to write to CSV file o3MonthAvg = fin->o3MonthAvg ; Y = fin->south_north ; X = fin->west_east ; Read the 1D coordinate arrays, conform them to 4D, ; then convert to 1D so we can write them to CSV ; file along with data. nl=0 o3MonthAvg@level = "nl="+nl+"; lowest level" o3MonthAvg=o3MonthAvg/1000 o3MonthAvg@units="ppmv" printVarSummary(o3MonthAvg) printMinMax(o3MonthAvg,0) print("Monthly Average Value") print(avg(o3MonthAvg)) print("-----") dims = dimsizes(o3MonthAvg) ; bot1d = ndtooned(conform_dims(dims,o3MonthAvg&bottom_top,0)) ; sou1d = ndtooned(conform_dims(dims,o3MonthAvg&south_north, 1)) ; wes1d = ndtooned(conform_dims(dims,o3MonthAvg&west_east, 2)) ;---Construct header line field_names = (/o3MonthAvg@o3MonthAvg + " [" + o3MonthAvg@ppm + "]" /) header = [/str_join(field_names,",")/] ;---Write header to CSV file. csv_filename = "o3_month_RACM.csv" system("rm -rf " + csv_filename) write_table(csv_filename, "w", header, "%s") ;---Convert 4D arrays to 1D for writing to CSV file o31d = ndtooned(o3MonthAvg) ; o31d=o3MonthAvg ;---Write data to file alist = [/o31d/] format = "%g" write_table(csv_filename, "a", alist, format) end