indexing typo in phys/module_fr_sfire_atm.F/interpolate_wind2fire_height/interpolate_h
[wrf-fire.git] / other / tign_history_api / tign_history.m
blobfe8c5623869c9ce3cb29e5307702fa92e6aa2741
1 % !*** tign_history ***
2 % !
3 % ! A simple api for reading/writing fire histories in wrfinput files for 
4 % ! gradual ignition.
5 % !
6 % ! 1. float tign_HIST(nx,ny) contains the fire history in the form of 
7 % !    seconds from ignition at each point.
8 % !
9 % ! The api takes care of all the particulars of reading/writing... it should
10 % ! be invisible to the caller.  There are 3 subroutines
11 % ! contained in this api (see implementation for calling syntax):
12 % !
13 % ! subroutine:
14 % !   write_tign_history, write (or add) ignition history data to
15 % !                      the input file
16 % !   read_tign_history, read all history data from the 
17 % !                     input file
18 % !   get_grid_info, get any relevant information (such as grid size) from 
19 % !                  input file
21 classdef tign_history
22     properties(Constant,Access=protected)
23         filefmt='wrfinput_d%02i';
24         xname='west_east_subgrid';
25         yname='south_north_subgrid';
26                 tname='Time';
27         xatm='west_east_stag';
28         yatm='south_north_stag';
29         tign_hist_name='TIGN_G';
30         dtname='DT';
31         dxname='DX';
32         dyname='DY';
33         xtype='float';
34         invalid=-9999999;
35                 write_time=0;
36     end
37     methods(Static)
38         function write_tign_history(idom,tign)
39             % The subroutine writes a level function to a wrfinput file in the
40             % current directory.
41             %
42             % idom : Input integer describing the the domain number that we will
43             %        write the array to.  This is only to determine the file name
44             %        as printf 'wrfinput_d%02i' idom.
45             %
46             % tign(nx,ny) : The fire history written to the file.
47             %
48             % Input arrays must have the correct size according the output of
49             % get_grid_info.
50             
51             f=tign_history.get_file_name(idom);
52             n=netcdf.open(f,'NC_WRITE');
53             netcdf.redef(n);
54            
55                         tdim=netcdf.inqdimid(n,tign_history.tname);
56             xdim=netcdf.inqdimid(n,tign_history.xname);
57             ydim=netcdf.inqdimid(n,tign_history.yname);
58             try
59                 vhid=netcdf.defvar(n,tign_history.tign_hist_name,tign_history.xtype,[xdim ydim tdim]);
60             catch
61                 vhid=netcdf.inqvarid(n,tign_history.tign_hist_name);
62             end
63             netcdf.enddef(n);
64             
65             nx=tign_history.get_grid_info(idom,'nx');
66             ny=tign_history.get_grid_info(idom,'ny');
67             
68             netcdf.putVar(n,vhid,[0 0 tign_history.write_time],[nx ny 1],tign);
69             netcdf.close(n);
70         end
71         function tign=read_tign_history(idom)
72             % The subroutine reads a level function from a wrfinput file in the
73             % current directory.
74             % 
75             % idom : Input integer describing the the domain number that we will
76             %        read the array from.  This is only to determine the file name
77             %        as printf 'wrfinput_d%02i' idom.
78             % 
79             % returns:
80             %
81             % tign(nx,ny,ntime) : The fire history from the file.
82             
83             nx=tign_history.get_grid_info(idom,'nx');
84             ny=tign_history.get_grid_info(idom,'ny');
85             
86             n=netcdf.open(tign_history.get_file_name(idom),'NC_NOWRITE');
87             lid=netcdf.inqvarid(n,tign_history.tign_hist_name);
88             tign=netcdf.getVar(n,lid);
89             tign=squeeze(tign(1:nx,1:ny,1));
90             netcdf.close(n);
91         end
92         function out=get_grid_info(idom,argin)
93             
94             % This subroutine inquires a wrfinput file in the current directory about
95             % information relevant to the computation and manipulation of tign history
96             % arrays.  
97             %
98             % idom : Input integer describing the the domain number that we will
99             %        read the array from.  This is only to determine the file name
100             %        as printf 'wrfinput_d%02i' idom.
101             %
102             % argin : a string specifying what is to be returned.  Current
103             %         valid inputs are:
104             %
105             %   nx,ny : The dimensions of the fire grid in the given file.
106             %
107             %   dx,dy : The grid resolution of the fire grid in meters.
108             %
109             %   dt : The atmospheric time step in seconds.
110             %
111             %   sr_x,sr_y : The atmospheric/fire grid refinement factor.
112             
113             n=netcdf.open(tign_history.get_file_name(idom),'NC_NOWRITE');
114             
115             ix=netcdf.inqdimid(n,tign_history.xname);
116             iy=netcdf.inqdimid(n,tign_history.yname);
117             ax=netcdf.inqdimid(n,tign_history.xatm);
118             ay=netcdf.inqdimid(n,tign_history.yatm);
119             [t,nx]=netcdf.inqdim(n,ix);
120             [t,ny]=netcdf.inqdim(n,iy);
121             [t,ax]=netcdf.inqdim(n,ax);
122             [t,ay]=netcdf.inqdim(n,ay);
123             sr_x=nx/ax;
124             sr_y=ny/ay;
125             
126             switch argin
127                 
128                 case 'nx'
129                     out=nx-sr_x;
130                     
131                 case 'ny'
132                     out=ny-sr_y;
133                     
134                 case 'dx'
135                     out=netcdf.inqatt(n,netcdf.getConstant('NC_GLOBAL'),tign_history.dxname);
136                     
137                 case 'dy'
138                     out=netcdf.inqatt(n,netcdf.getConstant('NC_GLOBAL'),tign_history.dyname);
140                 case 'dt'
141                     out=netcdf.inqatt(n,netcdf.getConstant('NC_GLOBAL'),tign_history.dtname);
142                     
143                 case 'sr_x'
144                     out=sr_x;
145                     
146                 case 'sr_y'
147                     out=sr_y;
148                     
149                 otherwise
150                     error('invalid input %s',argin);
151             end
152             
153     
154             netcdf.close(n);
155         end
156     end
157     methods(Static,Access=protected)
158         function f=get_file_name(idom)
159             f=sprintf(tign_history.filefmt,idom);
160         end
161         function check(ncerr)
162             if ncerr ~= 0
163                 error(netcdf.strerror(ncerr))
164             end
165         end
166     end