[ncl-talk] Dynamic variable names output to netcdf file

Tess Parker tess.parker at monash.edu
Fri Oct 7 05:25:42 MDT 2016


Hi -

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.

Thanks as always for your help!

*NCL SCRIPT:*
****************

load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/contrib/ut_string.ncl"
;/Calc_seasonal_means_Jasmin.ncl

begin
;
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

data_dir = "/badc/ecmwf-era-interim/data/gg/"

years         = ispan(1979,2015,1)
clim_years    = years
seas_len      = 3
months        = ispan(1,12,1)
month_string  = new(12,string)
do aa = 0,11
   if(months(aa).lt.10) then
      month_string(aa) = "0"+months(aa)
   else
      month_string(aa) = ""+months(aa)
   end if
end do
month_names  = (/ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec" /)
month_array  = (/"J","F","M","A","M","J","J","A","S","O","N","D"/)

season       = (/ (/12,1,2/),(/3,4,5/),(/6,7,8/),(/9,10,11/) /)
i_season     = season-1
season_names = (/ "DJF","MAM","JJA","SON"/)

input_var     = "Z"
input_lev     = 850
input_dir     = "ap"         ;ap, as, at, av

file_out    = True
out_file    = ""+input_var+"_seasonal_means_1979_2015.nc"
output_var  = str_lower(input_var)

if (file_out) then
   system("rm -f "+out_file)
   ncdf = addfile(out_file, "c")
end if

;; Test file for data dimensions
;;==============================

test_file = systemfunc("ls "+data_dir+input_dir+"/1979/01/01/
ggap197901010000.nc")
testf     = addfile(test_file,"r")
test_data = testf->$input_var$(:,{input_lev},::-1,:)
test_dims = dimsizes(test_data)
printVarSummary(test_data)
print(test_dims)

data_monthly_mean           = new((/12,test_dims(1),test_dims(2)/),float)
data_monthly_mean!0         = "month"
data_monthly_mean!1         = "latitude"
data_monthly_mean!2         = "longitude"
data_monthly_mean&latitude  = test_data&latitude
data_monthly_mean&longitude = test_data&longitude
printVarSummary(data_monthly_mean)

;; Read in 6-hourly data and construct monthly means
;;==================================================

do jj = 0, 1;dimsizes(months)-1
   in_files = systemfunc("ls
"+data_dir+input_dir+"/19{79,80}/"+month_string(jj)+"/*/*.nc")
   ;in_files = systemfunc("ls
"+data_dir+input_dir+"/*/"+month_string(jj)+"/*/*.nc")
   printVarSummary(in_files)
   print(in_files)
   inf = addfiles(in_files,"r")
   ListSetType(inf,"cat")
   data = inf[:]->$input_var$(:,{input_lev},::-1,:)
   printVarSummary(data)
   data_monthly_mean(jj,:,:) = dim_avg_n(data,0)
   delete([/in_files,inf,data/])
end do

printVarSummary(data_monthly_mean)

data_seasonal_clim           = new((/4,test_dims(1),test_dims(2)/),float)
data_seasonal_clim!0         = "season"
data_seasonal_clim!1         = "latitude"
data_seasonal_clim!2         = "longitude"
data_seasonal_clim&season    = season_names
data_seasonal_clim&latitude  = test_data&latitude
data_seasonal_clim&longitude = test_data&longitude

printVarSummary(data_seasonal_clim)

do ns = 0,3
   data_seasonal_clim(ns,:,:) =
dim_avg_n(data_monthly_mean(i_season(ns,:),:,:),0)
end do

printVarSummary(data_seasonal_clim)

if (file_out) then
   varname = output_var+"_seasonal_clim"
   print(varname)
   ncdf->$varname$ = data_seasonal_clim
end if

;*********************
end

*OUTPUT:*
***********
[snip]
Variable: data
Type: float
Total Size: 119537664 bytes
            29884416 values
Number of Dimensions: 3
Dimensions and sizes:    [t | 228] x [latitude | 256] x [longitude | 512]
Coordinates:
            t: [ 0..393.75]
            latitude: [-89.46294..89.46294]
            longitude: [ 0..359.2969]
Number Of Attributes: 13
  p :    850
  source :    GRIB data
  name :    Z
  title :    Geopotential
  date :    01/02/79
  time :    00:00
  long_name :    Geopotential
  standard_name :    geopotential
  units :    m**2 s**-2
  missing_value :    2e+20
  _FillValue :    2e+20
  valid_min :    -4084.883
  valid_max :    487005.9

Variable: data_monthly_mean
Type: float
Total Size: 6291456 bytes
            1572864 values
Number of Dimensions: 3
Dimensions and sizes:    [month | 12] x [latitude | 256] x [longitude | 512]
Coordinates:
            latitude: [-89.46294..89.46294]
            longitude: [ 0..359.2969]
Number Of Attributes: 1
  _FillValue :    9.96921e+36

Variable: data_seasonal_clim
Type: float
Total Size: 2097152 bytes
            524288 values
Number of Dimensions: 3
Dimensions and sizes:    [season | 4] x [latitude | 256] x [longitude | 512]
Coordinates:
            season: [DJF..SON]
            latitude: [-89.46294..89.46294]
            longitude: [ 0..359.2969]
Number Of Attributes: 1
  _FillValue :    9.96921e+36

Variable: varname
Type: string
Total Size: 8 bytes
            1 values
Number of Dimensions: 1
Dimensions and sizes:    [1]
Coordinates:
(0)    z_seasonal_clim
fatal:["NclFile.c":432]:FileAddVar: an error occurred while adding a
variable to a file, check to make sure data type is supported by the output
format
fatal:["Execute.c":8575]:Execute: Error occurred at or near line 110 in
file Calc_seasonal_means_Jasmin.ncl

*NCL_FILEDUMP:*
*********************

ncl_filedump Z_seasonal_means_1979_2015.nc

Variable: f
Type: file
filename:    Z_seasonal_means_1979_2015
path:    Z_seasonal_means_1979_2015.nc
   file global attributes:
   dimensions:
      season = 4
      latitude = 256
      longitude = 512
   variables:
      float z_seasonal_clim ( season, latitude, longitude )
         _FillValue :    9.96921e+36


-- 
Tess Parker
Associate Researcher
School of Earth, Atmosphere and Environment
Room 225, Building 28
9 Rainforest Walk
Monash University, Clayton VIC 3800
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20161007/fadc2fc7/attachment.html 


More information about the ncl-talk mailing list