<div dir="ltr"><div>Hi Tess,<br></div><div><br></div><div>I think the issue is the following:<br><br>Variable: data_seasonal_clim<br></div><div>[snip]<br></div><div>Number of Dimensions: 3<br>Dimensions and sizes: [season | 4] x [latitude | 256] x [longitude | 512]<br>Coordinates: <br> season: [DJF..SON] <************************<br> latitude: [-89.46294..89.46294]<br> longitude: [ 0..359.2969]<br><br><br></div><div>By netCDF rule, a coordinate variable *must* be a numeric monotonic one-dimensional array.<br></div><div>You have the names as type string.<br><br></div><div>NCL will allow this construction to occur. However, when writing to a netCDF file, this structure is neither recognized or allowed. I am pretty sure this applies to both netCDF-3 and netCDF-4. (FYI: netCDF-4 is a subset of HDF-5 and is HDF-5 ) <br></div><div> <br>data_seasonal_clim!0 = "season"<br>data_seasonal_clim!1 = "latitude"<br>data_seasonal_clim!2 = "longitude"<br>;;;data_seasonal_clim&season = season_names<br>data_seasonal_clim&season = ispan(0.3,1) ; ispan(1,4,1) .... whatever<br><br><br>season_names = (/ "DJF","MAM","JJA","SON"/)<br></div><div>season_names!0 = "season"<br></div><div>season_names&season = ispan(0.3,1) ; ispan(1,4,1) .... whatever<br><br>===<br></div><div>NOTE: netCDF-3 does *not* allow string variables. It does allow character variables. Hence, if you want string variable output, I think you should precede the file write with:<br> <br><pre><strong>setfileoption</strong>("nc","Format","NetCDF4")</pre><br></div><div>Good luck<br></div><div>D<br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Oct 7, 2016 at 5:25 AM, Tess Parker <span dir="ltr"><<a href="mailto:tess.parker@monash.edu" target="_blank">tess.parker@monash.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div>Hi -<br><br></div>I would like to dynamically assign the variable name when writing output to a netcdf file. The attached script returns an error message, although the file does seem to write out the correctly named variable with values? Can you suggest a reason for the error message? - I think it may be associated with the coordinate variables, but I'm not sure why this should be.<br><br></div><div>Thanks as always for your help!<br></div><div><br></div><b>NCL SCRIPT:</b><br>****************<br><br>load "$NCARG_ROOT/lib/ncarg/<wbr>nclscripts/csm/gsn_code.ncl"<br>load "$NCARG_ROOT/lib/ncarg/<wbr>nclscripts/csm/gsn_csm.ncl"<br>load "$NCARG_ROOT/lib/ncarg/<wbr>nclscripts/csm/contributed.<wbr>ncl"<br>load "$NCARG_ROOT/lib/ncarg/<wbr>nclscripts/contrib/ut_string.<wbr>ncl"<br>;/Calc_seasonal_means_Jasmin.<wbr>ncl<br><br>begin<br>; xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx<wbr>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx<wbr>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx<wbr>xxxxxxxxxx<br><br>data_dir = "/badc/ecmwf-era-interim/data/<wbr>gg/"<br><br>years = ispan(1979,2015,1)<br>clim_years = years<br>seas_len = 3<br>months = ispan(1,12,1)<br>month_string = new(12,string)<br>do aa = 0,11<br> if(months(aa).lt.10) then<br> month_string(aa) = "0"+months(aa)<br> else<br> month_string(aa) = ""+months(aa)<br> end if<br>end do<br>month_names = (/ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" /)<br>month_array = (/"J","F","M","A","M","J","J",<wbr>"A","S","O","N","D"/)<br><br>season = (/ (/12,1,2/),(/3,4,5/),(/6,7,8/)<wbr>,(/9,10,11/) /)<br>i_season = season-1<br>season_names = (/ "DJF","MAM","JJA","SON"/)<br><br>input_var = "Z"<br>input_lev = 850<br>input_dir = "ap" ;ap, as, at, av<br><br>file_out = True<br>out_file = ""+input_var+"_<a href="http://seasonal_means_1979_2015.nc" target="_blank">seasonal_means_<wbr>1979_2015.nc</a>"<br>output_var = str_lower(input_var)<br><br>if (file_out) then<br> system("rm -f "+out_file)<br> ncdf = addfile(out_file, "c")<br>end if<br><br>;; Test file for data dimensions<br>;;============================<wbr>==<br><br>test_file = systemfunc("ls "+data_dir+input_dir+"/1979/<wbr>01/01/<a href="http://ggap197901010000.nc" target="_blank">ggap197901010000.nc</a>")<br>testf = addfile(test_file,"r")<br>test_data = testf->$input_var$(:,{input_<wbr>lev},::-1,:)<br>test_dims = dimsizes(test_data)<br>printVarSummary(test_data)<br>print(test_dims)<br><br>data_monthly_mean = new((/12,test_dims(1),test_<wbr>dims(2)/),float)<br>data_monthly_mean!0 = "month"<br>data_monthly_mean!1 = "latitude"<br>data_monthly_mean!2 = "longitude"<br>data_monthly_mean&latitude = test_data&latitude<br>data_monthly_mean&longitude = test_data&longitude<br>printVarSummary(data_monthly_<wbr>mean)<br><br>;; Read in 6-hourly data and construct monthly means<br>;;============================<wbr>======================<br><br>do jj = 0, 1;dimsizes(months)-1<br> in_files = systemfunc("ls "+data_dir+input_dir+"/19{79,<wbr>80}/"+month_string(jj)+"/*/*.<wbr>nc")<br> ;in_files = systemfunc("ls "+data_dir+input_dir+"/*/"+<wbr>month_string(jj)+"/*/*.nc")<br> printVarSummary(in_files)<br> print(in_files)<br> inf = addfiles(in_files,"r")<br> ListSetType(inf,"cat")<br> data = inf[:]->$input_var$(:,{input_<wbr>lev},::-1,:)<br> printVarSummary(data)<br> data_monthly_mean(jj,:,:) = dim_avg_n(data,0)<br> delete([/in_files,inf,data/])<br>end do<br><br>printVarSummary(data_monthly_<wbr>mean)<br><br>data_seasonal_clim = new((/4,test_dims(1),test_<wbr>dims(2)/),float)<br>data_seasonal_clim!0 = "season"<br>data_seasonal_clim!1 = "latitude"<br>data_seasonal_clim!2 = "longitude"<br>data_seasonal_clim&season = season_names<br>data_seasonal_clim&latitude = test_data&latitude<br>data_seasonal_clim&longitude = test_data&longitude<br><br>printVarSummary(data_seasonal_<wbr>clim)<br><br>do ns = 0,3<br> data_seasonal_clim(ns,:,:) = dim_avg_n(data_monthly_mean(i_<wbr>season(ns,:),:,:),0)<br>end do<br><br>printVarSummary(data_seasonal_<wbr>clim)<br><br>if (file_out) then<br> varname = output_var+"_seasonal_clim"<br> print(varname)<br> ncdf->$varname$ = data_seasonal_clim<br>end if<br><br>;*********************<br>end<br><br></div><b>OUTPUT:</b><br><div>***********<br></div><div>[snip]<br></div><div>Variable: data<br>Type: float<br>Total Size: 119537664 bytes<br> 29884416 values<br>Number of Dimensions: 3<br>Dimensions and sizes: [t | 228] x [latitude | 256] x [longitude | 512]<br>Coordinates: <br> t: [ 0..393.75]<br> latitude: [-89.46294..89.46294]<br> longitude: [ 0..359.2969]<br>Number Of Attributes: 13<br> p : 850<br> source : GRIB data<br> name : Z<br> title : Geopotential<br> date : 01/02/79<br> time : 00:00<br> long_name : Geopotential<br> standard_name : geopotential<br> units : m**2 s**-2<br> missing_value : 2e+20<br> _FillValue : 2e+20<br> valid_min : -4084.883<br> valid_max : 487005.9<br><br>Variable: data_monthly_mean<br>Type: float<br>Total Size: 6291456 bytes<br> 1572864 values<br>Number of Dimensions: 3<br>Dimensions and sizes: [month | 12] x [latitude | 256] x [longitude | 512]<br>Coordinates: <br> latitude: [-89.46294..89.46294]<br> longitude: [ 0..359.2969]<br>Number Of Attributes: 1<br> _FillValue : 9.96921e+36<br><br>Variable: data_seasonal_clim<br>Type: float<br>Total Size: 2097152 bytes<br> 524288 values<br>Number of Dimensions: 3<br>Dimensions and sizes: [season | 4] x [latitude | 256] x [longitude | 512]<br>Coordinates: <br> season: [DJF..SON]<br> latitude: [-89.46294..89.46294]<br> longitude: [ 0..359.2969]<br>Number Of Attributes: 1<br> _FillValue : 9.96921e+36<br><br>Variable: varname<br>Type: string<br>Total Size: 8 bytes<br> 1 values<br>Number of Dimensions: 1<br>Dimensions and sizes: [1]<br>Coordinates: <br>(0) z_seasonal_clim<br>fatal:["NclFile.c":432]:<wbr>FileAddVar: an error occurred while adding a variable to a file, check to make sure data type is supported by the output format<br>fatal:["Execute.c":8575]:<wbr>Execute: Error occurred at or near line 110 in file Calc_seasonal_means_Jasmin.ncl<br><br><div><b>NCL_FILEDUMP:</b><br>*********************<br></div><div><br>ncl_filedump Z_seasonal_means_1979_2015.nc <br><br>Variable: f<br>Type: file<br>filename: Z_seasonal_means_1979_2015<br>path: Z_seasonal_means_1979_2015.nc<br> file global attributes:<br> dimensions:<br> season = 4<br> latitude = 256<br> longitude = 512<br> variables:<br> float z_seasonal_clim ( season, latitude, longitude )<br> _FillValue : 9.96921e+36<span class="HOEnZb"><font color="#888888"><br><br><br clear="all"><div><div><div><div><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div>-- <br>Tess Parker<br>Associate Researcher<br>School of Earth, Atmosphere and Environment<br>Room 225, Building 28<br></div><div>9 Rainforest Walk<br></div><div>Monash University, Clayton VIC 3800<br></div><br></div></div></div></div></div></div></div>
</div></div></div></font></span></div></div></div>
<br>______________________________<wbr>_________________<br>
ncl-talk mailing list<br>
<a href="mailto:ncl-talk@ucar.edu">ncl-talk@ucar.edu</a><br>
List instructions, subscriber options, unsubscribe:<br>
<a href="http://mailman.ucar.edu/mailman/listinfo/ncl-talk" rel="noreferrer" target="_blank">http://mailman.ucar.edu/<wbr>mailman/listinfo/ncl-talk</a><br>
<br></blockquote></div><br></div>