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" begin ;## path to wpp outfiles path = "/import/wrkdir1/asemenov/ERA_INTERIM_NEW/NOAA_optimum_interpolation_analysis/15_March/" ;## file name files = systemfunc("ls "+path+"avhrr-only-v2.*") ;## create array s = new ( (/30,1,1,720,1440/), "float" ) s@_FillValue = -99999 s = s@_FillValue s@long_name = "sst" s@units = "oC" s!0 = "file_n" s!1 = "time" s!2 = "zlev" s!3 = "lat" s!4 = "lon" printVarSummary(s) i = new ( (/30,1,1,720,1440/), "float" ) i@_FillValue = -99999 i = i@_FillValue i@long_name = "sea ice concentration" i@units = "%" i!0 = "file_n" i!1 = "time" i!2 = "zlev" i!3 = "lat" i!4 = "lon" printVarSummary(i) ;## read throught the files do j = 0, dimsizes(files)-1 fname = files(j) ;## open file print("Opening file: "+fname+".nc") f = addfile (fname+".nc", "r") lat=f->lat lon=f->lon zlev=f->zlev time=f->time ; sa=f->sst sa=short2flt(f->sst) printVarSummary(sa) ; ic=f->ice ic=short2flt(f->ice) printVarSummary(ic) s(j,:,:,:,:)=sa(:,:,:,:) i(j,:,:,:,:)=ic(:,:,:,:) end do printVarSummary(s) printMinMax(s,True) printVarSummary(i) printMinMax(i,True) S_avg=dim_avg_n_Wrap(s,0) I_avg=dim_avg_n_Wrap(i,0) printVarSummary(S_avg) printMinMax(S_avg,True) printVarSummary(I_avg) printMinMax(I_avg,True) ;=================================================================== ; Assume variables T, PS and ORO exist and that they have ; associated meta data: (a) coordinate variables time, lev, lat, lon ; and (b) attributes ;=================================================================== ntim = dimsizes(time) ; get dimension sizes nlev = dimsizes(zlev) nlat = dimsizes(lat) nlon = dimsizes(lon) diro = "./" ; Output directory filo = "example.nc" ; Output file system("/bin/rm -f " + diro + filo) ; remove if exists fout = addfile (diro + filo, "c") ; open output file ;=================================================================== ; explicitly declare file definition mode. Improve efficiency. ;=================================================================== setfileoption(fout,"DefineMode",True) ;=================================================================== ; create global attributes of the file ;=================================================================== fAtt = True ; assign file attributes fAtt@title = "NCL Efficient Approach to netCDF Creation" fAtt@source_file = "avhrr-only-v2.19820315.nc" fAtt@Conventions = "None" fAtt@creation_date = systemfunc ("date") fileattdef( fout, fAtt ) ; copy file attributes ;=================================================================== ; predefine the coordinate variables and their dimensionality ; Note: to get an UNLIMITED record dimension, we set the dimensionality ; to -1 (or the actual size) and set the dimension name to True. ;=================================================================== dimNames = (/"time", "lat", "lon", "zlev"/) dimSizes = (/ -1 , nlat, nlon, nlev /) dimUnlim = (/ True , False, False, False/) filedimdef(fout,dimNames,dimSizes,dimUnlim) ;=================================================================== ; predefine the the dimensionality of the variables to be written out ;=================================================================== ; Here we are using NCL functions to facilitate defining ; each variable's dimension name(s) and type. ; The following could be replaced with explicit, user defined dimension ; names different from those associated with the variable in memory. ; Say, PS(time,lat,lon) in the NCL script. They could be redefined for the file via: ; filevardef(fout, "PS" ,typeof(PS) ,(/"TIME","latitude","longitude"/)) ;=================================================================== filevardef(fout, "time" ,typeof(time),getvardims(time)) filevardef(fout, "zlev" ,typeof(zlev),getvardims(zlev) ) filevardef(fout, "lat" ,typeof(lat),getvardims(lat)) filevardef(fout, "lon" ,typeof(lon),getvardims(lon)) filevardef(fout, "S_avg" ,typeof(S_avg) ,getvardims(S_avg)) filevardef(fout, "I_avg" ,typeof(I_avg) ,getvardims(I_avg)) ; filevardef(fout, "TOPOG",typeof(ORO),getvardims(ORO)) ; variable name on the file ; different from name on script ;=================================================================== ; Copy attributes associated with each variable to the file ; All attributes associated with each variable will be copied. ;==================================================================== filevarattdef(fout,"S_avg",S_avg) ; copy T attributes filevarattdef(fout,"time" ,time) ; copy time attributes filevarattdef(fout,"zlev" ,zlev) ; copy lev attributes filevarattdef(fout,"lat" ,lat) ; copy lat attributes filevarattdef(fout,"lon" ,lon) ; copy lon attributes filevarattdef(fout,"I_avg" ,I_avg) ; copy PS attributes ; filevarattdef(fout,"TOPOG",ORO) ; copy TOPOG attributes ;=================================================================== ; explicitly exit file definition mode. **NOT REQUIRED** ;=================================================================== setfileoption(fout,"DefineMode",False) ;=================================================================== ; output only the data values since the dimensionality and such have ; been predefined. The "(/", "/)" syntax tells NCL to only output the ; data values to the predefined locations on the file. ;==================================================================== fout->time = (/time/) fout->zlev = (/zlev/) fout->lat = (/lat/) fout->lon = (/lon/) fout->I_avg = (/I_avg/) fout->S_avg = (/S_avg/) ; fout->TOPOG = (/ORO/) end