Home > fvcom_prepro > wrf2fvcom_U10V10.m

wrf2fvcom_U10V10

PURPOSE ^

function wrf2fvcom_U10V10(wrf_file,fvcom_grid_file,fvcom_forcing_file,infos)

SYNOPSIS ^

This is a script file.

DESCRIPTION ^

function wrf2fvcom_U10V10(wrf_file,fvcom_grid_file,fvcom_forcing_file,infos)
------------------------------------------------------------------------
 interpolate wind heat fields from WRF onto the FVCOM mesh 
------------------------------------------------------------------------
warning off

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 %function wrf2fvcom_U10V10(wrf_file,fvcom_grid_file,fvcom_forcing_file,infos)
0002 %------------------------------------------------------------------------
0003 % interpolate wind heat fields from WRF onto the FVCOM mesh
0004 %------------------------------------------------------------------------
0005 %warning off
0006 wrf_file = 'Skagit_WRF_U10V10_2009.nc';
0007 fvcom_grid_file =a 'skg4.3_grd.dat';
0008 fvcom_forcing_file = 'skagit_2009_U10V10.nc';
0009 infos = 'wrf from D. Ralston';
0010 %------------------------------------------------------------------------
0011 % open wrf file and read header
0012 %------------------------------------------------------------------------
0013 if(~exist(wrf_file))
0014    error(['file: ' wrf_file ' does not exist']);
0015 end;
0016 
0017 % open wrf data and check for time range
0018 nc = netcdf(wrf_file);
0019 tmp = nc{'Times'}(:);
0020 wrftime = greg2mjulian(str2num(tmp(:,1:4)),str2num(tmp(:,6:7)),...
0021            str2num(tmp(:,9:10)),str2num(tmp(:,12:13)),0,0);
0022 fprintf('beg time of WRF data %s\n',tmp(1,:));
0023 fprintf('end time of WRF data %s\n',tmp(end,:));
0024 ntimes = prod(size(wrftime));
0025 wrflat = nc{'XLAT'}(:,:);
0026 wrflon = nc{'XLONG'}(:,:);
0027 [nlat,nlon] = size(wrflat);
0028 
0029 wrfx = zeros(nlat,nlon);
0030 wrfy = zeros(nlat,nlon);
0031 
0032 % project wrf grid to Euclidean for interpolation
0033 for i=1:nlat
0034   [wrflon(i,:),wrflat(i,:),wrfx(i,:),wrfy(i,:)] = ...
0035       my_project(wrflon(i,:),wrflat(i,:),wrfx(i,:),wrfy(i,:),'forward');
0036 end;
0037 
0038 % load FVCOM mesh
0039 Mobj = read_fvcom_mesh(fvcom_grid_file);
0040 tri = Mobj.tri;
0041 x   = Mobj.x;
0042 y   = Mobj.y;
0043 nVerts = Mobj.nVerts;
0044 nElems = Mobj.nElems;
0045 nv   = tri;  
0046 fprintf('info for fvcom domain\n');
0047 fprintf('number of nodes: %d\n',nVerts);
0048 fprintf('number of elems: %d\n',nElems);
0049 
0050 xc = zeros(nElems,1);
0051 yc = zeros(nElems,1);
0052 for i=1:nElems
0053   xc(i) = sum(x(tri(i,1:3)))/3.;
0054   yc(i) = sum(y(tri(i,1:3)))/3.;
0055 end;
0056 
0057 %---------------------------------------------------------------
0058 % dump header for netcdf FVCOM forcing file
0059 %---------------------------------------------------------------
0060 nc = netcdf(fvcom_forcing_file, 'clobber');            
0061 nc.type = 'FVCOM U10/V10 Forcing File' ;
0062 nc.source = 'fvcom grid (unstructured) surface forcing';
0063 nc.references = 'http://fvcom.smast.umassd.edu, http://codfish.smast.umassd.edu'; 
0064 nc.institution = 'School for Marine Science and Technology' ;
0065 nc.history = 'wrf_2_fvcom_U10V10.m';
0066 nc.infos = infos;
0067   
0068 % dimensions
0069 nc('three') = 3;
0070 nc('nele') = int8(nElems);
0071 nc('node') = nVerts;
0072 nc('time') = 0;
0073 
0074 % time vars
0075 nc{'time'} = ncfloat('time');
0076 nc{'time'}.long_name = 'time';
0077 nc{'time'}.units = 'days since 1858-11-17 00:00:00';
0078 nc{'time'}.format = 'modified julian day (MJD)';
0079 nc{'time'}.time_zone = 'UTC';
0080   
0081 nc{'Itime'} = ncint('time');
0082 nc{'Itime'}.units = 'days since 1858-11-17 00:00:00';
0083 nc{'Itime'}.format = 'modified julian day (MJD)';
0084 nc{'Itime'}.time_zone = 'UTC';
0085 
0086 nc{'Itime2'} = ncint('time');
0087 nc{'Itime2'}.units = 'msec since 00:00:00';
0088 nc{'Itime2'}.time_zone = 'UTC';
0089 
0090 nc{'x'} = ncint('node');
0091 nc{'x'}.long_name = 'nodal x-coordinate';
0092 nc{'x'}.units = 'm';
0093   
0094 nc{'y'} = ncint('node');
0095 nc{'y'}.long_name = 'nodal y-coordinate';
0096 nc{'y'}.units = 'm';
0097 
0098 nc{'nv'} = ncint('three','nele');
0099 nc{'nv'}.long_name = 'nodes surrounding element';
0100 nc{'nv'}(1:3,1:nElems) = tri';
0101 
0102 nc{'U10'} = ncfloat('time','nele');
0103 nc{'U10'}.long_name = 'Eastward 10-m Velocity';
0104 nc{'U10'}.standard_name = 'Eastward Wind Speed';
0105 nc{'U10'}.units = 'm/s';
0106 nc{'U10'}.grid = 'fvcom_grid';
0107 nc{'U10'}.type = 'data';
0108 
0109 nc{'V10'} = ncfloat('time','nele');
0110 nc{'V10'}.long_name = 'Northward 10-m Velocity';
0111 nc{'V10'}.standard_name = 'Northtward Wind Speed';
0112 nc{'V10'}.units = 'm/s';
0113 nc{'V10'}.grid = 'fvcom_grid';
0114 nc{'V10'}.type = 'data';
0115 
0116 nc{'U10_node'} = ncfloat('time','node');
0117 nc{'U10_node'}.long_name = 'Eastward 10-m Velocity';
0118 nc{'U10_node'}.standard_name = 'Eastward Wind Speed';
0119 nc{'U10_node'}.units = 'm/s';
0120 nc{'U10_node'}.grid = 'fvcom_grid';
0121 nc{'U10_node'}.type = 'data';
0122 
0123 nc{'V10_node'} = ncfloat('time','node');
0124 nc{'V10_node'}.long_name = 'Northward 10-m Velocity';
0125 nc{'V10_node'}.standard_name = 'Northtward Wind Speed';
0126 nc{'V10_node'}.units = 'm/s';
0127 nc{'V10_node'}.grid = 'fvcom_grid';
0128 nc{'V10_node'}.type = 'data';
0129 
0130 
0131 % dump time
0132 nc{'time'}(1:ntimes) = wrftime;
0133 nc{'Itime'}(1:ntimes) = floor(wrftime); 
0134 nc{'Itime2'}(1:ntimes) = mod(wrftime,1)*24*3600*1000.;
0135 nc{'x'}(1:nVerts) = x;
0136 nc{'y'}(1:nVerts) = y;
0137 
0138 % read data from WRF grid, interpolate to FVCOM mesh
0139 fvcom_u10   = zeros(nElems,1);
0140 fvcom_v10   = zeros(nElems,1);
0141 fvcom_u10_node   = zeros(nVerts,1);
0142 fvcom_v10_node   = zeros(nVerts,1);
0143 
0144 nc2 = netcdf(wrf_file);
0145 icnt = 1;
0146 for i=1:ntimes
0147   fprintf('interpolating frame %d of %d\n',i,ntimes);
0148   U10  = nc2{'U10'}(i,:,:);
0149   V10  = nc2{'V10'}(i,:,:);
0150   fvcom_u10_node  = griddata(wrfx,wrfy,U10,x,y);
0151   fvcom_v10_node  = griddata(wrfx,wrfy,V10,x,y);
0152   for j=1:nElems
0153      fvcom_u10(j) = sum(fvcom_u10_node(tri(j,1:3)))/3.; 
0154      fvcom_v10(j) = sum(fvcom_v10_node(tri(j,1:3)))/3.; 
0155   end;
0156   nc{'U10'}(icnt,1:nElems) = fvcom_u10;
0157   nc{'V10'}(icnt,1:nElems) = fvcom_v10;
0158   nc{'U10_node'}(icnt,1:nVerts) = fvcom_u10_node;
0159   nc{'V10_node'}(icnt,1:nVerts) = fvcom_v10_node;
0160   icnt = icnt + 1;
0161 end;
0162 fprintf('interpolation complete\n');
0163 
0164 ierr = close(nc);
0165 ierr = close(nc2);

Generated on Wed 20-Feb-2019 16:06:01 by m2html © 2005