source: trunk/src/sauvegrads.m @ 28

Last change on this file since 28 was 28, checked in by pinsard, 15 years ago

bug fix on sauvegrads if now after 19991231

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