[GRASSLIST:5235] Re: netCDF to raster

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 textto_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 textto_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@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](http://nco.sourceforge.net, have a look at ncks/))

HTH
Paul

Hello to all

have you idea how I can do a unusual thing with grass???
I would like to draw a vector polyline (not straigth) using v.digit and then
to obtain some straigth line, regulary spaced, perpendicular to the
polyline..... like this: --|----|----|----|----|--
the problem is that I need a large number of these perpendiculars and I would
like to obtain them automatically after drawed the polyline...... :slight_smile:

I know it is a unusual question but I hope someone can help me......
thank you....

Bye Ivan

On Thursday 19 December 2002 12:31 am, Ivan Marchesini wrote:

Hello to all

have you idea how I can do a unusual thing with grass???
I would like to draw a vector polyline (not straigth) using v.digit and
then to obtain some straigth line, regulary spaced, perpendicular to the
polyline..... like this: --|----|----|----|----|--
the problem is that I need a large number of these perpendiculars and I
would like to obtain them automatically after drawed the polyline......
:slight_smile:

I know it is a unusual question but I hope someone can help me......
thank you....

Bye Ivan

Something like http://mpa.itc.it/radim/g51/lrs1.png ?

I have some first modules for Linear reference system (LRS) in GRASS:
v.lrs.create - create LRS from lines and points (milestones)
v.lrs.segment - create events (points/segments) for existing LRS
                and given distances
v.lrs.stationing - create stationing + labels along the lines for
                   existing LRS

If you don't need real LRS (i.e. you don't need to work with some
reference system used in real world like kilometers along the roads)
you could modify (simplify) v.lrs.stationing.
It is not yet in CVS but if you are interested I can put it on the Web.

Radim

Dear Radmin,
It seems that your modules are perfect for my intentions....
really you can put them on the Web???
I would be really gratefull !!!!!!!
Thank for your answer!!!!!

Thank you very much to all others answer me....

Bye

ivan

Alle 13:27, giovedì 19 dicembre 2002, Radim Blazek ha scritto:

On Thursday 19 December 2002 12:31 am, Ivan Marchesini wrote:
> Hello to all
>
> have you idea how I can do a unusual thing with grass???
> I would like to draw a vector polyline (not straigth) using v.digit and
> then to obtain some straigth line, regulary spaced, perpendicular to the
> polyline..... like this: --|----|----|----|----|--
> the problem is that I need a large number of these perpendiculars and I
> would like to obtain them automatically after drawed the polyline......
>
> :slight_smile:
>
> I know it is a unusual question but I hope someone can help me......
> thank you....
>
> Bye Ivan

Something like http://mpa.itc.it/radim/g51/lrs1.png ?

I have some first modules for Linear reference system (LRS) in GRASS:
v.lrs.create - create LRS from lines and points (milestones)
v.lrs.segment - create events (points/segments) for existing LRS
                and given distances
v.lrs.stationing - create stationing + labels along the lines for
                   existing LRS

If you don't need real LRS (i.e. you don't need to work with some
reference system used in real world like kilometers along the roads)
you could modify (simplify) v.lrs.stationing.
It is not yet in CVS but if you are interested I can put it on the Web.

Radim

On Thursday 19 December 2002 04:27 pm, Ivan Marchesini wrote:

Dear Radmin,
It seems that your modules are perfect for my intentions....
really you can put them on the Web???
I would be really gratefull !!!!!!!
Thank for your answer!!!!!

http://mpa.itc.it/radim/g51/v.lrs.tar.gz

but I forgot to tell that it is for GRASS 5.1, sorry.
Warning1: there is no documentation, I can help you by
an advice, but not unlimited.
Warning2: it is designed for real world (noncontinuous segments),
so it may seem to be a bit complicated first.

Do you want to use LRS created from mileposts (points with km)?

Radim

I'm trying to import the USA urban areas SDTS file from
http://data.geocomm.com/catalog/US/group21.html using v.in.sdts.
It seems to imports fine, everything plots correctly, but the dig_att file
contains 0.00000 for all of the label point longitudes. The lattitudes are
correct. I have imported other SDTS files (e.g. state boundaries) from the
same source to the same destination mapset without this problem. Any
suggestions?

When projecting lat/lon elevation data to Winkle Triple I get an instant
core dump. Is this projection routine known to work?

cheg01@attbi.com wrote:

When projecting lat/lon elevation data to Winkle Triple I get an instant
core dump. Is this projection routine known to work?

The Winkel Tripel projection doesn't have a defined inverse
projection, so you can't use it with r.proj. Instead, you would have
to convert the data to a sites map, project that with s.proj, then
convert it back to a raster with s.surf.rst.

--
Glynn Clements <glynn.clements@virgin.net>

----- Original Message -----
From: "Glynn Clements" <glynn.clements@virgin.net>
To: <cheg01@attbi.com>
Cc: <grasslist@baylor.edu>
Sent: Wednesday, December 25, 2002 2:30 AM
Subject: [GRASSLIST:5243] Re: Core dump on Winkle Triple projection

cheg01@attbi.com wrote:

> When projecting lat/lon elevation data to Winkle Triple I get an

instant

> core dump. Is this projection routine known to work?

The Winkel Tripel projection doesn't have a defined inverse
projection, so you can't use it with r.proj. Instead, you would have
to convert the data to a sites map, project that with s.proj, then
convert it back to a raster with s.surf.rst.

--
Glynn Clements <glynn.clements@virgin.net>

The process is failing on the froward projection, from Lat/Lon to Winkel
Triple. It should be averaging the coordinates from the "eqc" Cylindrical
Equal Area (Plate Carree) and the Aitoff projections. I tested the input
files on the Robinson projection and they worked fine. The eqc projection
by itself works but the Aitoff projection is failing. gdb seems to show
garbage in the arguments to G_remove. I am running Grass 5.0.0pre3. Here is
the gdb output:

Mapset <PERMANENT> in Location <globe_winkel>
GRASS-GRID > gdb r.proj
(gdb) r
Starting program: /usr/local/grass5/bin/r.proj

Program received signal SIGTRAP, Trace/breakpoint trap.
0xef7829c0 in ?? ()
(gdb) c
Continuing.

OPTION: input raster map
     key: input
required: YES
enter option > b10g

You have chosen:
  input=b10g
Is this correct? (y/n) [y]

OPTION: location of input map
     key: location
required: YES
enter option > globe

You have chosen:
  location=globe
Is this correct? (y/n) [y]

OPTION: mapset of input map
     key: mapset
required: NO
enter option > PERMANENT

You have chosen:
  mapset=PERMANENT
Is this correct? (y/n) [y]

OPTION: path to GRASS database of input location
     key: dbase
required: NO
enter option >

OPTION: output raster map
     key: output
required: NO
enter option >

FLAG: Set the following flag?
    List raster files in input location and exit?(y/n) [n]

Program received signal SIGSEGV, Segmentation fault.
0x0 in ?? ()
(gdb) bt
#0 0x0 in ?? ()
#1 0x1ade4 in G_suppress_masking () at auto_mask.c:65
#2 0x1a17c in G_remove (element=0xefffec00 "?`", name=0xefffebf8 "?`")
    at remove.c:43
#3 0x184fc in G_quant_perform_f (q=0xefffecf8, fcell=0xefffed40,
    cell=0xefffed88, n=-268440064) at quant.c:859
#4 0x17f8c in G_quant_reverse_rule_order (q=0xefffecf8) at quant.c:672
(gdb)

cheg01@attbi.com wrote:

> cheg01@attbi.com wrote:
>
> > When projecting lat/lon elevation data to Winkle Triple I get an instant
> > core dump. Is this projection routine known to work?
>
> The Winkel Tripel projection doesn't have a defined inverse
> projection, so you can't use it with r.proj. Instead, you would have
> to convert the data to a sites map, project that with s.proj, then
> convert it back to a raster with s.surf.rst.

The process is failing on the froward projection, from Lat/Lon to Winkel
Triple.

Note that r.proj works "in reverse". IOW, for each cell in the
destination location, it transforms the coordinates of the cell's
centre using the *inverse* projection, to get a coordinate in the
source location. It then reads the value of the cell at that
coordinate from the source raster, and stores the value in the
original cell in the destination location.

It should be averaging the coordinates from the "eqc" Cylindrical
Equal Area (Plate Carree) and the Aitoff projections. I tested the input
files on the Robinson projection and they worked fine. The eqc projection
by itself works but the Aitoff projection is failing.

"eqc" has a defined inverse; "aitoff" doesn't.

gdb seems to show
garbage in the arguments to G_remove. I am running Grass 5.0.0pre3. Here is
the gdb output:

My guess is that r.proj has already "crashed" before this point. With
5.0.0 on Linux, r.proj segfaults at address 0, which is what I expect
(although we should probably trap this and display an error message).

Ultimately, if you want to project into a location whose projection
doesn't have a defined inverse, you will have to convert to sites
project that (s.proj/v.proj use the forward projection), then convert
back to raster.

--
Glynn Clements <glynn.clements@virgin.net>

----- Original Message -----
From: "Glynn Clements" <glynn.clements@virgin.net>
To: <cheg01@attbi.com>
Cc: <grasslist@baylor.edu>
Sent: Saturday, January 18, 2003 10:40 PM
Subject: Re: [GRASSLIST:5330] Re: Core dump on Winkle Triple projection

>
Note that r.proj works "in reverse". IOW, for each cell in the
destination location, it transforms the coordinates of the cell's
centre using the *inverse* projection, to get a coordinate in the
source location. It then reads the value of the cell at that
coordinate from the source raster, and stores the value in the
original cell in the destination location.

I understand, because the destination map can have any arbitrary resolution
you have to lookup or interpolate the value in the source map for a given
cell location in the destination map.

Ultimately, if you want to project into a location whose projection
doesn't have a defined inverse, you will have to convert to sites
project that (s.proj/v.proj use the forward projection), then convert
back to raster.

This way the projection is independent of the resolution of the destination
map.

--
Glynn Clements <glynn.clements@virgin.net>