source: trunk/src/sauvegrads.m @ 85

Last change on this file since 85 was 85, checked in by pinsard, 14 years ago

better representation of variable parts in filenames

  • Property svn:keywords set to Id
File size: 3.9 KB
Line 
1function[h]=sauvegrads(nomfich,matrice,posit)
2% SAUVEGRADS create a data file (.dat) and a definion file (.ctl)
3
4%+
5%
6% ============
7% sauvegrads.m
8% ============
9%
10% ---------------------------------------------------------
11% create a data file (.dat) and a definion file (.ctl)
12% ---------------------------------------------------------
13%
14% SYNOPSYS
15% ========
16%
17% ``SAUVEGRADS(NOMFICH,MATRICE,POSIT)``
18%
19% DESCRIPTION
20% ===========
21%
22% Create a data file (:file:`.dat`) and a definion file (:file:`.ctl`).
23%
24% Those files should be used with GRADS using :
25%
26% ::
27%
28%   grads$ open nomfich.ctl
29%
30%
31% Inputs arguments are :
32%
33% NOMFICH  suffixe of the files
34%          :file:`{nomfich}.dat` will be data file
35%          :file:`nomfich}.ctl` will be definition file.
36%
37% MATRICE name of the 2D array to be saved
38%
39% POSIT    Coordinates telling the position of MATRICE reagrding the world map.
40%          Those coordinates [LON INCLON LAT INCLAT] are latitude and longitude
41%          of initial point and latitude and longitude steps. All in degrees.
42%
43% Warning : MATRICE will be reversed verticaly between MATLAB and GRADS.
44% This is die by the fact that latitude progress upwards when in MATLAB
45% increasing indexes, we go downards.
46%
47% Voir aussi XGRADS
48%
49% TODO
50% ====
51%
52% what about files path ?
53%
54% use h return values
55%
56% EVOLUTIONS
57% ==========
58%
59% $Id$
60%
61% JMG le 10/03/94
62%
63% - fplod 2009-01-12T16:46:41Z aedon.locean-ipsl.upmc.fr (Darwin)
64%
65%   * date of day was not ok after 19991231
66%
67% - fplod 2009-01-05T11:01:41Z aedon.locean-ipsl.upmc.fr (Darwin)
68%
69%   * translation
70%   * comment in ReST format
71%
72%-
73%
74h=[];
75if nargin < 3
76        error('Il n''y a pas assez d''arguments.');
77elseif nargin > 3
78        error('Il y a trop d''arguments.')
79end
80
81% orientation de la matrice
82% sans le flipud la matrice sera inverser verticalement entre grads et matlab
83% ce qui est normal puisque l'on utilise les coordonn??es Longitude,latitude
84% sous grads et en incrementant la latitude on progresse vers le haut
85% alors qu'avec matlab on progresse vers le bas en incrementant l'indice
86%matrice=flipud(matrice);
87mat1=fliplr(matrice);
88mat2=rot90(mat1);
89nomfichdat=[nomfich,'.dat'];
90nomfichctl=[nomfich,'.ctl'];
91
92fprintf(1,'creation of data file %s \n',nomfichdat);
93% ouverture du fichier .dat
94fid=fopen(nomfichdat,'w');
95% ecriture de la matrice
96fwrite(fid,mat2,'float32');
97fclose(fid);
98
99% init des variables
100[sxp syp]=size(posit);
101if (syp~=4)
102  error('eee : incorrect syntaxe, 4 arguments must be given for POSIT');
103end
104lon=posit(1);
105inclon=posit(2);
106lat=posit(3);
107inclat=posit(4);
108% ici on peut rajouter les test pour voir si les coordonn??es en degr??es sont bonnes
109if (abs(lon)>180)
110  error('La longitude doit ??tre comprise entre -180 et 180');
111elseif (abs(lat>90))
112  error('La latitude doit ??tre comprise entre -90 et 90')
113end
114
115% Init date for record
116date1=date;
117date2=[date1(1:2),upper(date1(4:6)),date1(8:11)];
118% test sur les arguments
119[dimy dimx]=size(matrice);
120
121fprintf(1,'creation of definition file %s \n',nomfichctl);
122% ouverture du fichier .ctl
123fid=fopen(nomfichctl,'w');
124
125% ecriture ligne par ligne du fichier
126texte=['DSET ^',nomfichdat,' ',10];
127fwrite(fid,texte,'char');
128texte=['UNDEF -2E5633 ',10];
129fwrite(fid,texte,'char');
130texte=['TITLE File generated by sauvegrads de MATLAB ',10];
131fwrite(fid,texte,'char');
132texte=['XDEF ',num2str(dimx),' LINEAR ',num2str(lon),'  ',num2str(inclon),' ',10];
133fwrite(fid,texte,'char');
134texte=['YDEF ',num2str(dimy),' LINEAR ',num2str(lat),'  ',num2str(inclat),' ',10];
135fwrite(fid,texte,'char');
136texte=['ZDEF ',num2str(dimx*dimy),' LINEAR  1  1 ',10];
137fwrite(fid,texte,'char');
138texte=['TDEF 1 LINEAR ',num2str(date2),' 1DY ',10];
139fwrite(fid,texte,'char');
140texte=['VARS 1 ',10];
141fwrite(fid,texte,'char');
142texte=[nomfich,'      0   99  ',num2str(dimx*dimy),'   Matrice d''isovaleurs de MATLAB ',10];
143fwrite(fid,texte,'char');
144texte=['ENDVARS ',10];
145fwrite(fid,texte,'char');
146
147% fermeture du fichier
148fclose(fid);
Note: See TracBrowser for help on using the repository browser.