open a file in Fortran - file

I wanted to open two files in Fortran, but get the following error message:
Fortran Runtime Error: End of line
infile_T contains years from 1970 to 2100 and temperature values for these years. Infile_CO2 contains years for the same time period and CO2-values. I only want to load in the years and temperature values from 1970 to 1998.
Here is my code:
integer j,p, Jahre_CO2
character*20 infile_T, infile_CO2
parameter(infile_T='temp2m.obs',
1 infile_CO2='yco2.sze',
1 anfja=0,
1 endja=29,
1 nt=endja-anfja+1)
real T_beob(anfja:endja),Jahre_T, CO2_beob(anfja:endja)
open(11,file=infile_T)
do ja=anfja,endja
read(11,*) Jahre_T,T_beob(ja)
enddo
close(11)
print *,Jahre_T
open(12, file=infile_CO2)
do p=anfja,endja
read(12,*) Jahre_CO2,CO2_beob(p)
enddo
close(12)
write(6,*) 'Daten eingelesen !',T_beob

Make sure your files contain all the data you expect them to contain. If your files contain 130 lines and you're only reading in 30, you shouldn't receive an end of file error.
Check the format of the data. Usually 'years' (Jahre) are integers ; so you have Jahre_CO2 as integer, but Jahre_T as real -- double check this.

Related

Reading a 2-dimensional table with Fortran

I am trying to write a program that would simply read a table defining a dependency of a coefficient on temperature (table of 2 columns, 27 lines) and that would use the data in an equation. The rest of the variables defined in the subroutine are provided by a FEM program (t(1) and timinc).
My program seems to loop through the whole table, but I have a suspicion that it reads the last row of the table 27 times. Please can someone explain to me the problem?
subroutine mycode(eqcpnc,t,timinc)
#ifdef _IMPLICITNONE
implicit none
#else
implicit logical (a-z)
#endif
real*8 a, dtdl, eqcpnc, t, timinc
integer iostat, ios, n
dimension t(1), dtdl(1)
c
open (1, file='Coefficient.txt', iostat=ios)
n=0
do
read(1, *, iostat=ios) dtdl(1), a
if (ios/=0) exit
n=n+1
enddo
rewind(1, iostat=ios)
c
eqcpnc=t(1)*a**timinc
c
close(1)
return
end
The original table 'Coefficient.txt' looks like this:
27.85 5.22E-19
77.85 1.47E-18
127.85 4.17E-18
177.85 1.19E-17
227.85 3.44E-17
277.85 1.02E-16
327.85 3.23E-16
377.85 1.12E-15
427.85 4.50E-15
477.85 2.23E-14
527.85 1.42E-13
577.85 1.16E-12
627.85 1.20E-11
677.85 1.44E-10
727.85 1.69E-09
777.85 1.85E-08
827.85 2.31E-07
877.85 5.69E-07
927.85 1.34E-06
977.85 3.04E-06
1027.85 6.64E-06
1077.85 1.40E-05
1127.85 2.84E-05
1177.85 5.61E-05
1227.85 1.08E-04
1277.85 2.01E-04
1327.85 3.65E-04

Getting back empty arrays/ variables when uploading .txt file to Matlab

I am trying to load a .txt file with data to Matlab to use for some calculations. However, when I run the code the variables/arrays come back empty or blank.
Below I have the code I am using.
%% importing the data
% Open file in the memory
fileID = fopen('rainfall.txt');
% Read the txt file with formats: Integer, Integer, Float
% Treat multiple delimiters, which is "space" in here, as one. Put the data
% in a variable called chunk.
chunk = textscan(fileID,'%d %d %f','Delimiter',' ',...
'MultipleDelimsAsOne',1);
% Close file from the memory.
fclose(fileID);
% date
dt = chunk{:,1};
% hour
hr = chunk{:,2};
% precip
r = chunk{:,3};
% remove extra variables from Matlab workspace
clear fileID ans
In the Workspace tab in Matlab it shows chunk to be an empty 1x3 cell. This results in dt, hr, and r not having any values either and are listed as having a value of []. So my best guess is that something is going wrong with loading in the data to Matlab.
Also, here is small portion of the data I am working with. This is exactly how it is written in the .txt file as well.
STATION DATE HPCP
----------------- -------------- --------
COOP:132367 20040116 22:00 0.01
COOP:132367 20040116 23:00 0.01
COOP:132367 20040117 00:00 0.04
COOP:132367 20040117 01:00 0.02
COOP:132367 20040117 02:00 0.00
In the actual file I have a lot more data than what I have listed here, but this should give an idea of what the data looks like and how it's formatted.
From the textscan help page:
textscan attempts to match the data in the file to the conversion specifier in formatSpec. The textscan function reapplies formatSpec throughout the entire file and stops when it cannot match formatSpec to the data.
So the first problem is the title lines. You should discard them. For example, by manually reading 2 lines (using fgetl).
Next, you should make sure that the format matches the data. You tried reading 2 integers and a float but you also have the station name.
I think the following should be ok:
fileID = fopen('rainfall.txt');
l = fgetl(fileID);
l = fgetl(fileID);
chunk = textscan(fileID,'%s:%d %d %d %f','Delimiter',' ',...
'MultipleDelimsAsOne',1);

Open file with formatted variable name in Julia

