[15] | 1 | function[h]=sauvegrads(nomfich,matrice,posit) |
---|
[85] | 2 | % SAUVEGRADS create a data file (.dat) and a definion file (.ctl) |
---|
[16] | 3 | |
---|
| 4 | %+ |
---|
[10] | 5 | % |
---|
[86] | 6 | % .. _sauvegrads.m: |
---|
| 7 | % |
---|
[75] | 8 | % ============ |
---|
| 9 | % sauvegrads.m |
---|
| 10 | % ============ |
---|
| 11 | % |
---|
| 12 | % --------------------------------------------------------- |
---|
[85] | 13 | % create a data file (.dat) and a definion file (.ctl) |
---|
[75] | 14 | % --------------------------------------------------------- |
---|
[15] | 15 | % |
---|
[16] | 16 | % SYNOPSYS |
---|
| 17 | % ======== |
---|
[10] | 18 | % |
---|
[16] | 19 | % ``SAUVEGRADS(NOMFICH,MATRICE,POSIT)`` |
---|
[10] | 20 | % |
---|
[16] | 21 | % DESCRIPTION |
---|
| 22 | % =========== |
---|
[10] | 23 | % |
---|
[85] | 24 | % Create a data file (:file:`.dat`) and a definion file (:file:`.ctl`). |
---|
[16] | 25 | % |
---|
| 26 | % Those files should be used with GRADS using : |
---|
[71] | 27 | % |
---|
[16] | 28 | % :: |
---|
| 29 | % |
---|
| 30 | % grads$ open nomfich.ctl |
---|
| 31 | % |
---|
| 32 | % |
---|
| 33 | % Inputs arguments are : |
---|
| 34 | % |
---|
| 35 | % NOMFICH suffixe of the files |
---|
[85] | 36 | % :file:`{nomfich}.dat` will be data file |
---|
| 37 | % :file:`nomfich}.ctl` will be definition file. |
---|
[16] | 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 | % |
---|
[10] | 63 | % JMG le 10/03/94 |
---|
[16] | 64 | % |
---|
[28] | 65 | % - fplod 2009-01-12T16:46:41Z aedon.locean-ipsl.upmc.fr (Darwin) |
---|
| 66 | % |
---|
| 67 | % * date of day was not ok after 19991231 |
---|
| 68 | % |
---|
[16] | 69 | % - fplod 2009-01-05T11:01:41Z aedon.locean-ipsl.upmc.fr (Darwin) |
---|
| 70 | % |
---|
| 71 | % * translation |
---|
| 72 | % * comment in ReST format |
---|
| 73 | % |
---|
| 74 | %- |
---|
| 75 | % |
---|
[15] | 76 | h=[]; |
---|
[10] | 77 | if nargin < 3 |
---|
[91] | 78 | error('Il n''y a pas assez d''arguments.'); |
---|
[10] | 79 | elseif nargin > 3 |
---|
[91] | 80 | error('Il y a trop d''arguments.') |
---|
[10] | 81 | end |
---|
| 82 | |
---|
| 83 | % orientation de la matrice |
---|
| 84 | % sans le flipud la matrice sera inverser verticalement entre grads et matlab |
---|
[15] | 85 | % ce qui est normal puisque l'on utilise les coordonn??es Longitude,latitude |
---|
[10] | 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 | |
---|
[15] | 94 | fprintf(1,'creation of data file %s \n',nomfichdat); |
---|
[10] | 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) |
---|
[15] | 104 | error('eee : incorrect syntaxe, 4 arguments must be given for POSIT'); |
---|
[10] | 105 | end |
---|
| 106 | lon=posit(1); |
---|
| 107 | inclon=posit(2); |
---|
| 108 | lat=posit(3); |
---|
| 109 | inclat=posit(4); |
---|
[15] | 110 | % ici on peut rajouter les test pour voir si les coordonn??es en degr??es sont bonnes |
---|
[10] | 111 | if (abs(lon)>180) |
---|
[15] | 112 | error('La longitude doit ??tre comprise entre -180 et 180'); |
---|
[10] | 113 | elseif (abs(lat>90)) |
---|
[15] | 114 | error('La latitude doit ??tre comprise entre -90 et 90') |
---|
[10] | 115 | end |
---|
| 116 | |
---|
[28] | 117 | % Init date for record |
---|
[10] | 118 | date1=date; |
---|
[28] | 119 | date2=[date1(1:2),upper(date1(4:6)),date1(8:11)]; |
---|
[10] | 120 | % test sur les arguments |
---|
| 121 | [dimy dimx]=size(matrice); |
---|
| 122 | |
---|
[15] | 123 | fprintf(1,'creation of definition file %s \n',nomfichctl); |
---|
[10] | 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'); |
---|
[28] | 132 | texte=['TITLE File generated by sauvegrads de MATLAB ',10]; |
---|
[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 |
---|
[15] | 150 | fclose(fid); |
---|