1 | function[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 | % |
---|
76 | h=[]; |
---|
77 | if nargin < 3 |
---|
78 | error('Il n''y a pas assez d''arguments.'); |
---|
79 | elseif nargin > 3 |
---|
80 | error('Il y a trop d''arguments.') |
---|
81 | end |
---|
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); |
---|
89 | mat1=fliplr(matrice); |
---|
90 | mat2=rot90(mat1); |
---|
91 | nomfichdat=[nomfich,'.dat']; |
---|
92 | nomfichctl=[nomfich,'.ctl']; |
---|
93 | |
---|
94 | fprintf(1,'creation of data file %s \n',nomfichdat); |
---|
95 | % ouverture du fichier .dat |
---|
96 | fid=fopen(nomfichdat,'w'); |
---|
97 | % ecriture de la matrice |
---|
98 | fwrite(fid,mat2,'float32'); |
---|
99 | fclose(fid); |
---|
100 | |
---|
101 | % init des variables |
---|
102 | [sxp syp]=size(posit); |
---|
103 | if (syp~=4) |
---|
104 | error('eee : incorrect syntaxe, 4 arguments must be given for POSIT'); |
---|
105 | end |
---|
106 | lon=posit(1); |
---|
107 | inclon=posit(2); |
---|
108 | lat=posit(3); |
---|
109 | inclat=posit(4); |
---|
110 | % ici on peut rajouter les test pour voir si les coordonn??es en degr??es sont bonnes |
---|
111 | if (abs(lon)>180) |
---|
112 | error('La longitude doit ??tre comprise entre -180 et 180'); |
---|
113 | elseif (abs(lat>90)) |
---|
114 | error('La latitude doit ??tre comprise entre -90 et 90') |
---|
115 | end |
---|
116 | |
---|
117 | % Init date for record |
---|
118 | date1=date; |
---|
119 | date2=[date1(1:2),upper(date1(4:6)),date1(8:11)]; |
---|
120 | % test sur les arguments |
---|
121 | [dimy dimx]=size(matrice); |
---|
122 | |
---|
123 | fprintf(1,'creation of definition file %s \n',nomfichctl); |
---|
124 | % ouverture du fichier .ctl |
---|
125 | fid=fopen(nomfichctl,'w'); |
---|
126 | |
---|
127 | % ecriture ligne par ligne du fichier |
---|
128 | texte=['DSET ^',nomfichdat,' ',10]; |
---|
129 | fwrite(fid,texte,'char'); |
---|
130 | texte=['UNDEF -2E5633 ',10]; |
---|
131 | fwrite(fid,texte,'char'); |
---|
132 | texte=['TITLE File generated by sauvegrads de MATLAB ',10]; |
---|
133 | fwrite(fid,texte,'char'); |
---|
134 | texte=['XDEF ',num2str(dimx),' LINEAR ',num2str(lon),' ',num2str(inclon),' ',10]; |
---|
135 | fwrite(fid,texte,'char'); |
---|
136 | texte=['YDEF ',num2str(dimy),' LINEAR ',num2str(lat),' ',num2str(inclat),' ',10]; |
---|
137 | fwrite(fid,texte,'char'); |
---|
138 | texte=['ZDEF ',num2str(dimx*dimy),' LINEAR 1 1 ',10]; |
---|
139 | fwrite(fid,texte,'char'); |
---|
140 | texte=['TDEF 1 LINEAR ',num2str(date2),' 1DY ',10]; |
---|
141 | fwrite(fid,texte,'char'); |
---|
142 | texte=['VARS 1 ',10]; |
---|
143 | fwrite(fid,texte,'char'); |
---|
144 | texte=[nomfich,' 0 99 ',num2str(dimx*dimy),' Matrice d''isovaleurs de MATLAB ',10]; |
---|
145 | fwrite(fid,texte,'char'); |
---|
146 | texte=['ENDVARS ',10]; |
---|
147 | fwrite(fid,texte,'char'); |
---|
148 | |
---|
149 | % fermeture du fichier |
---|
150 | fclose(fid); |
---|