I have a list of files numbered gll_01.tab, gll_02.tab, ...., gll_20.tab in a subdirectory of my parent directory. These files are tabular data files.
I want to open/read files with user-specified input.
I can do:
a = 3
open("directory/gll_0$a.tab")
But using this approach, I would have to define two separate variable names for (01 to 09) and for (10 to 18). How can I use variables or strings with name 02, 03, ..., etc?
In python, I can have an equivalent command:
a = 4
g = '{:02d}'.format(a)
f = open('directory/gll_%s.tab' %g)
Is there an equivalent string formatting command in Julia?
A simple answer in this case would be to use lpad:
a = 3
open("directory/gll_$(lpad(a,2,"0")).tab")
If you need more fancy formatting you can use e.g. https://github.com/JuliaIO/Formatting.jl, in this case this would be:
using Formatting
a = 3
open("directory/gll_$(fmt("0>2", a)).tab")
Another option is to use #sprintf, docs are here. With that you can use %02d as a formatting option that would pad a digit d to length 2 with 0s preceding it:
julia> using Printf # this is in the standard library
julia> #sprintf("directory/gll_%02d.tab", 1)
"directory/gll_01.tab"
You can use this in your open statements too. Here they are in action:
julia> for i in 5:10
println("$i file is: $(#sprintf("directory/gll_%02d.tab",i))")
end
5 file is: directory/gll_05.tab
6 file is: directory/gll_06.tab
7 file is: directory/gll_07.tab
8 file is: directory/gll_08.tab
9 file is: directory/gll_09.tab
10 file is: directory/gll_10.tab

Standardizing Heterogeneous Age Data in SPSS or Excel

I'm trying to standardize a column of Age data (i.e. into years old / months old) using SPSS / SPSS Syntax / Excel. My intuition is to use a series of DO IF loops i.e.:
DO IF CHAR.INDEX(Age, "y")>1... for years
DO IF CHAR.INDEX(Age, "m")>1... for months
DO IF CHAR.INDEX(Age, "d")>1... for days
and have the program reference the number(s) immediately preceding the string as a quantity of years / months / days and add it to a total in a new variable which could be in days (the smallest unit) which could later be converted to years.
For example for a cell "3 yr 5 mo": add 3*365 + 5*30.5 = 1248 days old to a new variable (something like "DaysOld").
Examples of Cell contents (numbers without any strings assumed to be years):
2
5 months
11 days
1.7
13 yr
22 yrs
13 months
10 mo
6/19/2016
3y10m
10m
12y
3.5 years
3 years
11 mos
1 year 10 months
1 year, two months
20 Y
13 y/o
3 years in 2014
The following syntax will solve a lot of cases, but definitely not all of them (eg. "1.7" or "3 years in 2014"). You'll need to do more work on it, but this should get you started nicely...
First I recreate your sample data to work with:
data list list/age (a30).
begin data
"2"
"5 months"
"11 days"
"1.7"
"13 yr"
"22 yrs"
"13 Months"
"10 mo"
"6/19/2016"
"3y10m"
"10m"
"12y"
"3.5 years"
"3 YEARS"
"11 mos"
"1 year 10 months"
"1 year, two months"
"20 Y"
"13 y/o"
"3 years in 2014"
end data.
Now to work:
* some necessary definitions.
string ageCleaned (a30) chr (a1) nm d m y (a5).
compute ageCleaned="".
* my first step is to create a "cleaned" age variable (it's possible to
manage without this variable but using this is better for debugging and
improving the method).
* in the `ageCleaned` variable I only keep digits, periods (for decimal
point) and the characters "d", "m", "y".
do if CHAR.INDEX(lower(age),'ymd',1)>0.
loop #chrN=1 to char.length(age).
compute chr=lower(char.substr(age,#chrN,1)).
if CHAR.INDEX(chr,'0123456789ymd.',1)>0 ageCleaned=concat(rtrim(ageCleaned),chr).
end loop.
end if.
* the following line accounts for the word "days" which in the `ageCleaned`
variable has turned into the characters "dy".
compute ageCleaned=replace(ageCleaned,"dy","d").
exe.
* now I can work through the `ageCleaned` variable, accumulating digits
until I meet a character, then assigning the accumulated number to the
right variable according to that character ("d", "m" or "y").
compute nm="".
loop #chrN=1 to char.length(ageCleaned).
compute chr=char.substr(ageCleaned,#chrN,1).
do if CHAR.INDEX(chr,'0123456789.',1)>0.
compute nm=concat(rtrim(nm),chr).
else.
if chr="y" y=nm.
if chr="m" m=nm.
if chr="d" d=nm.
compute nm="".
end if.
end loop.
exe.
* we now have the numbers in string format, so after turning them into
numbers they are ready for use in calculations.
alter type d m y (f8.2).
compute DaysOld=sum(365*y, 30.5*m, d).

Read from txt file to matrix after a specific expression

I wanted via matlab to read a table of data from a txt file after a specific expression and a number of non desired lines for example the AA.txt have:
Information about students :
AAAA
BBBB
1 10 100
2 3 15
! ! ! a number of lines
10 6 9
I have like information the expression 'Information about students', the number of skipped lines 2 and the number of columns 3 and rows 10 in desired matrix.
if I understand correctly, you wanna skip the first 3 lines (assuming them as headers) and then reading the rest.
I would follow this procedure:
fid = fopen(filename,'r');
A = textscan(fid,'%f %f %f','HeaderLines',3,'Delimiter','\r\n');
I currently do not have access to MATLAB, but I do believe it will work.

Resources