Nazer
Nazer

Reputation: 3764

Writing R dataframes into a unique file format

I am looking for some one who is knowledgeable about both R and file formats/extensions. I am trying to convert some directions for Excel to an R script. I start with a file that is .txt and want to end up with a file that is .met. However, writing the file with the proper extension doesn't necessarily give it the right format.

I think that the .met file is unique to the program I am running (APSIM). I can google information about other .met files, but they not the same thing. So, what is a .met file? Maybe you can tell me, if I tell you how it is created.

To make these files in Excel:

  1. Input data (meterological data -- daily temp, rain)
  2. Adjust the space of the columns so they are wider than necessary, but not too wide.
  3. Save the file as Formatted Text (space delimited)(*.prn) file.
  4. Go to the folder and change the file extension to .met
  5. Try to use the file, but find the spacing is not exactly right, go back to #1.

Official Documentation and File Format Description

Here is what a few lines of your average met file looks like:

year          day           radn          maxt          mint          rain
()            ()            (MJ/m^2)      (oC)          (oC)          (mm)
 1985         1        8.2342         -8.3300      -15.0000     1.27
 1985         2        8.5313         -8.3300      -20.5600     0.00
 1985         3        7.1630         -5.5600      -18.3300     0.00
 1985         4        7.6064         2.7800      -12.7800     0.00
 1985         5        7.0866         3.3300      -8.3300     0.00
 1985         6        7.5471         6.1100      -1.6700     0.00
 1985         7        8.7017         3.3300      -3.3300     0.00
 1985         8        7.7457         1.1100      -15.5600     0.00
 1985         9        2.7429         -5.0000      -9.4400     1.78
 1985         10        5.9049         -5.0000      -12.2200     3.81

Once I have all of this data made up in dataframes, all I want to do is write them into a format my program will recognize (.met).

Upvotes: 0

Views: 792

Answers (2)

Ananta
Ananta

Reputation: 3711

I guess this answers your question, you can use all your header here, no need to open separately and work on that.

dat<-read.table(text="year          day           radn          maxt          mint          rain
()            ()            (MJ/m^2)      (oC)          (oC)          (mm)
 1985         1        8.2342         -8.3300      -15.0000     1.27
 1985         2        8.5313         -8.3300      -20.5600     0.00
 1985         3        7.1630         -5.5600      -18.3300     0.00
 1985         4        7.6064         2.7800      -12.7800     0.00
 1985         5        7.0866         3.3300      -8.3300     0.00
 1985         6        7.5471         6.1100      -1.6700     0.00
 1985         7        8.7017         3.3300      -3.3300     0.00
 1985         8        7.7457         1.1100      -15.5600     0.00
 1985         9        2.7429         -5.0000      -9.4400     1.78
 1985         10        5.9049         -5.0000      -12.2200     3.81", header=T)

#Now header with constants
latC=5
tavC=5
ampC=5
header=paste0("weather.met.weather
latitude = ",latC," (DECIMAL DEGREES)
tav = ", tavC," (oC) ! annual average ambient temperature
amp = ",ampC," (oC) ! annual amplitude in mean monthly temperature")

write(header, "met.met") #writing header
write.table(dat, "met.met", append=T, sep=paste(rep(" ",5), collapse=""), row.names=F, quote=F) #appending to that, work with how many spaces you need, I used 5 here

Adding "row.names=F" make the column number equal to the number of column names

Upvotes: 1

rawr
rawr

Reputation: 20811

Based on the description and comments, it seems that a .met file simply requires more spaces than normal. This can be achieved simply by

write.table(df, file = 'this.met', sep = '     ', 
            rownames = FALSE, quote = FALSE)

where sep contains multiple spaces for wider columns. Glad your fix was simple :)

Upvotes: 1

Related Questions