Documentation of write_covar


Global Index (short | long) | Local contents | Local Index (short | long)


Help text

cd /home/disk/hayes2/dvimont/nmc/ugrd.vgrd

Cross-Reference Information

This script calls

Listing of script write_covar


clear
ncstartup
cd /home/disk/hayes2/dvimont/nmc/vgrd.tmp

!cp -f ../biff2.nc biff.nc

%  Get lat lon and level data from original file

nc = netcdf('biff.nc');
  lat = nc{'lat'}(:);
  lon = nc{'lon'}(:);
  level = nc{'level'}(:);
  time = nc{'time'}(:);
nc = close(nc);

nlon = length(lon);
nlat = length(lat);
nlev = length(level);
ntim = 480;

cdfid = ncmex('OPEN', 'biff.nc', 'write')
status = ncmex('REDEF', cdfid)
[varid, rcode] = ncmex('VARID', cdfid, 'vt')
status = ncmex('VARRENAME', cdfid, varid, 'uv')
status = ncmex('CLOSE', cdfid)


nc = netcdf('biff.nc', 'write');
  nc.title = 'Covar. U and V';
  nc.history = 'Converted to MATLAB from GRADS by Dan Vimont, 29 March, 1999';
  nc.description = 'Data from NCEP initialized reanalysis (4x/day).  These are interpolated to pressure surfaces from model (sigma) surfaces'
  nc.platform = 'Model'
  nc.Conventions = 'COARDS';

  v = nc{'vt'};
  v.units = 'K*m/s';
  v.long_name = 'Covariance between v and T';
  v.var_desc = 'UV-Covar';

nc = close(nc);

%  Calculated ahead of time, the add_offset and scale_factor
%
%  Note:  single precision allows 16 bits, one of which is used
%  as the +/- sign.  Usually the mv is 32766 (2^15 - 2).  So, 
%  constrain the adjusted series between +/- 32765.
%
%  You can get max and min for the data through the following
%  routine:
%

cd /home/disk/hayes2/dvimont/nmc/vgrd.tmp
for ind = 1:nlev;

  filin = ['vgrd.tmp.' num2str(level(ind)) '.mon.bin']
  fid = fopen(filin, 'r', 'b');
  tmp = fread(fid, inf, 'float32');

  amax(ind) = max(tmp);
  amin(ind) = min(tmp);

end

%  Now, fit max to 32765, min to -32765

  ao = mean([max(amax) min(amin)])
  ao = 27.44
  sf = (max(amax)-min(amin))/(2*32765)
  sf = 0.02;
%  nc{'vt'}.add_offset(:) = ao;
%  nc{'vt'}.scale_factor(:) = sf;

!ncatted -a add_offset,vt,o,f,27.44 biff.nc
!ncatted -a scale_factor,vt,o,f,0.014 biff.nc

nc = netcdf('biff.nc', 'write');

for ind = 1:nlev;

  filin = ['vgrd.tmp.' num2str(level(ind)) '.mon.bin']
  fid = fopen(filin, 'r', 'b');
  tmp = fread(fid, inf, 'float32');

%  Reshape the data into netcdf format

  tmp = reshape(tmp, nlon, nlat, ntim);
  xdim = size(tmp);
  [tmp, xdim] = shift_dim(tmp, xdim, [2 3]);
  tmp = reshape(tmp, ntim, nlat, nlon);
  tmp = reshape(shiftdim(tmp, 1), nlat, nlon*ntim);
  tmp = flipud(tmp);
  tmp = shiftdim(reshape(tmp, nlat, nlon, ntim), 2);
  tmp = reshape(tmp, ntim, 1, nlat, nlon);

  tem = tmp - ao;
  tem = tem ./ sf;

  nc{'vt'}(:,ind,:,:) = tem;

end

nc = close(nc);

nc = netcdf('biff.nc', 'write');

nc{'vt'}.missing_value(:) = [];