source: trunk/src/sauvegrads.m @ 91

Last change on this file since 91 was 91, checked in by pinsard, 10 years ago

fix thanks to coding rules

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