[GRASSLIST:5235] Re: netCDF to raster

Paul Hasenohr phasenohr at cls.fr
Wed Dec 18 10:40:43 EST 2002


Hi Vidya,

Please find below the script I used. It is written in a "proprietary" 
language but I think that you can do the same things in a perl script with 
calls to nco utilities (http://nco.sourceforge.net)
By this way, I converted MSLA (sea level anomalies) netCDF files (coverage: 
0E - 360E / 90S - 90N) to GRASS raster ascii files (180W - 180E / 90S - 90N).

I need to be able to modify the point values of the map so as to modify the 
variables of the netCDF file that i shall reconvert the raster map to.
In order to do this, I think the easiest way is to use a perl or shell 
script to create a very simple cdf file (just one array) from the output of 
r.out.ascii -h. Mainly you will have to change space character with a comma.
Then you use ncgen to generate a netCDF file with just an array called SLA 
for example.
Then you use nco tools to replace the variable SLA in the original netCDF 
file (or a copy !) with the new one.

I hope that I will have some time by the end of this year to write a script 
to convert GRASS ascii raster file to a netCDF file...

Of course, it would be better to write a r.out.cdf and a r.in.cdf modules 
compiled directly against netCDF libraries but I don't know C !

Paul


#!CDFTools
#CDFTools Cdf2AsciiPaul.oce fic_in_netcdf num_grille fic_ou_grille null_value

variables FichierCDF, Grille, Latitudes, Longitudes;
variables IndexLat, IndexLon;
variables FichierAscii, Valeur;

open grid file named argv[0] as FichierCDF for read;
read grid to_integer(argv[1]) from file FichierCDF to Grille;
Latitudes       = get_latitudes(Grille);
Longitudes      = get_longitudes(Grille);

open text file named argv[2] as FichierAscii for write;

write text "north: 90.00"//"\n" to file FichierAscii;
write text "south: -90.00"//"\n" to file FichierAscii;
write text "east: 180.00"//"\n" to file FichierAscii;
write text "west: -180.00"//"\n" to file FichierAscii;
write text "rows: 720"//"\n" to file FichierAscii;
write text "cols: 1440"//"\n" to file FichierAscii;
write text "null: "//to_string(argv[3])//"\n" to file FichierAscii;
write text "type: float"//"\n"//"\n" to file FichierAscii;

IndexLat        = number(Latitudes)-1;
while IndexLat >= 0 do
   IndexLon      = 720;
   while IndexLon < number(Longitudes) do
     Valeur      = get_element(Grille, IndexLat, IndexLon, 0);
     if Valeur != DEFAULT_FLOAT then
#     if Valeur == DEFAULT_FLOAT   then
#      Valeur = 9999;
#     end if;
       write text        to_string(Valeur)//" "
         to file FichierAscii;
     else
         Valeur="9999 ";
         write text Valeur to file FichierAscii;
     end if;
     IndexLon    = IndexLon+1;
   end while;
   IndexLon      = 0;
   while IndexLon < (number(Longitudes)/2) do
     Valeur      = get_element(Grille, IndexLat, IndexLon, 0);
     if Valeur != DEFAULT_FLOAT then
#     if Valeur == DEFAULT_FLOAT   then
#      Valeur = 9999;
#     end if;
       write text        to_string(Valeur)//" "
         to file FichierAscii;
     else
         Valeur="9999 ";
         write text Valeur to file FichierAscii;
     end if;
     IndexLon    = IndexLon+1;
   end while;

   IndexLat = IndexLat - 1;
   write text "\n" to file FichierAscii;
end while;



At 15:40 16/12/2002 -0800, you wrote:
>Hi paul,
>I guess i will be comfortable with the 2nd option.
>Could u pls send me the script if it is possible ?
>Also, after converting the netCDF file to the raster map form,
>I need to be able to modify the point values of the map so as to modify
>the variables of the netCDF file that i shall reconvert the raster map to.
>Like r.support is used to modify the header information how can i actually
>go about modifying the variables of the netCDF by means of its
>corresponding raster map ?
>
>Thanx in advance.
>Vidya Kotamraju
>
>--- Paul Hasenohr <phasenohr at cls.fr> wrote:
> > Hello Vidya,
> >
> > I know two ways to import netCDF files into GRASS.
> > 1) You install R and you use R inside GRASS. R is
> > able to read netCDF
> > files, so you can export your variable in GRASS. I
> > used this method six
> > months ago and it worked fine.
> >
> > 2) An other option
> > You convert your netCDF file to an ascii file
> > readable with the r.in.ascii
> > command.
> > I prefer this option because it is less system
> > resources consuming.
> > I made a shell script to convert netCDF file into
> > GRASS raster ascii file
> > but I use netCDF tools developed by my company and
> > there are
> > not  publically available. However, if you are
> > interested in, I can send it
> > to you and I believe that it is easily portable with
> > the nco tools
> > (http://nco.sourceforge.net, have a look at ncks)
> >
> > HTH
> > Paul
> >
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/grass-user/attachments/20021218/4a910070/attachment.html


More information about the grass-user mailing list