PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/_po/roms/roms_ncep/ncep_gencdl.m

http://m--pack.googlecode.com/
MATLAB | 338 lines | 218 code | 62 blank | 58 comment | 21 complexity | e5d44b028faecfe7f7e586f680db0ada MD5 | raw file
  1. function [cdl_name,nc_name]=ncep_gencdl(type,dims,label,ncgen,desc)
  2. %NCEP_GENCDL Create NetCDF cdl and nc file of ROMS bulk forcings
  3. % Generates cdl and NetCDF file for several types of forcings.
  4. % The cdl file has the name type.cdl.
  5. % NetCDF file has the name type_label.nc and is created by ncgen -b.
  6. %
  7. % Syntax:
  8. % [CDL_NAME,NC_NAME] = NCEP_GENCDL(TYPE,DIMS,LABEL,NC,DESC)
  9. %
  10. %
  11. % Inputs:
  12. % TYPE Type of forcing, currently can be:
  13. % - wind
  14. % - cloud
  15. % - radiation
  16. % - humidity
  17. % - pressure
  18. % - precipitation
  19. % - temperature
  20. % - all: all together
  21. % DIMS Dimensions x,y and t [ <x,y,t> ]
  22. % LABEL Label to use in the name of the NetCDF file [ 'frc' ]
  23. % NC String with ncgen executable, if not used, then ncgen is not used
  24. % and .nc file not created
  25. % DESC Description to use as global attribute [ '<type> forcing file; <DEC>' ]
  26. %
  27. % Outputs:
  28. % CDL_NAME Name of the cdl file created
  29. % NC_NAME Name of the NetCDF file created
  30. %
  31. % Comments:
  32. % If NCGEN is not specified, NetCDF file is not created.
  33. % NetCDF is created by ncgen -b type.cdl
  34. %
  35. % Example:
  36. % type = 'cloud';
  37. % dims = [30 20 100];
  38. % label = 'test';
  39. % nc = '/usr/local/NetCDF/bin/ncgen';
  40. % [fcdl,fnc] = ncep_gencdl(type,dims,label,nc)
  41. %
  42. % MMA 28-5-2004, martinho@fis.ua.pt
  43. %
  44. % See also NCEP_GENFRC, NCEP_GEN
  45. % Department of Physics
  46. % University of Aveiro, Portugal
  47. % ??-10-2004 - Added extra data to radiation file
  48. if nargin < 5
  49. desc = '';
  50. end
  51. if nargin < 4
  52. nc = 0;
  53. else
  54. nc = 1;
  55. end
  56. if nargin < 3
  57. label='frc';
  58. end
  59. if nargin < 2
  60. disp('## dims must be specified...');
  61. return
  62. end
  63. if nargin == 0
  64. disp('## type and dims must be specified...');
  65. end
  66. types={'wind','cloud','radiation','humidity','pressure','precipitation','temperature'};
  67. if ~isequal(type,'all')
  68. types={type};
  69. end
  70. cdl_name = [];
  71. nc_name = [];
  72. x = dims(3);
  73. y = dims(2);
  74. t = dims(1);
  75. cdl = [type,'.cdl'];
  76. fid = fopen(cdl,'w');
  77. % ------------------------------------- first line:
  78. fname=[type,'_',label];
  79. fprintf(fid,'netcdf %s {\n',fname);
  80. % ------------------------------------- set dimensions:
  81. fprintf(fid,'dimensions:\n');
  82. fprintf(fid,' xi_rho = %d ;\n',x);
  83. fprintf(fid,' eta_rho = %d ;\n',y);
  84. for n=1:length(types)
  85. [dims,info,info_t,info_file] = get_info(types{n});
  86. for i=1:length(dims)
  87. fprintf(fid,' %s = %d ;\n',dims(i).time,t);
  88. end
  89. end
  90. % ------------------------------------- set variables:
  91. fprintf(fid,'variables:\n');
  92. if isempty(info)
  93. disp(['## unknown type: ',type]);
  94. fclose(fid);
  95. delete(cdl);
  96. return;
  97. end
  98. for n=1:length(types)
  99. [dims,info,info_t,info_file] = get_info(types{n});
  100. % ------------------ set time:
  101. for i=1:length(info_t)
  102. fprintf(fid,' %s\n',info_t(i).name);
  103. fprintf(fid,' %s\n',info_t(i).lname);
  104. fprintf(fid,' %s\n',info_t(i).units);
  105. fprintf(fid,' %s\n',info_t(i).field);
  106. end
  107. % ------------------ set vars:
  108. for i=1:length(info)
  109. fprintf(fid,' %s\n',info(i).name);
  110. fprintf(fid,' %s\n',info(i).lname);
  111. fprintf(fid,' %s\n',info(i).units);
  112. fprintf(fid,' %s\n',info(i).field);
  113. fprintf(fid,' %s\n',info(i).time);
  114. end
  115. end
  116. % ------------------------------------- set global attributes:
  117. if isequal(type,'all')
  118. info_file.description = [':description = "bulk fluxes forcing file" ;'];
  119. end
  120. % apply input description:
  121. info_file.description = [info_file.description(1:end-3),' ',desc,'" ;'];
  122. fprintf(fid,'\n');
  123. fprintf(fid,'// global attributes:\n');
  124. fprintf(fid,' %s\n',info_file.description);
  125. fprintf(fid,' %s\n',info_file.author);
  126. fprintf(fid,' %s\n',info_file.date);
  127. % ------------------------------------- last line:
  128. fprintf(fid,'}');
  129. fclose(fid);
  130. %=============================================== create NetCDF file
  131. if nc
  132. disp(['## creating ',fname,'.nc from ',cdl]);
  133. eval(['! ',ncgen,' -b ',cdl]);
  134. end
  135. %---------------- output:
  136. cdl_name = cdl;
  137. nc_name = [fname,'.nc'];
  138. function [dims,info,info_t,info_file] = get_info(type)
  139. switch type
  140. case 'wind'
  141. dims.time = 'wind_time';
  142. v{1} = 'Uwind';
  143. v{2} = 'Vwind';
  144. info_t.name = ['double ',dims.time,'(',dims.time,') ;' ];
  145. info_t.lname = [' ',dims.time,':long_name = "surface wind time" ;' ];
  146. info_t.units = [' ',dims.time,':units = "day" ;' ];
  147. info_t.field = [' ',dims.time,':field = "time, scalar, series" ;' ];
  148. %info_t.cycle = ' wind_time:cycle_length = 360.0 ;'; % cycle not needed!
  149. info(1).name = ['float ',v{1},'(',dims.time,', eta_rho, xi_rho) ;' ];
  150. info(1).lname = [' ',v{1},':long_name = "surface u-wind component" ;' ];
  151. info(1).units = [' ',v{1},':units = "meter second-1" ;' ];
  152. info(1).field = [' ',v{1},':field = "u-wind, scalar, series" ;' ];
  153. info(1).time = [' ',v{1},':time = "',dims.time,'" ;' ];
  154. info(2).name = ['float ',v{2},'(',dims.time,', eta_rho, xi_rho) ;' ];
  155. info(2).lname = [' ',v{2},':long_name = "surface v-wind component" ;' ];
  156. info(2).units = [' ',v{2},':units = "meter second-1" ;' ];
  157. info(2).field = [' ',v{2},':field = "v-wind, scalar, series" ;' ];
  158. info(2).time = [' ',v{2},':time = "',dims.time,'" ;' ];
  159. case 'cloud'
  160. dims.time = 'cloud_time'; v = 'cloud';
  161. info_t.name = ['double ',dims.time,'(',dims.time,') ;' ];
  162. info_t.lname = [' ',dims.time,':long_name = "time for cloud fraction" ;' ];
  163. info_t.units = [' ',dims.time,':units = "day" ;' ];
  164. info_t.field = [' ',dims.time,':field = "time, scalar, series" ;' ];
  165. info.name = ['float ',v,'(',dims.time,', eta_rho, xi_rho) ;' ];
  166. info.lname = [' ',v,':long_name = "cloud fraction" ;' ];
  167. info.units = [' ',v,':units = "nondimensional" ;' ];
  168. info.field = [' ',v,':field = "cloud, scalar, series" ;' ];
  169. info.time = [' ',v,':time = "',dims.time,'" ;' ];
  170. case 'radiation'
  171. dims(1).time = 'srf_time'; v{1} = 'swrad'; % shortwave
  172. dims(2).time = 'lrf_time'; v{2} = 'lwrad'; % longwave
  173. dims(3).time = 'lhf_time'; v{3} = 'latent'; % latent
  174. dims(4).time = 'sen_time'; v{4} = 'sensible'; % sensible
  175. dims(5).time = 'shf_time'; v{5} = 'shflux'; % surface net
  176. info_t(1).name = ['double ',dims(1).time,'(',dims(1).time,') ;' ];
  177. info_t(1).lname = [' ',dims(1).time,':long_name = "time for solar shortwave radiation flux" ;' ];
  178. info_t(1).units = [' ',dims(1).time,':units = "day" ;' ];
  179. info_t(1).field = [' ',dims(1).time,':field = "time, scalar, series" ;' ];
  180. info_t(2).name = ['double ',dims(2).time,'(',dims(2).time,') ;'];
  181. info_t(2).lname = [' ',dims(2).time,':long_name = "time for net longwave radiation flux" ;' ];
  182. info_t(2).units = [' ',dims(2).time,':units = "day" ;' ];
  183. info_t(2).field = [' ',dims(2).time,':field = "time, scalar, series" ;' ];
  184. info_t(3).name = ['double ',dims(3).time,'(',dims(3).time,') ;' ];
  185. info_t(3).lname = [' ',dims(3).time,':long_name = "time for net latent heat flux" ;' ];
  186. info_t(3).units = [' ',dims(3).time,':units = "day" ;' ];
  187. info_t(3).field = [' ',dims(3).time,':field = "time, scalar, series" ;' ];
  188. info_t(4).name = ['double ',dims(4).time,'(',dims(4).time,') ;' ];
  189. info_t(4).lname = [' ',dims(4).time,':long_name = "time for sensible heat flux" ;' ];
  190. info_t(4).units = [' ',dims(4).time,':units = "day" ;' ];
  191. info_t(4).field = [' ',dims(4).time,':field = "time, scalar, series" ;' ];
  192. info_t(5).name = ['double ',dims(5).time,'(',dims(5).time,') ;' ];
  193. info_t(5).lname = [' ',dims(5).time,':long_name = "time for surface net heat flux" ;' ];
  194. info_t(5).units = [' ',dims(5).time,':units = "day" ;' ];
  195. info_t(5).field = [' ',dims(5).time,':field = "time, scalar, series" ;' ];
  196. info(1).name = ['float ',v{1},'(',dims(1).time,', eta_rho, xi_rho) ;' ];
  197. info(1).lname = [' ',v{1},':long_name = "solar shortwave radiation flux" ;' ];
  198. info(1).units = [' ',v{1},':units = "Watts meter-2" ;' ];
  199. info(1).field = [' ',v{1},':field = "shortwave radiation, scalar, series" ;' ];
  200. info(1).time = [' ',v{1},':time = "',dims(1).time,'" ;' ];
  201. info(2).name = ['float ',v{2},'(',dims(2).time,', eta_rho, xi_rho) ;' ];
  202. info(2).lname = [' ',v{2},':long_name = "net longwave radiation flux" ;' ];
  203. info(2).units = [' ',v{2},':units = "Watts meter-2" ;' ];
  204. info(2).field = [' ',v{2},':field = "longwave radiation, scalar, series" ;' ];
  205. info(2).time = [' ',v{2},':time = "',dims(2).time,'" ;' ];
  206. info(3).name = ['float ',v{3},'(',dims(3).time,', eta_rho, xi_rho) ;' ];
  207. info(3).lname = [' ',v{3},':long_name = "net latent heat flux" ;' ];
  208. info(3).units = [' ',v{3},':units = "Watts meter-2" ;' ];
  209. info(3).field = [' ',v{3},':field = "latent heat flux, scalar, series" ;' ];
  210. info(3).time = [' ',v{3},':time = "',dims(3).time,'" ;' ];
  211. info(4).name = ['float ',v{4},'(',dims(4).time,', eta_rho, xi_rho) ;' ];
  212. info(4).lname = [' ',v{4},':long_name = "net sensible heat flux" ;' ];
  213. info(4).units = [' ',v{4},':units = "Watts meter-2" ;' ];
  214. info(4).field = [' ',v{4},':field = "sensible heat flux, scalar, series" ;' ];
  215. info(4).time = [' ',v{4},':time = "',dims(4).time,'" ;' ];
  216. info(5).name = ['float ',v{5},'(',dims(5).time,', eta_rho, xi_rho) ;' ];
  217. info(5).lname = [' ',v{5},':long_name = "surface net heat flux" ;' ];
  218. info(5).units = [' ',v{5},':units = "Watts meter-2" ;' ];
  219. info(5).field = [' ',v{5},':field = "surface net heat flux, scalar, series" ;' ];
  220. info(5).time = [' ',v{5},':time = "',dims(5).time,'" ;' ];
  221. case 'humidity'
  222. dims.time = 'qair_time'; v = 'Qair';
  223. info_t.name = ['double ',dims.time,'(',dims.time,') ;' ];
  224. info_t.lname = [' ',dims.time,':long_name = "time for surface air relative humidity" ;' ];
  225. info_t.units = [' ',dims.time,':units = "day" ;' ];
  226. info_t.field = [' ',dims.time,':field = "time, scalar, series" ;' ];
  227. info.name = ['float ',v,'(',dims.time,', eta_rho, xi_rho) ;' ];
  228. info.lname = [' ',v,':long_name = "surface air relative humidity" ;' ];
  229. info.units = [' ',v,':units = "percentage" ;' ];
  230. info.field = [' ',v,':field = "Qair, scalar, series" ;' ];
  231. info.time = [' ',v,':time = "',dims.time,'" ;' ];
  232. case 'pressure'
  233. dims.time = 'pair_time'; v = 'Pair';
  234. info_t.name = ['double ',dims.time,'(',dims.time,') ;' ];
  235. info_t.lname = [' ',dims.time,':long_name = "time for surface air pressure" ;' ];
  236. info_t.units = [' ',dims.time,':units = "day" ;' ];
  237. info_t.field = [' ',dims.time,':field = "time, scalar, series" ;' ];
  238. info.name = ['float ',v,'(',dims.time,', eta_rho, xi_rho) ;' ];
  239. info.lname = [' ',v,':long_name = "surface air pressure" ;' ];
  240. info.units = [' ',v,':units = "milibar" ;' ];
  241. info.field = [' ',v,':field = "Pair, scalar, series" ;' ];
  242. info.time = [' ',v,':time = "',dims.time,'" ;' ];
  243. case 'precipitation'
  244. dims.time = 'rain_time'; v = 'rain';
  245. info_t.name = ['double ',dims.time,'(',dims.time,') ;' ];
  246. info_t.lname = [' ',dims.time,':long_name = "time for rain fall rate" ;' ];
  247. info_t.units = [' ',dims.time,':units = "day" ;' ];
  248. info_t.field = [' ',dims.time,':field = "time, scalar, series" ;' ];
  249. info.name = ['float ',v,'(',dims.time,', eta_rho, xi_rho) ;' ];
  250. info.lname = [' ',v,':long_name = "rain fall rate" ;' ];
  251. info.units = [' ',v,':units = "kilogram meter-2 second-1" ;' ];
  252. info.field = [' ',v,':field = "rain, scalar, series" ;' ];
  253. info.time = [' ',v,':time = "',dims.time,'" ;' ];
  254. case 'temperature'
  255. dims.time = 'tair_time'; v = 'Tair';
  256. info_t.name = ['double ',dims.time,'(',dims.time,') ;' ];
  257. info_t.lname = [' ',dims.time,':long_name = "time for surface air temperature" ;' ];
  258. info_t.units = [' ',dims.time,':units = "day" ;' ];
  259. info_t.field = [' ',dims.time,':field = "time, scalar, series" ;' ];
  260. info.name = ['float ',v,'(',dims.time,', eta_rho, xi_rho) ;' ];
  261. info.lname = [' ',v,':long_name = "surface air temperature" ;' ];
  262. info.units = [' ',v,':units = "Celsius" ;' ];
  263. info.field = [' ',v,':field = "Tair, scalar, series" ;' ];
  264. info.time = [' ',v,':time = "',dims.time,'" ;' ];
  265. otherwise
  266. dims = [];
  267. info_t = [];
  268. info = [];
  269. end
  270. info_file.description = [':description = "',type,' forcing file" ;'];
  271. evalc('[a,logname]=unix(''whoami'');','logname=[];');
  272. %strrep(logname,char(10),'p'); % remove line feed (ascii 10)
  273. logname=logname(1:end-1);
  274. info_file.author = [':author = "',logname,'" ;'];
  275. info_file.date = [':date = "',datestr(now),'" ;'];
  276. return